plan-cost-excel.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. package api
  2. import (
  3. "fmt"
  4. "github.com/xuri/excelize/v2"
  5. "go.mongodb.org/mongo-driver/bson/primitive"
  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. Content *SupplierPlanCost
  16. }
  17. func (b *PlanCostExcel) drawTitle() error {
  18. tileIndex := b.Offset + 1
  19. startCell := fmt.Sprintf("A%d", tileIndex)
  20. err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("M%d", tileIndex))
  21. if err != nil {
  22. return err
  23. }
  24. style, err := b.Excel.NewStyle(&excelize.Style{
  25. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  26. Font: &excelize.Font{Bold: true, Size: 18}})
  27. if err != nil {
  28. return err
  29. }
  30. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  31. if err != nil {
  32. return err
  33. }
  34. b.Excel.SetRowHeight(b.SheetName, tileIndex, 23)
  35. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  36. return nil
  37. }
  38. func (b *PlanCostExcel) drawTableTitle() error {
  39. row := b.Offset + 2
  40. //A采购项目 B规格(克) 尺寸C-D 数量E 单价F-G 交货时间H 备注I
  41. // A产品名称
  42. var drawCol = func(prefix string, value string) error {
  43. left1Cell := fmt.Sprintf("%s%d", prefix, row)
  44. left2Cell := fmt.Sprintf("%s%d", prefix, row+1)
  45. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  46. if err != nil {
  47. return err
  48. }
  49. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  50. if err != nil {
  51. return err
  52. }
  53. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  54. }
  55. // var drawCol1 = func(prefix string, value1 string, value2 string) error {
  56. // topCell := fmt.Sprintf("%s%d", prefix, row)
  57. // bottomCell := fmt.Sprintf("%s%d", prefix, row+1)
  58. // b.Excel.SetCellStyle(b.SheetName, topCell, topCell, b.AlignCenterStyle)
  59. // b.Excel.SetCellValue(b.SheetName, topCell, value1)
  60. // b.Excel.SetCellStyle(b.SheetName, bottomCell, bottomCell, b.AlignCenterStyle)
  61. // b.Excel.SetCellValue(b.SheetName, bottomCell, value2)
  62. // return nil
  63. // }
  64. var drawCol2 = func(prefix1 string, prefix2 string, value1 string, value2 string, value3 string) error {
  65. left1Cell := fmt.Sprintf("%s%d", prefix1, row)
  66. left2Cell := fmt.Sprintf("%s%d", prefix2, row)
  67. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  68. if err != nil {
  69. return err
  70. }
  71. if err != nil {
  72. fmt.Println(err)
  73. return err
  74. }
  75. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  76. if err != nil {
  77. return err
  78. }
  79. b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
  80. val2Cel := fmt.Sprintf("%s%d", prefix1, row+1)
  81. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  82. b.Excel.SetCellValue(b.SheetName, val2Cel, value2)
  83. val3Cel := fmt.Sprintf("%s%d", prefix2, row+1)
  84. b.Excel.SetCellStyle(b.SheetName, val3Cel, val3Cel, b.AlignCenterStyle)
  85. b.Excel.SetCellValue(b.SheetName, val3Cel, value3)
  86. return nil
  87. }
  88. var drawCol3 = func(prefix1 string, prefix2 string, prefix3 string, value1 string, value2 string, value3 string, value4 string) error {
  89. left1Cell := fmt.Sprintf("%s%d", prefix1, row)
  90. // left2Cell := fmt.Sprintf("%s%d", prefix2, row)
  91. left3Cell := fmt.Sprintf("%s%d", prefix3, row)
  92. err := b.Excel.MergeCell(b.SheetName, left1Cell, left3Cell)
  93. if err != nil {
  94. return err
  95. }
  96. if err != nil {
  97. fmt.Println(err)
  98. return err
  99. }
  100. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left3Cell, b.AlignCenterStyle)
  101. if err != nil {
  102. return err
  103. }
  104. b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
  105. val2Cel := fmt.Sprintf("%s%d", prefix1, row+1)
  106. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  107. b.Excel.SetCellValue(b.SheetName, val2Cel, value2)
  108. val3Cel := fmt.Sprintf("%s%d", prefix2, row+1)
  109. b.Excel.SetCellStyle(b.SheetName, val3Cel, val3Cel, b.AlignCenterStyle)
  110. b.Excel.SetCellValue(b.SheetName, val3Cel, value3)
  111. val4Cel := fmt.Sprintf("%s%d", prefix3, row+1)
  112. b.Excel.SetCellStyle(b.SheetName, val4Cel, val4Cel, b.AlignCenterStyle)
  113. b.Excel.SetCellValue(b.SheetName, val4Cel, value4)
  114. return nil
  115. }
  116. drawCol("A", "产品配料名称")
  117. drawCol2("B", "C", "材料/工序", "材料", "工序")
  118. drawCol("D", "供应商名称")
  119. drawCol3("E", "F", "G", "规格", "厚度(纸克)", "长", "宽")
  120. drawCol("H", "单位")
  121. drawCol("I", "下单数量")
  122. drawCol("J", "实际数量")
  123. drawCol("K", "单价")
  124. drawCol("L", "预算金额")
  125. drawCol("M", "实际金额")
  126. return nil
  127. }
  128. func (b *PlanCostExcel) drawSupplierContent() error {
  129. supplierId, err := primitive.ObjectIDFromHex(b.Content.SupplierId)
  130. if err != nil {
  131. return err
  132. }
  133. row := b.Offset + 4
  134. comps := b.Content.Pack.Components
  135. var totalPlanPrice float32 = 0.00
  136. var confirmTotalPlanPrice float64 = 0.00
  137. if len(comps) > 0 {
  138. for _, comp := range comps {
  139. var totalOrderRealPrice float32 = 0.00
  140. var totalConfirmRealPrice float64 = 0.00
  141. if len(comp.Mats) > 0 {
  142. startRow := 0
  143. for _, mat := range comp.Mats {
  144. if mat.Supplier.SupplierInfo != nil {
  145. if supplierId == mat.Supplier.SupplierInfo.Id {
  146. // 材料
  147. if startRow == 0 {
  148. startRow = row
  149. }
  150. supplierName := mat.Supplier.SupplierInfo.Name
  151. matHeigth := fmt.Sprintf("%d", mat.BatchSizeHeight)
  152. b.FormatToEmpty(&matHeigth)
  153. matWidth := fmt.Sprintf("%d", mat.BatchSizeWidth)
  154. b.FormatToEmpty(&matWidth)
  155. orderCount := fmt.Sprintf("%d", int(mat.Supplier.OrderCount))
  156. b.FormatToEmpty(&orderCount)
  157. // 实际数量
  158. confirmCount := fmt.Sprintf("%d", mat.ConfirmCount)
  159. b.FormatToEmpty(&confirmCount)
  160. // 单价
  161. orderPrice := fmt.Sprintf("%.2f", mat.Supplier.OrderPrice)
  162. b.FormatToEmpty(&orderPrice)
  163. totalOrderRealPrice += mat.Supplier.OrderRealPrice
  164. orderRealPrice := fmt.Sprintf("%.2f", mat.Supplier.OrderRealPrice)
  165. b.FormatToEmpty(&orderRealPrice)
  166. // 实际金额
  167. confirmPrice := mat.Supplier.OrderPrice * float64(mat.ConfirmCount)
  168. totalConfirmRealPrice += confirmPrice
  169. confirmRealPrice := fmt.Sprintf("%.2f", confirmPrice)
  170. b.FormatToEmpty(&confirmRealPrice)
  171. b.drawRow(row, "", mat.MatInfo.Name, "", supplierName, mat.MatInfo.Norm, matHeigth, matWidth, mat.MatInfo.Unit, orderCount, confirmCount, orderPrice, orderRealPrice, confirmRealPrice)
  172. row++
  173. }
  174. }
  175. if len(mat.Crafts) > 0 {
  176. for _, craft := range mat.Crafts {
  177. if craft.Supplier.SupplierInfo != nil {
  178. // 工序
  179. if supplierId == craft.Supplier.SupplierInfo.Id {
  180. if startRow == 0 {
  181. startRow = row
  182. }
  183. craftSupplierName := craft.Supplier.SupplierInfo.Name
  184. carftOrderCount := fmt.Sprintf("%d", int(craft.Supplier.OrderCount))
  185. b.FormatToEmpty(&carftOrderCount)
  186. carftHeigth := fmt.Sprintf("%d", craft.BatchSizeHeight)
  187. b.FormatToEmpty(&carftHeigth)
  188. carftWidth := fmt.Sprintf("%d", craft.BatchSizeWidth)
  189. b.FormatToEmpty(&carftWidth)
  190. // 实际数量
  191. confirmCraftCount := fmt.Sprintf("%d", craft.ConfirmCount)
  192. b.FormatToEmpty(&confirmCraftCount)
  193. carftOrderPrice := fmt.Sprintf("%.2f", craft.Supplier.OrderPrice)
  194. b.FormatToEmpty(&carftOrderPrice)
  195. totalOrderRealPrice += craft.Supplier.OrderRealPrice
  196. carftOrderRealPrice := fmt.Sprintf("%.2f", craft.Supplier.OrderRealPrice)
  197. b.FormatToEmpty(&carftOrderRealPrice)
  198. // 实际金额
  199. confirmCraftPrice := craft.Supplier.OrderPrice * float64(craft.ConfirmCount)
  200. totalConfirmRealPrice += confirmCraftPrice
  201. confirmCraftRealPrice := fmt.Sprintf("%.2f", confirmCraftPrice)
  202. b.FormatToEmpty(&confirmCraftRealPrice)
  203. b.drawRow(row, "", "", craft.CraftInfo.Name, craftSupplierName, craft.CraftInfo.Norm, carftHeigth, carftWidth, craft.CraftInfo.Unit, carftOrderCount, confirmCraftCount, carftOrderPrice, carftOrderRealPrice, confirmCraftRealPrice)
  204. row++
  205. }
  206. }
  207. }
  208. }
  209. }
  210. if startRow != 0 {
  211. endRow := row - 1
  212. // 组件名字
  213. startACell := fmt.Sprintf("%s%d", "A", startRow)
  214. endACell := fmt.Sprintf("%s%d", "A", endRow)
  215. b.Excel.MergeCell(b.SheetName, startACell, endACell)
  216. err := b.Excel.SetCellStyle(b.SheetName, startACell, endACell, b.AlignCenterStyle)
  217. if err != nil {
  218. return err
  219. }
  220. b.Excel.SetCellValue(b.SheetName, startACell, comp.Name)
  221. }
  222. }
  223. totalPlanPrice += totalOrderRealPrice
  224. confirmTotalPlanPrice += totalConfirmRealPrice
  225. }
  226. // 生产汇总金额
  227. startACell := fmt.Sprintf("%s%d", "A", row)
  228. endKCell := fmt.Sprintf("%s%d", "K", row)
  229. LCell := fmt.Sprintf("%s%d", "L", row)
  230. MCell := fmt.Sprintf("%s%d", "M", row)
  231. b.Excel.MergeCell(b.SheetName, startACell, endKCell)
  232. b.Excel.SetCellStyle(b.SheetName, startACell, endKCell, b.AlignCenterStyle)
  233. b.Excel.SetCellValue(b.SheetName, startACell, "生产计划汇总金额")
  234. // 生产预算汇总
  235. b.Excel.SetCellStyle(b.SheetName, LCell, LCell, b.AlignCenterStyle)
  236. planTotalPrice := fmt.Sprintf("%.2f", totalPlanPrice)
  237. b.FormatToEmpty(&planTotalPrice)
  238. b.Excel.SetCellValue(b.SheetName, LCell, planTotalPrice)
  239. // 生产实际汇总
  240. b.Excel.SetCellStyle(b.SheetName, MCell, MCell, b.AlignCenterStyle)
  241. planConfirmTotalPrice := fmt.Sprintf("%.2f", confirmTotalPlanPrice)
  242. b.FormatToEmpty(&planConfirmTotalPrice)
  243. b.Excel.SetCellValue(b.SheetName, MCell, planConfirmTotalPrice)
  244. }
  245. return nil
  246. }
  247. func (b *PlanCostExcel) drawRow(rowIndex int, values ...string) {
  248. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"}
  249. for i, c := range charas {
  250. v := ""
  251. if i < len(values) {
  252. v = values[i]
  253. }
  254. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  255. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  256. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  257. b.Excel.SetRowHeight(b.SheetName, rowIndex, 21)
  258. }
  259. }
  260. func (b *PlanCostExcel) drawAllContent() error {
  261. row := b.Offset + 4
  262. comps := b.Content.Pack.Components
  263. var confirmTotalPlanPrice float64 = 0.00
  264. if len(comps) > 0 {
  265. for _, comp := range comps {
  266. var totalConfirmRealPrice float64 = 0.00
  267. if len(comp.Mats) > 0 {
  268. startRow := row
  269. for _, mat := range comp.Mats {
  270. // 材料
  271. supplierName := ""
  272. if mat.Supplier.SupplierInfo != nil {
  273. supplierName = mat.Supplier.SupplierInfo.Name
  274. }
  275. matHeigth := fmt.Sprintf("%d", mat.BatchSizeHeight)
  276. b.FormatToEmpty(&matHeigth)
  277. matWidth := fmt.Sprintf("%d", mat.BatchSizeWidth)
  278. b.FormatToEmpty(&matWidth)
  279. orderCount := fmt.Sprintf("%d", int(mat.Supplier.OrderCount))
  280. b.FormatToEmpty(&orderCount)
  281. // 实际数量
  282. confirmCount := fmt.Sprintf("%d", mat.ConfirmCount)
  283. b.FormatToEmpty(&confirmCount)
  284. // 单价
  285. orderPrice := fmt.Sprintf("%.2f", mat.Supplier.OrderPrice)
  286. b.FormatToEmpty(&orderPrice)
  287. orderRealPrice := fmt.Sprintf("%.2f", mat.Supplier.OrderRealPrice)
  288. b.FormatToEmpty(&orderRealPrice)
  289. // 实际金额
  290. confirmPrice := mat.Supplier.OrderPrice * float64(mat.ConfirmCount)
  291. totalConfirmRealPrice += confirmPrice
  292. confirmRealPrice := fmt.Sprintf("%.2f", confirmPrice)
  293. b.FormatToEmpty(&confirmRealPrice)
  294. b.drawRow(row, "", mat.MatInfo.Name, "", supplierName, mat.MatInfo.Norm, matHeigth, matWidth, mat.MatInfo.Unit, orderCount, confirmCount, orderPrice, orderRealPrice, confirmRealPrice)
  295. row++
  296. if len(mat.Crafts) > 0 {
  297. // 工序
  298. for _, craft := range mat.Crafts {
  299. craftSupplierName := ""
  300. if craft.Supplier.SupplierInfo != nil {
  301. craftSupplierName = craft.Supplier.SupplierInfo.Name
  302. }
  303. carftOrderCount := fmt.Sprintf("%d", int(craft.Supplier.OrderCount))
  304. b.FormatToEmpty(&carftOrderCount)
  305. carftHeigth := fmt.Sprintf("%d", craft.BatchSizeHeight)
  306. b.FormatToEmpty(&carftHeigth)
  307. carftWidth := fmt.Sprintf("%d", craft.BatchSizeWidth)
  308. b.FormatToEmpty(&carftWidth)
  309. // 实际数量
  310. confirmCraftCount := fmt.Sprintf("%d", craft.ConfirmCount)
  311. b.FormatToEmpty(&confirmCraftCount)
  312. carftOrderPrice := fmt.Sprintf("%.2f", craft.Supplier.OrderPrice)
  313. b.FormatToEmpty(&carftOrderPrice)
  314. carftOrderRealPrice := fmt.Sprintf("%.2f", craft.Supplier.OrderRealPrice)
  315. b.FormatToEmpty(&carftOrderRealPrice)
  316. // 实际金额
  317. confirmCraftPrice := craft.Supplier.OrderPrice * float64(craft.ConfirmCount)
  318. totalConfirmRealPrice += confirmCraftPrice
  319. confirmCraftRealPrice := fmt.Sprintf("%.2f", confirmCraftPrice)
  320. b.FormatToEmpty(&confirmCraftRealPrice)
  321. b.drawRow(row, "", "", craft.CraftInfo.Name, craftSupplierName, craft.CraftInfo.Norm, carftHeigth, carftWidth, craft.CraftInfo.Unit, carftOrderCount, confirmCraftCount, carftOrderPrice, carftOrderRealPrice, confirmCraftRealPrice)
  322. row++
  323. }
  324. }
  325. }
  326. endRow := row - 1
  327. // 组件名字
  328. startACell := fmt.Sprintf("%s%d", "A", startRow)
  329. endACell := fmt.Sprintf("%s%d", "A", endRow)
  330. b.Excel.MergeCell(b.SheetName, startACell, endACell)
  331. err := b.Excel.SetCellStyle(b.SheetName, startACell, endACell, b.AlignCenterStyle)
  332. if err != nil {
  333. return err
  334. }
  335. b.Excel.SetCellValue(b.SheetName, startACell, comp.Name)
  336. }
  337. }
  338. // 生产汇总金额
  339. startACell := fmt.Sprintf("%s%d", "A", row)
  340. endKCell := fmt.Sprintf("%s%d", "K", row)
  341. LCell := fmt.Sprintf("%s%d", "L", row)
  342. MCell := fmt.Sprintf("%s%d", "M", row)
  343. b.Excel.MergeCell(b.SheetName, startACell, endKCell)
  344. b.Excel.SetCellStyle(b.SheetName, startACell, endKCell, b.AlignCenterStyle)
  345. b.Excel.SetCellValue(b.SheetName, startACell, "生产计划汇总金额")
  346. // 生产预算汇总
  347. b.Excel.SetCellStyle(b.SheetName, LCell, LCell, b.AlignCenterStyle)
  348. planTotalPrice := fmt.Sprintf("%.2f", b.Content.TotalPrice)
  349. b.FormatToEmpty(&planTotalPrice)
  350. b.Excel.SetCellValue(b.SheetName, LCell, planTotalPrice)
  351. // 生产实际汇总
  352. b.Excel.SetCellStyle(b.SheetName, MCell, MCell, b.AlignCenterStyle)
  353. planConfirmTotalPrice := fmt.Sprintf("%.2f", confirmTotalPlanPrice)
  354. b.FormatToEmpty(&planConfirmTotalPrice)
  355. b.Excel.SetCellValue(b.SheetName, MCell, planConfirmTotalPrice)
  356. }
  357. return nil
  358. }
  359. func (b *PlanCostExcel) Draws() {
  360. b.drawTitle()
  361. b.drawTableTitle()
  362. if b.Content.SupplierId != "" {
  363. b.drawSupplierContent()
  364. } else {
  365. b.drawAllContent()
  366. }
  367. }
  368. func NewPlanCostExcel(f *excelize.File) *PlanCostExcel {
  369. border := []excelize.Border{
  370. {Type: "top", Style: 1, Color: "000000"},
  371. {Type: "left", Style: 1, Color: "000000"},
  372. {Type: "right", Style: 1, Color: "000000"},
  373. {Type: "bottom", Style: 1, Color: "000000"},
  374. }
  375. styleLeft, _ := f.NewStyle(&excelize.Style{
  376. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  377. Border: border,
  378. Font: &excelize.Font{Size: 10},
  379. })
  380. b := &PlanCostExcel{
  381. Title: "生产成本表",
  382. SheetName: "Sheet1",
  383. Excel: f,
  384. Offset: 0,
  385. AlignCenterStyle: styleLeft,
  386. }
  387. f.SetColWidth(b.SheetName, "A", "D", 12)
  388. f.SetColWidth(b.SheetName, "E", "M", 10)
  389. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  390. return b
  391. }
  392. func (b *PlanCostExcel) FormatToEmpty(str *string) {
  393. if *str == "0" || *str == "0.00" {
  394. *str = ""
  395. }
  396. }