| | |
| | | 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 |
| | | } |