plan-cost-excel.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. package api
  2. import (
  3. "fmt"
  4. "github.com/xuri/excelize/v2"
  5. )
  6. // 生产成本表
  7. type PlanCostExcel struct {
  8. Row int
  9. Title string
  10. Excel *excelize.File
  11. SheetName string
  12. AlignCenterStyle int
  13. Content *SupplierPlanSummary
  14. }
  15. func (b *PlanCostExcel) drawTitle() error {
  16. b.Row++
  17. startCell := fmt.Sprintf("A%d", b.Row)
  18. err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("O%d", b.Row))
  19. if err != nil {
  20. return err
  21. }
  22. style, err := b.Excel.NewStyle(&excelize.Style{
  23. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  24. Font: &excelize.Font{Bold: true, Size: 18}})
  25. if err != nil {
  26. return err
  27. }
  28. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  29. if err != nil {
  30. return err
  31. }
  32. b.Excel.SetRowHeight(b.SheetName, b.Row, 23)
  33. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  34. return nil
  35. }
  36. func (b *PlanCostExcel) drawTableTitle() error {
  37. b.Row++
  38. var drawCol = func(prefix string, value string) error {
  39. left1Cell := fmt.Sprintf("%s%d", prefix, b.Row)
  40. left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1)
  41. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  42. if err != nil {
  43. return err
  44. }
  45. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  46. if err != nil {
  47. return err
  48. }
  49. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  50. }
  51. var drawCol2 = func(prefix1 string, prefix2 string, value1 string, value2 string, value3 string) error {
  52. left1Cell := fmt.Sprintf("%s%d", prefix1, b.Row)
  53. left2Cell := fmt.Sprintf("%s%d", prefix2, b.Row)
  54. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  55. if err != nil {
  56. return err
  57. }
  58. if err != nil {
  59. fmt.Println(err)
  60. return err
  61. }
  62. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  63. if err != nil {
  64. return err
  65. }
  66. b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
  67. val2Cel := fmt.Sprintf("%s%d", prefix1, b.Row+1)
  68. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  69. b.Excel.SetCellValue(b.SheetName, val2Cel, value2)
  70. val3Cel := fmt.Sprintf("%s%d", prefix2, b.Row+1)
  71. b.Excel.SetCellStyle(b.SheetName, val3Cel, val3Cel, b.AlignCenterStyle)
  72. b.Excel.SetCellValue(b.SheetName, val3Cel, value3)
  73. return nil
  74. }
  75. var drawCol3 = func(prefix1 string, prefix2 string, prefix3 string, value1 string, value2 string, value3 string, value4 string) error {
  76. left1Cell := fmt.Sprintf("%s%d", prefix1, b.Row)
  77. left3Cell := fmt.Sprintf("%s%d", prefix3, b.Row)
  78. err := b.Excel.MergeCell(b.SheetName, left1Cell, left3Cell)
  79. if err != nil {
  80. return err
  81. }
  82. if err != nil {
  83. fmt.Println(err)
  84. return err
  85. }
  86. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left3Cell, b.AlignCenterStyle)
  87. if err != nil {
  88. return err
  89. }
  90. b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
  91. val2Cel := fmt.Sprintf("%s%d", prefix1, b.Row+1)
  92. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  93. b.Excel.SetCellValue(b.SheetName, val2Cel, value2)
  94. val3Cel := fmt.Sprintf("%s%d", prefix2, b.Row+1)
  95. b.Excel.SetCellStyle(b.SheetName, val3Cel, val3Cel, b.AlignCenterStyle)
  96. b.Excel.SetCellValue(b.SheetName, val3Cel, value3)
  97. val4Cel := fmt.Sprintf("%s%d", prefix3, b.Row+1)
  98. b.Excel.SetCellStyle(b.SheetName, val4Cel, val4Cel, b.AlignCenterStyle)
  99. b.Excel.SetCellValue(b.SheetName, val4Cel, value4)
  100. return nil
  101. }
  102. drawCol("A", "产品名称")
  103. drawCol("A", "产品部件名称")
  104. drawCol2("B", "C", "类型/项目", "类型", "项目")
  105. drawCol("D", "下单数量")
  106. drawCol("E", "实际数量")
  107. drawCol("F", "状态") // 生成订单状态 审核状态 发送状态 完成状态
  108. drawCol("G", "供应商名称")
  109. drawCol("H", "单价")
  110. drawCol3("I", "J", "K", "规格", "厚度(纸克)", "长", "宽")
  111. drawCol("L", "单位")
  112. drawCol("M", "下单单价")
  113. drawCol("N", "预算金额")
  114. drawCol("O", "实际金额")
  115. return nil
  116. }
  117. func (b *PlanCostExcel) drawRow(rowIndex int, values ...string) {
  118. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O"}
  119. for i, c := range charas {
  120. v := ""
  121. if i < len(values) {
  122. v = values[i]
  123. }
  124. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  125. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  126. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  127. b.Excel.SetRowHeight(b.SheetName, rowIndex, 32)
  128. }
  129. }
  130. func (b *PlanCostExcel) drawAllContent() error {
  131. b.Row += 2
  132. // summaryPlans
  133. // 预算金额汇总
  134. var totalBudgetPrice float64 = 0.00
  135. // 实际金额汇总
  136. var totalRealPrice float64 = 0.00
  137. for _, splan := range b.Content.Plans {
  138. plan := splan.Plan
  139. comps := plan.Pack.Components
  140. // 预算金额汇总
  141. var planBudgetPrice float64 = 0.00
  142. // 实际金额汇总
  143. var planRealPrice float64 = 0.00
  144. if len(comps) > 0 {
  145. for _, comp := range comps {
  146. var perBudgetPrice float64 = 0.00
  147. var perRealPrice float64 = 0.00
  148. if len(comp.Stages) > 0 {
  149. startRow := b.Row
  150. cates := map[string][]int{}
  151. for _, stage := range comp.Stages {
  152. matHeigth := fmt.Sprintf("%d", stage.BatchSizeHeight)
  153. b.FormatToEmpty(&matHeigth)
  154. matWidth := fmt.Sprintf("%d", stage.BatchSizeWidth)
  155. b.FormatToEmpty(&matWidth)
  156. orderCount := fmt.Sprintf("%d", int(stage.OrderCount))
  157. b.FormatToEmpty(&orderCount)
  158. // 实际数量
  159. realCount := fmt.Sprintf("%d", stage.ConfirmCount)
  160. b.FormatToEmpty(&realCount)
  161. // 单价
  162. price := fmt.Sprintf("%.3f", stage.OrderPrice)
  163. b.FormatToEmpty(&price)
  164. // 预算金额
  165. budgetPrice := fmt.Sprintf("%.3f", stage.OrderPrice*float64(stage.OrderCount))
  166. perBudgetPrice += stage.OrderPrice * float64(stage.OrderCount)
  167. b.FormatToEmpty(&budgetPrice)
  168. // 实际金额
  169. perRealPrice += stage.OrderPrice * float64(stage.ConfirmCount)
  170. realPrice := fmt.Sprintf("%.3f", stage.OrderPrice*float64(stage.ConfirmCount))
  171. b.FormatToEmpty(&realPrice)
  172. unit := stage.Unit
  173. if stage.Unit == "吨" || stage.Unit == "平方米" {
  174. unit = "张"
  175. }
  176. supplierName := ""
  177. if stage.SupplierInfo != nil {
  178. supplierName = stage.SupplierInfo.Name
  179. }
  180. stageType := ""
  181. if stage.BillType > 0 {
  182. stage.Type = stage.BillType
  183. }
  184. if stage.Type == 1 {
  185. stageType = "材料采购"
  186. }
  187. if stage.Type == 2 {
  188. stageType = "工艺"
  189. }
  190. if stage.Type == 3 {
  191. stageType = "成品采购"
  192. }
  193. // 状态
  194. stop := false
  195. stageStatus := ""
  196. if len(stage.BillId) < 1 {
  197. stageStatus = "未生成订单"
  198. stop = true
  199. }
  200. // 审核状态
  201. if !stop {
  202. if splan.Reviewed[stage.BillId] == 1 {
  203. stageStatus = "已审核"
  204. } else {
  205. stageStatus = "未审核"
  206. stop = true
  207. }
  208. }
  209. // 接单状态
  210. if !stop {
  211. if splan.IsAck[stage.BillId] {
  212. stageStatus = "已接单"
  213. } else {
  214. stageStatus = "未接单"
  215. stop = true
  216. }
  217. }
  218. // 完成状态
  219. if splan.State[stage.BillId] == "complete" {
  220. stageStatus = "已完成"
  221. } else if splan.State[stage.BillId] == "created" {
  222. stageStatus = "进行中"
  223. }
  224. 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)
  225. if stage.SupplierInfo != nil {
  226. cates[stage.SupplierInfo.Name] = append(cates[stage.SupplierInfo.Name], b.Row)
  227. }
  228. b.Row++
  229. }
  230. for supplierName, cate := range cates {
  231. mergeStartRow := cate[0]
  232. mergeEndRow := cate[len(cate)-1]
  233. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("G%d", mergeStartRow), fmt.Sprintf("G%d", mergeEndRow))
  234. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("G%d", mergeEndRow), supplierName)
  235. }
  236. endRow := b.Row - 1
  237. // 组件名字
  238. startACell := fmt.Sprintf("%s%d", "A", startRow)
  239. endACell := fmt.Sprintf("%s%d", "A", endRow)
  240. b.Excel.MergeCell(b.SheetName, startACell, endACell)
  241. err := b.Excel.SetCellStyle(b.SheetName, startACell, endACell, b.AlignCenterStyle)
  242. if err != nil {
  243. return err
  244. }
  245. b.Excel.SetCellValue(b.SheetName, startACell, comp.Name)
  246. }
  247. // 预算
  248. planBudgetPrice += perBudgetPrice
  249. // 实际金额
  250. planRealPrice += perRealPrice
  251. }
  252. }
  253. totalBudgetPrice += planBudgetPrice
  254. totalRealPrice += planRealPrice
  255. }
  256. summaryEndRow := b.Row
  257. startACell := fmt.Sprintf("%s%d", "A", summaryEndRow)
  258. endMell := fmt.Sprintf("%s%d", "M", summaryEndRow)
  259. NCell := fmt.Sprintf("%s%d", "N", summaryEndRow)
  260. OCell := fmt.Sprintf("%s%d", "O", summaryEndRow)
  261. b.Excel.MergeCell(b.SheetName, startACell, endMell)
  262. b.Excel.SetCellStyle(b.SheetName, startACell, endMell, b.AlignCenterStyle)
  263. b.Excel.SetCellValue(b.SheetName, startACell, "生产计划汇总金额")
  264. // 生产预算汇总
  265. b.Excel.SetCellStyle(b.SheetName, NCell, NCell, b.AlignCenterStyle)
  266. totalOrderRealPrice := fmt.Sprintf("%.3f", totalBudgetPrice)
  267. b.FormatToEmpty(&totalOrderRealPrice)
  268. b.Excel.SetCellValue(b.SheetName, NCell, totalOrderRealPrice)
  269. // 生产实际汇总
  270. b.Excel.SetCellStyle(b.SheetName, OCell, OCell, b.AlignCenterStyle)
  271. totalRealPricef := fmt.Sprintf("%.3f", totalRealPrice)
  272. b.FormatToEmpty(&totalRealPricef)
  273. b.Excel.SetCellValue(b.SheetName, OCell, totalRealPricef)
  274. return nil
  275. }
  276. func (b *PlanCostExcel) drawSupplierContent() error {
  277. b.Row += 2
  278. supplier := ""
  279. // summaryPlans
  280. // 预算金额汇总
  281. var totalBudgetPrice float64 = 0.00
  282. // 实际金额汇总
  283. var totalRealPrice float64 = 0.00
  284. for _, splan := range b.Content.Plans {
  285. plan := splan.Plan
  286. comps := plan.Pack.Components
  287. // 预算金额汇总
  288. var planBudgetPrice float64 = 0.00
  289. // 实际金额汇总
  290. var planRealPrice float64 = 0.00
  291. if len(comps) > 0 {
  292. for _, comp := range comps {
  293. var perBudgetPrice float64 = 0.00
  294. var perRealPrice float64 = 0.00
  295. if len(comp.Stages) > 0 {
  296. startRow := 0
  297. cates := map[string][]int{}
  298. for _, stage := range comp.Stages {
  299. if stage.SupplierInfo != nil {
  300. if b.Content.SupplierId == stage.SupplierInfo.Id {
  301. supplier = stage.SupplierInfo.Name
  302. // 材料
  303. if startRow == 0 {
  304. startRow = b.Row
  305. }
  306. matHeigth := fmt.Sprintf("%d", stage.BatchSizeHeight)
  307. b.FormatToEmpty(&matHeigth)
  308. matWidth := fmt.Sprintf("%d", stage.BatchSizeWidth)
  309. b.FormatToEmpty(&matWidth)
  310. orderCount := fmt.Sprintf("%d", int(stage.OrderCount))
  311. b.FormatToEmpty(&orderCount)
  312. // 实际数量
  313. realCount := fmt.Sprintf("%d", stage.ConfirmCount)
  314. b.FormatToEmpty(&realCount)
  315. // 单价
  316. price := fmt.Sprintf("%.3f", stage.OrderPrice)
  317. b.FormatToEmpty(&price)
  318. // 预算金额
  319. budgetPrice := fmt.Sprintf("%.3f", stage.OrderPrice*float64(stage.OrderCount))
  320. perBudgetPrice += stage.OrderPrice * float64(stage.OrderCount)
  321. b.FormatToEmpty(&budgetPrice)
  322. // 实际金额
  323. perRealPrice += stage.OrderPrice * float64(stage.ConfirmCount)
  324. realPrice := fmt.Sprintf("%.3f", stage.OrderPrice*float64(stage.ConfirmCount))
  325. b.FormatToEmpty(&realPrice)
  326. unit := stage.Unit
  327. if stage.Unit == "吨" || stage.Unit == "平方米" {
  328. unit = "张"
  329. }
  330. supplierName := ""
  331. if stage.SupplierInfo != nil {
  332. supplierName = stage.SupplierInfo.Name
  333. }
  334. stageType := ""
  335. if stage.BillType > 0 {
  336. stage.Type = stage.BillType
  337. }
  338. if stage.Type == 1 {
  339. stageType = "材料采购"
  340. }
  341. if stage.Type == 2 {
  342. stageType = "工艺"
  343. }
  344. if stage.Type == 3 {
  345. stageType = "成品采购"
  346. }
  347. // 状态
  348. stop := false
  349. stageStatus := ""
  350. if len(stage.BillId) < 1 {
  351. stageStatus = "未生成订单"
  352. stop = true
  353. }
  354. // 审核状态
  355. if !stop {
  356. if splan.Reviewed[stage.BillId] == 1 {
  357. stageStatus = "已审核"
  358. } else {
  359. stageStatus = "未审核"
  360. stop = true
  361. }
  362. }
  363. // 接单状态
  364. if !stop {
  365. if splan.IsAck[stage.BillId] {
  366. stageStatus = "已接单"
  367. } else {
  368. stageStatus = "未接单"
  369. stop = true
  370. }
  371. }
  372. // 完成状态
  373. if splan.State[stage.BillId] == "complete" {
  374. stageStatus = "已完成"
  375. } else if splan.State[stage.BillId] == "created" {
  376. stageStatus = "进行中"
  377. }
  378. 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)
  379. if stage.SupplierInfo != nil {
  380. cates[stage.SupplierInfo.Name] = append(cates[stage.SupplierInfo.Name], b.Row)
  381. }
  382. b.Row++
  383. }
  384. }
  385. }
  386. // 合并同一供应商名
  387. for supplierName, cate := range cates {
  388. mergeStartRow := cate[0]
  389. mergeEndRow := cate[len(cate)-1]
  390. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("G%d", mergeStartRow), fmt.Sprintf("G%d", mergeEndRow))
  391. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("G%d", mergeEndRow), supplierName)
  392. }
  393. if startRow != 0 {
  394. endRow := b.Row - 1
  395. // 组件名字
  396. startACell := fmt.Sprintf("%s%d", "A", startRow)
  397. endACell := fmt.Sprintf("%s%d", "A", endRow)
  398. b.Excel.MergeCell(b.SheetName, startACell, endACell)
  399. err := b.Excel.SetCellStyle(b.SheetName, startACell, endACell, b.AlignCenterStyle)
  400. if err != nil {
  401. return err
  402. }
  403. b.Excel.SetCellValue(b.SheetName, startACell, comp.Name)
  404. }
  405. }
  406. // 预算
  407. totalBudgetPrice += perBudgetPrice
  408. // 预算
  409. totalRealPrice += perRealPrice
  410. }
  411. }
  412. totalBudgetPrice += planBudgetPrice
  413. totalRealPrice += planRealPrice
  414. }
  415. summaryEndRow := b.Row
  416. startACell := fmt.Sprintf("%s%d", "A", summaryEndRow)
  417. endMell := fmt.Sprintf("%s%d", "M", summaryEndRow)
  418. NCell := fmt.Sprintf("%s%d", "N", summaryEndRow)
  419. OCell := fmt.Sprintf("%s%d", "O", summaryEndRow)
  420. b.Excel.MergeCell(b.SheetName, startACell, endMell)
  421. b.Excel.SetCellStyle(b.SheetName, startACell, endMell, b.AlignCenterStyle)
  422. b.Excel.SetCellValue(b.SheetName, startACell, "生产计划汇总金额")
  423. // 生产预算汇总
  424. b.Excel.SetCellStyle(b.SheetName, NCell, NCell, b.AlignCenterStyle)
  425. totalOrderRealPrice := fmt.Sprintf("%.3f", totalBudgetPrice)
  426. b.FormatToEmpty(&totalOrderRealPrice)
  427. b.Excel.SetCellValue(b.SheetName, NCell, totalOrderRealPrice)
  428. // 生产实际汇总
  429. b.Excel.SetCellStyle(b.SheetName, OCell, OCell, b.AlignCenterStyle)
  430. totalRealPricef := fmt.Sprintf("%.3f", totalRealPrice)
  431. b.FormatToEmpty(&totalRealPricef)
  432. b.Excel.SetCellValue(b.SheetName, OCell, totalRealPricef)
  433. // 供应商名字标题
  434. style, err := b.Excel.NewStyle(&excelize.Style{
  435. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  436. })
  437. if err != nil {
  438. return err
  439. }
  440. err = b.Excel.SetCellStyle(b.SheetName, "A1", "G1", style)
  441. if err != nil {
  442. return err
  443. }
  444. b.Excel.SetRowHeight(b.SheetName, 1, 32)
  445. b.Excel.SetCellValue(b.SheetName, "A1", fmt.Sprintf("【%s】-生产成本表", supplier))
  446. return nil
  447. }
  448. func (b *PlanCostExcel) Draws() {
  449. b.drawTitle()
  450. b.drawTableTitle()
  451. if !b.Content.SupplierId.IsZero() {
  452. b.drawSupplierContent()
  453. } else {
  454. b.drawAllContent()
  455. }
  456. }
  457. func NewPlanCostExcel(f *excelize.File) *PlanCostExcel {
  458. border := []excelize.Border{
  459. {Type: "top", Style: 1, Color: "000000"},
  460. {Type: "left", Style: 1, Color: "000000"},
  461. {Type: "right", Style: 1, Color: "000000"},
  462. {Type: "bottom", Style: 1, Color: "000000"},
  463. }
  464. styleLeft, _ := f.NewStyle(&excelize.Style{
  465. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  466. Border: border,
  467. Font: &excelize.Font{Size: 10},
  468. })
  469. b := &PlanCostExcel{
  470. Title: "生产成本表",
  471. SheetName: "Sheet1",
  472. Excel: f,
  473. AlignCenterStyle: styleLeft,
  474. }
  475. f.SetColWidth(b.SheetName, "A", "D", 16)
  476. f.SetColWidth(b.SheetName, "E", "F", 12)
  477. f.SetColWidth(b.SheetName, "G", "G", 25)
  478. f.SetColWidth(b.SheetName, "H", "K", 16)
  479. f.SetColWidth(b.SheetName, "L", "L", 10)
  480. f.SetColWidth(b.SheetName, "M", "O", 16)
  481. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  482. return b
  483. }
  484. func (b *PlanCostExcel) FormatToEmpty(str *string) {
  485. if *str == "0" || *str == "0.000" {
  486. *str = ""
  487. }
  488. }