| | |
| | | if err != nil { |
| | | return ecode.DBErr |
| | | } |
| | | var amountInvoiced decimal.Decimal |
| | | var amountInvoiced, amountNotInvoiced decimal.Decimal |
| | | rightProducts := NewProductsService().PickRightProducts(invoice.Products, serviceContract.Products) |
| | | for _, product := range rightProducts { |
| | | amountInvoiced = serviceContract.AmountInvoiced.Add(product.Amount.Mul(product.Price)) |
| | | amountNotInvoiced = serviceContract.AmountUnInvoiced.Sub(product.Amount.Mul(product.Price)) |
| | | } |
| | | amountInvoiced = amountInvoiced.Round(2) |
| | | if amountInvoiced.GreaterThan(serviceContract.AmountReceivable) { |
| | | return ecode.SContractInvoiceProductPriceGreaterThanReceivableAmountErr |
| | | } |
| | | amountNotInvoiced = amountNotInvoiced.Round(2) |
| | | //if amountInvoiced.GreaterThan(serviceContract.AmountReceivable) { |
| | | // return ecode.SContractInvoiceProductPriceGreaterThanReceivableAmountErr |
| | | //} |
| | | err = model.WithTransaction(func(db *gorm.DB) error { |
| | | err = model.NewInvoiceSearch().Create(invoice) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | return model.NewServiceContractSearch().SetId(invoice.SourceId).UpdateByMap(map[string]interface{}{ |
| | | "amount_invoiced": amountInvoiced, |
| | | "amount_invoiced": amountInvoiced, |
| | | "amount_not_invoiced": amountNotInvoiced, |
| | | }) |
| | | }) |
| | | if err != nil { |