| | |
| | | package purchase |
| | | |
| | | import ( |
| | | "context" |
| | | "errors" |
| | | "github.com/shopspring/decimal" |
| | | "github.com/spf13/cast" |
| | | "gorm.io/gorm" |
| | | "srm/global" |
| | | "srm/model/common/request" |
| | | "srm/model/purchase" |
| | | purchaserequest "srm/model/purchase/request" |
| | | "srm/proto/qualityinspect" |
| | | "srm/service/test" |
| | | ) |
| | | |
| | | type PurchaseService struct{} |
| | |
| | | |
| | | //@function: CreatePurchase |
| | | //@description: 创建采购单 |
| | | //@param: e model.Purchase |
| | | //@param: params *purchaserequest.AddPurchase |
| | | //@return: err error |
| | | |
| | | func (exa *PurchaseService) CreatePurchase(params purchaserequest.AddPurchase) (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 { |
| | | err = global.GVA_DB.Create(¶ms.Purchase).Error |
| | | err = tx.Create(&record).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | for _, product := range params.ProductList { |
| | | product.PurchaseId = cast.ToInt(params.Purchase.ID) |
| | | for _, product := range productList { |
| | | product.PurchaseId = cast.ToInt(record.ID) |
| | | } |
| | | return global.GVA_DB.Create(params.ProductList).Error |
| | | return tx.Create(productList).Error |
| | | }) |
| | | |
| | | return err |
| | | } |
| | | |
| | | //@function: DeleteFileChunk |
| | | 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 |
| | | //@description: 删除采购单 |
| | | //@param: e model.Purchase |
| | | //@param: id uint |
| | | //@return: err error |
| | | |
| | | func (exa *PurchaseService) DeletePurchase(id uint) (err error) { |
| | | func (slf *PurchaseService) DeletePurchase(id uint) (err error) { |
| | | err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { |
| | | err = global.GVA_DB.Where("id = ?", id).Delete(&purchase.Purchase{}).Error |
| | | err = tx.Where("id = ?", id).Delete(&purchase.Purchase{}).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | return global.GVA_DB.Where("purchase_id = ?", id).Delete(&purchase.PurchaseProducts{}).Error |
| | | return tx.Where("purchase_id = ?", id).Delete(&purchase.PurchaseProducts{}).Error |
| | | }) |
| | | return err |
| | | } |
| | | |
| | | //@function: UpdatePurchase |
| | | //@description: 更新采购单 |
| | | //@param: e *model.Purchase |
| | | //@param: params *purchaserequest.AddPurchase |
| | | //@return: err error |
| | | |
| | | func (exa *PurchaseService) UpdatePurchase(params *purchaserequest.AddPurchase) (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 { |
| | | err = global.GVA_DB.Updates(params.Purchase).Error |
| | | err = tx.Where("id = ?", params.ID).Updates(params).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | err = global.GVA_DB.Where("purchase_id = ?", params.Purchase.ID).Delete(&purchase.PurchaseProducts{}).Error |
| | | err = tx.Where("purchase_id = ?", params.ID).Delete(&purchase.PurchaseProducts{}).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | for _, product := range params.ProductList { |
| | | for _, product := range productList { |
| | | product.ID = 0 |
| | | product.PurchaseId = cast.ToInt(params.Purchase.ID) |
| | | product.PurchaseId = cast.ToInt(params.ID) |
| | | } |
| | | return global.GVA_DB.Create(params.ProductList).Error |
| | | return tx.Create(productList).Error |
| | | }) |
| | | return err |
| | | } |
| | |
| | | //@param: id uint |
| | | //@return: purchase model.Purchase, err error |
| | | |
| | | func (exa *PurchaseService) GetPurchase(id uint) (purchase purchase.Purchase, err error) { |
| | | err = global.GVA_DB.Where("id = ?", id).First(&purchase).Error |
| | | func (slf *PurchaseService) GetPurchase(id uint) (purchase purchase.Purchase, err error) { |
| | | err = global.GVA_DB.Where("id = ?", id).Preload("Supplier").First(&purchase).Error |
| | | return |
| | | } |
| | | |
| | |
| | | //@param: info request.PageInfo |
| | | //@return: list interface{}, total int64, err error |
| | | |
| | | func (exa *PurchaseService) GetPurchaseList(info request.PageInfo) (list interface{}, total int64, err error) { |
| | | func (slf *PurchaseService) GetPurchaseList(info request.PageInfo) (list interface{}, total int64, err error) { |
| | | limit := info.PageSize |
| | | offset := info.PageSize * (info.Page - 1) |
| | | db := global.GVA_DB.Model(&purchase.Purchase{}) |
| | | var ids []uint |
| | | var purchaseList = make([]*purchase.Purchase, 0) |
| | | if info.Keyword != "" { |
| | | db.Distinct("purchases.id").Joins("left join purchase_products on purchase_products.purchase_id = purchases.id"). |
| | | Joins("left join Product on Product.Id = purchase_products.product_id"). |
| | | Joins("left join supplier on supplier.Id = purchases.supplier_id"). |
| | | Where("purchases.name like ?", "%"+info.Keyword+"%"). |
| | | Or("Product.name like ?", "%"+info.Keyword+"%"). |
| | | Or("supplier.name like ?", "%"+info.Keyword+"%") |
| | | db.Distinct("srm_purchase.id").Joins("left join srm_purchase_products on srm_purchase_products.purchase_id = srm_purchase.id"). |
| | | Joins("left join material on material.id = srm_purchase_products.product_id"). |
| | | Joins("left join srm_supplier on srm_supplier.Id = srm_purchase.supplier_id"). |
| | | Where("srm_purchase.name like ?", "%"+info.Keyword+"%"). |
| | | Or("material.name like ?", "%"+info.Keyword+"%"). |
| | | Or("srm_supplier.name like ?", "%"+info.Keyword+"%") |
| | | err = db.Limit(limit).Offset(offset).Find(&ids).Error |
| | | if err != nil { |
| | | return purchaseList, total, err |
| | | } |
| | | } |
| | | var purchaseList []purchase.Purchase |
| | | |
| | | err = db.Count(&total).Error |
| | | if err != nil { |
| | | return purchaseList, total, err |
| | | } |
| | | err = db.Limit(limit).Offset(offset).Find(&purchaseList).Error |
| | | if len(ids) != 0 { |
| | | db = global.GVA_DB.Model(&purchase.Purchase{}) |
| | | err = db.Where("id in (?)", ids).Find(&purchaseList).Error |
| | | } else { |
| | | err = db.Limit(limit).Offset(offset).Find(&purchaseList).Error |
| | | } |
| | | |
| | | return purchaseList, total, err |
| | | } |
| | | |
| | | //@function: GetPurchaseProductList |
| | | //@description: 分页获取采购单产品列表 |
| | | //@param: sysUserAuthorityID string |
| | | //@param: purchaseId int |
| | | //@return: list interface{}, err error |
| | | |
| | | func (exa *PurchaseService) GetPurchaseProductList(purchaseId uint) (list []*purchase.PurchaseProducts, err error) { |
| | | 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 |
| | | } |
| | | |
| | | //@function: Submit |
| | | //@description: 提交采购单 |
| | | //@param: id uint |
| | | //@return: err error |
| | | |
| | | func (slf *PurchaseService) Submit(id int, status purchase.OrderStatus) (err error) { |
| | | |
| | | //purchaseData, err := slf.GetPurchase(id) |
| | | //if err != nil { |
| | | // return err |
| | | //} |
| | | //var targetStatus purchase.OrderStatus |
| | | //switch purchaseData.Status { |
| | | //case purchase.OrderStatusConfirmed: |
| | | // targetStatus = purchase.OrderStatusReceived |
| | | //case purchase.OrderStatusReceived: |
| | | // targetStatus = purchase.OrderStatusStored |
| | | //case purchase.OrderStatusStored: |
| | | // targetStatus = purchase.OrderStatusCompleted |
| | | //} |
| | | err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { |
| | | err = tx.Where("id = ?", id).Model(&purchase.Purchase{}).Updates(map[string]interface{}{"status": status}).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | |
| | | //switch targetStatus { |
| | | //case purchase.OrderStatusReceived: |
| | | // return SendInspect(purchaseData) |
| | | //case purchase.OrderStatusStored: |
| | | //case purchase.OrderStatusCompleted: |
| | | //} |
| | | return nil |
| | | }) |
| | | return err |
| | | } |
| | | |
| | | func SendInspect(record purchase.Purchase) error { |
| | | productList, err := NewPurchaseService().GetPurchaseProductList(record.ID) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | productIds := make([]uint, 0, len(productList)) |
| | | for _, product := range productList { |
| | | productIds = append(productIds, product.ID) |
| | | } |
| | | productService := &test.ProductService{} |
| | | _, productMap, err := productService.GetProducts(productIds) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | inspectOrders := make([]*qualityinspect.QualityInspect, 0, len(productList)) |
| | | for _, productItem := range productList { |
| | | product := productMap[productItem.ID] |
| | | if product == nil { |
| | | 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(), |
| | | 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) |
| | | return err |
| | | } |
| | | |
| | | 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("id not in ?", ids).Delete(&purchase.PurchaseType{}).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | if len(newRecords) > 0 { |
| | | err = tx.Create(newRecords).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | } |
| | | |
| | | for _, item := range list { |
| | | if item.ID != 0 { |
| | | err = tx.Where("id = ?", item.ID).Updates(item).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | } |
| | | } |
| | | return nil |
| | | }) |
| | | return err |
| | | } |
| | | |
| | | func (slf *PurchaseService) GetPurchaseTypeList() (list []*purchase.PurchaseType, err error) { |
| | | db := global.GVA_DB.Model(&purchase.PurchaseType{}) |
| | | list = make([]*purchase.PurchaseType, 0) |
| | | err = db.Order("pin desc, sort desc, id asc").Find(&list).Error |
| | | return list, err |
| | | } |