plan-cost-excel.go 13 KB

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