report.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/db/repo"
  5. "errors"
  6. "fmt"
  7. "github.com/gin-gonic/gin"
  8. "github.com/xuri/excelize/v2"
  9. )
  10. // 统计报表 按时间范围,供应商 包装-计划(多选) 维度进行过滤,形成报表。可以下载
  11. func Report(r *GinRouter) {
  12. // 加工列表
  13. r.GET("/report/produce/list", ReportProduceList)
  14. // 采购列表
  15. r.GET("/report/purchase/list", ReportPurchaseList)
  16. r.GET("/report/produce/download", ReportProduceDownload)
  17. r.GET("/report/purchase/download", ReportPurchaseDownload)
  18. }
  19. // 加工单
  20. func ReportProduceList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  21. page, size, query := UtilQueryPageSize(c)
  22. // 条件处理
  23. // 获取采购单符合条件的信息
  24. return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  25. CollectName: repo.CollectionBillProduce,
  26. Query: handleReportQuery(query),
  27. Page: page,
  28. Size: size,
  29. })
  30. }
  31. // 采购单
  32. func ReportPurchaseList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  33. page, size, query := UtilQueryPageSize(c)
  34. return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  35. CollectName: repo.CollectionBillPurchase,
  36. Query: handleReportQuery(query),
  37. Page: page,
  38. Size: size,
  39. })
  40. }
  41. func ReportProduceDownload(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  42. _, _, query := UtilQueryPageSize(c)
  43. // 获取采符合条件的信息
  44. produces := []model.ProduceBill{}
  45. err := repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  46. CollectName: repo.CollectionBillProduce,
  47. Query: handleReportQuery(query),
  48. }, &produces)
  49. if err != nil || len(produces) < 1 {
  50. return nil, errors.New("数据不存在")
  51. }
  52. f := excelize.NewFile()
  53. index := f.NewSheet("Sheet1")
  54. sheetName := f.GetSheetName(index)
  55. f.SetActiveSheet(index)
  56. f.SetDefaultFont("宋体")
  57. border := []excelize.Border{
  58. {Type: "top", Style: 1, Color: "000000"},
  59. {Type: "left", Style: 1, Color: "000000"},
  60. {Type: "right", Style: 1, Color: "000000"},
  61. {Type: "bottom", Style: 1, Color: "000000"},
  62. }
  63. alignCenterStyle, _ := f.NewStyle(&excelize.Style{
  64. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  65. Border: border,
  66. })
  67. offset := 0
  68. companyName := getCompanyName(apictx)
  69. var budgetCount float64 = 0
  70. var realCount float64 = 0
  71. var row int = 0
  72. for _, produce := range produces {
  73. produceExcel := NewReportProduceExcel(f)
  74. produceExcel.Content = &produce
  75. produceExcel.Title = fmt.Sprintf("%s加工单", companyName)
  76. //设置对应的数据
  77. produceExcel.Offset = offset
  78. produceExcel.Draws()
  79. budgetCount += produceExcel.BudgetCount
  80. realCount += produceExcel.RealCount
  81. offset += 15
  82. row = produceExcel.Row
  83. }
  84. row++
  85. startCell := fmt.Sprintf("%s%d", "A", row)
  86. endCell := fmt.Sprintf("%s%d", "H", row)
  87. f.MergeCell(sheetName, startCell, endCell)
  88. f.SetCellStyle(sheetName, startCell, endCell, alignCenterStyle)
  89. f.SetCellValue(sheetName, startCell, "汇总金额")
  90. // 预算金额汇总
  91. budgetCountCell := fmt.Sprintf("%s%d", "I", row)
  92. budgetCountStr := ""
  93. if realCount > 0 {
  94. budgetCountStr = fmt.Sprintf("%.2f", budgetCount)
  95. }
  96. f.SetCellValue(sheetName, budgetCountCell, budgetCountStr)
  97. f.SetCellStyle(sheetName, budgetCountCell, budgetCountCell, alignCenterStyle)
  98. // 实际金额汇总
  99. RealCountCell := fmt.Sprintf("%s%d", "J", row)
  100. realCountStr := ""
  101. if realCount > 0 {
  102. realCountStr = fmt.Sprintf("%.2f", realCount)
  103. }
  104. f.SetCellValue(sheetName, RealCountCell, realCountStr)
  105. f.SetCellStyle(sheetName, RealCountCell, RealCountCell, alignCenterStyle)
  106. f.SetRowHeight(sheetName, row, 21)
  107. c.Header("Content-Type", "application/octet-stream")
  108. c.Header("Content-Disposition", "attachment; filename="+"reportProduce.xlsx")
  109. c.Header("Content-Transfer-Encoding", "binary")
  110. err = f.Write(c.Writer)
  111. if err != nil {
  112. return nil, err
  113. }
  114. return nil, nil
  115. }
  116. func ReportPurchaseDownload(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  117. _, _, query := UtilQueryPageSize(c)
  118. purchases := []model.PurchaseBill{}
  119. err := repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  120. CollectName: repo.CollectionBillPurchase,
  121. Query: handleReportQuery(query),
  122. }, &purchases)
  123. if err != nil || len(purchases) < 1 {
  124. return nil, errors.New("数据不存在")
  125. }
  126. f := excelize.NewFile()
  127. index := f.NewSheet("Sheet1")
  128. sheetName := f.GetSheetName(index)
  129. f.SetActiveSheet(index)
  130. f.SetDefaultFont("宋体")
  131. border := []excelize.Border{
  132. {Type: "top", Style: 1, Color: "000000"},
  133. {Type: "left", Style: 1, Color: "000000"},
  134. {Type: "right", Style: 1, Color: "000000"},
  135. {Type: "bottom", Style: 1, Color: "000000"},
  136. }
  137. alignCenterStyle, _ := f.NewStyle(&excelize.Style{
  138. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  139. Border: border,
  140. })
  141. offset := 0
  142. companyName := getCompanyName(apictx)
  143. var budgetCount float64 = 0
  144. var realCount float64 = 0
  145. var row int = 0
  146. for _, purchase := range purchases {
  147. purchaseExcel := NewReportPurchaseExcel(f)
  148. purchaseExcel.Content = &purchase
  149. purchaseExcel.Title = fmt.Sprintf("%s原材料采购单", companyName)
  150. //设置对应的数据
  151. purchaseExcel.Offset = offset
  152. purchaseExcel.Draws()
  153. budgetCount += purchaseExcel.BudgetCount
  154. realCount += purchaseExcel.RealCount
  155. offset += 15
  156. row = purchaseExcel.Row
  157. }
  158. row++
  159. startCell := fmt.Sprintf("%s%d", "A", row)
  160. endCell := fmt.Sprintf("%s%d", "H", row)
  161. f.MergeCell(sheetName, startCell, endCell)
  162. f.SetCellStyle(sheetName, startCell, endCell, alignCenterStyle)
  163. f.SetCellValue(sheetName, startCell, "汇总金额")
  164. // 预算金额汇总
  165. budgetCountCell := fmt.Sprintf("%s%d", "I", row)
  166. budgetCountStr := ""
  167. if realCount > 0 {
  168. budgetCountStr = fmt.Sprintf("%.2f", budgetCount)
  169. }
  170. f.SetCellValue(sheetName, budgetCountCell, budgetCountStr)
  171. f.SetCellStyle(sheetName, budgetCountCell, budgetCountCell, alignCenterStyle)
  172. // 实际金额汇总
  173. RealCountCell := fmt.Sprintf("%s%d", "J", row)
  174. realCountStr := ""
  175. if realCount > 0 {
  176. realCountStr = fmt.Sprintf("%.2f", realCount)
  177. }
  178. f.SetCellValue(sheetName, RealCountCell, realCountStr)
  179. f.SetCellStyle(sheetName, RealCountCell, RealCountCell, alignCenterStyle)
  180. f.SetRowHeight(sheetName, row, 21)
  181. c.Header("Content-Type", "application/octet-stream")
  182. c.Header("Content-Disposition", "attachment; filename="+"reportPurchase.xlsx")
  183. c.Header("Content-Transfer-Encoding", "binary")
  184. err = f.Write(c.Writer)
  185. if err != nil {
  186. return nil, err
  187. }
  188. return nil, nil
  189. }