| | |
| | | |
| | | import ( |
| | | "context" |
| | | "errors" |
| | | "github.com/shopspring/decimal" |
| | | "github.com/spf13/cast" |
| | | "gorm.io/gorm" |
| | |
| | | //@param: params *purchaserequest.AddPurchase |
| | | //@return: err error |
| | | |
| | | func (slf *PurchaseService) CreatePurchase(params *purchase.Purchase, productList []*purchase.PurchaseProducts) (err error) { |
| | | func (slf *PurchaseService) CreatePurchase(record *purchase.Purchase, productList []*purchase.PurchaseProducts) (err error) { |
| | | err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { |
| | | var quantity decimal.Decimal |
| | | var totalPrice decimal.Decimal |
| | | var realTotalPrice decimal.Decimal |
| | | for _, product := range productList { |
| | | quantity = quantity.Add(product.Amount) |
| | | totalPrice = totalPrice.Add(product.Price.Mul(product.Amount)) |
| | | } |
| | | params.Quantity = quantity |
| | | err = tx.Create(¶ms).Error |
| | | if !totalPrice.Equal(record.TotalPrice) { |
| | | return errors.New("价税总计计算错误") |
| | | } |
| | | if !quantity.Equal(record.Quantity) { |
| | | return errors.New("产品数量计算错误") |
| | | } |
| | | realTotalPrice = record.CalcRealTotalPrice() |
| | | if !realTotalPrice.Equal(record.RealTotalPrice) { |
| | | return errors.New("最终价格计算错误") |
| | | } |
| | | err = tx.Create(&record).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | for _, product := range productList { |
| | | product.PurchaseId = cast.ToInt(params.ID) |
| | | product.PurchaseId = cast.ToInt(record.ID) |
| | | } |
| | | return tx.Create(productList).Error |
| | | }) |