plan-summary-excel.go 14 KB

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