dsmzx
2024-06-24 462071ca05c30354057a6eb3839a4dc6ac6f659a
controllers/operation.go
@@ -3,6 +3,7 @@
import (
   "context"
   "errors"
   "fmt"
   "github.com/gin-gonic/gin"
   uuid "github.com/satori/go.uuid"
   "github.com/shopspring/decimal"
@@ -714,6 +715,7 @@
      util.ResponseFormat(c, code.RequestParamError, "id为0")
      return
   }
   // 获取操作消息
   operation, err := models.NewOperationSearch().SetPreload(true).SetID(id).First()
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, "未找到相关出入库信息:"+err.Error())
@@ -725,13 +727,16 @@
   }
   var fileUrl string
   // 获取公司名称
   companyName := conf.WebConf.CompanyName
   if companyName == "" {
      util.ResponseFormat(c, code.RequestParamError, "缺少工厂名称配置")
      return
   }
   // BaseOperationTypeIncoming 收货
   if operation.BaseOperationType == constvar.BaseOperationTypeIncoming {
      if companyName == "jialian" {
         // 入库
         fileUrl, err = JialianOperation(constvar.FileTemplateCategory_JialianInput, operation)
         if err != nil {
            util.ResponseFormat(c, code.RequestParamError, err.Error())
@@ -744,8 +749,9 @@
            return
         }
      }
   } else if operation.BaseOperationType == constvar.BaseOperationTypeOutgoing {
   } else if operation.BaseOperationType == constvar.BaseOperationTypeOutgoing { // 交货
      if companyName == "jialian" {
         // 出库
         fileUrl, err = JialianOperation(constvar.FileTemplateCategory_JialianOutput, operation)
         if err != nil {
            util.ResponseFormat(c, code.RequestParamError, err.Error())
@@ -770,38 +776,83 @@
}
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())
   }
   // 根据模板路径获取模板消息
   readerCloser, err := http.HttpGetWithReadCloser(template.FileUrl)
   if err != nil {
      return "", errors.New("获取模版失败:" + err.Error())
   }
   // 读取模板内容
   f, err := excelize.OpenReader(readerCloser)
   //fl, err := os.Open("E:\\家纺出入库单.xlsx")
   //f, err := excelize.OpenReader(fl)
   if err != nil {
      return "", errors.New("读取excel模版失败:" + err.Error())
   }
   readerCloser.Close()
   defer f.Close()
   f.SetCellValue("Sheet1", "H2", operation.OperationDate)
   f.SetCellValue("Sheet1", "O2", operation.Number)
   sheet := "Sheet1"
   if category == constvar.FileTemplateCategory_JialianInput {
      sheet = "入库单"
   } else if category == constvar.FileTemplateCategory_JialianOutput {
      sheet = "出库单"
   }
   rowIndex := 5
   totalAmount := decimal.NewFromInt(0)
   totalPrice := decimal.NewFromInt(0)
   // 第二行
   f.SetCellValue(sheet, "K2", operation.Number) // 单号
   // 第三行
   f.SetCellValue(sheet, "D3", operation.CompanyName) // 公司
   f.SetCellValue(sheet, "G3", "")                    // 部门
   // 时间
   date := operation.OperationDate
   f.SetCellValue(sheet, "I3", date[0:4])  // 年
   f.SetCellValue(sheet, "L3", date[5:7])  // 月
   f.SetCellValue(sheet, "N3", date[8:10]) // 日
   attributeMap := make(map[string]uint)
   attributeValueMap := make(map[string]string)
   attributes, err := models.NewAttributeSearch().SetEntityType(1).FindNotTotal()
   if err != nil {
      return "", errors.New("获取动态属性失败:" + err.Error())
   }
   for _, v := range attributes {
      attributeMap[v.Name] = v.ID
   }
   attributeValues, err := models.NewAttributeValueSearch().FindNotTotal()
   if err != nil {
      return "", errors.New("获取动态属性值失败:" + err.Error())
   }
   for _, v := range attributeValues {
      attributeValueMap[fmt.Sprintf("%d%s", v.AttributeID, v.EntityID)] = v.Value
   }
   // 第六行 循环填值
   totalAmount := decimal.NewFromInt(0) // 总金额
   totalPrice := decimal.NewFromInt(0)  // 总价
   rowIndex := 6
   for i, v := range operation.Details {
      //设置表单最多9条数据
      if i > 8 {
      //设置表单最多6条数据
      if i > 6 {
         break
      }
      f.SetCellValue("Sheet1", "A"+strconv.Itoa(rowIndex), v.Product.Name)
      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())
      f.SetCellValue(sheet, "B"+strconv.Itoa(rowIndex), v.Product.Name)                                                          // 产品名称
      f.SetCellValue(sheet, "C"+strconv.Itoa(rowIndex), v.Product.Unit)                                                          //规格
      f.SetCellValue(sheet, "D"+strconv.Itoa(rowIndex), attributeValueMap[fmt.Sprintf("%d%s", attributeMap["颜色"], v.ProductId)]) //颜色
      f.SetCellValue(sheet, "E"+strconv.Itoa(rowIndex), attributeValueMap[fmt.Sprintf("%d%s", attributeMap["面料"], v.ProductId)]) //面料
      f.SetCellValue(sheet, "F"+strconv.Itoa(rowIndex), v.TotalGrossWeight.String())                                             //重量
      f.SetCellValue(sheet, "G"+strconv.Itoa(rowIndex), v.Amount.String())                                                       //数量
      f.SetCellValue(sheet, "H"+strconv.Itoa(rowIndex), v.Product.SalePrice.String())                                            // 单价
      // 金额
      if !v.Product.SalePrice.IsZero() {
         ap := v.Amount.Mul(v.Product.SalePrice)
         totalPrice = totalPrice.Add(ap)
@@ -809,53 +860,54 @@
         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))
               f.SetCellValue(sheet, string('N'+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])
               f.SetCellValue(sheet, string('M'-n)+strconv.Itoa(rowIndex), s[:j])
               break
            }
            f.SetCellValue("Sheet1", string('L'-n)+strconv.Itoa(rowIndex), string(s[j]))
            f.SetCellValue(sheet, string('M'-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))
            f.SetCellValue(sheet, string('M'+n)+"12", string(a))
         }
      } else {
         f.SetCellValue("Sheet1", "M14", "0")
         f.SetCellValue("Sheet1", "N14", "0")
         f.SetCellValue(sheet, "N12", "0")
         f.SetCellValue(sheet, "O12", "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])
            f.SetCellValue(sheet, string('M'-n)+"12", s[:j])
            break
         }
         f.SetCellValue("Sheet1", string('L'-n)+"14", string(s[j]))
         f.SetCellValue(sheet, string('M'-n)+"12", 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)
   // 备注 第13行
   f.SetCellValue(sheet, "B13", "备注:"+operation.Remark)
   // 第14行
   f.SetCellValue(sheet, "C14", operation.Manager)    // 审核
   f.SetCellValue(sheet, "F14", operation.Accountant) // 保管
   f.SetCellValue(sheet, "I14", operation.Custodian)  //经办人
   buf, err := f.WriteToBuffer()
   if err != nil {
      return "", err
@@ -863,6 +915,7 @@
   fileUrl, err := upload.UploadFileToSeaWeed(string(constvar.FileType_File), uuid.NewV4().String()+".xlsx", buf.Bytes())
   if err != nil {
      //ioutil.WriteFile("E:\\"+uuid.NewV4().String()+".xlsx", buf.Bytes(), 0644)
      logx.Errorf("file upload err: %v", err)
      return "", err
   }