| | |
| | | |
| | | import ( |
| | | "errors" |
| | | "fmt" |
| | | "github.com/gin-gonic/gin" |
| | | "github.com/shopspring/decimal" |
| | | "github.com/spf13/cast" |
| | | "gorm.io/gorm" |
| | | "strconv" |
| | |
| | | //检查明细部分 |
| | | for _, v := range params.Details { |
| | | if v.ProductId == "" { |
| | | return errors.New("productID为0") |
| | | return errors.New("productID为空") |
| | | } |
| | | if v.ProductName == "" { |
| | | return errors.New("产品名称异常") |
| | |
| | | // @Param object body request.UpdateOperation true "入库信息" |
| | | // @Param id path int true "入库信息id" |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-wms/v1/operation/operation/{id} [put] |
| | | // @Router /api-wms/v1/operation/operation/{id} [post] |
| | | func (slf OperationController) Update(c *gin.Context) { |
| | | id := cast.ToUint(c.Param("id")) |
| | | if id == 0 { |
| | |
| | | util.ResponseFormat(c, code.RequestParamError, err.Error()) |
| | | return |
| | | } |
| | | fmt.Printf("%+v\n", *reqParams.Details[0]) |
| | | fmt.Printf("%+v\n", *reqParams.Details[1]) |
| | | fmt.Println("===============================================") |
| | | fmt.Printf("%+v\n", *params.Details[0]) |
| | | fmt.Printf("%+v\n", *params.Details[1]) |
| | | if err := models.WithTransaction(func(tx *gorm.DB) error { |
| | | if err := models.NewOperationDetailsSearch().SetOrm(tx).SetOperationId(params.Id).Delete(); err != nil { |
| | | return err |
| | |
| | | util.ResponseFormat(c, code.RequestError, "该出入库信息无法完成") |
| | | return |
| | | } |
| | | //operationType, err := models.NewOperationTypeSearch().SetID(uint(operation.OperationTypeId)).First() |
| | | //if err != nil { |
| | | // util.ResponseFormat(c, code.RequestError, err.Error()) |
| | | // return |
| | | //} |
| | | //if operationType.BaseOperationType == constvar.BaseOperationTypeIncoming { |
| | | // if location, err := models.NewLocationSearch().SetType(int(constvar.LocationTypeVendor)).First(); err != nil { |
| | | // return err |
| | | // } else { |
| | | // params.FromLocationId = location.Id |
| | | // } |
| | | // if params.ToLocationId == 0 { |
| | | // return errors.New("请选择目标位置") |
| | | // } |
| | | //} |
| | | operationType, err := models.NewOperationTypeSearch().SetID(uint(operation.OperationTypeId)).First() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestError, err.Error()) |
| | | return |
| | | } |
| | | if err := models.WithTransaction(func(tx *gorm.DB) error { |
| | | |
| | | if err := models.NewOperationSearch().SetOrm(tx).SetID(id).Update(&models.Operation{Status: constvar.OperationStatus_Finish}); err != nil { |
| | | return err |
| | | } |
| | | if err := models.NewMaterialSearch().Orm.Exec("update material INNER JOIN wms_operation_details on wms_operation_details.product_id=material.id INNER JOIN wms_operation on wms_operation.id=wms_operation_details.operation_id set material.amount=material.amount + wms_operation_details.quantity where wms_operation.id=?", id).Error; err != nil { |
| | | return err |
| | | if operationType.BaseOperationType == constvar.BaseOperationTypeIncoming { |
| | | if err := models.NewMaterialSearch().Orm.Exec("update material INNER JOIN wms_operation_details on wms_operation_details.product_id=material.id INNER JOIN wms_operation on wms_operation.id=wms_operation_details.operation_id set material.amount=material.amount + wms_operation_details.quantity where wms_operation.id=?", id).Error; err != nil { |
| | | return err |
| | | } |
| | | } else if operationType.BaseOperationType == constvar.BaseOperationTypeOutgoing { |
| | | var listProdtId []string |
| | | var listProdt []*models.Material |
| | | mapProdt := make(map[string]decimal.Decimal) |
| | | listDetails, err := models.NewOperationDetailsSearch().SetOperationId(operation.Id).FindAll() |
| | | if err != nil { |
| | | return err |
| | | } |
| | | for _, v := range listDetails { |
| | | listProdtId = append(listProdtId, v.ProductId) |
| | | mapProdt[v.ProductId] = v.Quantity |
| | | } |
| | | if err := models.NewMaterialSearch().Orm.Where("id IN ?", listProdtId).Find(&listProdt).Error; err != nil { |
| | | return err |
| | | } |
| | | for _, v := range listProdt { |
| | | if value, ok := mapProdt[v.ID]; !ok { |
| | | return errors.New("产品种类异常") |
| | | } else { |
| | | if v.Amount.LessThan(value) { |
| | | return errors.New(fmt.Sprintf("产品:%v,库存:%v,出库:%v,数量不够,无法完成出库操作", v.Name, v.Amount.String(), value.String())) |
| | | } |
| | | } |
| | | } |
| | | if err := models.NewMaterialSearch().Orm.Exec("update material INNER JOIN wms_operation_details on wms_operation_details.product_id=material.id INNER JOIN wms_operation on wms_operation.id=wms_operation_details.operation_id set material.amount=material.amount - wms_operation_details.quantity where wms_operation.id=?", id).Error; err != nil { |
| | | return err |
| | | } |
| | | } |
| | | return nil |
| | | }); err != nil { |