From 40fa6f86f5f7ac35206d6b953f4e5e82b2145ce9 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期五, 11 八月 2023 14:23:52 +0800
Subject: [PATCH] 新增、修改销售明细时更改价税合计,应收金额,未开票金额

---
 service/salesDetails.go |   28 +++++++++++++++++++++++++++-
 model/salesDetails.go   |    1 +
 2 files changed, 28 insertions(+), 1 deletions(-)

diff --git a/model/salesDetails.go b/model/salesDetails.go
index 301794b..25211e3 100644
--- a/model/salesDetails.go
+++ b/model/salesDetails.go
@@ -36,6 +36,7 @@
 		AmountReceived      decimal.Decimal   `gorm:"column:amount_received;type:decimal(12,2);comment:宸叉敹閲戦" json:"amountReceived"`        // 宸叉敹閲戦
 		AmountInvoiced      decimal.Decimal   `gorm:"column:amount_invoiced;type:decimal(12,2);comment:宸插紑绁ㄩ噾棰�" json:"amountInvoiced"`       // 宸插紑绁ㄩ噾棰�
 		AmountUnInvoiced    decimal.Decimal   `gorm:"column:amount_not_invoiced;type:decimal(12,2);comment:鏈紑绁ㄩ噾棰�" json:"amountUnInvoiced"` // 鏈紑绁ㄩ噾棰�
+		AmountTotal         decimal.Decimal   `gorm:"column:amount_total;type:decimal(12,2);comment:浠风◣鍚堣" json:"amountTotal"`              // 浠风◣鍚堣
 		gorm.Model          `json:"-"`
 	}
 
diff --git a/service/salesDetails.go b/service/salesDetails.go
index 906fc2e..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
@@ -32,10 +44,24 @@
 
 func (SalesDetailsService) UpdateSalesDetails(salesDetails *model.SalesDetails) int {
 	// check salesDetails exist
-	_, err := model.NewSalesDetailsSearch().SetId(salesDetails.Id).First()
+	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