plan-cost-excel.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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("L%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. return nil
  126. }
  127. func (b *PlanCostExcel) drawSupplierContent() error {
  128. supplierId, err := primitive.ObjectIDFromHex(b.Content.SupplierId)
  129. if err != nil {
  130. return err
  131. }
  132. row := b.Offset + 4
  133. comps := b.Content.Pack.Components
  134. var totalPlanPrice float32 = 0.00
  135. if len(comps) > 0 {
  136. for _, comp := range comps {
  137. var totalOrderRealPrice float32 = 0.00
  138. if len(comp.Mats) > 0 {
  139. startRow := 0
  140. for _, mat := range comp.Mats {
  141. if mat.Supplier.SupplierInfo != nil {
  142. if supplierId == mat.Supplier.SupplierInfo.Id {
  143. // 材料
  144. startRow = row
  145. supplierName := mat.Supplier.SupplierInfo.Name
  146. matHeigth := fmt.Sprintf("%d", mat.BatchSizeHeight)
  147. b.FormatToEmpty(&matHeigth)
  148. matWidth := fmt.Sprintf("%d", mat.BatchSizeWidth)
  149. b.FormatToEmpty(&matWidth)
  150. orderCount := fmt.Sprintf("%d", int(mat.Supplier.OrderCount))
  151. b.FormatToEmpty(&orderCount)
  152. orderPrice := fmt.Sprintf("%.2f", mat.Supplier.OrderPrice)
  153. b.FormatToEmpty(&orderPrice)
  154. totalOrderRealPrice += mat.Supplier.OrderRealPrice
  155. orderRealPrice := fmt.Sprintf("%.2f", mat.Supplier.OrderRealPrice)
  156. b.FormatToEmpty(&orderRealPrice)
  157. b.drawRow(row, "", mat.MatInfo.Name, "", supplierName, mat.MatInfo.Norm, matHeigth, matWidth, mat.MatInfo.Unit, orderCount, orderPrice, orderRealPrice, "")
  158. row++
  159. }
  160. }
  161. if len(mat.Crafts) > 0 {
  162. for _, craft := range mat.Crafts {
  163. if craft.Supplier.SupplierInfo != nil {
  164. // 工序
  165. if supplierId == craft.Supplier.SupplierInfo.Id {
  166. startRow = row
  167. craftSupplierName := craft.Supplier.SupplierInfo.Name
  168. carftOrderCount := fmt.Sprintf("%d", int(craft.Supplier.OrderCount))
  169. b.FormatToEmpty(&carftOrderCount)
  170. carftHeigth := fmt.Sprintf("%d", craft.BatchSizeHeight)
  171. b.FormatToEmpty(&carftHeigth)
  172. carftWidth := fmt.Sprintf("%d", craft.BatchSizeWidth)
  173. b.FormatToEmpty(&carftWidth)
  174. carftOrderPrice := fmt.Sprintf("%.2f", craft.Supplier.OrderPrice)
  175. b.FormatToEmpty(&carftOrderPrice)
  176. totalOrderRealPrice += craft.Supplier.OrderRealPrice
  177. carftOrderRealPrice := fmt.Sprintf("%.2f", craft.Supplier.OrderRealPrice)
  178. b.FormatToEmpty(&carftOrderRealPrice)
  179. b.drawRow(row, "", "", craft.CraftInfo.Name, craftSupplierName, craft.CraftInfo.Norm, carftHeigth, carftWidth, craft.CraftInfo.Unit, carftOrderCount, carftOrderPrice, carftOrderRealPrice, "")
  180. row++
  181. }
  182. }
  183. }
  184. }
  185. }
  186. if startRow != 0 {
  187. endRow := row - 1
  188. // 组件名字
  189. startACell := fmt.Sprintf("%s%d", "A", startRow)
  190. endACell := fmt.Sprintf("%s%d", "A", endRow)
  191. b.Excel.MergeCell(b.SheetName, startACell, endACell)
  192. err := b.Excel.SetCellStyle(b.SheetName, startACell, endACell, b.AlignCenterStyle)
  193. if err != nil {
  194. return err
  195. }
  196. b.Excel.SetCellValue(b.SheetName, startACell, comp.Name)
  197. // 供应商组件汇总金额
  198. startLCell := fmt.Sprintf("%s%d", "L", startRow)
  199. endLCell := fmt.Sprintf("%s%d", "L", endRow)
  200. b.Excel.MergeCell(b.SheetName, startLCell, endLCell)
  201. err = b.Excel.SetCellStyle(b.SheetName, startLCell, endLCell, b.AlignCenterStyle)
  202. if err != nil {
  203. return err
  204. }
  205. compTotalPrice := fmt.Sprintf("%.2f", totalOrderRealPrice)
  206. b.FormatToEmpty(&compTotalPrice)
  207. b.Excel.SetCellValue(b.SheetName, startLCell, compTotalPrice)
  208. }
  209. }
  210. totalPlanPrice += totalOrderRealPrice
  211. }
  212. // 生产汇总金额
  213. startACell := fmt.Sprintf("%s%d", "A", row)
  214. endKCell := fmt.Sprintf("%s%d", "K", row)
  215. endLCell := fmt.Sprintf("%s%d", "L", row)
  216. b.Excel.MergeCell(b.SheetName, startACell, endKCell)
  217. b.Excel.SetCellStyle(b.SheetName, startACell, endKCell, b.AlignCenterStyle)
  218. b.Excel.SetCellValue(b.SheetName, startACell, "生产计划汇总金额")
  219. b.Excel.SetCellStyle(b.SheetName, endLCell, endLCell, b.AlignCenterStyle)
  220. planTotalPrice := fmt.Sprintf("%.2f", totalPlanPrice)
  221. b.FormatToEmpty(&planTotalPrice)
  222. b.Excel.SetCellValue(b.SheetName, endLCell, planTotalPrice)
  223. }
  224. return nil
  225. }
  226. func (b *PlanCostExcel) drawRow(rowIndex int, values ...string) {
  227. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"}
  228. for i, c := range charas {
  229. v := ""
  230. if i < len(values) {
  231. v = values[i]
  232. }
  233. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  234. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  235. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  236. b.Excel.SetRowHeight(b.SheetName, rowIndex, 21)
  237. }
  238. }
  239. func (b *PlanCostExcel) drawAllContent() error {
  240. row := b.Offset + 4
  241. comps := b.Content.Pack.Components
  242. if len(comps) > 0 {
  243. for _, comp := range comps {
  244. if len(comp.Mats) > 0 {
  245. startRow := row
  246. for _, mat := range comp.Mats {
  247. // 材料
  248. supplierName := ""
  249. if mat.Supplier.SupplierInfo != nil {
  250. supplierName = mat.Supplier.SupplierInfo.Name
  251. }
  252. matHeigth := fmt.Sprintf("%d", mat.BatchSizeHeight)
  253. b.FormatToEmpty(&matHeigth)
  254. matWidth := fmt.Sprintf("%d", mat.BatchSizeWidth)
  255. b.FormatToEmpty(&matWidth)
  256. orderCount := fmt.Sprintf("%d", int(mat.Supplier.OrderCount))
  257. b.FormatToEmpty(&orderCount)
  258. orderPrice := fmt.Sprintf("%.2f", mat.Supplier.OrderPrice)
  259. b.FormatToEmpty(&orderPrice)
  260. orderRealPrice := fmt.Sprintf("%.2f", mat.Supplier.OrderRealPrice)
  261. b.FormatToEmpty(&orderRealPrice)
  262. b.drawRow(row, "", mat.MatInfo.Name, "", supplierName, mat.MatInfo.Norm, matHeigth, matWidth, mat.MatInfo.Unit, orderCount, orderPrice, orderRealPrice, "")
  263. row++
  264. if len(mat.Crafts) > 0 {
  265. // 工序
  266. for _, craft := range mat.Crafts {
  267. craftSupplierName := ""
  268. if craft.Supplier.SupplierInfo != nil {
  269. craftSupplierName = craft.Supplier.SupplierInfo.Name
  270. }
  271. carftOrderCount := fmt.Sprintf("%d", int(craft.Supplier.OrderCount))
  272. b.FormatToEmpty(&carftOrderCount)
  273. carftHeigth := fmt.Sprintf("%d", craft.BatchSizeHeight)
  274. b.FormatToEmpty(&carftHeigth)
  275. carftWidth := fmt.Sprintf("%d", craft.BatchSizeWidth)
  276. b.FormatToEmpty(&carftWidth)
  277. carftOrderPrice := fmt.Sprintf("%.2f", craft.Supplier.OrderPrice)
  278. b.FormatToEmpty(&carftOrderPrice)
  279. carftOrderRealPrice := fmt.Sprintf("%.2f", craft.Supplier.OrderRealPrice)
  280. b.FormatToEmpty(&carftOrderRealPrice)
  281. b.drawRow(row, "", "", craft.CraftInfo.Name, craftSupplierName, craft.CraftInfo.Norm, carftHeigth, carftWidth, craft.CraftInfo.Unit, carftOrderCount, carftOrderPrice, carftOrderRealPrice, "")
  282. row++
  283. }
  284. }
  285. }
  286. endRow := row - 1
  287. // 组件名字
  288. startACell := fmt.Sprintf("%s%d", "A", startRow)
  289. endACell := fmt.Sprintf("%s%d", "A", endRow)
  290. b.Excel.MergeCell(b.SheetName, startACell, endACell)
  291. err := b.Excel.SetCellStyle(b.SheetName, startACell, endACell, b.AlignCenterStyle)
  292. if err != nil {
  293. return err
  294. }
  295. b.Excel.SetCellValue(b.SheetName, startACell, comp.Name)
  296. // 组件汇总金额
  297. startLCell := fmt.Sprintf("%s%d", "L", startRow)
  298. endLCell := fmt.Sprintf("%s%d", "L", endRow)
  299. b.Excel.MergeCell(b.SheetName, startLCell, endLCell)
  300. err = b.Excel.SetCellStyle(b.SheetName, startLCell, endLCell, b.AlignCenterStyle)
  301. if err != nil {
  302. return err
  303. }
  304. compTotalPrice := fmt.Sprintf("%.2f", comp.TotalPrice)
  305. b.FormatToEmpty(&compTotalPrice)
  306. b.Excel.SetCellValue(b.SheetName, startLCell, compTotalPrice)
  307. }
  308. }
  309. // 生产汇总金额
  310. startACell := fmt.Sprintf("%s%d", "A", row)
  311. endKCell := fmt.Sprintf("%s%d", "K", row)
  312. endLCell := fmt.Sprintf("%s%d", "L", row)
  313. b.Excel.MergeCell(b.SheetName, startACell, endKCell)
  314. b.Excel.SetCellStyle(b.SheetName, startACell, endKCell, b.AlignCenterStyle)
  315. b.Excel.SetCellValue(b.SheetName, startACell, "生产计划汇总金额")
  316. b.Excel.SetCellStyle(b.SheetName, endLCell, endLCell, b.AlignCenterStyle)
  317. planTotalPrice := fmt.Sprintf("%.2f", b.Content.TotalPrice)
  318. b.FormatToEmpty(&planTotalPrice)
  319. b.Excel.SetCellValue(b.SheetName, endLCell, planTotalPrice)
  320. }
  321. return nil
  322. }
  323. func (b *PlanCostExcel) Draws() {
  324. b.drawTitle()
  325. b.drawTableTitle()
  326. if b.Content.SupplierId != "" {
  327. b.drawSupplierContent()
  328. } else {
  329. b.drawAllContent()
  330. }
  331. }
  332. func NewPlanCostExcel(f *excelize.File) *PlanCostExcel {
  333. border := []excelize.Border{
  334. {Type: "top", Style: 1, Color: "000000"},
  335. {Type: "left", Style: 1, Color: "000000"},
  336. {Type: "right", Style: 1, Color: "000000"},
  337. {Type: "bottom", Style: 1, Color: "000000"},
  338. }
  339. styleLeft, _ := f.NewStyle(&excelize.Style{
  340. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  341. Border: border,
  342. Font: &excelize.Font{Size: 10},
  343. })
  344. b := &PlanCostExcel{
  345. Title: "生产成本表",
  346. SheetName: "Sheet1",
  347. Excel: f,
  348. Offset: 0,
  349. AlignCenterStyle: styleLeft,
  350. }
  351. f.SetColWidth(b.SheetName, "A", "D", 12)
  352. f.SetColWidth(b.SheetName, "E", "L", 10)
  353. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  354. return b
  355. }
  356. func (b *PlanCostExcel) FormatToEmpty(str *string) {
  357. if *str == "0" || *str == "0.00" {
  358. *str = ""
  359. }
  360. }