plan-cost-excel.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. stageStatus := ""
  195. if len(stage.BillId) < 1 {
  196. stageStatus = "未生成订单"
  197. } else {
  198. if splan.State[stage.BillId] == "created" {
  199. stageStatus = "进行中"
  200. // 审核状态
  201. if splan.Reviewed[stage.BillId] == 1 {
  202. stageStatus = "已审核"
  203. if splan.IsSend[stage.BillId] {
  204. stageStatus = "已发送"
  205. if splan.IsAck[stage.BillId] {
  206. stageStatus = "已接单"
  207. } else {
  208. stageStatus = "未接单"
  209. }
  210. } else {
  211. stageStatus = "未发送"
  212. }
  213. } else {
  214. stageStatus = "未审核"
  215. }
  216. } else if splan.State[stage.BillId] == "complete" {
  217. stageStatus = "已完成"
  218. } else {
  219. stageStatus = "未生成订单"
  220. }
  221. }
  222. 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)
  223. // if stage.SupplierInfo != nil {
  224. // cates[stage.SupplierInfo.Name] = append(cates[stage.SupplierInfo.Name], b.Row)
  225. // }
  226. b.Row++
  227. }
  228. // fmt.Println(cates)
  229. // for supplierName, cate := range cates {
  230. // mergeStartRow := cate[0]
  231. // mergeEndRow := cate[len(cate)-1]
  232. // b.Excel.MergeCell(b.SheetName, fmt.Sprintf("G%d", mergeStartRow), fmt.Sprintf("G%d", mergeEndRow))
  233. // b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("G%d", mergeEndRow), supplierName)
  234. // }
  235. endRow := b.Row - 1
  236. // 组件名字
  237. startACell := fmt.Sprintf("%s%d", "A", startRow)
  238. endACell := fmt.Sprintf("%s%d", "A", endRow)
  239. b.Excel.MergeCell(b.SheetName, startACell, endACell)
  240. err := b.Excel.SetCellStyle(b.SheetName, startACell, endACell, b.AlignCenterStyle)
  241. if err != nil {
  242. return err
  243. }
  244. b.Excel.SetCellValue(b.SheetName, startACell, comp.Name)
  245. }
  246. // 预算
  247. planBudgetPrice += perBudgetPrice
  248. // 实际金额
  249. planRealPrice += perRealPrice
  250. }
  251. }
  252. totalBudgetPrice += planBudgetPrice
  253. totalRealPrice += planRealPrice
  254. }
  255. summaryEndRow := b.Row
  256. startACell := fmt.Sprintf("%s%d", "A", summaryEndRow)
  257. endMell := fmt.Sprintf("%s%d", "M", summaryEndRow)
  258. NCell := fmt.Sprintf("%s%d", "N", summaryEndRow)
  259. OCell := fmt.Sprintf("%s%d", "O", summaryEndRow)
  260. b.Excel.MergeCell(b.SheetName, startACell, endMell)
  261. b.Excel.SetCellStyle(b.SheetName, startACell, endMell, b.AlignCenterStyle)
  262. b.Excel.SetCellValue(b.SheetName, startACell, "生产计划汇总金额")
  263. // 生产预算汇总
  264. b.Excel.SetCellStyle(b.SheetName, NCell, NCell, b.AlignCenterStyle)
  265. totalOrderRealPrice := fmt.Sprintf("%.3f", totalBudgetPrice)
  266. b.FormatToEmpty(&totalOrderRealPrice)
  267. b.Excel.SetCellValue(b.SheetName, NCell, totalOrderRealPrice)
  268. // 生产实际汇总
  269. b.Excel.SetCellStyle(b.SheetName, OCell, OCell, b.AlignCenterStyle)
  270. totalRealPricef := fmt.Sprintf("%.3f", totalRealPrice)
  271. b.FormatToEmpty(&totalRealPricef)
  272. b.Excel.SetCellValue(b.SheetName, OCell, totalRealPricef)
  273. return nil
  274. }
  275. func (b *PlanCostExcel) drawSupplierContent() error {
  276. b.Row += 2
  277. supplier := ""
  278. // summaryPlans
  279. // 预算金额汇总
  280. var totalBudgetPrice float64 = 0.00
  281. // 实际金额汇总
  282. var totalRealPrice float64 = 0.00
  283. for _, splan := range b.Content.Plans {
  284. plan := splan.Plan
  285. comps := plan.Pack.Components
  286. // 预算金额汇总
  287. var planBudgetPrice float64 = 0.00
  288. // 实际金额汇总
  289. var planRealPrice float64 = 0.00
  290. if len(comps) > 0 {
  291. for _, comp := range comps {
  292. var perBudgetPrice float64 = 0.00
  293. var perRealPrice float64 = 0.00
  294. if len(comp.Stages) > 0 {
  295. startRow := 0
  296. // cates := map[string][]int{}
  297. for _, stage := range comp.Stages {
  298. if stage.SupplierInfo != nil {
  299. if b.Content.SupplierId == stage.SupplierInfo.Id {
  300. supplier = stage.SupplierInfo.Name
  301. // 材料
  302. if startRow == 0 {
  303. startRow = b.Row
  304. }
  305. matHeigth := fmt.Sprintf("%d", stage.BatchSizeHeight)
  306. b.FormatToEmpty(&matHeigth)
  307. matWidth := fmt.Sprintf("%d", stage.BatchSizeWidth)
  308. b.FormatToEmpty(&matWidth)
  309. orderCount := fmt.Sprintf("%d", int(stage.OrderCount))
  310. b.FormatToEmpty(&orderCount)
  311. // 实际数量
  312. realCount := fmt.Sprintf("%d", stage.ConfirmCount)
  313. b.FormatToEmpty(&realCount)
  314. // 单价
  315. price := fmt.Sprintf("%.3f", stage.OrderPrice)
  316. b.FormatToEmpty(&price)
  317. // 预算金额
  318. budgetPrice := fmt.Sprintf("%.3f", stage.OrderPrice*float64(stage.OrderCount))
  319. perBudgetPrice += stage.OrderPrice * float64(stage.OrderCount)
  320. b.FormatToEmpty(&budgetPrice)
  321. // 实际金额
  322. perRealPrice += stage.OrderPrice * float64(stage.ConfirmCount)
  323. realPrice := fmt.Sprintf("%.3f", stage.OrderPrice*float64(stage.ConfirmCount))
  324. b.FormatToEmpty(&realPrice)
  325. unit := stage.Unit
  326. if stage.Unit == "吨" || stage.Unit == "平方米" {
  327. unit = "张"
  328. }
  329. supplierName := ""
  330. if stage.SupplierInfo != nil {
  331. supplierName = stage.SupplierInfo.Name
  332. }
  333. stageType := ""
  334. if stage.BillType > 0 {
  335. stage.Type = stage.BillType
  336. }
  337. if stage.Type == 1 {
  338. stageType = "材料采购"
  339. }
  340. if stage.Type == 2 {
  341. stageType = "工艺"
  342. }
  343. if stage.Type == 3 {
  344. stageType = "成品采购"
  345. }
  346. // 状态
  347. stageStatus := ""
  348. if len(stage.BillId) < 1 {
  349. stageStatus = "未生成订单"
  350. } else {
  351. if splan.State[stage.BillId] == "created" {
  352. stageStatus = "进行中"
  353. // 审核状态
  354. if splan.Reviewed[stage.BillId] == 1 {
  355. stageStatus = "已审核"
  356. if splan.IsSend[stage.BillId] {
  357. stageStatus = "已发送"
  358. if splan.IsAck[stage.BillId] {
  359. stageStatus = "已接单"
  360. } else {
  361. stageStatus = "未接单"
  362. }
  363. } else {
  364. stageStatus = "未发送"
  365. }
  366. } else {
  367. stageStatus = "未审核"
  368. }
  369. } else if splan.State[stage.BillId] == "complete" {
  370. stageStatus = "已完成"
  371. } else {
  372. stageStatus = "未生成订单"
  373. }
  374. }
  375. 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)
  376. // if stage.SupplierInfo != nil {
  377. // cates[stage.SupplierInfo.Name] = append(cates[stage.SupplierInfo.Name], b.Row)
  378. // }
  379. b.Row++
  380. }
  381. }
  382. }
  383. // todo 有bug 合并同一供应商名
  384. // for supplierName, cate := range cates {
  385. // mergeStartRow := cate[0]
  386. // mergeEndRow := cate[len(cate)-1]
  387. // b.Excel.MergeCell(b.SheetName, fmt.Sprintf("G%d", mergeStartRow), fmt.Sprintf("G%d", mergeEndRow))
  388. // b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("G%d", mergeEndRow), supplierName)
  389. // }
  390. if startRow != 0 {
  391. endRow := b.Row - 1
  392. // 组件名字
  393. startACell := fmt.Sprintf("%s%d", "A", startRow)
  394. endACell := fmt.Sprintf("%s%d", "A", endRow)
  395. b.Excel.MergeCell(b.SheetName, startACell, endACell)
  396. err := b.Excel.SetCellStyle(b.SheetName, startACell, endACell, b.AlignCenterStyle)
  397. if err != nil {
  398. return err
  399. }
  400. b.Excel.SetCellValue(b.SheetName, startACell, comp.Name)
  401. }
  402. }
  403. // 预算
  404. totalBudgetPrice += perBudgetPrice
  405. // 预算
  406. totalRealPrice += perRealPrice
  407. }
  408. }
  409. totalBudgetPrice += planBudgetPrice
  410. totalRealPrice += planRealPrice
  411. }
  412. summaryEndRow := b.Row
  413. startACell := fmt.Sprintf("%s%d", "A", summaryEndRow)
  414. endMell := fmt.Sprintf("%s%d", "M", summaryEndRow)
  415. NCell := fmt.Sprintf("%s%d", "N", summaryEndRow)
  416. OCell := fmt.Sprintf("%s%d", "O", summaryEndRow)
  417. b.Excel.MergeCell(b.SheetName, startACell, endMell)
  418. b.Excel.SetCellStyle(b.SheetName, startACell, endMell, b.AlignCenterStyle)
  419. b.Excel.SetCellValue(b.SheetName, startACell, "生产计划汇总金额")
  420. // 生产预算汇总
  421. b.Excel.SetCellStyle(b.SheetName, NCell, NCell, b.AlignCenterStyle)
  422. totalOrderRealPrice := fmt.Sprintf("%.3f", totalBudgetPrice)
  423. b.FormatToEmpty(&totalOrderRealPrice)
  424. b.Excel.SetCellValue(b.SheetName, NCell, totalOrderRealPrice)
  425. // 生产实际汇总
  426. b.Excel.SetCellStyle(b.SheetName, OCell, OCell, b.AlignCenterStyle)
  427. totalRealPricef := fmt.Sprintf("%.3f", totalRealPrice)
  428. b.FormatToEmpty(&totalRealPricef)
  429. b.Excel.SetCellValue(b.SheetName, OCell, totalRealPricef)
  430. // 供应商名字标题
  431. style, err := b.Excel.NewStyle(&excelize.Style{
  432. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  433. })
  434. if err != nil {
  435. return err
  436. }
  437. err = b.Excel.SetCellStyle(b.SheetName, "A1", "G1", style)
  438. if err != nil {
  439. return err
  440. }
  441. b.Excel.SetRowHeight(b.SheetName, 1, 32)
  442. b.Excel.SetCellValue(b.SheetName, "A1", fmt.Sprintf("【%s】-生产成本表", supplier))
  443. return nil
  444. }
  445. func (b *PlanCostExcel) Draws() {
  446. b.drawTitle()
  447. b.drawTableTitle()
  448. if !b.Content.SupplierId.IsZero() {
  449. b.drawSupplierContent()
  450. } else {
  451. b.drawAllContent()
  452. }
  453. }
  454. func NewPlanCostExcel(f *excelize.File) *PlanCostExcel {
  455. border := []excelize.Border{
  456. {Type: "top", Style: 1, Color: "000000"},
  457. {Type: "left", Style: 1, Color: "000000"},
  458. {Type: "right", Style: 1, Color: "000000"},
  459. {Type: "bottom", Style: 1, Color: "000000"},
  460. }
  461. styleLeft, _ := f.NewStyle(&excelize.Style{
  462. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  463. Border: border,
  464. Font: &excelize.Font{Size: 10},
  465. })
  466. b := &PlanCostExcel{
  467. Title: "生产成本表",
  468. SheetName: "Sheet1",
  469. Excel: f,
  470. AlignCenterStyle: styleLeft,
  471. }
  472. f.SetColWidth(b.SheetName, "A", "D", 16)
  473. f.SetColWidth(b.SheetName, "E", "F", 12)
  474. f.SetColWidth(b.SheetName, "G", "G", 25)
  475. f.SetColWidth(b.SheetName, "H", "K", 16)
  476. f.SetColWidth(b.SheetName, "L", "L", 10)
  477. f.SetColWidth(b.SheetName, "M", "O", 16)
  478. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  479. return b
  480. }
  481. func (b *PlanCostExcel) FormatToEmpty(str *string) {
  482. if *str == "0" || *str == "0.000" {
  483. *str = ""
  484. }
  485. }