plan-cost-excel.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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("M%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. drawCol3("E", "F", "G", "规格", "厚度(纸克)", "长", "宽")
  113. drawCol("H", "单位")
  114. drawCol("I", "下单数量")
  115. drawCol("J", "实际数量")
  116. drawCol("K", "单价") // ??? 下单单价
  117. drawCol("L", "预算金额")
  118. drawCol("M", "实际金额")
  119. return nil
  120. }
  121. func (b *PlanCostExcel) drawRow(rowIndex int, values ...string) {
  122. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"}
  123. for i, c := range charas {
  124. v := ""
  125. if i < len(values) {
  126. v = values[i]
  127. }
  128. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  129. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  130. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  131. b.Excel.SetRowHeight(b.SheetName, rowIndex, 21)
  132. }
  133. }
  134. func (b *PlanCostExcel) drawSupplierContent() error {
  135. b.Row += 2
  136. supplierId, err := primitive.ObjectIDFromHex(b.Content.SupplierId)
  137. if err != nil {
  138. return err
  139. }
  140. supplier := ""
  141. comps := b.Content.Pack.Components
  142. // 预算金额汇总
  143. var totalBudgetPrice float64 = 0.00
  144. // 实际金额汇总
  145. var totalRealPrice float64 = 0.00
  146. if len(comps) > 0 {
  147. for _, comp := range comps {
  148. var perBudgetPrice float64 = 0.00
  149. var perRealPrice float64 = 0.00
  150. if len(comp.Stages) > 0 {
  151. startRow := 0
  152. cates := map[string][]int{}
  153. for _, stage := range comp.Stages {
  154. if stage.SupplierInfo != nil {
  155. if supplierId == stage.SupplierInfo.Id {
  156. supplier = stage.SupplierInfo.Name
  157. // 材料
  158. if startRow == 0 {
  159. startRow = b.Row
  160. }
  161. matHeigth := fmt.Sprintf("%d", stage.BatchSizeHeight)
  162. b.FormatToEmpty(&matHeigth)
  163. matWidth := fmt.Sprintf("%d", stage.BatchSizeWidth)
  164. b.FormatToEmpty(&matWidth)
  165. orderCount := fmt.Sprintf("%d", int(stage.OrderCount))
  166. b.FormatToEmpty(&orderCount)
  167. // 实际数量
  168. realCount := fmt.Sprintf("%d", stage.RealCount)
  169. b.FormatToEmpty(&realCount)
  170. // 单价
  171. price := fmt.Sprintf("%.3f", stage.OrderPrice)
  172. b.FormatToEmpty(&price)
  173. // 预算金额
  174. budgetPrice := fmt.Sprintf("%.3f", stage.OrderPrice*float64(stage.OrderCount))
  175. perBudgetPrice += stage.OrderPrice * float64(stage.OrderCount)
  176. b.FormatToEmpty(&budgetPrice)
  177. // 实际金额
  178. perRealPrice += stage.OrderPrice * float64(stage.RealCount)
  179. realPrice := fmt.Sprintf("%.3f", stage.OrderPrice*float64(stage.RealCount))
  180. b.FormatToEmpty(&realPrice)
  181. if stage.Unit == "吨" || stage.Unit == "平方米" {
  182. stage.Unit = "张"
  183. }
  184. // 生成单据时已billType为准
  185. stageType := ""
  186. if stage.BillType > 0 {
  187. stage.Type = stage.BillType
  188. }
  189. if stage.Type == 1 {
  190. stageType = "材料采购"
  191. }
  192. if stage.Type == 2 {
  193. stageType = "工艺"
  194. }
  195. if stage.Type == 3 {
  196. stageType = "成品采购"
  197. }
  198. b.drawRow(b.Row, "", stageType, stage.Name, "", stage.Norm, matHeigth, matWidth, stage.Unit, orderCount, realCount, price, budgetPrice, realPrice)
  199. cates[stage.SupplierInfo.Name] = append(cates[stage.SupplierInfo.Name], b.Row)
  200. b.Row++
  201. }
  202. }
  203. }
  204. // 合并同一供应商名
  205. for supplierName, cate := range cates {
  206. mergeStartRow := cate[0]
  207. mergeEndRow := cate[len(cate)-1]
  208. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("D%d", mergeStartRow), fmt.Sprintf("D%d", mergeEndRow))
  209. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("D%d", mergeEndRow), supplierName)
  210. }
  211. if startRow != 0 {
  212. endRow := b.Row - 1
  213. // 组件名字
  214. startACell := fmt.Sprintf("%s%d", "A", startRow)
  215. endACell := fmt.Sprintf("%s%d", "A", endRow)
  216. b.Excel.MergeCell(b.SheetName, startACell, endACell)
  217. err := b.Excel.SetCellStyle(b.SheetName, startACell, endACell, b.AlignCenterStyle)
  218. if err != nil {
  219. return err
  220. }
  221. b.Excel.SetCellValue(b.SheetName, startACell, comp.Name)
  222. }
  223. }
  224. // 预算
  225. totalBudgetPrice += perBudgetPrice
  226. // 预算
  227. totalRealPrice += perRealPrice
  228. }
  229. }
  230. // 生产汇总金额
  231. startACell := fmt.Sprintf("%s%d", "A", b.Row)
  232. endKCell := fmt.Sprintf("%s%d", "K", b.Row)
  233. LCell := fmt.Sprintf("%s%d", "L", b.Row)
  234. MCell := fmt.Sprintf("%s%d", "M", b.Row)
  235. b.Excel.MergeCell(b.SheetName, startACell, endKCell)
  236. b.Excel.SetCellStyle(b.SheetName, startACell, endKCell, b.AlignCenterStyle)
  237. b.Excel.SetCellValue(b.SheetName, startACell, fmt.Sprintf("【%s】生产计划汇总金额", supplier))
  238. // 生产预算汇总
  239. b.Excel.SetCellStyle(b.SheetName, LCell, LCell, b.AlignCenterStyle)
  240. planOrderRealPrice := fmt.Sprintf("%.3f", totalBudgetPrice)
  241. b.FormatToEmpty(&planOrderRealPrice)
  242. b.Excel.SetCellValue(b.SheetName, LCell, planOrderRealPrice)
  243. // 生产实际汇总
  244. b.Excel.SetCellStyle(b.SheetName, MCell, MCell, b.AlignCenterStyle)
  245. planRealPrice := fmt.Sprintf("%.3f", totalRealPrice)
  246. b.FormatToEmpty(&planRealPrice)
  247. b.Excel.SetCellValue(b.SheetName, MCell, planRealPrice)
  248. return nil
  249. }
  250. // type merge
  251. func (b *PlanCostExcel) drawAllContent() error {
  252. b.Row += 2
  253. comps := b.Content.Pack.Components
  254. // 预算金额汇总
  255. var totalBudgetPrice float64 = 0.00
  256. // 实际金额汇总
  257. var totalRealPrice float64 = 0.00
  258. if len(comps) > 0 {
  259. for _, comp := range comps {
  260. var perBudgetPrice float64 = 0.00
  261. var perRealPrice float64 = 0.00
  262. if len(comp.Stages) > 0 {
  263. startRow := b.Row
  264. cates := map[string][]int{}
  265. for _, stage := range comp.Stages {
  266. matHeigth := fmt.Sprintf("%d", stage.BatchSizeHeight)
  267. b.FormatToEmpty(&matHeigth)
  268. matWidth := fmt.Sprintf("%d", stage.BatchSizeWidth)
  269. b.FormatToEmpty(&matWidth)
  270. orderCount := fmt.Sprintf("%d", int(stage.OrderCount))
  271. b.FormatToEmpty(&orderCount)
  272. // 实际数量
  273. realCount := fmt.Sprintf("%d", stage.RealCount)
  274. b.FormatToEmpty(&realCount)
  275. // 单价
  276. price := fmt.Sprintf("%.3f", stage.OrderPrice)
  277. b.FormatToEmpty(&price)
  278. // 预算金额
  279. budgetPrice := fmt.Sprintf("%.3f", stage.OrderPrice*float64(stage.OrderCount))
  280. perBudgetPrice += stage.OrderPrice * float64(stage.OrderCount)
  281. b.FormatToEmpty(&budgetPrice)
  282. // 实际金额
  283. perRealPrice += stage.OrderPrice * float64(stage.RealCount)
  284. realPrice := fmt.Sprintf("%.3f", stage.RealPrice)
  285. b.FormatToEmpty(&realPrice)
  286. if stage.Unit == "吨" || stage.Unit == "平方米" {
  287. stage.Unit = "张"
  288. }
  289. supplierName := ""
  290. if stage.SupplierInfo != nil {
  291. supplierName = stage.SupplierInfo.Name
  292. }
  293. stageType := ""
  294. if stage.BillType > 0 {
  295. stage.Type = stage.BillType
  296. }
  297. if stage.Type == 1 {
  298. stageType = "材料采购"
  299. }
  300. if stage.Type == 2 {
  301. stageType = "工艺"
  302. }
  303. if stage.Type == 3 {
  304. stageType = "成品采购"
  305. }
  306. b.drawRow(b.Row, "", stageType, stage.Name, supplierName, stage.Norm, matHeigth, matWidth, stage.Unit, orderCount, realCount, price, budgetPrice, realPrice)
  307. if stage.SupplierInfo != nil {
  308. cates[stage.SupplierInfo.Name] = append(cates[stage.SupplierInfo.Name], b.Row)
  309. }
  310. b.Row++
  311. }
  312. for supplierName, cate := range cates {
  313. mergeStartRow := cate[0]
  314. mergeEndRow := cate[len(cate)-1]
  315. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("D%d", mergeStartRow), fmt.Sprintf("D%d", mergeEndRow))
  316. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("D%d", mergeEndRow), supplierName)
  317. }
  318. endRow := b.Row - 1
  319. // 组件名字
  320. startACell := fmt.Sprintf("%s%d", "A", startRow)
  321. endACell := fmt.Sprintf("%s%d", "A", endRow)
  322. b.Excel.MergeCell(b.SheetName, startACell, endACell)
  323. err := b.Excel.SetCellStyle(b.SheetName, startACell, endACell, b.AlignCenterStyle)
  324. if err != nil {
  325. return err
  326. }
  327. b.Excel.SetCellValue(b.SheetName, startACell, comp.Name)
  328. }
  329. // 预算
  330. totalBudgetPrice += perBudgetPrice
  331. // 实际金额
  332. totalRealPrice += perRealPrice
  333. }
  334. }
  335. startACell := fmt.Sprintf("%s%d", "A", b.Row)
  336. endKCell := fmt.Sprintf("%s%d", "K", b.Row)
  337. LCell := fmt.Sprintf("%s%d", "L", b.Row)
  338. MCell := fmt.Sprintf("%s%d", "M", b.Row)
  339. b.Excel.MergeCell(b.SheetName, startACell, endKCell)
  340. b.Excel.SetCellStyle(b.SheetName, startACell, endKCell, b.AlignCenterStyle)
  341. b.Excel.SetCellValue(b.SheetName, startACell, "生产计划汇总金额")
  342. // 生产预算汇总
  343. b.Excel.SetCellStyle(b.SheetName, LCell, LCell, b.AlignCenterStyle)
  344. planOrderRealPrice := fmt.Sprintf("%.3f", totalBudgetPrice)
  345. b.FormatToEmpty(&planOrderRealPrice)
  346. b.Excel.SetCellValue(b.SheetName, LCell, planOrderRealPrice)
  347. // 生产实际汇总
  348. b.Excel.SetCellStyle(b.SheetName, MCell, MCell, b.AlignCenterStyle)
  349. planRealPrice := fmt.Sprintf("%.3f", totalRealPrice)
  350. b.FormatToEmpty(&planRealPrice)
  351. b.Excel.SetCellValue(b.SheetName, MCell, planRealPrice)
  352. return nil
  353. }
  354. func (b *PlanCostExcel) Draws() {
  355. b.drawTitle()
  356. b.drawTableTitle()
  357. if b.Content.SupplierId != "" {
  358. b.drawSupplierContent()
  359. } else {
  360. b.drawAllContent()
  361. }
  362. }
  363. func NewPlanCostExcel(f *excelize.File) *PlanCostExcel {
  364. border := []excelize.Border{
  365. {Type: "top", Style: 1, Color: "000000"},
  366. {Type: "left", Style: 1, Color: "000000"},
  367. {Type: "right", Style: 1, Color: "000000"},
  368. {Type: "bottom", Style: 1, Color: "000000"},
  369. }
  370. styleLeft, _ := f.NewStyle(&excelize.Style{
  371. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  372. Border: border,
  373. Font: &excelize.Font{Size: 10},
  374. })
  375. b := &PlanCostExcel{
  376. Title: "生产成本表",
  377. SheetName: "Sheet1",
  378. Excel: f,
  379. Offset: 0,
  380. AlignCenterStyle: styleLeft,
  381. }
  382. f.SetColWidth(b.SheetName, "A", "D", 12)
  383. f.SetColWidth(b.SheetName, "E", "M", 10)
  384. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  385. return b
  386. }
  387. func (b *PlanCostExcel) FormatToEmpty(str *string) {
  388. if *str == "0" || *str == "0.000" {
  389. *str = ""
  390. }
  391. }