| | |
| | | "wms/extend/util" |
| | | "wms/models" |
| | | "wms/pkg/timex" |
| | | "wms/proto/init_client" |
| | | "wms/proto/inventory_order" |
| | | "wms/proto/purchase_wms" |
| | | "wms/request" |
| | | ) |
| | | |
| | |
| | | if params.PageInfo.Check() { |
| | | search.SetPage(params.Page, params.PageSize) |
| | | } |
| | | rules, total, err := search.SetPreload(true).SetKeyword(params.KeyWord).Find() |
| | | rules, total, err := search.SetPreload(true).SetKeyword(params.KeyWord).SetLocationId(params.LocationId).SetProductId(params.ProductId).Find() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查询重订货规则列表失败") |
| | | return |
| | |
| | | productIds = append(productIds, rule.ProductId) |
| | | locationIds = append(locationIds, rule.LocationId) |
| | | } |
| | | if params.LocationId != 0 { |
| | | locationIds = []int{params.LocationId} |
| | | } |
| | | if params.ProductId != "" { |
| | | productIds = []string{params.ProductId} |
| | | } |
| | | //在库 |
| | | var status = []constvar.OperationStatus{constvar.OperationStatus_Finish, constvar.OperationStatus_Ready} |
| | | amounts, err := models.NewLocationProductAmountSearch().SetProductIds(productIds).SetLocationIds(locationIds).Find() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查询在库数量失败") |
| | | return |
| | | } |
| | | for _, rule := range rules { |
| | | for _, amount := range amounts { |
| | | if rule.ProductId == amount.ProductId && rule.LocationId == amount.LocationId { |
| | | rule.Amount = rule.Amount.Add(amount.Amount) |
| | | } |
| | | } |
| | | } |
| | | |
| | | //预测 |
| | | //入库就绪 |
| | | var status = []constvar.OperationStatus{constvar.OperationStatus_Ready} |
| | | var operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeIncoming, constvar.BaseOperationTypeInternal} |
| | | amount, err := GetProductAmount(productIds, locationIds, nil, status, operationType) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查询重订货规则列表失败") |
| | | return |
| | | } |
| | | mp := make(map[int]decimal.Decimal) |
| | | for _, rule := range rules { |
| | | for _, productAmount := range amount { |
| | | if rule.ProductId == productAmount.ProductId && rule.LocationId == productAmount.ToLocationId && |
| | | productAmount.Status == constvar.OperationStatus_Finish { |
| | | rule.Amount = rule.Amount.Add(productAmount.Amount) |
| | | } |
| | | if rule.ProductId == productAmount.ProductId && rule.LocationId == productAmount.ToLocationId && |
| | | productAmount.Status == constvar.OperationStatus_Ready { |
| | | mp[rule.Id] = mp[rule.Id].Add(productAmount.Amount) |
| | | if rule.ProductId == productAmount.ProductId && rule.LocationId == productAmount.ToLocationId { |
| | | rule.Prediction = rule.Prediction.Add(productAmount.Amount) |
| | | } |
| | | } |
| | | rule.Prediction = rule.Amount.Add(rule.Prediction) |
| | | } |
| | | //预测 |
| | | status = []constvar.OperationStatus{constvar.OperationStatus_Finish, constvar.OperationStatus_Ready} |
| | | operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeInternal} |
| | | //出库就绪 |
| | | operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeInternal, constvar.BaseOperationTypeDisuse} |
| | | amount, err = GetProductAmount(productIds, nil, locationIds, status, operationType) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查询重订货规则列表失败") |
| | |
| | | for _, rule := range rules { |
| | | for _, productAmount := range amount { |
| | | if rule.ProductId == productAmount.ProductId && rule.LocationId == productAmount.FromLocationId { |
| | | rule.Prediction = rule.Prediction.Add(productAmount.Amount) |
| | | rule.Prediction = rule.Prediction.Sub(productAmount.Amount) |
| | | } |
| | | } |
| | | rule.Prediction = rule.Amount.Add(mp[rule.Id]).Sub(rule.Prediction) |
| | | } |
| | | var result []*models.ReorderRule |
| | | if params.Type == "" { |
| | | result = rules |
| | | } else { |
| | | for _, rule := range rules { |
| | | if rule.MinInventory.GreaterThan(rule.Prediction) { |
| | | result = append(result, rule) |
| | | } |
| | | } |
| | | } |
| | | |
| | | util.ResponseFormatList(c, code.Success, rules, int(total)) |
| | | util.ResponseFormatList(c, code.Success, result, int(total)) |
| | | } |
| | | |
| | | // 计算在库与预测数量 |
| | |
| | | var pa []request.ProductAmount |
| | | search := models.NewOperationDetailsSearch() |
| | | search.Orm = search.Orm.Model(&models.OperationDetails{}). |
| | | Select("wms_operation_details.product_id, wms_operation_details.amount, wms_operation.to_location_id as to_location_id, " + |
| | | "wms_operation.from_location_id as from_location_id, wms_operation.status"). |
| | | Select("wms_operation_details.product_id, wms_operation_details.amount, wms_operation_details.to_location_id as to_location_id, " + |
| | | "wms_operation_details.from_location_id as from_location_id, wms_operation.base_operation_type"). |
| | | Joins("left join wms_operation on wms_operation_details.operation_id = wms_operation.id") |
| | | if len(productIds) > 0 { |
| | | search.Orm.Where("wms_operation_details.product_id in (?)", productIds) |
| | | } |
| | | if len(toLocationIds) > 0 { |
| | | search.Orm.Where("wms_operation.to_location_id in (?)", toLocationIds) |
| | | search.Orm.Where("wms_operation_details.to_location_id in (?)", toLocationIds) |
| | | } |
| | | if len(fromLocationIds) > 0 { |
| | | search.Orm.Where("wms_operation.from_location_id in (?)", fromLocationIds) |
| | | search.Orm.Where("wms_operation_details.from_location_id in (?)", fromLocationIds) |
| | | } |
| | | if len(status) > 0 { |
| | | search.Orm.Where("wms_operation.status in (?)", status) |
| | |
| | | productIds = append(productIds, params.ProductId) |
| | | locationIds = append(locationIds, params.LocationId) |
| | | amount := decimal.NewFromInt(0) |
| | | p := decimal.NewFromInt(0) |
| | | prediction := decimal.NewFromInt(0) |
| | | //在库 |
| | | var status = []constvar.OperationStatus{constvar.OperationStatus_Finish, constvar.OperationStatus_Ready} |
| | | var operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeIncoming, constvar.BaseOperationTypeInternal} |
| | | list, err := GetProductAmount(productIds, locationIds, nil, status, operationType) |
| | | find, err := models.NewLocationProductAmountSearch().SetProductIds(productIds).SetLocationIds(locationIds).Find() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查询重订货规则列表失败") |
| | | util.ResponseFormat(c, code.RequestParamError, "查询在库数量失败") |
| | | return |
| | | } |
| | | for _, productAmount := range list { |
| | | if params.ProductId == productAmount.ProductId && params.LocationId == productAmount.ToLocationId && |
| | | productAmount.Status == constvar.OperationStatus_Finish { |
| | | for _, productAmount := range find { |
| | | if params.ProductId == productAmount.ProductId && params.LocationId == productAmount.LocationId { |
| | | amount = amount.Add(productAmount.Amount) |
| | | } |
| | | if params.ProductId == productAmount.ProductId && params.LocationId == productAmount.ToLocationId && |
| | | productAmount.Status == constvar.OperationStatus_Ready { |
| | | p = p.Add(productAmount.Amount) |
| | | } |
| | | } |
| | | |
| | | //预测 |
| | | status = []constvar.OperationStatus{constvar.OperationStatus_Finish, constvar.OperationStatus_Ready} |
| | | operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeInternal} |
| | | list, err = GetProductAmount(productIds, nil, locationIds, status, operationType) |
| | | //入库就绪 |
| | | var status = []constvar.OperationStatus{constvar.OperationStatus_Ready} |
| | | var operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeIncoming, constvar.BaseOperationTypeInternal} |
| | | list, err := GetProductAmount(productIds, locationIds, nil, status, operationType) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查询重订货规则列表失败") |
| | | return |
| | |
| | | prediction = prediction.Add(productAmount.Amount) |
| | | } |
| | | } |
| | | prediction = amount.Add(p).Sub(prediction) |
| | | prediction = amount.Add(prediction) |
| | | //出库就绪 |
| | | operationType = []constvar.BaseOperationType{constvar.BaseOperationTypeOutgoing, constvar.BaseOperationTypeInternal, constvar.BaseOperationTypeDisuse} |
| | | list, err = GetProductAmount(productIds, nil, locationIds, status, operationType) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查询重订货规则列表失败") |
| | | return |
| | | } |
| | | for _, productAmount := range list { |
| | | if params.ProductId == productAmount.ProductId && params.LocationId == productAmount.FromLocationId { |
| | | prediction = prediction.Sub(productAmount.Amount) |
| | | } |
| | | } |
| | | m := make(map[string]int64) |
| | | m["amount"] = amount.IntPart() |
| | | m["prediction"] = prediction.IntPart() |
| | |
| | | util.ResponseFormat(c, code.Success, "更新成功") |
| | | } |
| | | |
| | | //var InventoryOrderServiceConn *grpc.ClientConn |
| | | // |
| | | //func InitInventoryOrderServiceConn() { |
| | | // var err error |
| | | // InventoryOrderServiceConn, err = grpc.Dial(conf.GrpcServerConf.ApsAddr, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| | | // if err != nil { |
| | | // logx.Errorf("grpc dial product service error: %v", err.Error()) |
| | | // return |
| | | // } |
| | | //} |
| | | // |
| | | //func CloseInventoryOrderServiceConn() { |
| | | // if InventoryOrderServiceConn != nil { |
| | | // InventoryOrderServiceConn.Close() |
| | | // } |
| | | //} |
| | | |
| | | // OrderAgain |
| | | // @Tags 重订货规则 |
| | | // @Summary 再订一次 |
| | |
| | | util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") |
| | | return |
| | | } |
| | | if params.Route == "采购" { |
| | | client := purchase_wms.NewPurchaseServiceClient(init_client.SrmConn) |
| | | resp, err := client.GetSupplierListByProductId(c, &purchase_wms.GetSupplierListByProductIdRequest{ProductId: params.ProductId}) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "grpc调用失败") |
| | | return |
| | | } |
| | | util.ResponseFormat(c, code.Success, resp.List) |
| | | return |
| | | } |
| | | client := inventory_order.NewInventoryOrderServiceClient(init_client.ApsConn) |
| | | order, err := client.CreateNewOrder(c, &inventory_order.CreateNewOrderRequest{ |
| | | OrderNumber: params.OrderNumber.IntPart(), |
| | | Unit: params.Unit, |
| | | ProductId: params.ProductId, |
| | | Customer: "WMS推送", |
| | | }) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "grpc调用失败") |
| | | return |
| | | } |
| | | err = orderAgain(params, order.OrderId) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "重订失败") |
| | | return |
| | | } |
| | | util.ResponseFormat(c, code.Success, "重订成功") |
| | | } |
| | | |
| | | func orderAgain(params models.ReorderRule, SourceNumber string) error { |
| | | location, err := models.NewLocationSearch().SetID(params.LocationId).First() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查询位置信息失败") |
| | | return |
| | | return err |
| | | } |
| | | houseCode := strings.Split(location.JointName, "/")[0] |
| | | var operationType models.OperationType |
| | | err = models.NewOperationTypeSearch().Orm.Model(&models.OperationType{}).Joins("left join wms_warehouse on wms_job_type.warehouse_id = wms_warehouse.id"). |
| | | Where("wms_job_type.base_operation_type = 1 and wms_warehouse.code = ?", houseCode).First(&operationType).Error |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查询位置信息失败") |
| | | return |
| | | return err |
| | | } |
| | | var operation models.Operation |
| | | var details models.OperationDetails |
| | | details.ProductId = params.ProductId |
| | | details.Amount = params.OrderNumber |
| | | details.FromLocationID = 1 |
| | | details.ToLocationID = params.LocationId |
| | | operation.Details = append(operation.Details, &details) |
| | | operation.BaseOperationType = constvar.BaseOperationTypeIncoming |
| | | operation.Status = constvar.OperationStatus_Ready |
| | | operation.OperationTypeId = operationType.Id |
| | | operation.OperationTypeName = operationType.Name |
| | | operation.OperationDate = timex.TimeToString2(time.Now()) |
| | | //todo 供应商位置 |
| | | operation.FromLocationID = 1 |
| | | operation.LocationID = params.LocationId |
| | | operation.Number = strconv.FormatInt(time.Now().Unix(), 10) |
| | | operation.ToLocationID = params.LocationId |
| | | |
| | | operation.SourceNumber = SourceNumber |
| | | |
| | | err = models.WithTransaction(func(db *gorm.DB) error { |
| | | if err = models.NewOperationSearch().SetOrm(db).Create(&operation); err != nil { |
| | | return err |
| | | } |
| | | params.OrderNumber = decimal.NewFromInt(0) |
| | | err = models.NewReorderRuleSearch().SetID(params.Id).Update(¶ms) |
| | | err = models.NewReorderRuleSearch().SetOrm(db).SetID(params.Id).Update(¶ms) |
| | | return err |
| | | }) |
| | | |
| | | return err |
| | | } |
| | | |
| | | // SubmitOrder |
| | | // @Tags 重订货规则 |
| | | // @Summary 再订一次 |
| | | // @Produce application/json |
| | | // @Param object body models.ReorderRule true "参数" |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-wms/v1/reorderRule/submitOrder [post] |
| | | func (slf ReorderRuleController) SubmitOrder(c *gin.Context) { |
| | | var params models.ReorderRule |
| | | client := purchase_wms.NewPurchaseServiceClient(init_client.SrmConn) |
| | | resp, err := client.CreatePurchaseByWms(c, &purchase_wms.CreatePurchaseByWmsRequest{ |
| | | SupplierId: params.SupplierId, |
| | | ProductId: params.ProductId, |
| | | Amount: params.OrderNumber.IntPart(), |
| | | Source: "APS", |
| | | }) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "grpc调用失败") |
| | | return |
| | | } |
| | | err = orderAgain(params, resp.PurchaseNumber) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "重订失败") |
| | | return |
| | | } |
| | | util.ResponseFormat(c, code.Success, "重订成功") |
| | | } |