123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- package api
- import (
- "box-cost/db/model"
- "fmt"
- _ "image/gif"
- _ "image/jpeg"
- _ "image/png"
- "github.com/xuri/excelize/v2"
- )
- type ProcessBillExcel struct {
- Offset int
- Row int
- Title string //标题
- Excel *excelize.File
- SheetName string
- AlignCenterStyle int
- Content *model.PurchaseBill
- Signatures []*model.Signature
- }
- func (b *ProcessBillExcel) drawTitle() error {
- tileIndex := b.Offset + 1
- startCell := fmt.Sprintf("A%d", tileIndex)
- err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("I%d", tileIndex))
- 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, tileIndex, 23)
- b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
- return nil
- }
- func (b *ProcessBillExcel) drawSubTitles() error {
- row := b.Offset + 2
- 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("E%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("F%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
- }
- //第一行
- drawLeft(row, "类别:"+b.Content.Type)
- drawRight(row, "单号:"+b.Content.SerialNumber)
- b.Excel.SetRowHeight(b.SheetName, row, 21)
- //第二行
- drawLeft(row+1, "供应商名称:"+b.Content.Supplier)
- timeformat := b.Content.CreateTime.Local().Format("2006年01月02号 15:04:05")
- drawRight(row+1, "下单时间:"+timeformat)
- b.Excel.SetRowHeight(b.SheetName, row+1, 21)
- //第三行
- drawLeft(row+2, "产品名称:"+b.Content.ProductName)
- status := ""
- if b.Content.Status == "complete" {
- status = fmt.Sprintf("已完成(%d)", b.Content.ConfirmCount)
- }
- if b.Content.Status == "created" {
- status = "进行中"
- }
- drawRight(row+2, "状态:"+status)
- b.Excel.SetRowHeight(b.SheetName, row+2, 21)
- return nil
- }
- func (b *ProcessBillExcel) drawTableTitle() error {
- row := b.Offset + 5
- // 品名 规格 数量 单位 单价 金额
- var drawCol = func(prefix string, value string) error {
- left1Cell := fmt.Sprintf("%s%d", prefix, row)
- left2Cell := fmt.Sprintf("%s%d", prefix, 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)
- }
- drawCol("A", "采购项目")
- drawCol("B", "规格")
- drawCol("C", "数量")
- drawCol("D", "单位")
- drawCol("E", "单价")
- drawCol("F", "预算金额")
- drawCol("G", "实际金额")
- drawCol("H", "交货时间")
- drawCol("I", "备注")
- return nil
- }
- func (b *ProcessBillExcel) drawTableContent() error {
- row := b.Offset + 7
- b.Row = row
- var DrawRow = func(rowIndex int, values ...string) {
- charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I"}
- 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)
- }
- }
- process := b.Content.Process
- if len(process) > 0 {
- for _, ps := range process {
- // 不是订单工序
- if !ps.IsBill {
- continue
- }
- deliveryTime := ps.DeliveryTime.Local().Format("2006-01-02")
- confirmCount := "-"
- realAmount := "-"
- // 预算金额
- budgetAmount := fmt.Sprintf("%.2f", float64(ps.Count)*ps.Price)
- b.FormatToEmpty(&budgetAmount)
- if b.Content.Status == "complete" {
- // 实际完成数
- confirmCount = fmt.Sprintf("%d", b.Content.ConfirmCount)
- b.FormatToEmpty(&confirmCount)
- // 实际金额
- realAmount = fmt.Sprintf("%.2f", float64(b.Content.ConfirmCount)*ps.Price)
- b.FormatToEmpty(&realAmount)
- }
- // 采购项目 规格 数量 单位 单价 预算金额 实际金额 交货时间 备注
- count := fmt.Sprintf("%d", ps.Count)
- price := fmt.Sprintf("%.2f", ps.Price)
- b.FormatToEmpty(&price)
- DrawRow(row, ps.Name, ps.Norm, count, ps.Unit, price, budgetAmount, realAmount, deliveryTime, ps.Remark)
- row++
- b.Row++
- }
- }
- return nil
- }
- func (b *ProcessBillExcel) drawTableFooter() error {
- left1Cell := fmt.Sprintf("A%d", 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,
- })
- b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
- b.Excel.SetCellValue(b.SheetName, left1Cell, "送货地址:")
- addCel := fmt.Sprintf("B%d", b.Row)
- b.Excel.MergeCell(b.SheetName, addCel, fmt.Sprintf("E%d", b.Row))
- b.Excel.SetCellStyle(b.SheetName, addCel, fmt.Sprintf("E%d", b.Row), styleLeft)
- b.Excel.SetCellValue(b.SheetName, addCel, b.Content.SendTo)
- sureCel := fmt.Sprintf("F%d", b.Row)
- b.Excel.MergeCell(b.SheetName, sureCel, fmt.Sprintf("I%d", b.Row))
- b.Excel.SetCellStyle(b.SheetName, sureCel, fmt.Sprintf("I%d", b.Row), styleLeft)
- b.Excel.SetCellValue(b.SheetName, sureCel, "供应商签字:")
- b.Excel.SetRowHeight(b.SheetName, b.Row, 30)
- return nil
- }
- func (b *ProcessBillExcel) drawRemark() error {
- // 填备注
- remarkTitleCell := fmt.Sprintf("A%d", b.Row)
- b.Excel.SetCellValue(b.SheetName, remarkTitleCell, "备注:")
- b.Row++
- remarkContentScell := fmt.Sprintf("A%d", b.Row)
- // 预留五行备注内容 // TODO 获取内容换行符个数然后动态确定内容占用多少行单元格
- b.Row += 5
- remarkContentEcell := fmt.Sprintf("L%d", b.Row)
- b.Excel.MergeCell(b.SheetName, remarkContentScell, remarkContentEcell)
- b.Excel.SetCellValue(b.SheetName, remarkContentScell, b.Content.Remark)
- return nil
- }
- func (b *ProcessBillExcel) drawTableSignature() error {
- b.Row += 2
- style1, _ := b.Excel.NewStyle(&excelize.Style{
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
- // Border: border,
- })
- fontCell := fmt.Sprintf("E%d", b.Row)
- imageCell1 := fmt.Sprintf("F%d", b.Row)
- imageCell2 := fmt.Sprintf("H%d", b.Row)
- eRow := b.Row + 2
- b.Excel.MergeCell(b.SheetName, fontCell, fmt.Sprintf("E%d", eRow))
- b.Excel.SetCellStyle(b.SheetName, fontCell, fontCell, style1)
- b.Excel.SetCellValue(b.SheetName, fontCell, "审核签字:")
- b.Excel.MergeCell(b.SheetName, imageCell1, fmt.Sprintf("F%d", eRow))
- b.Excel.SetCellStyle(b.SheetName, imageCell1, imageCell1, style1)
- b.Excel.SetCellValue(b.SheetName, imageCell1, "")
- b.Excel.MergeCell(b.SheetName, imageCell2, fmt.Sprintf("H%d", eRow))
- 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.Url, sign.Format)
- }
- }
- }
- return nil
- }
- func (b *ProcessBillExcel) Draws() {
- b.drawTitle()
- b.drawSubTitles()
- b.drawTableTitle()
- b.drawTableContent()
- b.drawTableFooter()
- b.drawRemark()
- b.drawTableSignature()
- }
- func NewProcessBill(f *excelize.File) *ProcessBillExcel {
- 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 := &ProcessBillExcel{
- Title: "原材料采购单",
- SheetName: "Sheet1",
- Excel: f,
- Offset: 0,
- AlignCenterStyle: styleLeft,
- Signatures: make([]*model.Signature, 0),
- }
- f.SetColWidth(b.SheetName, "A", "I", 14)
- f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
- return b
- }
- func (b *ProcessBillExcel) FormatToEmpty(str *string) {
- if *str == "0" || *str == "0.00" {
- *str = ""
- }
- }
- func (b *ProcessBillExcel) PrintPurchType() string {
- return "process"
- }
- func (b *ProcessBillExcel) SetContent(content *model.PurchaseBill) {
- b.Content = content
- }
- func (b *ProcessBillExcel) SetTitle(title string) {
- b.Title = title
- }
- func (b *ProcessBillExcel) SetSignatures(sign []*model.Signature) {
- b.Signatures = sign
- }
|