plan-cost-excel.go 14 KB

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