| | |
| | | if req.Number == "" { |
| | | return nil, errors.New("采购编号不能为空") |
| | | } |
| | | err := global.GVA_DB.Model(&purchase.Purchase{}).Where("number = ?", req.Number). |
| | | Updates(map[string]interface{}{"status": purchase.OrderStatusStored}).Error |
| | | m := make(map[string]interface{}) |
| | | m["status"] = purchase.OrderStatusStored |
| | | if req.Status > 0 { |
| | | m["status"] = req.Status |
| | | } |
| | | err := global.GVA_DB.Model(&purchase.Purchase{}).Where("number = ?", req.Number).Updates(m).Error |
| | | return new(UpdatePurchaseStatusResponse), err |
| | | } |
| | | |
| | |
| | | return nil, errors.New("产品id为空") |
| | | } |
| | | var product test.SupplierMaterial |
| | | err := global.GVA_DB.Model(&test.SupplierMaterial{}).Where("number = ?", req.ProductId).First(&product).Error |
| | | err := global.GVA_DB.Model(&test.SupplierMaterial{}).Where("number = ?", req.ProductId).Order("id desc").First(&product).Error |
| | | if err != nil { |
| | | if err == gorm.ErrRecordNotFound { |
| | | var material test.Material |
| | |
| | | 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 |
| | |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | | purchaseIds := make([]uint, 0) |
| | | for _, p := range ps { |
| | | purchaseIds = append(purchaseIds, p.ID) |
| | | } |
| | | if len(purchaseIds) == 0 { |
| | | return nil, errors.New("没有查到采购单") |
| | | } |
| | | pps := make([]*purchase.PurchaseProducts, 0) |
| | | err = global.GVA_DB.Model(&purchase.PurchaseProducts{}).Where("purchase_id in (?)", purchaseIds).Preload("Product").Find(&pps).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) |
| | | if p.Status == purchase.OrderStatusStored || p.Status == purchase.OrderStatusCompleted { |
| | | info.FinishAmount = info.Amount |
| | | } |
| | | for _, pp := range pps { |
| | | if int(p.ID) == pp.PurchaseId { |
| | | ni := info |
| | | ni.ProductId = pp.Product.Number |
| | | ni.ProductName = pp.Product.Name |
| | | ni.Specs = pp.Product.Specifications |
| | | ni.Unit = pp.Product.Unit |
| | | ni.Amount = pp.Amount.IntPart() |
| | | infos = append(infos, &ni) |
| | | } |
| | | } |
| | | } |
| | | resp := new(GetPurchaseInfoResponse) |
| | | resp.Infos = infos |
| | |
| | | resp.Exist = true |
| | | return resp, nil |
| | | } |
| | | |
| | | func (s *Server) CreatePurchaseByAps(ctx context.Context, req *CreatePurchaseByApsRequest) (*CreatePurchaseByWmsResponse, error) { |
| | | if len(req.Req) == 0 { |
| | | return nil, errors.New("产品id为空") |
| | | } |
| | | productIds := make([]string, 0) |
| | | amount := int64(0) |
| | | numMap := make(map[string]int64) |
| | | for _, request := range req.Req { |
| | | productIds = append(productIds, request.ProductId) |
| | | numMap[request.ProductId] = request.Amount |
| | | amount += request.Amount |
| | | } |
| | | var products []test.SupplierMaterial |
| | | var newProducts []test.SupplierMaterial |
| | | err := global.GVA_DB.Model(&test.SupplierMaterial{}).Where("number in (?)", productIds).Order("id desc").Find(&products).Error |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | | for _, id := range productIds { |
| | | flag := false |
| | | for _, product := range products { |
| | | if id == product.Number { |
| | | newProducts = append(newProducts, product) |
| | | flag = true |
| | | break |
| | | } |
| | | } |
| | | if !flag { |
| | | var material test.Material |
| | | var product test.SupplierMaterial |
| | | err = global.GVA_DB.Model(&test.Material{}).Where("id = ?", id).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 |
| | | err = global.GVA_DB.Create(&product).Error |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | | newProducts = append(newProducts, product) |
| | | } |
| | | } |
| | | |
| | | //采购单 |
| | | var purchaseRecord purchase.Purchase |
| | | purchaseRecord.OrderSource = "APS推送" |
| | | purchaseRecord.Name = "APS采购" |
| | | purchaseRecord.ID = 0 |
| | | purchaseRecord.Status = purchase.OrderStatusConfirmed |
| | | purchaseRecord.HandledBy = "admin" |
| | | purchaseRecord.Creator = "admin" |
| | | purchaseRecord.Number = fmt.Sprintf("CG%v", time.Now().Unix()) |
| | | purchaseRecord.Principal = "admin" |
| | | purchaseRecord.OrderType = "采购订单" |
| | | purchaseRecord.Quantity = decimal.NewFromInt(amount) |
| | | purchaseRecord.SourceOrder = req.SourceOrder |
| | | |
| | | price := float64(0) |
| | | var pps []purchase.PurchaseProducts |
| | | for _, product := range newProducts { |
| | | //产品 |
| | | var pp purchase.PurchaseProducts |
| | | pp.ProductId = int(product.ID) |
| | | pp.Amount = decimal.NewFromInt(numMap[product.Number]) |
| | | pp.Price = decimal.NewFromFloat(product.PurchasePrice) |
| | | price += product.PurchasePrice |
| | | pp.Total = pp.Amount.Mul(pp.Price) |
| | | pp.Remark = "APS采购" |
| | | pps = append(pps, pp) |
| | | } |
| | | purchaseRecord.TotalPrice = purchaseRecord.Quantity.Mul(decimal.NewFromFloat(price)) |
| | | purchaseRecord.UnInvoiceAmount = purchaseRecord.TotalPrice |
| | | purchaseRecord.ShouldPayAmount = purchaseRecord.TotalPrice |
| | | |
| | | err = global.GVA_DB.Transaction(func(tx *gorm.DB) error { |
| | | err = tx.Create(&purchaseRecord).Error |
| | | if err != nil { |
| | | return err |
| | | } |
| | | for i := 0; i < len(pps); i++ { |
| | | pps[i].PurchaseId = int(purchaseRecord.ID) |
| | | } |
| | | return tx.Create(&pps).Error |
| | | }) |
| | | if err != nil { |
| | | return nil, err |
| | | } |
| | | resp := new(CreatePurchaseByWmsResponse) |
| | | resp.PurchaseNumber = purchaseRecord.Number |
| | | return resp, nil |
| | | } |