plan-cost-excel.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "fmt"
  5. "github.com/xuri/excelize/v2"
  6. )
  7. // 生产成本表
  8. type PlanCostExcel struct {
  9. Offset int
  10. Title string //标题
  11. Excel *excelize.File
  12. SheetName string
  13. AlignCenterStyle int
  14. Content *model.ProductPlan
  15. }
  16. func (b *PlanCostExcel) drawTitle() error {
  17. tileIndex := b.Offset + 1
  18. startCell := fmt.Sprintf("A%d", tileIndex)
  19. err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("L%d", tileIndex))
  20. if err != nil {
  21. return err
  22. }
  23. style, err := b.Excel.NewStyle(&excelize.Style{
  24. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  25. Font: &excelize.Font{Bold: true, Size: 18}})
  26. if err != nil {
  27. return err
  28. }
  29. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  30. if err != nil {
  31. return err
  32. }
  33. b.Excel.SetRowHeight(b.SheetName, tileIndex, 23)
  34. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  35. return nil
  36. }
  37. func (b *PlanCostExcel) drawTableTitle() error {
  38. row := b.Offset + 2
  39. //A采购项目 B规格(克) 尺寸C-D 数量E 单价F-G 交货时间H 备注I
  40. // A产品名称
  41. var drawCol = func(prefix string, value string) error {
  42. left1Cell := fmt.Sprintf("%s%d", prefix, row)
  43. left2Cell := fmt.Sprintf("%s%d", prefix, row+1)
  44. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  45. if err != nil {
  46. return err
  47. }
  48. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  49. if err != nil {
  50. return err
  51. }
  52. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  53. }
  54. // var drawCol1 = func(prefix string, value1 string, value2 string) error {
  55. // topCell := fmt.Sprintf("%s%d", prefix, row)
  56. // bottomCell := fmt.Sprintf("%s%d", prefix, row+1)
  57. // b.Excel.SetCellStyle(b.SheetName, topCell, topCell, b.AlignCenterStyle)
  58. // b.Excel.SetCellValue(b.SheetName, topCell, value1)
  59. // b.Excel.SetCellStyle(b.SheetName, bottomCell, bottomCell, b.AlignCenterStyle)
  60. // b.Excel.SetCellValue(b.SheetName, bottomCell, value2)
  61. // return nil
  62. // }
  63. var drawCol2 = func(prefix1 string, prefix2 string, value1 string, value2 string, value3 string) error {
  64. left1Cell := fmt.Sprintf("%s%d", prefix1, row)
  65. left2Cell := fmt.Sprintf("%s%d", prefix2, row)
  66. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  67. if err != nil {
  68. return err
  69. }
  70. if err != nil {
  71. fmt.Println(err)
  72. return err
  73. }
  74. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  75. if err != nil {
  76. return err
  77. }
  78. b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
  79. val2Cel := fmt.Sprintf("%s%d", prefix1, row+1)
  80. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  81. b.Excel.SetCellValue(b.SheetName, val2Cel, value2)
  82. val3Cel := fmt.Sprintf("%s%d", prefix2, row+1)
  83. b.Excel.SetCellStyle(b.SheetName, val3Cel, val3Cel, b.AlignCenterStyle)
  84. b.Excel.SetCellValue(b.SheetName, val3Cel, value3)
  85. return nil
  86. }
  87. var drawCol3 = func(prefix1 string, prefix2 string, prefix3 string, value1 string, value2 string, value3 string, value4 string) error {
  88. left1Cell := fmt.Sprintf("%s%d", prefix1, row)
  89. // left2Cell := fmt.Sprintf("%s%d", prefix2, row)
  90. left3Cell := fmt.Sprintf("%s%d", prefix3, row)
  91. err := b.Excel.MergeCell(b.SheetName, left1Cell, left3Cell)
  92. if err != nil {
  93. return err
  94. }
  95. if err != nil {
  96. fmt.Println(err)
  97. return err
  98. }
  99. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left3Cell, b.AlignCenterStyle)
  100. if err != nil {
  101. return err
  102. }
  103. b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
  104. val2Cel := fmt.Sprintf("%s%d", prefix1, row+1)
  105. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  106. b.Excel.SetCellValue(b.SheetName, val2Cel, value2)
  107. val3Cel := fmt.Sprintf("%s%d", prefix2, row+1)
  108. b.Excel.SetCellStyle(b.SheetName, val3Cel, val3Cel, b.AlignCenterStyle)
  109. b.Excel.SetCellValue(b.SheetName, val3Cel, value3)
  110. val4Cel := fmt.Sprintf("%s%d", prefix3, row+1)
  111. b.Excel.SetCellStyle(b.SheetName, val4Cel, val4Cel, b.AlignCenterStyle)
  112. b.Excel.SetCellValue(b.SheetName, val4Cel, value4)
  113. return nil
  114. }
  115. drawCol("A", "产品配料名称")
  116. drawCol2("B", "C", "材料/工序", "材料", "工序")
  117. drawCol("D", "供应商名称")
  118. drawCol3("E", "F", "G", "规格", "厚度(纸克)", "长", "宽")
  119. drawCol("H", "单位")
  120. drawCol("I", "下单数量")
  121. drawCol("J", "单价(预算)")
  122. drawCol("K", "预算金额")
  123. drawCol("L", "汇总金额")
  124. return nil
  125. }
  126. func (b *PlanCostExcel) drawTableContent() error {
  127. row := b.Offset + 4
  128. var DrawRow = func(rowIndex int, values ...string) {
  129. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"}
  130. for i, c := range charas {
  131. v := ""
  132. if i < len(values) {
  133. v = values[i]
  134. }
  135. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  136. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  137. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  138. b.Excel.SetRowHeight(b.SheetName, rowIndex, 21)
  139. }
  140. }
  141. comps := b.Content.Pack.Components
  142. if len(comps) > 0 {
  143. // row1 := row
  144. for _, comp := range comps {
  145. if len(comp.Mats) > 0 {
  146. startRow := row
  147. for _, mat := range comp.Mats {
  148. // 材料
  149. supplierName := ""
  150. if mat.Supplier.SupplierInfo != nil {
  151. supplierName = mat.Supplier.SupplierInfo.Name
  152. }
  153. matHeigth := fmt.Sprintf("%d", mat.BatchSizeHeight)
  154. b.FormatToEmpty(&matHeigth)
  155. matWidth := fmt.Sprintf("%d", mat.BatchSizeWidth)
  156. b.FormatToEmpty(&matWidth)
  157. orderCount := fmt.Sprintf("%d", int(mat.Supplier.OrderCount))
  158. b.FormatToEmpty(&orderCount)
  159. orderPrice := fmt.Sprintf("%.2f", mat.Supplier.OrderPrice)
  160. b.FormatToEmpty(&orderPrice)
  161. orderRealPrice := fmt.Sprintf("%.2f", mat.Supplier.OrderRealPrice)
  162. b.FormatToEmpty(&orderRealPrice)
  163. DrawRow(row, "", mat.MatInfo.Name, "", supplierName, mat.MatInfo.Norm, matHeigth, matWidth, mat.MatInfo.Unit, orderCount, orderPrice, orderRealPrice, "")
  164. row++
  165. if len(mat.Crafts) > 0 {
  166. // 工序
  167. for _, craft := range mat.Crafts {
  168. craftSupplierName := ""
  169. if craft.Supplier.SupplierInfo != nil {
  170. craftSupplierName = craft.Supplier.SupplierInfo.Name
  171. }
  172. carftOrderCount := fmt.Sprintf("%d", int(craft.Supplier.OrderCount))
  173. b.FormatToEmpty(&carftOrderCount)
  174. carftHeigth := fmt.Sprintf("%d", craft.BatchSizeHeight)
  175. b.FormatToEmpty(&carftHeigth)
  176. carftWidth := fmt.Sprintf("%d", craft.BatchSizeWidth)
  177. b.FormatToEmpty(&carftWidth)
  178. carftOrderPrice := fmt.Sprintf("%.2f", craft.Supplier.OrderPrice)
  179. b.FormatToEmpty(&carftOrderPrice)
  180. carftOrderRealPrice := fmt.Sprintf("%.2f", craft.Supplier.OrderRealPrice)
  181. b.FormatToEmpty(&carftOrderRealPrice)
  182. DrawRow(row, "", "", craft.CraftInfo.Name, craftSupplierName, craft.CraftInfo.Norm, carftHeigth, carftWidth, craft.CraftInfo.Unit, carftOrderCount, carftOrderPrice, carftOrderRealPrice, "")
  183. row++
  184. }
  185. }
  186. }
  187. endRow := row - 1
  188. // 组件名字
  189. startACell := fmt.Sprintf("%s%d", "A", startRow)
  190. endACell := fmt.Sprintf("%s%d", "A", endRow)
  191. b.Excel.MergeCell(b.SheetName, startACell, endACell)
  192. err := b.Excel.SetCellStyle(b.SheetName, startACell, endACell, b.AlignCenterStyle)
  193. if err != nil {
  194. return err
  195. }
  196. b.Excel.SetCellValue(b.SheetName, startACell, comp.Name)
  197. // 组件汇总金额
  198. startLCell := fmt.Sprintf("%s%d", "L", startRow)
  199. endLCell := fmt.Sprintf("%s%d", "L", endRow)
  200. b.Excel.MergeCell(b.SheetName, startLCell, endLCell)
  201. err = b.Excel.SetCellStyle(b.SheetName, startLCell, endLCell, b.AlignCenterStyle)
  202. if err != nil {
  203. return err
  204. }
  205. compTotalPrice := fmt.Sprintf("%.2f", comp.TotalPrice)
  206. b.FormatToEmpty(&compTotalPrice)
  207. b.Excel.SetCellValue(b.SheetName, startLCell, compTotalPrice)
  208. }
  209. }
  210. // 生产汇总金额
  211. startACell := fmt.Sprintf("%s%d", "A", row)
  212. endKCell := fmt.Sprintf("%s%d", "K", row)
  213. endLCell := fmt.Sprintf("%s%d", "L", row)
  214. b.Excel.MergeCell(b.SheetName, startACell, endKCell)
  215. b.Excel.SetCellStyle(b.SheetName, startACell, endKCell, b.AlignCenterStyle)
  216. b.Excel.SetCellValue(b.SheetName, startACell, "生产计划汇总金额")
  217. b.Excel.SetCellStyle(b.SheetName, endLCell, endLCell, b.AlignCenterStyle)
  218. planTotalPrice := fmt.Sprintf("%.2f", b.Content.TotalPrice)
  219. b.FormatToEmpty(&planTotalPrice)
  220. b.Excel.SetCellValue(b.SheetName, endLCell, planTotalPrice)
  221. }
  222. return nil
  223. }
  224. func (b *PlanCostExcel) Draws() {
  225. b.drawTitle()
  226. b.drawTableTitle()
  227. b.drawTableContent()
  228. }
  229. func NewPlanCostExcel(f *excelize.File) *PlanCostExcel {
  230. border := []excelize.Border{
  231. {Type: "top", Style: 1, Color: "000000"},
  232. {Type: "left", Style: 1, Color: "000000"},
  233. {Type: "right", Style: 1, Color: "000000"},
  234. {Type: "bottom", Style: 1, Color: "000000"},
  235. }
  236. styleLeft, _ := f.NewStyle(&excelize.Style{
  237. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  238. Border: border,
  239. Font: &excelize.Font{Size: 10},
  240. })
  241. b := &PlanCostExcel{
  242. Title: "生产成本表",
  243. SheetName: "Sheet1",
  244. Excel: f,
  245. Offset: 0,
  246. AlignCenterStyle: styleLeft,
  247. }
  248. f.SetColWidth(b.SheetName, "A", "D", 12)
  249. f.SetColWidth(b.SheetName, "E", "L", 10)
  250. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  251. return b
  252. }
  253. func (b *PlanCostExcel) FormatToEmpty(str *string) {
  254. if *str == "0" || *str == "0.00" {
  255. *str = ""
  256. }
  257. }