| | |
| | | |
| | | import ( |
| | | "errors" |
| | | "fmt" |
| | | "github.com/gin-gonic/gin" |
| | | "github.com/shopspring/decimal" |
| | | "github.com/spf13/cast" |
| | | "gorm.io/gorm" |
| | | "strconv" |
| | |
| | | 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 { |