| | |
| | | if req.ProductId == "" { |
| | | return nil, errors.New("产品编号不能为空") |
| | | } |
| | | var products []test.Product |
| | | err := global.GVA_DB.Model(&test.Product{}).Where("number = ?", req.ProductId).Preload("Supplier").Find(&products).Error |
| | | var products []test.SupplierMaterial |
| | | err := global.GVA_DB.Model(&test.SupplierMaterial{}).Where("number = ?", req.ProductId).Preload("Supplier").Find(&products).Error |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | |
| | | } |
| | | |
| | | func (s *Server) CreatePurchaseByWms(ctx context.Context, req *CreatePurchaseByWmsRequest) (*CreatePurchaseByWmsResponse, error) { |
| | | if req.SupplierId == 0 || req.ProductId == "" { |
| | | return nil, errors.New("供应商id或产品id为空") |
| | | if req.ProductId == "" { |
| | | return nil, errors.New("产品id为空") |
| | | } |
| | | var pt purchase.PurchaseType |
| | | err := global.GVA_DB.Model(&purchase.PurchaseType{}).First(&pt).Error |
| | | var product test.SupplierMaterial |
| | | err := global.GVA_DB.Model(&test.SupplierMaterial{}).Where("number = ?", req.ProductId).First(&product).Error |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | | var product test.Product |
| | | err = global.GVA_DB.Model(&test.Product{}).Where("number = ? and supplier_id = ?", req.ProductId, req.SupplierId).First(&product).Error |
| | | if err != nil { |
| | | return nil, err |
| | | if err == gorm.ErrRecordNotFound { |
| | | var material test.Material |
| | | err = global.GVA_DB.Model(&test.Material{}).Where("id = ?", req.ProductId).First(&material).Error |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | | product.Name = material.Name |
| | | product.Number = material.ID |
| | | product.Unit = material.Unit |
| | | product.PurchasePrice = material.PurchasePrice.InexactFloat64() |
| | | product.Specifications = material.Specs |
| | | product.ModelNumber = material.Type |
| | | product.Name = material.Name |
| | | product.Name = material.Name |
| | | err = global.GVA_DB.Create(&product).Error |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | | } else { |
| | | return nil, err |
| | | } |
| | | } |
| | | |
| | | //采购单 |
| | | var purchaseRecord purchase.Purchase |
| | | purchaseRecord.PurchaseTypeId = int(pt.ID) |
| | | purchaseRecord.SupplierId = int(req.SupplierId) |
| | | purchaseRecord.OrderSource = "WMS推送" |
| | | purchaseRecord.Name = "WMS补货" |
| | | |
| | | //purchaseRecord.SupplierId = int(req.SupplierId) |
| | | if req.Source == "WMS" { |
| | | purchaseRecord.OrderSource = "WMS推送" |
| | | purchaseRecord.Name = "WMS补货" |
| | | } else if req.Source == "APS" { |
| | | purchaseRecord.OrderSource = "APS推送" |
| | | purchaseRecord.Name = "APS采购" |
| | | } |
| | | |
| | | purchaseRecord.ID = 0 |
| | | purchaseRecord.Status = purchase.OrderStatusConfirmed |
| | | purchaseRecord.HandledBy = "admin" |
| | |
| | | pp.Amount = purchaseRecord.Quantity |
| | | pp.Price = decimal.NewFromFloat(product.PurchasePrice) |
| | | pp.Total = purchaseRecord.TotalPrice |
| | | pp.Remark = "WMS补货" |
| | | if req.Source == "WMS" { |
| | | pp.Remark = "WMS补货" |
| | | } else if req.Source == "APS" { |
| | | pp.Remark = "APS采购" |
| | | } |
| | | |
| | | err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { |
| | | err = tx.Create(&purchaseRecord).Error |
| | |
| | | resp.PurchaseNumber = purchaseRecord.Number |
| | | return resp, nil |
| | | } |
| | | |
| | | func (s *Server) GetPurchaseInfo(ctx context.Context, req *GetPurchaseInfoRequest) (*GetPurchaseInfoResponse, error) { |
| | | if len(req.PurchaseNumbers) == 0 { |
| | | return nil, errors.New("采购单编码不能为空") |
| | | } |
| | | var ps []purchase.Purchase |
| | | err := global.GVA_DB.Model(&purchase.Purchase{}).Where("number in (?)", req.PurchaseNumbers).Preload("Supplier").Find(&ps).Error |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | | infos := make([]*PurchaseInfo, 0) |
| | | for _, p := range ps { |
| | | var info PurchaseInfo |
| | | info.PurchaseNumber = p.Number |
| | | info.PurchaseName = p.Name |
| | | info.SupplierName = p.Supplier.Name |
| | | info.Amount = p.Quantity.IntPart() |
| | | info.Status = int64(p.Status) |
| | | infos = append(infos, &info) |
| | | } |
| | | resp := new(GetPurchaseInfoResponse) |
| | | resp.Infos = infos |
| | | return resp, nil |
| | | } |
| | | |
| | | func (s *Server) ExistSupplier(ctx context.Context, req *ExistSupplierRequest) (*ExistSupplierResponse, error) { |
| | | resp := new(ExistSupplierResponse) |
| | | if len(req.ProductId) == 0 { |
| | | resp.Exist = false |
| | | return resp, nil |
| | | } |
| | | var products []test.SupplierMaterial |
| | | err := global.GVA_DB.Model(&test.SupplierMaterial{}).Where("number in (?)", req.ProductId).Find(&products).Error |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | | for _, number := range req.ProductId { |
| | | exit := false |
| | | for _, product := range products { |
| | | if number == product.Number && product.SupplierId > 0 { |
| | | exit = true |
| | | break |
| | | } |
| | | } |
| | | if !exit { |
| | | resp.Exist = exit |
| | | return resp, nil |
| | | } |
| | | } |
| | | resp.Exist = true |
| | | return resp, nil |
| | | } |