| | |
| | | |
| | | 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 = DealPrice(record, productList) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { |
| | | var quantity decimal.Decimal |
| | | for _, product := range productList { |
| | | quantity = quantity.Add(product.Amount) |
| | | } |
| | | params.Quantity = quantity |
| | | err = tx.Create(¶ms).Error |
| | | 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 |
| | | }) |
| | | |
| | | return err |
| | | } |
| | | |
| | | func DealPrice(record *purchase.Purchase, productList []*purchase.PurchaseProducts) 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)) |
| | | } |
| | | record.Quantity = quantity |
| | | if !totalPrice.Equal(record.TotalPrice) { |
| | | return errors.New("价税总计计算错误") |
| | | } |
| | | realTotalPrice = record.CalcRealTotalPrice() |
| | | if !realTotalPrice.Equal(record.RealTotalPrice) { |
| | | return errors.New("最终价格计算错误") |
| | | } |
| | | record.UnInvoiceAmount = record.RealTotalPrice |
| | | record.ShouldPayAmount = record.RealTotalPrice |
| | | return nil |
| | | } |
| | | |
| | | //@function: DeletePurchase |
| | |
| | | //@return: err error |
| | | |
| | | func (slf *PurchaseService) UpdatePurchase(params *purchase.Purchase, productList []*purchase.PurchaseProducts) (err error) { |
| | | err = DealPrice(params, productList) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { |
| | | var quantity decimal.Decimal |
| | | for _, product := range productList { |
| | | quantity = quantity.Add(product.Amount) |
| | | } |
| | | params.Quantity = quantity |
| | | err = tx.Where("id = ?", params.ID).Updates(params).Error |
| | | if err != nil { |
| | | return err |
| | |
| | | Or("Product.name like ?", "%"+info.Keyword+"%"). |
| | | Or("supplier.name like ?", "%"+info.Keyword+"%") |
| | | } |
| | | var purchaseList []purchase.Purchase |
| | | var purchaseList []*purchase.Purchase |
| | | err = db.Count(&total).Error |
| | | if err != nil { |
| | | return purchaseList, total, err |
| | |
| | | func (slf *PurchaseService) GetPurchaseProductList(purchaseId uint) (list []*purchase.PurchaseProducts, err error) { |
| | | db := global.GVA_DB.Model(&purchase.PurchaseProducts{}) |
| | | list = make([]*purchase.PurchaseProducts, 0) |
| | | err = db.Where("purchase_id = ?", purchaseId).Find(&list).Error |
| | | err = db.Where("purchase_id = ?", purchaseId).Preload("Product").Find(&list).Error |
| | | return list, err |
| | | } |
| | | |
| | |
| | | continue |
| | | } |
| | | inspectOrder := &qualityinspect.QualityInspect{ |
| | | InspectType: qualityinspect.InspectType_InspectTypePurchase, |
| | | MaterialType: qualityinspect.MaterialType_MaterialTypeRaw, |
| | | MaterialName: product.Name, |
| | | MaterialId: product.Number, |
| | | MaterialTp: product.ModelNumber, |
| | | MaterialUnit: product.Unit, |
| | | Supplier: record.Supplier.Name, |
| | | WarehouseName: "采购总仓", |
| | | ReportAmount: productItem.Amount.InexactFloat64(), |
| | | InspectMethod: qualityinspect.InspectMethod_InspectMethodAll, |
| | | InspectAmount: productItem.Amount.InexactFloat64(), |
| | | InspectType: qualityinspect.InspectType_InspectTypePurchase, |
| | | MaterialType: qualityinspect.MaterialType_MaterialTypeRaw, |
| | | MaterialName: product.Name, |
| | | MaterialId: product.Number, |
| | | MaterialTp: product.ModelNumber, |
| | | MaterialUnit: product.Unit, |
| | | Supplier: record.Supplier.Name, |
| | | WarehouseName: "采购总仓", |
| | | ReportAmount: productItem.Amount.InexactFloat64(), |
| | | InspectMethod: qualityinspect.InspectMethod_InspectMethodAll, |
| | | InspectAmount: productItem.Amount.InexactFloat64(), |
| | | PurchaseOrderId: record.Number, |
| | | } |
| | | inspectOrders = append(inspectOrders, inspectOrder) |
| | | } |
| | | if len(inspectOrders) == 0 { |
| | | return nil |
| | | } |
| | | inspectRequest := qualityinspect.SendPurchaseInspectRequest{List: inspectOrders} |
| | | _, err = qualityinspect.NewQualityInspectServiceClient(qualityinspect.Conn).SendPurchaseInspect(context.Background(), &inspectRequest) |
| | |
| | | } |
| | | |
| | | func (slf *PurchaseService) SavePurchaseType(list []*purchase.PurchaseType) (err error) { |
| | | ids := make([]uint, 0) |
| | | newRecords := make([]*purchase.PurchaseType, 0) |
| | | for _, item := range list { |
| | | if item.ID != 0 { |
| | | ids = append(ids, item.ID) |
| | | } else { |
| | | newRecords = append(newRecords, item) |
| | | } |
| | | } |
| | | err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { |
| | | err = tx.Where("1 = ?", 1).Delete(&purchase.PurchaseType{}).Error |
| | | err = tx.Where("id not in ?", ids).Delete(&purchase.PurchaseType{}).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | for _, item := range list { |
| | | item.ID = 0 |
| | | if len(newRecords) > 0 { |
| | | err = tx.Create(newRecords).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | } |
| | | return tx.Create(list).Error |
| | | |
| | | for _, item := range list { |
| | | if item.ID != 0 { |
| | | err = tx.Save(item).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | } |
| | | } |
| | | return nil |
| | | }) |
| | | return err |
| | | } |