plan-cost-excel.go 16 KB

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