From 87cad05224d857ec0e113ae03b006d2a52ec3049 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期三, 16 八月 2023 11:55:26 +0800
Subject: [PATCH] Merge branch 'zq'

---
 service/salesRefund.go |  100 ++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 92 insertions(+), 8 deletions(-)

diff --git a/service/salesRefund.go b/service/salesRefund.go
index 8f7e52b..cf548d9 100644
--- a/service/salesRefund.go
+++ b/service/salesRefund.go
@@ -4,12 +4,14 @@
 	"aps_crm/constvar"
 	"aps_crm/model"
 	"aps_crm/pkg/ecode"
+	"github.com/shopspring/decimal"
+	"gorm.io/gorm"
 )
 
 type SalesRefundService struct{}
 
 func (SalesRefundService) AddSalesRefund(salesRefund *model.SalesRefund) int {
-	salesReturnRecord, err := model.NewSalesReturnSearch().SetId(salesRefund.SourceId).SetPreload(true).Find()
+	salesReturnRecord, err := model.NewSalesReturnSearch().SetId(salesRefund.SourceId).SetPreload(true).First()
 	if err != nil {
 		return ecode.SalesReturnNotExist
 	}
@@ -17,8 +19,25 @@
 	if code := CheckProduct(salesReturnRecord.Products, salesRefund.Products); code != ecode.OK {
 		return code
 	}
+	var amount decimal.Decimal
+	for _, product := range salesRefund.Products {
+		amount = amount.Add(product.Amount.Mul(product.Price))
+	}
 
-	err = model.NewSalesRefundSearch().Create(salesRefund)
+	err = model.WithTransaction(func(db *gorm.DB) error {
+		err = model.NewSalesRefundSearch().Create(salesRefund)
+		if err != nil {
+			return err
+		}
+		salesReturnRecord.AmountHasRefund = salesReturnRecord.AmountHasRefund.Add(amount).Round(2)
+		salesReturnRecord.AmountShouldRefund = salesReturnRecord.AmountTotal.Sub(salesReturnRecord.AmountHasRefund).Round(2)
+		err = model.NewSalesReturnSearch().SetId(salesRefund.SourceId).Update(salesReturnRecord)
+		if err != nil {
+			return err
+		}
+		return nil
+	})
+
 	if err != nil {
 		return ecode.SalesRefundExist
 	}
@@ -42,27 +61,59 @@
 	return ecode.OK
 }
 
-func (SalesRefundService) DeleteSalesRefund(id int) int {
-	_, err := model.NewSalesRefundSearch().SetId(id).Find()
+func (slf SalesRefundService) DeleteSalesRefund(id int) int {
+	refund, err := model.NewSalesRefundSearch().SetId(id).First()
 	if err != nil {
 		return ecode.SalesRefundNotExist
 	}
+	salesReturnRecord, err := model.NewSalesReturnSearch().SetId(refund.SourceId).SetPreload(true).First()
+	if err != nil {
+		return ecode.SalesReturnNotExist
+	}
 
-	err = model.NewSalesRefundSearch().SetId(id).Delete()
+	var amount decimal.Decimal
+	for _, product := range refund.Products {
+		amount = amount.Add(product.Amount.Mul(product.Price))
+	}
+
+	err = model.WithTransaction(func(db *gorm.DB) error {
+		err = model.NewSalesRefundSearch().SetId(id).Delete()
+		if err != nil {
+			return err
+		}
+		salesReturnRecord.AmountHasRefund = salesReturnRecord.AmountHasRefund.Sub(amount).Round(2)
+		salesReturnRecord.AmountShouldRefund = salesReturnRecord.AmountTotal.Sub(salesReturnRecord.AmountHasRefund).Round(2)
+		err = model.NewSalesReturnSearch().SetId(refund.SourceId).Update(salesReturnRecord)
+		if err != nil {
+			return err
+		}
+		return nil
+	})
+
 	if err != nil {
 		return ecode.SalesRefundNotExist
 	}
 	return ecode.OK
 }
 
+func (slf SalesRefundService) BatchDeleteSalesRefund(ids []int) (failIds []int, code int) {
+	for _, id := range ids {
+		code = slf.DeleteSalesRefund(id)
+		if code != ecode.OK {
+			failIds = append(failIds, id)
+		}
+	}
+	return failIds, code
+}
+
 func (SalesRefundService) UpdateSalesRefund(salesRefund *model.SalesRefund) int {
 	// check salesRefund exist
-	_, err := model.NewSalesRefundSearch().SetId(salesRefund.Id).Find()
+	oldRefund, err := model.NewSalesRefundSearch().SetId(salesRefund.Id).First()
 	if err != nil {
 		return ecode.SalesRefundNotExist
 	}
 
-	salesReturnRecord, err := model.NewSalesReturnSearch().SetId(salesRefund.SourceId).SetPreload(true).Find()
+	salesReturnRecord, err := model.NewSalesReturnSearch().SetId(salesRefund.SourceId).SetPreload(true).First()
 	if err != nil {
 		return ecode.SalesReturnNotExist
 	}
@@ -71,7 +122,29 @@
 		return code
 	}
 
-	err = model.NewSalesRefundSearch().SetId(salesRefund.Id).Update(salesRefund)
+	var diffAmount decimal.Decimal
+
+	for _, product := range salesRefund.Products {
+		diffAmount = diffAmount.Add(product.Amount.Mul(product.Price))
+	}
+	for _, product := range oldRefund.Products {
+		diffAmount = diffAmount.Sub(product.Amount.Mul(product.Price))
+	}
+
+	err = model.WithTransaction(func(db *gorm.DB) error {
+		err = model.NewSalesRefundSearch().SetId(salesRefund.Id).Update(salesRefund)
+		if err != nil {
+			return err
+		}
+		salesReturnRecord.AmountHasRefund = salesReturnRecord.AmountHasRefund.Sub(diffAmount).Round(2)
+		salesReturnRecord.AmountShouldRefund = salesReturnRecord.AmountTotal.Sub(salesReturnRecord.AmountHasRefund).Round(2)
+		err = model.NewSalesReturnSearch().SetId(salesRefund.SourceId).Update(salesReturnRecord)
+		if err != nil {
+			return err
+		}
+		return nil
+	})
+
 	if err != nil {
 		return ecode.SalesRefundSetErr
 	}
@@ -91,3 +164,14 @@
 	}
 	return contacts, total, ecode.OK
 }
+
+func (SalesRefundService) GetSalesRefundListByIds(ids []int) ([]*model.SalesRefund, int) {
+	// get contact list
+	salesRefunds, err := model.NewSalesRefundSearch().
+		SetIds(ids).
+		Find()
+	if err != nil {
+		return nil, ecode.SalesReturnListErr
+	}
+	return salesRefunds, ecode.OK
+}

--
Gitblit v1.8.0