| | |
| | | "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).SetPreload(true).First() |
| | | salesReturnRecord, err := model.NewSalesReturnSearch().SetId(salesRefund.SalesReturnId).SetPreload(true).First() |
| | | if err != nil { |
| | | return ecode.SalesReturnNotExist |
| | | } |
| | |
| | | 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.SourceId).Update(salesReturnRecord) |
| | | err = model.NewSalesReturnSearch().SetId(salesRefund.SalesReturnId).Update(salesReturnRecord) |
| | | if err != nil { |
| | | code = ecode.DBErr |
| | | return err |
| | | } |
| | | return nil |
| | | }) |
| | | |
| | | if err != nil { |
| | | return ecode.SalesRefundExist |
| | | return code |
| | | } |
| | | |
| | | return ecode.OK |
| | |
| | | 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 |
| | | } |
| | | } |
| | |
| | | if err != nil { |
| | | return ecode.SalesRefundNotExist |
| | | } |
| | | salesReturnRecord, err := model.NewSalesReturnSearch().SetId(refund.SourceId).SetPreload(true).First() |
| | | salesReturnRecord, err := model.NewSalesReturnSearch().SetId(refund.SalesReturnId).SetPreload(true).First() |
| | | if err != nil { |
| | | return ecode.SalesReturnNotExist |
| | | } |
| | |
| | | } |
| | | 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) |
| | | err = model.NewSalesReturnSearch().SetId(refund.SalesReturnId).Update(salesReturnRecord) |
| | | if err != nil { |
| | | return err |
| | | } |
| | |
| | | return ecode.SalesRefundNotExist |
| | | } |
| | | |
| | | salesReturnRecord, err := model.NewSalesReturnSearch().SetId(salesRefund.SourceId).SetPreload(true).First() |
| | | salesReturnRecord, err := model.NewSalesReturnSearch().SetId(salesRefund.SalesReturnId).SetPreload(true).First() |
| | | if err != nil { |
| | | return ecode.SalesReturnNotExist |
| | | } |
| | |
| | | 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) |
| | | err = model.NewSalesReturnSearch().SetId(salesRefund.SourceId).Update(salesReturnRecord) |
| | | |
| | | 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 |