|
@@ -1,441 +0,0 @@
|
|
|
-package api
|
|
|
-
|
|
|
-import (
|
|
|
- "box-cost/db/model"
|
|
|
- "fmt"
|
|
|
-
|
|
|
- "github.com/xuri/excelize/v2"
|
|
|
-)
|
|
|
-
|
|
|
-type ReportProduceExcel struct {
|
|
|
- Offset int
|
|
|
- Row int
|
|
|
-
|
|
|
- Title string //标题
|
|
|
-
|
|
|
- Excel *excelize.File
|
|
|
-
|
|
|
- SheetName string
|
|
|
-
|
|
|
- AlignCenterStyle int
|
|
|
-
|
|
|
- Content *model.ProduceBill
|
|
|
-
|
|
|
- BudgetAmount float64
|
|
|
- RealAmount float64
|
|
|
-}
|
|
|
-
|
|
|
-func (b *ReportProduceExcel) drawTitle() error {
|
|
|
- b.Row++
|
|
|
-
|
|
|
- // 设置外边距
|
|
|
- startCell := fmt.Sprintf("A%d", b.Row)
|
|
|
- endCell := fmt.Sprintf("J%d", b.Row)
|
|
|
- marginLeft := excelize.PageMarginLeft(0.15)
|
|
|
-
|
|
|
- if b.Content.IsPrint {
|
|
|
- // A加工项目 B规格 C纸张 D来纸尺寸 E印刷尺寸 F下单数量 G完成数量 H单价 I预算金额 J实际金额 K交货时间 L备注
|
|
|
- b.Excel.SetColWidth(b.SheetName, "A", "A", 16)
|
|
|
- b.Excel.SetColWidth(b.SheetName, "B", "B", 14)
|
|
|
- b.Excel.SetColWidth(b.SheetName, "C", "C", 10)
|
|
|
- b.Excel.SetColWidth(b.SheetName, "D", "E", 14)
|
|
|
- b.Excel.SetColWidth(b.SheetName, "F", "K", 12)
|
|
|
- b.Excel.SetColWidth(b.SheetName, "J", "J", 18)
|
|
|
-
|
|
|
- } else {
|
|
|
- // 不是打印
|
|
|
- // A加工项目 B规格 C下单数量 D完成数量 E单价 F预算金额 G实际金额 H交货时间 I备注
|
|
|
- endCell = fmt.Sprintf("I%d", b.Row)
|
|
|
- marginLeft = excelize.PageMarginLeft(0.6)
|
|
|
- b.Excel.SetColWidth(b.SheetName, "A", "A", 16)
|
|
|
- b.Excel.SetColWidth(b.SheetName, "B", "B", 14)
|
|
|
- b.Excel.SetColWidth(b.SheetName, "C", "H", 12)
|
|
|
- b.Excel.SetColWidth(b.SheetName, "I", "I", 18)
|
|
|
- // 是覆膜
|
|
|
- if b.Content.IsLam {
|
|
|
-
|
|
|
- // A加工项目 B规格 C覆膜尺寸 D下单数量 E完成数量 F元/吨,G元/张 H预算金额 I实际金额 J交货时间 K备注
|
|
|
- endCell = fmt.Sprintf("K%d", b.Row)
|
|
|
- marginLeft = excelize.PageMarginLeft(0.3)
|
|
|
- b.Excel.SetColWidth(b.SheetName, "A", "A", 16)
|
|
|
- b.Excel.SetColWidth(b.SheetName, "B", "C", 14)
|
|
|
- // 覆膜规格
|
|
|
- b.Excel.SetColWidth(b.SheetName, "D", "J", 12)
|
|
|
- b.Excel.SetColWidth(b.SheetName, "K", "K", 18)
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- b.Excel.SetPageMargins(b.SheetName, marginLeft)
|
|
|
- err := b.Excel.MergeCell(b.SheetName, startCell, endCell)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- style, err := b.Excel.NewStyle(&excelize.Style{
|
|
|
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
|
|
|
- Font: &excelize.Font{Bold: true, Size: 18}})
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- b.Excel.SetRowHeight(b.SheetName, b.Row, 23)
|
|
|
- b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (b *ReportProduceExcel) drawSubTitles() error {
|
|
|
- b.Row++
|
|
|
-
|
|
|
- styleLeft, err := b.Excel.NewStyle(&excelize.Style{
|
|
|
- Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
|
|
|
- Font: &excelize.Font{Size: 11}})
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- styleRight, err := b.Excel.NewStyle(&excelize.Style{
|
|
|
- Alignment: &excelize.Alignment{Horizontal: "right", Vertical: "center"},
|
|
|
- Font: &excelize.Font{Size: 11}})
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- var drawLeft = func(rowIndex int, value string) error {
|
|
|
- //左边1
|
|
|
- left1Cell := fmt.Sprintf("A%d", rowIndex)
|
|
|
- err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("I%d", rowIndex))
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- b.Excel.SetCellValue(b.SheetName, left1Cell, value)
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- var drawRight = func(rowIndex int, value string) error {
|
|
|
- right1Cell := fmt.Sprintf("J%d", rowIndex)
|
|
|
- err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("L%d", rowIndex))
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- b.Excel.SetCellValue(b.SheetName, right1Cell, value)
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- // !isPrint
|
|
|
- if !b.Content.IsPrint {
|
|
|
-
|
|
|
- drawLeft = func(rowIndex int, value string) error {
|
|
|
- //左边1
|
|
|
- left1Cell := fmt.Sprintf("A%d", rowIndex)
|
|
|
- err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("F%d", rowIndex))
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- b.Excel.SetCellValue(b.SheetName, left1Cell, value)
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- drawRight = func(rowIndex int, value string) error {
|
|
|
- right1Cell := fmt.Sprintf("G%d", rowIndex)
|
|
|
- err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("I%d", rowIndex))
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- b.Excel.SetCellValue(b.SheetName, right1Cell, value)
|
|
|
- return nil
|
|
|
- }
|
|
|
- if b.Content.IsLam {
|
|
|
- drawLeft = func(rowIndex int, value string) error {
|
|
|
- //左边1
|
|
|
- left1Cell := fmt.Sprintf("A%d", rowIndex)
|
|
|
- err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("H%d", rowIndex))
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- b.Excel.SetCellValue(b.SheetName, left1Cell, value)
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- drawRight = func(rowIndex int, value string) error {
|
|
|
- right1Cell := fmt.Sprintf("I%d", rowIndex)
|
|
|
- err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("K%d", rowIndex))
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- b.Excel.SetCellValue(b.SheetName, right1Cell, value)
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- //第一行
|
|
|
- drawLeft(b.Row, "类别:"+b.Content.Type)
|
|
|
- drawRight(b.Row, "单号:"+b.Content.SerialNumber)
|
|
|
- b.Excel.SetRowHeight(b.SheetName, b.Row, 21)
|
|
|
-
|
|
|
- //第二行
|
|
|
- drawLeft(b.Row+1, "供应商名称:"+b.Content.Supplier)
|
|
|
- timeformat := b.Content.CreateTime.Local().Format("2006年01月02号 15:04:05")
|
|
|
- drawRight(b.Row+1, "下单时间:"+timeformat)
|
|
|
- b.Excel.SetRowHeight(b.SheetName, b.Row+1, 21)
|
|
|
-
|
|
|
- //第三行
|
|
|
- drawLeft(b.Row+2, "产品名称:"+b.Content.ProductName)
|
|
|
- status := ""
|
|
|
- if b.Content.Status == "complete" {
|
|
|
- status = "已完成"
|
|
|
- }
|
|
|
- if b.Content.Status == "created" {
|
|
|
- status = "进行中"
|
|
|
- }
|
|
|
- drawRight(b.Row+2, "状态:"+status)
|
|
|
- b.Excel.SetRowHeight(b.SheetName, b.Row+2, 21)
|
|
|
-
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (b *ReportProduceExcel) drawTableTitle() error {
|
|
|
- b.Row += 3
|
|
|
-
|
|
|
- //A加工项目 B规格(克) 尺寸C-D 数量E 单价F-G 交货时间H 备注I
|
|
|
- var drawCol = func(prefix string, value string) error {
|
|
|
- left1Cell := fmt.Sprintf("%s%d", prefix, b.Row)
|
|
|
- left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1)
|
|
|
-
|
|
|
- err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
|
|
|
- }
|
|
|
-
|
|
|
- var drawCol2 = func(prefix string, value1 string, value2 string) error {
|
|
|
-
|
|
|
- left1Cell := fmt.Sprintf("%s%d", prefix, b.Row)
|
|
|
- left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1)
|
|
|
- err := b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
|
|
|
- b.Excel.SetCellValue(b.SheetName, left2Cell, value2)
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- var drawCol3 = func(prefix1 string, prefix2 string, value1 string, value2 string, value3 string) error {
|
|
|
- left1Cell := fmt.Sprintf("%s%d", prefix1, b.Row)
|
|
|
- left2Cell := fmt.Sprintf("%s%d", prefix2, b.Row)
|
|
|
- err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
- return err
|
|
|
- }
|
|
|
- err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
|
|
|
-
|
|
|
- val2Cel := fmt.Sprintf("%s%d", prefix1, b.Row+1)
|
|
|
- b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
|
|
|
- b.Excel.SetCellValue(b.SheetName, val2Cel, value2)
|
|
|
-
|
|
|
- val3Cel := fmt.Sprintf("%s%d", prefix2, b.Row+1)
|
|
|
- b.Excel.SetCellStyle(b.SheetName, val3Cel, val3Cel, b.AlignCenterStyle)
|
|
|
- b.Excel.SetCellValue(b.SheetName, val3Cel, value3)
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- unit := b.Content.Produces[0].Unit
|
|
|
- if b.Content.IsPrint {
|
|
|
- drawCol("A", "加工项目")
|
|
|
- drawCol("B", "规格")
|
|
|
- drawCol("C", "纸张")
|
|
|
- drawCol("D", "来纸尺寸")
|
|
|
- drawCol("E", "印刷尺寸")
|
|
|
- drawCol("F", "下单数量")
|
|
|
- drawCol("G", "完成数量")
|
|
|
- drawCol2("H", "单价", unit)
|
|
|
- drawCol("I", "预算金额")
|
|
|
- drawCol("J", "实际金额")
|
|
|
- drawCol("K", "交货时间")
|
|
|
- drawCol("L", "备注")
|
|
|
-
|
|
|
- } else {
|
|
|
- if b.Content.IsLam {
|
|
|
- drawCol("A", "加工项目")
|
|
|
- drawCol("B", "规格")
|
|
|
- drawCol("C", "覆膜尺寸")
|
|
|
- drawCol("D", "下单数量")
|
|
|
- drawCol("E", "完成数量")
|
|
|
- unit2 := b.Content.Produces[0].Unit2
|
|
|
- drawCol3("E", "G", "单价", unit, unit2)
|
|
|
- drawCol("H", "预算金额")
|
|
|
- drawCol("I", "实际金额")
|
|
|
- drawCol("J", "交货时间")
|
|
|
- drawCol("K", "备注")
|
|
|
-
|
|
|
- } else {
|
|
|
- drawCol("A", "加工项目")
|
|
|
- drawCol("B", "规格")
|
|
|
- drawCol("C", "下单数量")
|
|
|
- drawCol("D", "完成数量")
|
|
|
- drawCol2("E", "单价", unit)
|
|
|
- drawCol("F", "预算金额")
|
|
|
- drawCol("G", "实际金额")
|
|
|
- drawCol("H", "交货时间")
|
|
|
- drawCol("I", "备注")
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (b *ReportProduceExcel) drawTableContent() error {
|
|
|
- b.Row += 2
|
|
|
- var DrawRow = func(rowIndex int, values ...string) {
|
|
|
- charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"}
|
|
|
- // !isPrint
|
|
|
- if !b.Content.IsPrint {
|
|
|
- charas = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I"}
|
|
|
- if b.Content.IsLam {
|
|
|
- charas = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"}
|
|
|
- }
|
|
|
- }
|
|
|
- for i, c := range charas {
|
|
|
- v := ""
|
|
|
- if i < len(values) {
|
|
|
- v = values[i]
|
|
|
- }
|
|
|
- b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
|
|
|
- val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
|
|
|
- b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
|
|
|
-
|
|
|
- // var magN float64 = 1
|
|
|
- // lenv := len([]rune(v))
|
|
|
- // magN = math.Ceil(float64(lenv) / 10)
|
|
|
- // b.Excel.SetRowHeight(b.SheetName, rowIndex, 21*magN)
|
|
|
- b.Excel.SetRowHeight(b.SheetName, rowIndex, 46)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- produces := b.Content.Produces
|
|
|
-
|
|
|
- if len(produces) > 0 {
|
|
|
- for _, produce := range produces {
|
|
|
- realCount := ""
|
|
|
- price := produce.OrderPrice
|
|
|
- priceStr := fmt.Sprintf("%.3f", price)
|
|
|
- b.FormatToEmpty(&priceStr)
|
|
|
- // 预算金额
|
|
|
- budgetAmount := fmt.Sprintf("%.3f", produce.OrderPrice*float64(produce.OrderCount))
|
|
|
- b.FormatToEmpty(&budgetAmount)
|
|
|
- // 实际金额
|
|
|
- realPrice := ""
|
|
|
- // 实际完成数
|
|
|
- realCount = fmt.Sprintf("%d", produce.ConfirmCount)
|
|
|
- b.FormatToEmpty(&realCount)
|
|
|
- realPrice = fmt.Sprintf("%.3f", produce.OrderPrice*float64(produce.ConfirmCount))
|
|
|
- b.FormatToEmpty(&realPrice)
|
|
|
-
|
|
|
- deliveryTime := produce.DeliveryTime.Local().Format("2006-01-02")
|
|
|
-
|
|
|
- // !isPrint
|
|
|
- if !b.Content.IsPrint {
|
|
|
-
|
|
|
- if b.Content.IsLam {
|
|
|
- DrawRow(b.Row, produce.Name, produce.Norm, produce.PrintSize, fmt.Sprintf("%d", produce.OrderCount), realCount, priceStr, fmt.Sprintf("%.3f", produce.Price2), budgetAmount, realPrice, deliveryTime, produce.Remark)
|
|
|
-
|
|
|
- } else {
|
|
|
- DrawRow(b.Row, produce.Name, produce.Norm, fmt.Sprintf("%d", produce.OrderCount), realCount, priceStr, budgetAmount, realPrice, deliveryTime, produce.Remark)
|
|
|
- }
|
|
|
- } else {
|
|
|
- DrawRow(b.Row, produce.Name, produce.Norm, produce.Paper, produce.PaperSize, produce.PrintSize, fmt.Sprintf("%d", produce.OrderCount), realCount, priceStr, budgetAmount, realPrice, deliveryTime, produce.Remark)
|
|
|
-
|
|
|
- }
|
|
|
- b.Row++
|
|
|
- b.BudgetAmount += produce.OrderPrice * float64(produce.OrderCount)
|
|
|
- b.RealAmount += produce.OrderPrice * float64(produce.ConfirmCount)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (b *ReportProduceExcel) Draws() {
|
|
|
- b.drawTitle()
|
|
|
- b.drawSubTitles()
|
|
|
- b.drawTableTitle()
|
|
|
- b.drawTableContent()
|
|
|
-}
|
|
|
-
|
|
|
-func NewReportProduceExcel(f *excelize.File) *ReportProduceExcel {
|
|
|
-
|
|
|
- border := []excelize.Border{
|
|
|
- {Type: "top", Style: 1, Color: "000000"},
|
|
|
- {Type: "left", Style: 1, Color: "000000"},
|
|
|
- {Type: "right", Style: 1, Color: "000000"},
|
|
|
- {Type: "bottom", Style: 1, Color: "000000"},
|
|
|
- }
|
|
|
-
|
|
|
- styleLeft, _ := f.NewStyle(&excelize.Style{
|
|
|
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
|
|
|
- Border: border,
|
|
|
- })
|
|
|
-
|
|
|
- b := &ReportProduceExcel{
|
|
|
- Title: "中鱼互动加工单",
|
|
|
- SheetName: "Sheet1",
|
|
|
- Excel: f,
|
|
|
- Offset: 0,
|
|
|
- AlignCenterStyle: styleLeft,
|
|
|
- }
|
|
|
- f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
|
|
|
-
|
|
|
- return b
|
|
|
-}
|
|
|
-
|
|
|
-func (b *ReportProduceExcel) FormatToEmpty(str *string) {
|
|
|
- if *str == "0" || *str == "0.000" {
|
|
|
- *str = ""
|
|
|
- }
|
|
|
-}
|