zhangqian
2024-06-13 97d6acaf340b19d66244967b00dd2fdff410e034
controllers/operation.go
@@ -2,7 +2,6 @@
import (
   "context"
   "encoding/json"
   "errors"
   "fmt"
   "github.com/gin-gonic/gin"
@@ -10,7 +9,6 @@
   "github.com/shopspring/decimal"
   "github.com/xuri/excelize/v2"
   "gorm.io/gorm"
   "os"
   "sort"
   "strconv"
   "strings"
@@ -19,8 +17,8 @@
   "wms/constvar"
   "wms/extend/code"
   "wms/extend/util"
   "wms/middleware"
   "wms/models"
   "wms/opa"
   "wms/pkg/logx"
   "wms/pkg/mysqlx"
   "wms/pkg/structx"
@@ -78,7 +76,8 @@
      util.ResponseFormat(c, code.RequestParamError, err.Error())
      return
   }
   userInfo := middleware.GetUserInfo(c)
   params.CreatedBy = userInfo.Username
   params.Status = constvar.OperationStatus_Ready
   //params.Number = strconv.FormatInt(time.Now().Unix(), 10)
   params.BaseOperationType = operationType.BaseOperationType
@@ -91,6 +90,23 @@
   if numberNum > 0 {
      util.ResponseFormat(c, code.RequestParamError, "单号已存在")
      return
   }
   var productIds []string
   for _, v := range params.Details {
      productIds = append(productIds, v.ProductId)
   }
   products, err := models.NewMaterialSearch().SetIDs(productIds).FindNotTotal()
   if err != nil {
      logx.Errorf("MonthStats get products err:%v", err)
      return
   }
   productMap := models.MaterialMap(products)
   for i, v := range params.Details {
      material := productMap[v.ProductId]
      if material != nil {
         params.Details[i].Note = material.Note
      }
   }
   if err := models.NewOperationSearch().Create(&params); err != nil {
@@ -158,7 +174,6 @@
            return errors.New("请选择源位置")
         }
      }
   }
   return nil
}
@@ -346,11 +361,16 @@
   }
   operation, err := models.NewOperationSearch().SetID(id).First()
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "未找到相关出入库信息:"+err.Error())
      util.ResponseFormat(c, code.RequestParamError, "未找到相关出入库单据:"+err.Error())
      return
   }
   userInfo := middleware.GetUserInfo(c)
   if operation.CreatedBy != userInfo.Username {
      util.ResponseFormat(c, code.RequestError, "别人的出入库单据无法进行删除")
      return
   }
   if operation.Status != constvar.OperationStatus_Ready {
      util.ResponseFormat(c, code.RequestError, "该入库信息无法进行删除")
      util.ResponseFormat(c, code.RequestError, "该出入库单据无法进行删除")
      return
   }
   if err := models.WithTransaction(func(tx *gorm.DB) error {
@@ -414,9 +434,14 @@
         mapLocAmount[strconv.Itoa(v.LocationId)+v.ProductId] = v
      }
   }
   userInfo := middleware.GetUserInfo(c)
   err = models.WithTransaction(func(tx *gorm.DB) error {
      if err := models.NewOperationSearch().SetOrm(tx).SetID(id).Update(&models.Operation{Status: constvar.OperationStatus_Finish, AuditDate: time.Now().Format("2006-01-02 15:04:05")}); err != nil {
      if err := models.NewOperationSearch().SetOrm(tx).SetID(id).Update(&models.Operation{
         Status:    constvar.OperationStatus_Finish,
         CheckedBy: userInfo.Username,
         CheckedAt: time.Now(),
         AuditDate: time.Now().Format("2006-01-02 15:04:05")}); err != nil {
         return err
      }
      if err := AddMoveHistory([]*models.Operation{operation}, tx); err != nil {
@@ -535,6 +560,8 @@
               Comment:           operation.Comment,
               BaseOperationType: constvar.BaseOperationTypeInternal,
               Details:           details,
               CheckedAt:         time.Now(),
               CheckedBy:         userInfo.Username,
            }).Error; err != nil {
               return err
            }
@@ -542,23 +569,23 @@
      }
      if operation.BaseOperationType == constvar.BaseOperationTypeOutgoing {
      if operation.BaseOperationType == constvar.BaseOperationTypeOutgoing || operation.BaseOperationType == constvar.BaseOperationTypeDisuse {
         for k, v := range listDetails {
            //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.Product.Amount.LessThan(v.Amount) {
                  return errors.New(fmt.Sprintf("产品:%v,库存:%v,出库:%v,数量不够,无法完成出库操作", v.Product.Name, v.Product.Amount.String(), v.Amount.String()))
               }
            }
            //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.Product.Amount.LessThan(v.Amount) {
            //      return errors.New(fmt.Sprintf("产品:%v,库存:%v,出库:%v,数量不够,无法完成出库操作", v.Product.Name, v.Product.Amount.String(), v.Amount.String()))
            //   }
            //}
            //todo ================end===============================
            if v.Product.Amount.LessThan(v.Amount) {
               return errors.New(fmt.Sprintf("产品:%v,库存:%v,出库:%v,数量不够,无法完成出库操作", v.Product.Name, v.Product.Amount.String(), v.Amount.String()))
@@ -614,12 +641,34 @@
            }
         }
      }
      if operation.BaseOperationType == constvar.BaseOperationTypeAdjust {
         for _, v := range listDetails {
            if locAmount, aok := mapLocAmount[strconv.Itoa(v.ToLocationID)+v.ProductId]; aok {
               locAmount.Amount = v.Amount
               if res := models.NewLocationProductAmountSearch().Orm.Where("id=?", locAmount.ID).Save(locAmount); res.Error != nil {
                  return res.Error
               }
            } else {
               if err := models.NewLocationProductAmountSearch().Create(&models.LocationProductAmount{
                  LocationId:        v.ToLocationID,
                  ProductCategoryID: v.Product.CategoryId,
                  ProductId:         v.ProductId,
                  Amount:            v.Amount,
                  CreateDate:        time.Now().Format("2006-01-02 15:04:05"),
               }); err != nil {
                  return err
               }
            }
         }
      }
      return nil
   })
   if err != nil {
      util.ResponseFormat(c, code.RequestError, err.Error())
      return
   }
   //修改其他系统订单状态
   if operation.BaseOperationType == constvar.BaseOperationTypeIncoming {
      if operation.Source != "" {
@@ -871,14 +920,19 @@
   }
   var fileUrl string
   companyName := conf.WebConf.CompanyName
   if companyName == "" {
      util.ResponseFormat(c, code.RequestParamError, "缺少工厂名称配置")
      return
   }
   if operation.BaseOperationType == constvar.BaseOperationTypeIncoming {
      if conf.WebConf.CompanyName == "jialian" {
         fileUrl, err = JialianInputOperation(operation)
      if companyName == "jialian" {
         fileUrl, err = JialianOperation(constvar.FileTemplateCategory_JialianInput, operation)
         if err != nil {
            util.ResponseFormat(c, code.RequestParamError, err.Error())
            return
         }
      } else {
      } else if companyName == "geruimi" {
         fileUrl, err = ExportInputSelfmade(constvar.FileTemplateCategory_Selfmade, operation)
         if err != nil {
            util.ResponseFormat(c, code.RequestParamError, err.Error())
@@ -886,21 +940,32 @@
         }
      }
   } else if operation.BaseOperationType == constvar.BaseOperationTypeOutgoing {
      fileUrl, err = ExportOutputOperation(constvar.FileTemplateCategory_Output, operation)
      if err != nil {
         util.ResponseFormat(c, code.RequestParamError, err.Error())
         return
      if companyName == "jialian" {
         fileUrl, err = JialianOperation(constvar.FileTemplateCategory_JialianOutput, operation)
         if err != nil {
            util.ResponseFormat(c, code.RequestParamError, err.Error())
            return
         }
      } else if companyName == "geruimi" {
         fileUrl, err = ExportInputSelfmade(constvar.FileTemplateCategory_Output, operation)
         if err != nil {
            util.ResponseFormat(c, code.RequestParamError, err.Error())
            return
         }
      }
   } else {
      util.ResponseFormat(c, code.RequestParamError, "该记录不支持打印")
      return
   }
   util.ResponseFormat(c, code.Success, fileUrl)
   m := make(map[string]string)
   m["url"] = fileUrl
   m["name"] = companyName
   util.ResponseFormat(c, code.Success, m)
}
func JialianInputOperation(operation *models.Operation) (string, error) {
   template, err := models.NewFileTemplateAttachmentSearch().SetCategory(14).First()
func JialianOperation(category constvar.FileTemplateCategory, operation *models.Operation) (string, error) {
   template, err := models.NewFileTemplateAttachmentSearch().SetCategory(category).First()
   if err != nil {
      return "", errors.New("获取模版记录失败:" + err.Error())
   }
@@ -916,12 +981,12 @@
   readerCloser.Close()
   defer f.Close()
   f.SetCellValue("Sheet1", "C2", operation.CompanyName)
   f.SetCellValue("Sheet1", "H2", operation.OperationDate)
   f.SetCellValue("Sheet1", "O2", operation.Number)
   rowIndex := 5
   totalAmount := decimal.NewFromInt(0)
   totalPrice := decimal.NewFromInt(0)
   for i, v := range operation.Details {
      //设置表单最多9条数据
      if i > 8 {
@@ -931,10 +996,57 @@
      f.SetCellValue("Sheet1", "B"+strconv.Itoa(rowIndex), v.Product.Type)
      f.SetCellValue("Sheet1", "C"+strconv.Itoa(rowIndex), v.Product.Unit)
      f.SetCellValue("Sheet1", "D"+strconv.Itoa(rowIndex), v.Amount.String())
      f.SetCellValue("Sheet1", "E"+strconv.Itoa(rowIndex), v.Product.SalePrice.String())
      if !v.Product.SalePrice.IsZero() {
         ap := v.Amount.Mul(v.Product.SalePrice)
         totalPrice = totalPrice.Add(ap)
         price := ap.String()
         split := strings.Split(price, ".")
         if len(split) == 2 {
            for n, a := range split[1] {
               f.SetCellValue("Sheet1", string('M'+n)+strconv.Itoa(rowIndex), string(a))
            }
         }
         s := split[0]
         n := 0
         for j := len(s) - 1; j >= 0; j-- {
            if n == 6 {
               f.SetCellValue("Sheet1", string('L'-n)+strconv.Itoa(rowIndex), s[:j])
               break
            }
            f.SetCellValue("Sheet1", string('L'-n)+strconv.Itoa(rowIndex), string(s[j]))
            n++
         }
      }
      rowIndex++
      totalAmount = totalAmount.Add(v.Amount)
   }
   //合计
   if !totalPrice.IsZero() {
      price := totalPrice.String()
      split := strings.Split(price, ".")
      if len(split) == 2 {
         for n, a := range split[1] {
            f.SetCellValue("Sheet1", string('M'+n)+"14", string(a))
         }
      } else {
         f.SetCellValue("Sheet1", "M14", "0")
         f.SetCellValue("Sheet1", "N14", "0")
      }
      s := split[0]
      n := 0
      for j := len(s) - 1; j >= 0; j-- {
         if n == 6 {
            f.SetCellValue("Sheet1", string('L'-n)+"14", s[:j])
            break
         }
         f.SetCellValue("Sheet1", string('L'-n)+"14", string(s[j]))
         n++
      }
   }
   f.SetCellValue("Sheet1", "D14", totalAmount)
   f.SetCellValue("Sheet1", "B15", operation.Manager)
   f.SetCellValue("Sheet1", "D15", operation.Accountant)
   f.SetCellValue("Sheet1", "F15", operation.Custodian)