jiangshuai
2023-11-08 2bfc37cbc679ceb5cef3b714e93ce4ebf55f9512
controllers/operation.go
@@ -1,11 +1,13 @@
package controllers
import (
   "encoding/json"
   "errors"
   "fmt"
   "github.com/gin-gonic/gin"
   "github.com/shopspring/decimal"
   "gorm.io/gorm"
   "os"
   "sort"
   "strconv"
   "time"
@@ -13,6 +15,7 @@
   "wms/extend/code"
   "wms/extend/util"
   "wms/models"
   "wms/opa"
   "wms/pkg/logx"
   "wms/pkg/structx"
   "wms/request"
@@ -71,7 +74,7 @@
}
func CheckDetailsRepeat(details []*models.OperationDetails) bool {
   detailIDs := []string{}
   var detailIDs []string
   var tempID string
   for _, v := range details {
      detailIDs = append(detailIDs, v.ProductId)
@@ -314,7 +317,7 @@
      util.ResponseFormat(c, code.RequestParamError, "id为0")
      return
   }
   operation, err := models.NewOperationSearch().SetID(id).First()
   operation, err := models.NewOperationSearch().SetPreload(true).SetID(id).First()
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "未找到相关出入库信息:"+err.Error())
      return
@@ -323,17 +326,12 @@
      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 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 operationType.BaseOperationType != constvar.BaseOperationTypeInternal {
      var listProdtId []string
      var listProdt []*models.Material
      mapProdt := make(map[string]decimal.Decimal)
@@ -345,96 +343,337 @@
         listProdtId = append(listProdtId, v.ProductId)
         mapProdt[v.ProductId] = v.Amount
      }
      if err := models.NewMaterialSearch().Orm.Where("id IN ?", listProdtId).Find(&listProdt).Error; err != nil {
         return err
      }
      if operationType.BaseOperationType == constvar.BaseOperationTypeIncoming {
      if operation.BaseOperationType == constvar.BaseOperationTypeIncoming {
         var operationInputs []*models.Operation
         for k, v := range listProdt {
            if value, ok := mapProdt[v.ID]; !ok {
            value, ok := mapProdt[v.ID]
            if !ok {
               return errors.New("产品种类异常")
            } else {
               listProdt[k].Amount = listProdt[k].Amount.Add(value)
               if err := tx.Save(listProdt[k]).Error; err != nil {
                  return err
            }
            listProdt[k].Amount = listProdt[k].Amount.Add(value)
            if err := tx.Save(listProdt[k]).Error; err != nil {
               return err
            }
            locationRule, err := models.NewLocationProductSearch().SetProductId(v.ID).SetAreaId(operation.ToLocationID).First()
            if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
               return err
            }
            if err == nil {
               operationTransfer := &models.Operation{
                  Number:            operation.Number,
                  SourceNumber:      operation.SourceNumber,
                  OperationTypeId:   0,
                  OperationTypeName: operation.OperationTypeName,
                  Status:            constvar.OperationStatus_Finish,
                  FromLocationID:    locationRule.AreaId,
                  ToLocationID:      locationRule.LocationId,
                  OperationDate:     operation.OperationDate,
                  ContacterID:       operation.ContacterID,
                  ContacterName:     operation.ContacterName,
                  CompanyID:         operation.CompanyID,
                  CompanyName:       operation.CompanyName,
                  Comment:           operation.Comment,
                  BaseOperationType: constvar.BaseOperationTypeInternal,
                  Details: []*models.OperationDetails{
                     {
                        ProductId: v.ID,
                        Amount:    value,
                     },
                  },
               }
               operationInputs = append(operationInputs, operationTransfer)
               //if err := tx.Create(&operationTransfer).Error; err != nil {
               //   return err
               //}
               //TODO:出入库的finish和报废的finish都要增加对location_product_amount表数量的更新,因为此表有ProductCategory字段,所以operation_details表中要增加ProductCategoryId字段
               var locAmount models.LocationProductAmount
               if err := models.NewLocationProductAmountSearch().Orm.
                  Table("wms_location_product_amount").
                  Joins("inner join wms_location_product on wms_location_product.id=wms_location_product_amount.location_product_id").
                  Where("wms_location_product.product_id=? and wms_location_product.location_id=?", v.ID, operation.ToLocationID).
                  First(&locAmount).Error; err != nil {
               locAmount, err := models.NewLocationProductAmountSearch().
                  SetProductId(v.ID).
                  SetLocationId(locationRule.LocationId).
                  First()
               if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
                  return err
               }
               locAmount.LocationId = locationRule.LocationId
               locAmount.ProductId = v.ID
               locAmount.CreateDate = time.Now().Format("2006-01-02 15:04:05")
               locAmount.Amount = locAmount.Amount.Add(value)
               if err := models.NewLocationProductAmountSearch().SetID(locAmount.Id).Update(&locAmount); err != nil {
               if res := models.NewLocationProductAmountSearch().Orm.Where("id=?", locAmount.ID).Save(locAmount); res.Error != nil {
                  return res.Error
               }
            } else {
               locationRule, err = models.NewLocationProductSearch().SetProductCategoryId(v.CategoryId).SetAreaId(operation.ToLocationID).First()
               if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
                  return err
               }
               if err == nil {
                  operationTransfer := &models.Operation{
                     Number:            operation.Number,
                     SourceNumber:      operation.SourceNumber,
                     OperationTypeId:   0,
                     OperationTypeName: operation.OperationTypeName,
                     Status:            constvar.OperationStatus_Finish,
                     FromLocationID:    locationRule.AreaId,
                     ToLocationID:      locationRule.LocationId,
                     OperationDate:     operation.OperationDate,
                     ContacterID:       operation.ContacterID,
                     ContacterName:     operation.ContacterName,
                     CompanyID:         operation.CompanyID,
                     CompanyName:       operation.CompanyName,
                     Comment:           operation.Comment,
                     BaseOperationType: constvar.BaseOperationTypeInternal,
                     Details: []*models.OperationDetails{
                        {
                           ProductId: v.ID,
                           Amount:    value,
                        },
                     },
                  }
                  operationInputs = append(operationInputs, operationTransfer)
                  //if err := tx.Create(&operationTransfer).Error; err != nil {
                  //   return err
                  //}
                  //TODO:出入库的finish和报废的finish都要增加对location_product_amount表数量的更新,因为此表有ProductCategory字段,所以operation_details表中要增加ProductCategoryId字段
                  locAmount, err := models.NewLocationProductAmountSearch().
                     SetProductId(v.ID).
                     SetLocationId(locationRule.LocationId).
                     First()
                  if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
                     return err
                  }
                  locAmount.LocationId = locationRule.LocationId
                  locAmount.ProductId = v.ID
                  locAmount.CreateDate = time.Now().Format("2006-01-02 15:04:05")
                  locAmount.Amount = locAmount.Amount.Add(value)
                  if res := models.NewLocationProductAmountSearch().Orm.Where("id=?", locAmount.ID).Save(locAmount); res.Error != nil {
                     return res.Error
                  }
               } else {
                  //TODO:出入库的finish和报废的finish都要增加对location_product_amount表数量的更新,因为此表有ProductCategory字段,所以operation_details表中要增加ProductCategoryId字段
                  locAmount, err := models.NewLocationProductAmountSearch().
                     SetProductId(v.ID).
                     SetLocationId(operation.ToLocationID).
                     First()
                  if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
                     return err
                  }
                  locAmount.LocationId = operation.ToLocationID
                  locAmount.ProductId = v.ID
                  locAmount.CreateDate = time.Now().Format("2006-01-02 15:04:05")
                  locAmount.Amount = locAmount.Amount.Add(value)
                  if res := models.NewLocationProductAmountSearch().Orm.Where("id=?", locAmount.ID).Save(locAmount); res.Error != nil {
                     return res.Error
                  }
               }
            }
         }
         if len(operationInputs) > 0 {
            if err := tx.Create(&operationInputs).Error; err != nil {
               return err
            }
         }
      }
      if operationType.BaseOperationType == constvar.BaseOperationTypeOutgoing {
      if operation.BaseOperationType == constvar.BaseOperationTypeOutgoing {
         for k, v := range listProdt {
            if value, ok := mapProdt[v.ID]; !ok {
            value, ok := mapProdt[v.ID]
            if !ok {
               return errors.New("产品种类异常")
            } else {
            }
            //todo 演示测试数据
            data, err := os.ReadFile("conf/input.json")
            if err != nil {
               return errors.New("文件读取失败")
            }
            m := make(map[string]interface{})
            err = json.Unmarshal(data, &m)
            if err != nil {
               return errors.New("格式转换失败")
            }
            if opa.OpaCheck(c, m, "operation") {
               if v.Amount.LessThan(value) {
                  return errors.New(fmt.Sprintf("产品:%v,库存:%v,出库:%v,数量不够,无法完成出库操作", v.Name, v.Amount.String(), value.String()))
               }
               listProdt[k].Amount = listProdt[k].Amount.Sub(value)
               if err := tx.Save(listProdt[k]).Error; err != nil {
                  return err
               }
               var locAmount models.LocationProductAmount
               if err := models.NewLocationProductAmountSearch().Orm.Table("wms_location_product_amount").Joins("inner join wms_location_product on wms_location_product.id=wms_location_product_amount.location_product_id").Where("wms_location_product.product_id=? and wms_location_product.location_id=?", v.ID, operation.FromLocationID).First(&locAmount).Error; err != nil {
                  return err
               }
               if locAmount.Amount.LessThan(value) {
                  return errors.New(fmt.Sprintf("产品:%v,库存:%v,出库:%v,数量不够,无法完成出库操作", v.Name, v.Amount.String(), value.String()))
               }
               locAmount.Amount = locAmount.Amount.Sub(value)
               if err := models.NewLocationProductAmountSearch().SetID(locAmount.Id).Update(&locAmount); err != nil {
                  return err
               }
            }
            listProdt[k].Amount = listProdt[k].Amount.Sub(value)
            if err := tx.Save(listProdt[k]).Error; err != nil {
               return err
            }
            locAmount, res := models.NewLocationProductAmountSearch().
               SetProductId(v.ID).
               SetLocationId(operation.ToLocationID).
               FirstRes()
            if res.Error != nil {
               return err
            }
            if locAmount.Amount.LessThan(value) {
               return errors.New(fmt.Sprintf("产品:%v,库存:%v,出库:%v,数量不够,无法完成出库操作", v.Name, v.Amount.String(), value.String()))
            }
            locAmount.Amount = locAmount.Amount.Sub(value)
            if err := models.NewLocationProductAmountSearch().SetID(locAmount.Id).Update(locAmount); err != nil {
               return err
            }
         }
      }
      if operationType.BaseOperationType == constvar.BaseOperationTypeInternal {
         for k, v := range listProdt {
            if value, ok := mapProdt[v.ID]; !ok {
      if operation.BaseOperationType == constvar.BaseOperationTypeInternal {
         var operationInputs []*models.Operation
         for _, v := range listProdt {
            value, ok := mapProdt[v.ID]
            if !ok {
               return errors.New("产品种类异常")
            } else {
               listProdt[k].Amount = listProdt[k].Amount.Add(value)
               if err := tx.Save(listProdt[k]).Error; err != nil {
                  return err
               }
               //TODO:出入库的finish和报废的finish都要增加对location_product_amount表数量的更新,因为此表有ProductCategory字段,所以operation_details表中要增加ProductCategoryId字段
               var fromAmount, toAmount models.LocationProductAmount
               if err := models.NewLocationProductAmountSearch().Orm.Table("wms_location_product_amount").Joins("inner join wms_location_product on wms_location_product.id=wms_location_product_amount.location_product_id").Where("wms_location_product.product_id=? and wms_location_product.location_id=?", v.ID, operation.FromLocationID).Find(&fromAmount).Error; err != nil {
                  return err
               }
               if fromAmount.Amount.LessThan(value) {
                  return errors.New(fmt.Sprintf("产品:%v,库存:%v,调拨:%v,数量不够,无法完成调拨操作", v.Name, v.Amount.String(), value.String()))
               }
               fromAmount.Amount = fromAmount.Amount.Sub(value)
               if err := models.NewLocationProductAmountSearch().SetID(fromAmount.Id).Update(&fromAmount); err != nil {
                  return err
               }
            }
            //listProdt[k].Amount = listProdt[k].Amount.Add(value)
            //if err := tx.Save(listProdt[k]).Error; err != nil {
            //   return err
            //}
            fromLocAmount, res := models.NewLocationProductAmountSearch().
               SetProductId(v.ID).
               SetLocationId(operation.FromLocationID).
               FirstRes()
            if res.Error != nil {
               return err
            }
            if fromLocAmount.Amount.LessThan(value) {
               return errors.New(fmt.Sprintf("产品:%v,库存:%v,调拨:%v,数量不够,无法完成调拨操作", v.Name, v.Amount.String(), value.String()))
            }
            fromLocAmount.Amount = fromLocAmount.Amount.Sub(value)
            if err := models.NewLocationProductAmountSearch().SetID(fromLocAmount.Id).Update(fromLocAmount); err != nil {
               return err
            }
               if err := models.NewLocationProductAmountSearch().Orm.Table("wms_location_product_amount").Joins("inner join wms_location_product on wms_location_product.id=wms_location_product_amount.location_product_id").Where("wms_location_product.product_id=? and wms_location_product.location_id=?", v.ID, operation.ToLocationID).Find(&toAmount).Error; err != nil {
            locationRule, err := models.NewLocationProductSearch().SetProductId(v.ID).SetAreaId(operation.ToLocationID).First()
            if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
               return err
            }
            if err == nil {
               operationTransfer := &models.Operation{
                  Number:            operation.Number,
                  SourceNumber:      operation.SourceNumber,
                  OperationTypeId:   0,
                  OperationTypeName: operation.OperationTypeName,
                  Status:            constvar.OperationStatus_Finish,
                  FromLocationID:    locationRule.AreaId,
                  ToLocationID:      locationRule.LocationId,
                  OperationDate:     operation.OperationDate,
                  ContacterID:       operation.ContacterID,
                  ContacterName:     operation.ContacterName,
                  CompanyID:         operation.CompanyID,
                  CompanyName:       operation.CompanyName,
                  Comment:           operation.Comment,
                  BaseOperationType: constvar.BaseOperationTypeInternal,
                  Details: []*models.OperationDetails{
                     {
                        ProductId: v.ID,
                        Amount:    value,
                     },
                  },
               }
               operationInputs = append(operationInputs, operationTransfer)
               //if err := tx.Create(&operationTransfer).Error; err != nil {
               //   return err
               //}
               locAmount, err := models.NewLocationProductAmountSearch().
                  SetProductId(v.ID).
                  SetLocationId(locationRule.LocationId).
                  First()
               if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
                  return err
               }
               toAmount.Amount = toAmount.Amount.Add(value)
               if err := models.NewLocationProductAmountSearch().SetID(toAmount.Id).Update(&toAmount); err != nil {
               locAmount.LocationId = locationRule.LocationId
               locAmount.ProductId = v.ID
               locAmount.CreateDate = time.Now().Format("2006-01-02 15:04:05")
               locAmount.Amount = locAmount.Amount.Add(value)
               if res := models.NewLocationProductAmountSearch().Orm.Where("id=?", locAmount.ID).Save(locAmount); res.Error != nil {
                  return res.Error
               }
            } else {
               locationRule, err = models.NewLocationProductSearch().SetProductCategoryId(v.CategoryId).SetAreaId(operation.ToLocationID).First()
               if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
                  return err
               }
               if err == nil {
                  operationTransfer := &models.Operation{
                     Number:            operation.Number,
                     SourceNumber:      operation.SourceNumber,
                     OperationTypeId:   0,
                     OperationTypeName: operation.OperationTypeName,
                     Status:            constvar.OperationStatus_Finish,
                     FromLocationID:    locationRule.AreaId,
                     ToLocationID:      locationRule.LocationId,
                     OperationDate:     operation.OperationDate,
                     ContacterID:       operation.ContacterID,
                     ContacterName:     operation.ContacterName,
                     CompanyID:         operation.CompanyID,
                     CompanyName:       operation.CompanyName,
                     Comment:           operation.Comment,
                     BaseOperationType: constvar.BaseOperationTypeInternal,
                     Details: []*models.OperationDetails{
                        {
                           ProductId: v.ID,
                           Amount:    value,
                        },
                     },
                  }
                  operationInputs = append(operationInputs, operationTransfer)
                  //if err := tx.Create(&operationTransfer).Error; err != nil {
                  //   return err
                  //}
                  locAmount, err := models.NewLocationProductAmountSearch().
                     SetProductId(v.ID).
                     SetLocationId(locationRule.LocationId).
                     First()
                  if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
                     return err
                  }
                  locAmount.LocationId = locationRule.LocationId
                  locAmount.ProductId = v.ID
                  locAmount.CreateDate = time.Now().Format("2006-01-02 15:04:05")
                  locAmount.Amount = locAmount.Amount.Add(value)
                  if res := models.NewLocationProductAmountSearch().Orm.Where("id=?", locAmount.ID).Save(locAmount); res.Error != nil {
                     return res.Error
                  }
               } else {
                  locAmount, err := models.NewLocationProductAmountSearch().
                     SetProductId(v.ID).
                     SetLocationId(operation.ToLocationID).
                     First()
                  if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
                     return err
                  }
                  locAmount.LocationId = operation.ToLocationID
                  locAmount.ProductId = v.ID
                  locAmount.CreateDate = time.Now().Format("2006-01-02 15:04:05")
                  locAmount.Amount = locAmount.Amount.Add(value)
                  if res := models.NewLocationProductAmountSearch().Orm.Where("id=?", locAmount.ID).Save(locAmount); res.Error != nil {
                     return res.Error
                  }
               }
            }
         }
         if len(operationInputs) > 0 {
            if err := tx.Create(&operationInputs).Error; err != nil {
               return err
            }
         }
      }
      //}
      return nil
   }); err != nil {
      util.ResponseFormat(c, code.RequestError, err.Error())