| | |
| | | "github.com/shopspring/decimal" |
| | | "github.com/xuri/excelize/v2" |
| | | "gorm.io/gorm" |
| | | "io/ioutil" |
| | | "sort" |
| | | "strconv" |
| | | "strings" |
| | |
| | | util.ResponseFormat(c, code.RequestParamError, err.Error()) |
| | | return |
| | | } |
| | | fileTemplateAttachments, err := models.NewFileTemplateAttachmentSearch().SetWarehouseId(oT.WarehouseId).Find() |
| | | warehouse, err := models.NewWarehouseSearch().SetID(oT.WarehouseId).First() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, err.Error()) |
| | | return |
| | | } |
| | | for _, fileTemplateAttachment := range fileTemplateAttachments { |
| | | if fileTemplateAttachment.Category == constvar.FileTemplateCategory_JialianInput1 { |
| | | //fileUrl, err = JialianOperation(constvar.FileTemplateCategory_JialianInput1, operation) |
| | | fileUrl, err = JialianOperation(fileTemplateAttachment.Category, operation) |
| | | // 出库 |
| | | //fileUrl, err = JialianOperation(constvar.FileTemplateCategory_JialianOutput1, operation) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, err.Error()) |
| | | return |
| | | } |
| | | break |
| | | } else if fileTemplateAttachment.Category == constvar.FileWarehouseCategory_JialianInput2 { |
| | | fileUrl, err = JialianOperation(fileTemplateAttachment.Category, operation) |
| | | // 出库 |
| | | //fileUrl, err = JialianOperation(constvar.FileTemplateCategory_JialianOutput1, operation) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, err.Error()) |
| | | return |
| | | } |
| | | break |
| | | } |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, err.Error()) |
| | | return |
| | | } |
| | | |
| | | if warehouse.FileTemplateCategoryIn == constvar.FileTemplateCategory_JialianInput1 { |
| | | fileUrl, err = JialianOperation2(warehouse.FileTemplateCategoryIn, operation) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, err.Error()) |
| | | return |
| | | } |
| | | |
| | | } else if warehouse.FileTemplateCategoryIn == constvar.FileWarehouseCategory_JialianInput2 { |
| | | fileUrl, err = JialianOperation1(warehouse.FileTemplateCategoryIn, operation) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, err.Error()) |
| | | return |
| | | } |
| | | |
| | | } |
| | | |
| | | } else if companyName == "geruimi" { |
| | | fileUrl, err = ExportInputSelfmade(constvar.FileTemplateCategory_Selfmade, operation) |
| | | if err != nil { |
| | |
| | | util.ResponseFormat(c, code.RequestParamError, err.Error()) |
| | | return |
| | | } |
| | | fileTemplateAttachments, err := models.NewFileTemplateAttachmentSearch().SetWarehouseId(oT.WarehouseId).Find() |
| | | warehouse, err := models.NewWarehouseSearch().SetID(oT.WarehouseId).First() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, err.Error()) |
| | | return |
| | | } |
| | | for _, fileTemplateAttachment := range fileTemplateAttachments { |
| | | if fileTemplateAttachment.Category == constvar.FileTemplateCategory_JialianOutput1 { |
| | | //fileUrl, err = JialianOperation(constvar.FileTemplateCategory_JialianInput1, operation) |
| | | fileUrl, err = JialianOperation(fileTemplateAttachment.Category, operation) |
| | | // 出库 |
| | | //fileUrl, err = JialianOperation(constvar.FileTemplateCategory_JialianOutput1, operation) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, err.Error()) |
| | | return |
| | | } |
| | | break |
| | | } else if fileTemplateAttachment.Category == constvar.FileWarehouseCategory_JialianOutput2 { |
| | | fileUrl, err = JialianOperation(fileTemplateAttachment.Category, operation) |
| | | // 出库 |
| | | //fileUrl, err = JialianOperation(constvar.FileTemplateCategory_JialianOutput1, operation) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, err.Error()) |
| | | return |
| | | } |
| | | break |
| | | } |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, err.Error()) |
| | | return |
| | | } |
| | | |
| | | if warehouse.FileTemplateCategoryOut == constvar.FileTemplateCategory_JialianOutput1 { |
| | | fileUrl, err = JialianOperation2(warehouse.FileTemplateCategoryOut, operation) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, err.Error()) |
| | | return |
| | | } |
| | | |
| | | } else if warehouse.FileTemplateCategoryOut == constvar.FileWarehouseCategory_JialianOutput2 { |
| | | fileUrl, err = JialianOperation1(warehouse.FileTemplateCategoryOut, 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.Success, m) |
| | | } |
| | | |
| | | func JialianOperation(category constvar.FileTemplateCategory, operation *models.Operation) (string, error) { |
| | | func JialianOperation1(category constvar.FileTemplateCategory, operation *models.Operation) (string, error) { |
| | | // 获取模板记录 |
| | | template, err := models.NewFileTemplateAttachmentSearch().SetCategory(category).First() |
| | | if err != nil { |
| | |
| | | defer f.Close() |
| | | |
| | | sheet := "Sheet1" |
| | | if category == constvar.FileTemplateCategory_JialianInput1 { |
| | | if category == constvar.FileWarehouseCategory_JialianInput2 { |
| | | sheet = "入库单" |
| | | } else if category == constvar.FileTemplateCategory_JialianOutput1 { |
| | | } else if category == constvar.FileWarehouseCategory_JialianOutput2 { |
| | | sheet = "出库单" |
| | | } |
| | | |
| | |
| | | break |
| | | } |
| | | f.SetCellValue(sheet, "B"+strconv.Itoa(rowIndex), v.Product.Name) // 产品名称 |
| | | f.SetCellValue(sheet, "C"+strconv.Itoa(rowIndex), v.Product.Unit) //规格 |
| | | f.SetCellValue(sheet, "C"+strconv.Itoa(rowIndex), v.Product.Specs) //规格 |
| | | 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()) //重量 |
| | |
| | | } |
| | | |
| | | fileUrl, err := upload.UploadFileToSeaWeed(string(constvar.FileType_File), uuid.NewV4().String()+".xlsx", buf.Bytes()) |
| | | ioutil.WriteFile("E:\\"+uuid.NewV4().String()+".xlsx", buf.Bytes(), 0644) |
| | | if err != nil { |
| | | //ioutil.WriteFile("E:\\"+uuid.NewV4().String()+".xlsx", buf.Bytes(), 0644) |
| | | logx.Errorf("file upload err: %v", err) |
| | |
| | | return fileUrl, nil |
| | | } |
| | | |
| | | func JialianOperation2(category constvar.FileTemplateCategory, operation *models.Operation) (string, error) { |
| | | // 获取模板记录 |
| | | template, err := models.NewFileTemplateAttachmentSearch().SetCategory(category).First() |
| | | if err != nil { |
| | | return "", errors.New("获取模版记录失败:" + err.Error()) |
| | | } |
| | | fmt.Println(template) |
| | | // 根据模板路径获取模板消息 |
| | | readerCloser, err := http.HttpGetWithReadCloser(template.FileUrl) |
| | | if err != nil { |
| | | return "", errors.New("获取模版失败:" + err.Error()) |
| | | } |
| | | |
| | | // 读取模板内容 |
| | | f, err := excelize.OpenReader(readerCloser) |
| | | if err != nil { |
| | | return "", errors.New("读取excel模版失败:" + err.Error()) |
| | | } |
| | | readerCloser.Close() |
| | | |
| | | defer f.Close() |
| | | |
| | | sheet := "Sheet1" |
| | | f.SetCellValue(sheet, "A2", "单位(部门):"+operation.CompanyName) // 单位部门 |
| | | f.SetCellValue(sheet, "H2", operation.OperationDate) // 时间 |
| | | f.SetCellValue(sheet, "O2", operation.Number) // 编号 |
| | | |
| | | totalAmount := decimal.NewFromInt(0) // 总金额 |
| | | totalPrice := decimal.NewFromInt(0) // 总价 |
| | | rowIndex := 5 |
| | | for i, v := range operation.Details { |
| | | //设置表单最多9条数据 |
| | | if i > 9 { |
| | | break |
| | | } |
| | | f.SetCellValue(sheet, "A"+strconv.Itoa(rowIndex), v.Product.Name) // 产品名称 |
| | | f.SetCellValue(sheet, "B"+strconv.Itoa(rowIndex), v.Product.Specs) //规格 |
| | | f.SetCellValue(sheet, "C"+strconv.Itoa(rowIndex), v.TotalGrossWeight.String()) //重量 |
| | | f.SetCellValue(sheet, "D"+strconv.Itoa(rowIndex), v.Amount.String()) //数量 |
| | | f.SetCellValue(sheet, "E"+strconv.Itoa(rowIndex), v.AuxiliaryAmount.String()) //辅助数量 |
| | | f.SetCellValue(sheet, "F"+strconv.Itoa(rowIndex), v.AuxiliaryUnit) //辅助单位 |
| | | f.SetCellValue(sheet, "G"+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(sheet, string('O'+n)+strconv.Itoa(rowIndex), string(a)) |
| | | } |
| | | } |
| | | s := split[0] |
| | | n := 0 |
| | | for j := len(s) - 1; j >= 0; j-- { |
| | | if n == 6 { |
| | | f.SetCellValue(sheet, string('N'-n)+strconv.Itoa(rowIndex), s[:j]) |
| | | break |
| | | } |
| | | f.SetCellValue(sheet, string('N'-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(sheet, string('N'+n)+"14", string(a)) |
| | | } |
| | | } else { |
| | | f.SetCellValue(sheet, "O14", "0") |
| | | f.SetCellValue(sheet, "P14", "0") |
| | | } |
| | | s := split[0] |
| | | n := 0 |
| | | for j := len(s) - 1; j >= 0; j-- { |
| | | if n == 6 { |
| | | f.SetCellValue(sheet, string('N'-n)+"14", s[:j]) |
| | | break |
| | | } |
| | | f.SetCellValue(sheet, string('N'-n)+"14", string(s[j])) |
| | | n++ |
| | | } |
| | | } |
| | | |
| | | f.SetCellValue(sheet, "Q5", operation.Remark) // 备注 |
| | | |
| | | // 第14行 |
| | | f.SetCellValue(sheet, "B15", operation.Manager) // 主管 |
| | | f.SetCellValue(sheet, "D15", operation.Accountant) // 会计 |
| | | f.SetCellValue(sheet, "H15", operation.Custodian) //保管员 |
| | | f.SetCellValue(sheet, "Q15", operation.Custodian) //经手 |
| | | buf, err := f.WriteToBuffer() |
| | | if err != nil { |
| | | return "", err |
| | | } |
| | | |
| | | fileUrl, err := upload.UploadFileToSeaWeed(string(constvar.FileType_File), uuid.NewV4().String()+".xlsx", buf.Bytes()) |
| | | ioutil.WriteFile("E:\\"+uuid.NewV4().String()+".xlsx", buf.Bytes(), 0644) |
| | | if err != nil { |
| | | logx.Errorf("file upload err: %v", err) |
| | | return "", err |
| | | } |
| | | return fileUrl, nil |
| | | } |
| | | |
| | | func ExportInputSelfmade(category constvar.FileTemplateCategory, operation *models.Operation) (string, error) { |
| | | template, err := models.NewFileTemplateAttachmentSearch().SetCategory(category).First() |
| | | if err != nil { |