From 530fed8ec225453572d57b15c200ab062c335457 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期三, 01 十一月 2023 19:20:21 +0800 Subject: [PATCH] 公海member_id使用0 --- service/salesRefund.go | 126 ++++++++++++++++++++++++++++++++++++++---- 1 files changed, 114 insertions(+), 12 deletions(-) diff --git a/service/salesRefund.go b/service/salesRefund.go index 14738b1..52a4a88 100644 --- a/service/salesRefund.go +++ b/service/salesRefund.go @@ -4,12 +4,15 @@ "aps_crm/constvar" "aps_crm/model" "aps_crm/pkg/ecode" + "errors" + "github.com/shopspring/decimal" + "gorm.io/gorm" ) type SalesRefundService struct{} func (SalesRefundService) AddSalesRefund(salesRefund *model.SalesRefund) int { - salesReturnRecord, err := model.NewSalesReturnSearch().SetId(salesRefund.SourceId).Find() + salesReturnRecord, err := model.NewSalesReturnSearch().SetId(salesRefund.SalesReturnId).SetPreload(true).First() if err != nil { return ecode.SalesReturnNotExist } @@ -17,10 +20,33 @@ 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)) + } + code := ecode.OK + err = model.WithTransaction(func(db *gorm.DB) error { + err = model.NewSalesRefundSearch().Create(salesRefund) + if err != nil { + code = ecode.DBErr + return err + } + salesReturnRecord.AmountHasRefund = salesReturnRecord.AmountHasRefund.Add(amount).Round(2) + if salesReturnRecord.AmountHasRefund.GreaterThan(salesReturnRecord.AmountTotal) { + code = ecode.SalesRefundProductBeyondTotalAmount + return errors.New("閫�璐т骇鍝佸搴旀�讳环瓒呭嚭搴旈��鎬讳环") + } + salesReturnRecord.AmountShouldRefund = salesReturnRecord.AmountTotal.Sub(salesReturnRecord.AmountHasRefund).Round(2) + err = model.NewSalesReturnSearch().SetId(salesRefund.SalesReturnId).Update(salesReturnRecord) + if err != nil { + code = ecode.DBErr + return err + } + return nil + }) - err = model.NewSalesRefundSearch().Create(salesRefund) if err != nil { - return ecode.SalesRefundExist + return code } return ecode.OK @@ -35,34 +61,66 @@ if returnProductsMap[product.Id] == nil { return ecode.SalesRefundProductNotExist } - if returnProductsMap[product.Id].Price != product.Price || product.Amount.GreaterThan(returnProductsMap[product.Id].Amount) { + if !returnProductsMap[product.Id].Price.Equal(product.Price) || product.Amount.GreaterThan(returnProductsMap[product.Id].Amount) { return ecode.SalesRefundProductPriceOrAmountErr } } 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.SalesReturnId).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.SalesReturnId).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.SalesReturnId).SetPreload(true).First() if err != nil { return ecode.SalesReturnNotExist } @@ -71,23 +129,67 @@ 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)) + } + + code := ecode.OK + err = model.WithTransaction(func(db *gorm.DB) error { + err = model.NewSalesRefundSearch().SetId(salesRefund.Id).Update(salesRefund) + if err != nil { + code = ecode.DBErr + return err + } + salesReturnRecord.AmountHasRefund = salesReturnRecord.AmountHasRefund.Sub(diffAmount).Round(2) + salesReturnRecord.AmountShouldRefund = salesReturnRecord.AmountTotal.Sub(salesReturnRecord.AmountHasRefund).Round(2) + + if salesReturnRecord.AmountHasRefund.GreaterThan(salesReturnRecord.AmountTotal) { + code = ecode.SalesRefundProductBeyondTotalAmount + return errors.New("閫�璐т骇鍝佸搴旀�讳环瓒呭嚭搴旈��鎬讳环") + } + + err = model.NewSalesReturnSearch().SetId(salesRefund.SalesReturnId).Update(salesReturnRecord) + if err != nil { + code = ecode.DBErr + return err + } + return nil + }) + if err != nil { - return ecode.SalesRefundSetErr + return code } return ecode.OK } -func (SalesRefundService) GetSalesRefundList(page, pageSize int, keywordType constvar.SalesRefundKeywordType, keyword string) ([]*model.SalesRefund, int64, int) { +func (SalesRefundService) GetSalesRefundList(page, pageSize int, keywordType constvar.SalesRefundKeywordType, keyword string, sourceId int, memberIds []int) ([]*model.SalesRefund, int64, int) { // get contact list contacts, total, err := model.NewSalesRefundSearch(). SetKeywordType(keywordType). SetKeyword(keyword). + SetSourceId(sourceId). SetPreload(true). + SetMemberIds(memberIds). SetPage(page, pageSize).FindAll() if err != nil { return nil, 0, ecode.SalesRefundListErr } 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