123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- package api
- import (
- "box-cost/db/model"
- "fmt"
- "strings"
- _ "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
- IsPdf string
- RowMap map[string]int
- RowWidthArray []float64
- RowsHeightArray []map[int]float64
- }
- // 批量设置行高
- func (b *PurchaseBillExcel) setRowsHeight() {
- for _, rowHeight := range b.RowsHeightArray {
- for row, height := range rowHeight {
- b.Excel.SetRowHeight(b.SheetName, row, height)
- }
- }
- }
- // 获取范围内单元格的宽度 A:F
- func (b *PurchaseBillExcel) getRangeWidth(r string) float64 {
- rg := strings.Split(r, ":")
- if len(rg) == 1 {
- start := b.RowMap[rg[0]]
- return b.RowWidthArray[start]
- } else if len(rg) == 2 {
- start := b.RowMap[rg[0]]
- end := b.RowMap[rg[1]]
- rowr := b.RowWidthArray[start : end+1]
- width := 0.0
- for _, v := range rowr {
- width += v
- }
- return width
- }
- return 0.0
- }
- func (b *PurchaseBillExcel) drawTitle() error {
- b.Row++
- startCell := fmt.Sprintf("A%d", b.Row)
- endCell := fmt.Sprintf("J%d", b.Row)
- marginLeft := excelize.PageMarginLeft(0.15)
- b.Excel.SetColWidth(b.SheetName, "A", "A", 16)
- b.Excel.SetColWidth(b.SheetName, "A", "B", 16)
- b.Excel.SetColWidth(b.SheetName, "C", "G", 9)
- b.Excel.SetColWidth(b.SheetName, "H", "I", 12)
- // b.Excel.SetColWidth(b.SheetName, "J", "J", 24.5)
- b.Excel.SetColWidth(b.SheetName, "J", "J", 16)
- 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 *PurchaseBillExcel) drawSubTitles() error {
- b.Row++
- styleLeft, err := b.Excel.NewStyle(&excelize.Style{
- Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
- 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
- }
- drawLeft := func(rowIndex int, value string) error {
- //左边1
- left1Cell := fmt.Sprintf("A%d", rowIndex)
- err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("G%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("H%d", rowIndex)
- err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("J%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
- }
- drawLall := func(rowIndex int, value string) error {
- //左边1
- left1Cell := fmt.Sprintf("A%d", rowIndex)
- err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("J%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
- }
- //第一行
- drawLeft(b.Row, "类别:"+b.Content.Type)
- drawRight(b.Row, "单号:"+b.Content.SerialNumber)
- b.Excel.SetRowHeight(b.SheetName, b.Row, 21)
- //第二行
- supplierContent := fmt.Sprintf("供应商名称:%s", b.Content.Supplier)
- drawLeft(b.Row+1, supplierContent)
- timeformat := b.Content.CreateTime.Local().Format("2006年01月02号 15:04:05")
- drawRight(b.Row+1, "下单时间:"+timeformat)
- // 设置行高
- b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row + 1: getRowHeight(supplierContent, b.getRangeWidth("A:G"))})
- //第三行
- drawLeft(b.Row+2, "产品名称:"+b.Content.ProductName)
- // 设置行高
- b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row + 2: getRowHeight("产品名称:"+b.Content.ProductName, b.getRangeWidth("A:G"))})
- 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)
- // 第四行
- drawLall(b.Row+3, "包含工序:"+b.Content.CompProduceName)
- // 设置行高
- b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row + 3: getRowHeight("包含工序:"+b.Content.CompProduceName, b.getRangeWidth("A:G"))})
- return nil
- }
- func (b *PurchaseBillExcel) drawTableTitle() error {
- b.Row += 4
- 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
- }
- unit1 := "元/张"
- unit2 := "元/吨"
- if len(b.Content.Paper) > 0 {
- if b.Content.Paper[0].PriceUnit != "" {
- unit1 = b.Content.Paper[0].PriceUnit
- }
- if b.Content.Paper[0].Price2Unit != "" {
- unit2 = b.Content.Paper[0].Price2Unit
- }
- }
- drawCol("A", "采购项目")
- drawCol("B", "规格(克)")
- drawCol2("C", "D", "尺寸(mm)", "长", "宽")
- drawCol("E", "下单数量")
- drawCol2("F", "G", "单价", unit1, unit2)
- drawCol("H", "预算金额")
- drawCol("I", "交货时间")
- drawCol("J", "备注")
- return nil
- }
- func (b *PurchaseBillExcel) drawTableContent() error {
- b.Row += 2
- var DrawRow = func(rowIndex int, values ...string) float64 {
- charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}
- // 获取改行最大行高
- max := getRowHeight(values[0], b.getRangeWidth(charas[0]))
- 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)
- if getRowHeight(v, b.getRangeWidth(c)) > max {
- max = getRowHeight(v, b.getRangeWidth(c))
- }
- }
- return max
- }
- 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)
- b.FormatToEmpty(&price)
- price2 := fmt.Sprintf("%.3f", paper.Price2)
- b.FormatToEmpty(&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))
- // !10.27 如果是固定价格
- if paper.IsFix == nil {
- _fix := false
- paper.IsFix = &_fix
- }
- if *paper.IsFix {
- realPrice = budgetAmount
- }
- b.FormatToEmpty(&realPrice)
- rowMaxHeight := DrawRow(b.Row, paper.Name, paper.Norm, paper.Height, paper.Width, fmt.Sprintf("%d", paper.OrderCount), price, price2, budgetAmount, deliveryTime, paper.Remark)
- // 设置行高
- b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: rowMaxHeight})
- b.Row++
- }
- }
- return nil
- }
- func (b *PurchaseBillExcel) drawTableFooter() error {
- 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", WrapText: true},
- Border: border,
- })
- sendToStartCell := fmt.Sprintf("A%d", b.Row)
- sendToEndCell := fmt.Sprintf("G%d", b.Row)
- supplierStartCell := fmt.Sprintf("H%d", b.Row)
- supplierEndCell := fmt.Sprintf("J%d", b.Row)
- b.Excel.MergeCell(b.SheetName, sendToStartCell, sendToEndCell)
- b.Excel.SetCellStyle(b.SheetName, sendToStartCell, sendToEndCell, styleLeft)
- b.Excel.SetCellValue(b.SheetName, sendToStartCell, "送货地址:"+b.Content.SendTo)
- b.Excel.MergeCell(b.SheetName, supplierStartCell, supplierEndCell)
- b.Excel.SetCellStyle(b.SheetName, supplierStartCell, supplierEndCell, styleLeft)
- b.Excel.SetCellValue(b.SheetName, supplierStartCell, "供应商签字:")
- // 设置行高
- b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: getRowHeight("送货地址:"+b.Content.SendTo, b.getRangeWidth("A:G"))})
- return nil
- }
- func (b *PurchaseBillExcel) drawSupplierRemark() error {
- 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", WrapText: true},
- Border: border,
- })
- sendToStartCell := fmt.Sprintf("A%d", b.Row)
- sendToEndCell := fmt.Sprintf("J%d", b.Row)
- b.Excel.MergeCell(b.SheetName, sendToStartCell, sendToEndCell)
- b.Excel.SetCellStyle(b.SheetName, sendToStartCell, sendToEndCell, styleLeft)
- b.Excel.SetCellValue(b.SheetName, sendToStartCell, "供应商备注:"+b.Content.SupplierRemark)
- // 设置行高
- b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: getRowHeight("供应商备注:"+b.Content.SupplierRemark, b.getRangeWidth("A:J"))})
- return nil
- }
- func (b *PurchaseBillExcel) drawTableSignature() error {
- b.Row += 2
- style1, _ := b.Excel.NewStyle(&excelize.Style{
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
- })
- // 制单人
- billUserCellS := fmt.Sprintf("A%d", b.Row+1)
- billUserCellE := fmt.Sprintf("B%d", b.Row+1)
- b.Excel.MergeCell(b.SheetName, billUserCellS, billUserCellE)
- b.Excel.SetCellValue(b.SheetName, billUserCellS, "制单人:"+b.Content.UserName)
- fontNum := "G"
- image1s := "H"
- image2s := "J"
- image1e := "I"
- image2e := "J"
- fontCell := fmt.Sprintf("%s%d", fontNum, b.Row)
- fontEndCell := fmt.Sprintf("%s%d", fontNum, b.Row+2)
- imageCell1 := fmt.Sprintf("%s%d", image1s, b.Row)
- imageEndCell1 := fmt.Sprintf("%s%d", image1e, b.Row+2)
- imageCell2 := fmt.Sprintf("%s%d", image2s, b.Row)
- imageEndCell2 := fmt.Sprintf("%s%d", image2e, b.Row+2)
- b.Excel.MergeCell(b.SheetName, fontCell, fontEndCell)
- b.Excel.SetCellStyle(b.SheetName, fontCell, fontCell, style1)
- b.Excel.SetCellValue(b.SheetName, fontCell, "审核签字:")
- // 签字图片1 I10-J12
- b.Excel.MergeCell(b.SheetName, imageCell1, imageEndCell1)
- b.Excel.SetCellStyle(b.SheetName, imageCell1, imageCell1, style1)
- b.Excel.SetCellValue(b.SheetName, imageCell1, "")
- // 签字图片2 K10-L12
- b.Excel.MergeCell(b.SheetName, imageCell2, imageEndCell2)
- b.Excel.SetCellStyle(b.SheetName, imageCell2, imageCell1, style1)
- b.Excel.SetCellValue(b.SheetName, imageCell2, "")
- 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)
- }
- }
- }
- return nil
- }
- func (b *PurchaseBillExcel) Draws() {
- b.drawTitle()
- b.drawSubTitles()
- b.drawTableTitle()
- b.drawTableContent()
- b.drawTableFooter()
- if len(b.Content.SupplierRemark) > 1 {
- b.drawSupplierRemark()
- }
- b.drawTableSignature()
- // 设置行高
- b.setRowsHeight()
- }
- 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", WrapText: true},
- Border: border,
- Font: &excelize.Font{Size: 10},
- })
- b := &PurchaseBillExcel{
- Title: "原材料采购单",
- SheetName: "Sheet1",
- Excel: f,
- Offset: 0,
- AlignCenterStyle: styleLeft,
- Signatures: make([]*model.Signature, 0),
- RowMap: map[string]int{"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6, "H": 7, "I": 8, "J": 9},
- RowWidthArray: []float64{16, 16, 9, 9, 9, 9, 9, 12, 12, 16},
- RowsHeightArray: make([]map[int]float64, 0),
- }
- f.SetPageMargins(b.SheetName, excelize.PageMarginTop(1), 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) SetSheetName(name string) {
- b.SheetName = name
- }
- func (b *PurchaseBillExcel) SetRow(row int) {
- b.Row = row
- }
- func (b *PurchaseBillExcel) SetIsPdf(isPdf string) {
- b.IsPdf = isPdf
- }
- func (b *PurchaseBillExcel) GetRow() int {
- return b.Row
- }
- func (b *PurchaseBillExcel) SetSignatures(sign interface{}) {
- b.Signatures = sign.([]*model.Signature)
- }
|