plan-cost-excel.go 13 KB

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