package api import ( "box-cost/db/model" "fmt" _ "image/gif" _ "image/jpeg" _ "image/png" "github.com/xuri/excelize/v2" ) type PurchaseBillExcel struct { Offset int Row int Title string Excel *excelize.File SheetName string AlignCenterStyle int Content *model.PurchaseBill Signatures []*model.Signature } func (b *PurchaseBillExcel) drawTitle() error { marginLeft := excelize.PageMarginLeft(0.15) b.Excel.SetPageMargins(b.SheetName, marginLeft) b.Row++ startCell := fmt.Sprintf("A%d", b.Row) err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("L%d", b.Row)) 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 *PurchaseBillExcel) 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("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 } var drawRight = func(rowIndex int, value string) error { right1Cell := fmt.Sprintf("G%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 } //第一行 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 *PurchaseBillExcel) 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(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 } drawCol("A", "采购项目") drawCol("B", "规格(克)") drawCol("E", "下单数量") drawCol("F", "完成数量") drawCol2("C", "D", "尺寸(mm)", "长", "宽") drawCol2("C", "D", "尺寸(mm)", "长", "宽") unit1 := "-" unit2 := "-" // ??? for ragne if len(b.Content.Paper) > 0 { if b.Content.Paper[0].PriceUnit != "" { unit1 = b.Content.Paper[0].PriceUnit } else if b.Content.Paper[0].Price2Unit != "" { unit2 = b.Content.Paper[0].Price2Unit } else { unit1 = "元/张" unit2 = "元/吨" } if b.Content.Paper[0].PriceUnit != "" && b.Content.Paper[0].Price2Unit != "" { unit1 = b.Content.Paper[0].PriceUnit unit2 = b.Content.Paper[0].Price2Unit } } drawCol2("G", "H", "单价", unit1, unit2) drawCol("I", "预算金额") drawCol("J", "实际金额") drawCol("K", "交货时间") drawCol("L", "备注") return nil } func (b *PurchaseBillExcel) 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"} 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) b.Excel.SetRowHeight(b.SheetName, rowIndex, 21) } } papers := b.Content.Paper if len(papers) > 0 { for _, paper := range papers { deliveryTime := paper.DeliveryTime.Local().Format("2006-01-02") realCount := "" realPrice := "" price := fmt.Sprintf("%.3f", paper.OrderPrice) price2 := fmt.Sprintf("%.3f", paper.Price2) // 预算金额 budgetAmount := fmt.Sprintf("%.3f", paper.OrderPrice*float64(paper.OrderCount)) b.FormatToEmpty(&budgetAmount) // 实际完成数 realCount = fmt.Sprintf("%d", paper.ConfirmCount) b.FormatToEmpty(&realCount) // 实际金额 realPrice = fmt.Sprintf("%.3f", paper.OrderPrice*float64(paper.ConfirmCount)) b.FormatToEmpty(&realPrice) DrawRow(b.Row, paper.Name, paper.Norm, paper.Height, paper.Width, fmt.Sprintf("%d", paper.OrderCount), realCount, price, price2, budgetAmount, realPrice, deliveryTime, paper.Remark) b.Row++ } } return nil } func (b *PurchaseBillExcel) drawTableFooter() error { row := b.Row 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, _ := b.Excel.NewStyle(&excelize.Style{ Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"}, Border: border, }) addCel := fmt.Sprintf("A%d", row) b.Excel.MergeCell(b.SheetName, addCel, fmt.Sprintf("G%d", row)) b.Excel.SetCellStyle(b.SheetName, addCel, fmt.Sprintf("G%d", row), styleLeft) b.Excel.SetCellValue(b.SheetName, addCel, "送货地址:"+b.Content.SendTo) sureCel := fmt.Sprintf("H%d", row) b.Excel.MergeCell(b.SheetName, sureCel, fmt.Sprintf("L%d", row)) b.Excel.SetCellStyle(b.SheetName, sureCel, fmt.Sprintf("L%d", row), styleLeft) b.Excel.SetCellValue(b.SheetName, sureCel, "供应商签字:") b.Excel.SetRowHeight(b.SheetName, row, 21) return nil } func (b *PurchaseBillExcel) drawTableSignature() error { b.Row += 2 // row := b.Row style1, _ := b.Excel.NewStyle(&excelize.Style{ Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"}, }) // 制单人 billUserCell := fmt.Sprintf("A%d", b.Row+1) b.Excel.SetCellValue(b.SheetName, billUserCell, "制单人:"+b.Content.UserName) // billUservCell := fmt.Sprintf("B%d", b.Row+1) // b.Excel.SetCellValue(b.SheetName, billUservCell, b.Content.UserName) fontCell := fmt.Sprintf("H%d", b.Row) imageCell1 := fmt.Sprintf("I%d", b.Row) imageCell2 := fmt.Sprintf("K%d", b.Row) b.Excel.MergeCell(b.SheetName, fontCell, fmt.Sprintf("H%d", b.Row+2)) b.Excel.SetCellStyle(b.SheetName, fontCell, fontCell, style1) b.Excel.SetCellValue(b.SheetName, fontCell, "审核签字:") // 签字图片1 I10-J12 b.Excel.MergeCell(b.SheetName, imageCell1, fmt.Sprintf("J%d", b.Row+2)) b.Excel.SetCellStyle(b.SheetName, imageCell1, imageCell1, style1) b.Excel.SetCellValue(b.SheetName, imageCell1, "") // 签字图片2 K10-L12 b.Excel.MergeCell(b.SheetName, imageCell2, fmt.Sprintf("L%d", b.Row+2)) b.Excel.SetCellStyle(b.SheetName, imageCell2, imageCell1, style1) b.Excel.SetCellValue(b.SheetName, imageCell2, "") // 状态为已审核时,签字 // `{ // "x_scale": 0.12, // "y_scale": 0.12, // "x_offset": 30, // "print_obj": true, // "lock_aspect_ratio": false, // "locked": false, // "positioning": "oncell" // }` if b.Content.Reviewed == 1 { imageCells := []string{imageCell1, imageCell2} if len(b.Signatures) > 0 { for k, sign := range b.Signatures { b.Excel.AddPicture(b.SheetName, imageCells[k], sign.Path, sign.Format) } } } b.Excel.SetRowHeight(b.SheetName, b.Row, 21) return nil } func (b *PurchaseBillExcel) Draws() { b.drawTitle() b.drawSubTitles() b.drawTableTitle() b.drawTableContent() b.drawTableFooter() b.drawTableSignature() } func NewPurchaseBill(f *excelize.File) *PurchaseBillExcel { 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"}, Border: border, Font: &excelize.Font{Size: 10}, }) b := &PurchaseBillExcel{ Title: "原材料采购单", SheetName: "Sheet1", Excel: f, Offset: 0, AlignCenterStyle: styleLeft, Signatures: make([]*model.Signature, 0), } // f.SetColWidth(b.SheetName, "A", "A", 12) // f.SetColWidth(b.SheetName, "B", "K", 9.5) // f.SetColWidth(b.SheetName, "L", "L", 10) f.SetColWidth(b.SheetName, "A", "A", 15) f.SetColWidth(b.SheetName, "B", "B", 15) f.SetColWidth(b.SheetName, "C", "G", 7) // f.SetColWidth(b.SheetName, "C", "H", 9.5) f.SetColWidth(b.SheetName, "H", "K", 10) f.SetColWidth(b.SheetName, "L", "L", 11) f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0)) return b } func (b *PurchaseBillExcel) FormatToEmpty(str *string) { if *str == "0" || *str == "0.000" { *str = "" } } func (b *PurchaseBillExcel) SetContent(content interface{}) { b.Content = content.(*model.PurchaseBill) } func (b *PurchaseBillExcel) SetTitle(title string) { b.Title = title } func (b *PurchaseBillExcel) SetRow(row int) { b.Row = row } func (b *PurchaseBillExcel) GetRow() int { return b.Row } func (b *PurchaseBillExcel) SetSignatures(sign interface{}) { b.Signatures = sign.([]*model.Signature) }