From 2def11ba83760b5be1361f10c3756cc0e9cfd165 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期一, 14 八月 2023 09:32:13 +0800 Subject: [PATCH] merge --- service/salesDetails.go | 30 ++++++++++++++++++++++++++++-- 1 files changed, 28 insertions(+), 2 deletions(-) diff --git a/service/salesDetails.go b/service/salesDetails.go index d504748..81e10b2 100644 --- a/service/salesDetails.go +++ b/service/salesDetails.go @@ -4,11 +4,23 @@ "aps_crm/constvar" "aps_crm/model" "aps_crm/pkg/ecode" + "github.com/shopspring/decimal" ) type SalesDetailsService struct{} func (SalesDetailsService) AddSalesDetails(salesDetails *model.SalesDetails) int { + salesDetails.AmountReceivable = decimal.Zero.Round(2) + salesDetails.AmountInvoiced = decimal.Zero.Round(2) + salesDetails.AmountReceived = decimal.Zero.Round(2) + salesDetails.AmountUnInvoiced = decimal.Zero.Round(2) + salesDetails.AmountTotal = decimal.Zero.Round(2) + for _, product := range salesDetails.Products { + salesDetails.AmountTotal = salesDetails.AmountTotal.Add(product.Amount.Mul(product.Price)) + } + salesDetails.AmountTotal = salesDetails.AmountTotal.Round(2) + salesDetails.AmountReceivable = salesDetails.AmountTotal + salesDetails.AmountUnInvoiced = salesDetails.AmountTotal err := model.NewSalesDetailsSearch().Create(salesDetails) if err != nil { return ecode.SalesDetailsExist @@ -18,7 +30,7 @@ } func (SalesDetailsService) DeleteSalesDetails(id int) int { - _, err := model.NewSalesDetailsSearch().SetId(id).Find() + _, err := model.NewSalesDetailsSearch().SetId(id).First() if err != nil { return ecode.SalesDetailsNotExist } @@ -32,10 +44,24 @@ func (SalesDetailsService) UpdateSalesDetails(salesDetails *model.SalesDetails) int { // check salesDetails exist - _, err := model.NewSalesDetailsSearch().SetId(salesDetails.Id).Find() + old, err := model.NewSalesDetailsSearch().SetId(salesDetails.Id).First() if err != nil { return ecode.SalesDetailsNotExist } + var totalAmount decimal.Decimal + totalAmount = salesDetails.AmountTotal + newProducts, removedProducts := NewProductsService().PickDiffProducts(salesDetails.Products, old.Products) + for _, product := range newProducts { + totalAmount = totalAmount.Add(product.Amount.Mul(product.Price)) + } + removedProductIds := make([]uint, 0, len(removedProducts)) + for _, product := range removedProducts { + totalAmount = totalAmount.Sub(product.Amount.Mul(product.Price)) + removedProductIds = append(removedProductIds, product.Id) + } + salesDetails.AmountTotal = totalAmount.Round(2) + salesDetails.AmountReceivable = salesDetails.AmountTotal.Sub(salesDetails.AmountReceived) + salesDetails.AmountUnInvoiced = salesDetails.AmountTotal.Sub(salesDetails.AmountInvoiced) err = model.NewSalesDetailsSearch().SetId(salesDetails.Id).Update(salesDetails) if err != nil { -- Gitblit v1.8.0