123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568 |
- package api
- import (
- "fmt"
- "github.com/xuri/excelize/v2"
- )
- // 生产成本表
- type PlanCostExcel struct {
- Row int
- Title string
- Excel *excelize.File
- SheetName string
- AlignCenterStyle int
- Content *SupplierPlanSummary
- }
- func (b *PlanCostExcel) drawTitle() error {
- b.Row++
- startCell := fmt.Sprintf("A%d", b.Row)
- err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("O%d", b.Row))
- if err != nil {
- return err
- }
- style, err := b.Excel.NewStyle(&excelize.Style{
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
- 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 *PlanCostExcel) drawTableTitle() error {
- b.Row++
- 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
- }
- var drawCol3 = func(prefix1 string, prefix2 string, prefix3 string, value1 string, value2 string, value3 string, value4 string) error {
- left1Cell := fmt.Sprintf("%s%d", prefix1, b.Row)
- left3Cell := fmt.Sprintf("%s%d", prefix3, b.Row)
- err := b.Excel.MergeCell(b.SheetName, left1Cell, left3Cell)
- if err != nil {
- return err
- }
- if err != nil {
- fmt.Println(err)
- return err
- }
- err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left3Cell, 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)
- val4Cel := fmt.Sprintf("%s%d", prefix3, b.Row+1)
- b.Excel.SetCellStyle(b.SheetName, val4Cel, val4Cel, b.AlignCenterStyle)
- b.Excel.SetCellValue(b.SheetName, val4Cel, value4)
- return nil
- }
- drawCol("A", "产品名称")
- drawCol("A", "产品部件名称")
- drawCol2("B", "C", "类型/项目", "类型", "项目")
- drawCol("D", "下单数量")
- drawCol("E", "实际数量")
- drawCol("F", "状态") // 生成订单状态 审核状态 发送状态 完成状态
- drawCol("G", "供应商名称")
- drawCol("H", "单价")
- drawCol3("I", "J", "K", "规格", "厚度(纸克)", "长", "宽")
- drawCol("L", "单位")
- drawCol("M", "下单单价")
- drawCol("N", "预算金额")
- drawCol("O", "实际金额")
- return nil
- }
- func (b *PlanCostExcel) drawRow(rowIndex int, values ...string) {
- charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O"}
- 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, 32)
- }
- }
- func (b *PlanCostExcel) drawAllContent() error {
- b.Row += 2
- // summaryPlans
- // 预算金额汇总
- var totalBudgetPrice float64 = 0.00
- // 实际金额汇总
- var totalRealPrice float64 = 0.00
- for _, splan := range b.Content.Plans {
- plan := splan.Plan
- comps := plan.Pack.Components
- // 预算金额汇总
- var planBudgetPrice float64 = 0.00
- // 实际金额汇总
- var planRealPrice float64 = 0.00
- if len(comps) > 0 {
- for _, comp := range comps {
- var perBudgetPrice float64 = 0.00
- var perRealPrice float64 = 0.00
- if len(comp.Stages) > 0 {
- startRow := b.Row
- // cates := map[string][]int{}
- for _, stage := range comp.Stages {
- matHeigth := fmt.Sprintf("%d", stage.BatchSizeHeight)
- b.FormatToEmpty(&matHeigth)
- matWidth := fmt.Sprintf("%d", stage.BatchSizeWidth)
- b.FormatToEmpty(&matWidth)
- orderCount := fmt.Sprintf("%d", int(stage.OrderCount))
- b.FormatToEmpty(&orderCount)
- // 实际数量
- realCount := fmt.Sprintf("%d", stage.ConfirmCount)
- b.FormatToEmpty(&realCount)
- // 单价
- price := fmt.Sprintf("%.3f", stage.OrderPrice)
- b.FormatToEmpty(&price)
- // 预算金额
- budgetPrice := fmt.Sprintf("%.3f", stage.OrderPrice*float64(stage.OrderCount))
- perBudgetPrice += stage.OrderPrice * float64(stage.OrderCount)
- b.FormatToEmpty(&budgetPrice)
- // 实际金额
- // !10.27 如果是固定价格
- realPrice := fmt.Sprintf("%.3f", stage.OrderPrice*float64(stage.ConfirmCount))
- if stage.IsFix == nil {
- _fix := false
- stage.IsFix = &_fix
- }
- if *stage.IsFix {
- realPrice = budgetPrice
- perRealPrice += stage.OrderPrice * float64(stage.OrderCount)
- } else {
- perRealPrice += stage.OrderPrice * float64(stage.ConfirmCount)
- }
- b.FormatToEmpty(&realPrice)
- unit := stage.Unit
- if stage.Unit == "吨" || stage.Unit == "平方米" {
- unit = "张"
- }
- supplierName := ""
- if stage.SupplierInfo != nil {
- supplierName = stage.SupplierInfo.Name
- }
- stageType := ""
- if stage.BillType > 0 {
- stage.Type = stage.BillType
- }
- if stage.Type == 1 {
- stageType = "材料采购"
- }
- if stage.Type == 2 {
- stageType = "工艺"
- }
- if stage.Type == 3 {
- stageType = "成品采购"
- }
- // 状态
- stageStatus := ""
- if len(stage.BillId) < 1 {
- stageStatus = "未生成订单"
- } else {
- if splan.State[stage.BillId] == "created" {
- stageStatus = "进行中"
- // 审核状态
- if splan.Reviewed[stage.BillId] == 1 {
- stageStatus = "已审核"
- if splan.IsSend[stage.BillId] {
- stageStatus = "已发送"
- if splan.IsAck[stage.BillId] {
- stageStatus = "已接单"
- } else {
- stageStatus = "未接单"
- }
- } else {
- stageStatus = "未发送"
- }
- } else {
- stageStatus = "未审核"
- }
- } else if splan.State[stage.BillId] == "complete" {
- stageStatus = "已完成"
- } else {
- stageStatus = "未生成订单"
- }
- }
- b.drawRow(b.Row, "", stageType, stage.Name, orderCount, realCount, stageStatus, supplierName, fmt.Sprintf("%.3f元/%s", stage.Price, stage.Unit), stage.Norm, matHeigth, matWidth, unit, price, budgetPrice, realPrice)
- // if stage.SupplierInfo != nil {
- // cates[stage.SupplierInfo.Name] = append(cates[stage.SupplierInfo.Name], b.Row)
- // }
- b.Row++
- }
- // fmt.Println(cates)
- // for supplierName, cate := range cates {
- // mergeStartRow := cate[0]
- // mergeEndRow := cate[len(cate)-1]
- // b.Excel.MergeCell(b.SheetName, fmt.Sprintf("G%d", mergeStartRow), fmt.Sprintf("G%d", mergeEndRow))
- // b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("G%d", mergeEndRow), supplierName)
- // }
- endRow := b.Row - 1
- // 组件名字
- startACell := fmt.Sprintf("%s%d", "A", startRow)
- endACell := fmt.Sprintf("%s%d", "A", endRow)
- b.Excel.MergeCell(b.SheetName, startACell, endACell)
- err := b.Excel.SetCellStyle(b.SheetName, startACell, endACell, b.AlignCenterStyle)
- if err != nil {
- return err
- }
- b.Excel.SetCellValue(b.SheetName, startACell, comp.Name)
- }
- // 预算
- planBudgetPrice += perBudgetPrice
- // 实际金额
- planRealPrice += perRealPrice
- }
- }
- totalBudgetPrice += planBudgetPrice
- totalRealPrice += planRealPrice
- }
- summaryEndRow := b.Row
- startACell := fmt.Sprintf("%s%d", "A", summaryEndRow)
- endMell := fmt.Sprintf("%s%d", "M", summaryEndRow)
- NCell := fmt.Sprintf("%s%d", "N", summaryEndRow)
- OCell := fmt.Sprintf("%s%d", "O", summaryEndRow)
- b.Excel.MergeCell(b.SheetName, startACell, endMell)
- b.Excel.SetCellStyle(b.SheetName, startACell, endMell, b.AlignCenterStyle)
- b.Excel.SetCellValue(b.SheetName, startACell, "生产计划汇总金额")
- // 生产预算汇总
- b.Excel.SetCellStyle(b.SheetName, NCell, NCell, b.AlignCenterStyle)
- totalOrderRealPrice := fmt.Sprintf("%.3f", totalBudgetPrice)
- b.FormatToEmpty(&totalOrderRealPrice)
- b.Excel.SetCellValue(b.SheetName, NCell, totalOrderRealPrice)
- // 生产实际汇总
- b.Excel.SetCellStyle(b.SheetName, OCell, OCell, b.AlignCenterStyle)
- totalRealPricef := fmt.Sprintf("%.3f", totalRealPrice)
- b.FormatToEmpty(&totalRealPricef)
- b.Excel.SetCellValue(b.SheetName, OCell, totalRealPricef)
- return nil
- }
- func (b *PlanCostExcel) drawSupplierContent() error {
- b.Row += 2
- supplier := ""
- // summaryPlans
- // 预算金额汇总
- var totalBudgetPrice float64 = 0.00
- // 实际金额汇总
- var totalRealPrice float64 = 0.00
- for _, splan := range b.Content.Plans {
- plan := splan.Plan
- comps := plan.Pack.Components
- // 预算金额汇总
- var planBudgetPrice float64 = 0.00
- // 实际金额汇总
- var planRealPrice float64 = 0.00
- if len(comps) > 0 {
- for _, comp := range comps {
- var perBudgetPrice float64 = 0.00
- var perRealPrice float64 = 0.00
- if len(comp.Stages) > 0 {
- startRow := 0
- // cates := map[string][]int{}
- for _, stage := range comp.Stages {
- if stage.SupplierInfo != nil {
- if b.Content.SupplierId == stage.SupplierInfo.Id {
- supplier = stage.SupplierInfo.Name
- // 材料
- if startRow == 0 {
- startRow = b.Row
- }
- matHeigth := fmt.Sprintf("%d", stage.BatchSizeHeight)
- b.FormatToEmpty(&matHeigth)
- matWidth := fmt.Sprintf("%d", stage.BatchSizeWidth)
- b.FormatToEmpty(&matWidth)
- orderCount := fmt.Sprintf("%d", int(stage.OrderCount))
- b.FormatToEmpty(&orderCount)
- // 实际数量
- realCount := fmt.Sprintf("%d", stage.ConfirmCount)
- b.FormatToEmpty(&realCount)
- // 单价
- price := fmt.Sprintf("%.3f", stage.OrderPrice)
- b.FormatToEmpty(&price)
- // 预算金额
- budgetPrice := fmt.Sprintf("%.3f", stage.OrderPrice*float64(stage.OrderCount))
- perBudgetPrice += stage.OrderPrice * float64(stage.OrderCount)
- b.FormatToEmpty(&budgetPrice)
- // 实际金额
- // !10.27 如果是固定价格
- realPrice := fmt.Sprintf("%.3f", stage.OrderPrice*float64(stage.ConfirmCount))
- if stage.IsFix == nil {
- _fix := false
- stage.IsFix = &_fix
- }
- if *stage.IsFix {
- realPrice = budgetPrice
- perRealPrice += stage.OrderPrice * float64(stage.OrderCount)
- } else {
- perRealPrice += stage.OrderPrice * float64(stage.ConfirmCount)
- }
- b.FormatToEmpty(&realPrice)
- unit := stage.Unit
- if stage.Unit == "吨" || stage.Unit == "平方米" {
- unit = "张"
- }
- supplierName := ""
- if stage.SupplierInfo != nil {
- supplierName = stage.SupplierInfo.Name
- }
- stageType := ""
- if stage.BillType > 0 {
- stage.Type = stage.BillType
- }
- if stage.Type == 1 {
- stageType = "材料采购"
- }
- if stage.Type == 2 {
- stageType = "工艺"
- }
- if stage.Type == 3 {
- stageType = "成品采购"
- }
- // 状态
- stageStatus := ""
- if len(stage.BillId) < 1 {
- stageStatus = "未生成订单"
- } else {
- if splan.State[stage.BillId] == "created" {
- stageStatus = "进行中"
- // 审核状态
- if splan.Reviewed[stage.BillId] == 1 {
- stageStatus = "已审核"
- if splan.IsSend[stage.BillId] {
- stageStatus = "已发送"
- if splan.IsAck[stage.BillId] {
- stageStatus = "已接单"
- } else {
- stageStatus = "未接单"
- }
- } else {
- stageStatus = "未发送"
- }
- } else {
- stageStatus = "未审核"
- }
- } else if splan.State[stage.BillId] == "complete" {
- stageStatus = "已完成"
- } else {
- stageStatus = "未生成订单"
- }
- }
- b.drawRow(b.Row, "", stageType, stage.Name, orderCount, realCount, stageStatus, supplierName, fmt.Sprintf("%.3f元/%s", stage.Price, stage.Unit), stage.Norm, matHeigth, matWidth, unit, price, budgetPrice, realPrice)
- b.Row++
- }
- }
- }
- if startRow != 0 {
- endRow := b.Row - 1
- // 组件名字
- startACell := fmt.Sprintf("%s%d", "A", startRow)
- endACell := fmt.Sprintf("%s%d", "A", endRow)
- b.Excel.MergeCell(b.SheetName, startACell, endACell)
- err := b.Excel.SetCellStyle(b.SheetName, startACell, endACell, b.AlignCenterStyle)
- if err != nil {
- return err
- }
- b.Excel.SetCellValue(b.SheetName, startACell, comp.Name)
- }
- }
- // 预算
- totalBudgetPrice += perBudgetPrice
- // 预算
- totalRealPrice += perRealPrice
- }
- }
- totalBudgetPrice += planBudgetPrice
- totalRealPrice += planRealPrice
- }
- summaryEndRow := b.Row
- startACell := fmt.Sprintf("%s%d", "A", summaryEndRow)
- endMell := fmt.Sprintf("%s%d", "M", summaryEndRow)
- NCell := fmt.Sprintf("%s%d", "N", summaryEndRow)
- OCell := fmt.Sprintf("%s%d", "O", summaryEndRow)
- b.Excel.MergeCell(b.SheetName, startACell, endMell)
- b.Excel.SetCellStyle(b.SheetName, startACell, endMell, b.AlignCenterStyle)
- b.Excel.SetCellValue(b.SheetName, startACell, "生产计划汇总金额")
- // 生产预算汇总
- b.Excel.SetCellStyle(b.SheetName, NCell, NCell, b.AlignCenterStyle)
- totalOrderRealPrice := fmt.Sprintf("%.3f", totalBudgetPrice)
- b.FormatToEmpty(&totalOrderRealPrice)
- b.Excel.SetCellValue(b.SheetName, NCell, totalOrderRealPrice)
- // 生产实际汇总
- b.Excel.SetCellStyle(b.SheetName, OCell, OCell, b.AlignCenterStyle)
- totalRealPricef := fmt.Sprintf("%.3f", totalRealPrice)
- b.FormatToEmpty(&totalRealPricef)
- b.Excel.SetCellValue(b.SheetName, OCell, totalRealPricef)
- // 供应商名字标题
- style, err := b.Excel.NewStyle(&excelize.Style{
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
- })
- if err != nil {
- return err
- }
- err = b.Excel.SetCellStyle(b.SheetName, "A1", "G1", style)
- if err != nil {
- return err
- }
- b.Excel.SetRowHeight(b.SheetName, 1, 32)
- b.Excel.SetCellValue(b.SheetName, "A1", fmt.Sprintf("【%s】-生产成本表", supplier))
- return nil
- }
- func (b *PlanCostExcel) Draws() {
- b.drawTitle()
- b.drawTableTitle()
- if !b.Content.SupplierId.IsZero() {
- b.drawSupplierContent()
- } else {
- b.drawAllContent()
- }
- }
- func NewPlanCostExcel(f *excelize.File) *PlanCostExcel {
- 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 := &PlanCostExcel{
- Title: "生产成本表",
- SheetName: "Sheet1",
- Excel: f,
- AlignCenterStyle: styleLeft,
- }
- f.SetColWidth(b.SheetName, "A", "D", 16)
- f.SetColWidth(b.SheetName, "E", "F", 12)
- f.SetColWidth(b.SheetName, "G", "G", 25)
- f.SetColWidth(b.SheetName, "H", "K", 16)
- f.SetColWidth(b.SheetName, "L", "L", 10)
- f.SetColWidth(b.SheetName, "M", "O", 16)
- f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
- return b
- }
- func (b *PlanCostExcel) FormatToEmpty(str *string) {
- if *str == "0" || *str == "0.000" {
- *str = ""
- }
- }
|