report.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. info := model.Setting{}
  69. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  70. CollectName: "infos",
  71. }, &info)
  72. var budgetCount float64 = 0
  73. var realCount float64 = 0
  74. var row int = 0
  75. for _, produce := range produces {
  76. produceExcel := NewReportProduceExcel(f)
  77. produceExcel.Content = &produce
  78. produceExcel.Title = fmt.Sprintf("%s加工单", info.CompanyName)
  79. //设置对应的数据
  80. produceExcel.Offset = offset
  81. produceExcel.Draws()
  82. budgetCount += produceExcel.BudgetCount
  83. realCount += produceExcel.RealCount
  84. offset += 15
  85. row = produceExcel.Row
  86. }
  87. row++
  88. startCell := fmt.Sprintf("%s%d", "A", row)
  89. endCell := fmt.Sprintf("%s%d", "H", row)
  90. f.MergeCell(sheetName, startCell, endCell)
  91. f.SetCellStyle(sheetName, startCell, endCell, alignCenterStyle)
  92. f.SetCellValue(sheetName, startCell, "汇总金额")
  93. // 预算金额汇总
  94. budgetCountCell := fmt.Sprintf("%s%d", "I", row)
  95. budgetCountStr := ""
  96. if realCount > 0 {
  97. budgetCountStr = fmt.Sprintf("%.2f", budgetCount)
  98. }
  99. f.SetCellValue(sheetName, budgetCountCell, budgetCountStr)
  100. f.SetCellStyle(sheetName, budgetCountCell, budgetCountCell, alignCenterStyle)
  101. // 实际金额汇总
  102. RealCountCell := fmt.Sprintf("%s%d", "J", row)
  103. realCountStr := ""
  104. if realCount > 0 {
  105. realCountStr = fmt.Sprintf("%.2f", realCount)
  106. }
  107. f.SetCellValue(sheetName, RealCountCell, realCountStr)
  108. f.SetCellStyle(sheetName, RealCountCell, RealCountCell, alignCenterStyle)
  109. f.SetRowHeight(sheetName, row, 21)
  110. c.Header("Content-Type", "application/octet-stream")
  111. c.Header("Content-Disposition", "attachment; filename="+"reportProduce.xlsx")
  112. c.Header("Content-Transfer-Encoding", "binary")
  113. err = f.Write(c.Writer)
  114. if err != nil {
  115. return nil, err
  116. }
  117. return nil, nil
  118. }
  119. func ReportPurchaseDownload(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  120. _, _, query := UtilQueryPageSize(c)
  121. purchases := []model.PurchaseBill{}
  122. err := repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  123. CollectName: repo.CollectionBillPurchase,
  124. Query: handleReportQuery(query),
  125. }, &purchases)
  126. if err != nil || len(purchases) < 1 {
  127. return nil, errors.New("数据不存在")
  128. }
  129. f := excelize.NewFile()
  130. index := f.NewSheet("Sheet1")
  131. sheetName := f.GetSheetName(index)
  132. f.SetActiveSheet(index)
  133. f.SetDefaultFont("宋体")
  134. border := []excelize.Border{
  135. {Type: "top", Style: 1, Color: "000000"},
  136. {Type: "left", Style: 1, Color: "000000"},
  137. {Type: "right", Style: 1, Color: "000000"},
  138. {Type: "bottom", Style: 1, Color: "000000"},
  139. }
  140. alignCenterStyle, _ := f.NewStyle(&excelize.Style{
  141. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  142. Border: border,
  143. })
  144. offset := 0
  145. info := model.Setting{}
  146. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  147. CollectName: "infos",
  148. }, &info)
  149. var budgetCount float64 = 0
  150. var realCount float64 = 0
  151. var row int = 0
  152. for _, purchase := range purchases {
  153. purchaseExcel := NewReportPurchaseExcel(f)
  154. purchaseExcel.Content = &purchase
  155. purchaseExcel.Title = fmt.Sprintf("%s原材料采购单", info.CompanyName)
  156. //设置对应的数据
  157. purchaseExcel.Offset = offset
  158. purchaseExcel.Draws()
  159. budgetCount += purchaseExcel.BudgetCount
  160. realCount += purchaseExcel.RealCount
  161. offset += 15
  162. row = purchaseExcel.Row
  163. }
  164. row++
  165. startCell := fmt.Sprintf("%s%d", "A", row)
  166. endCell := fmt.Sprintf("%s%d", "H", row)
  167. f.MergeCell(sheetName, startCell, endCell)
  168. f.SetCellStyle(sheetName, startCell, endCell, alignCenterStyle)
  169. f.SetCellValue(sheetName, startCell, "汇总金额")
  170. // 预算金额汇总
  171. budgetCountCell := fmt.Sprintf("%s%d", "I", row)
  172. budgetCountStr := ""
  173. if realCount > 0 {
  174. budgetCountStr = fmt.Sprintf("%.2f", budgetCount)
  175. }
  176. f.SetCellValue(sheetName, budgetCountCell, budgetCountStr)
  177. f.SetCellStyle(sheetName, budgetCountCell, budgetCountCell, alignCenterStyle)
  178. // 实际金额汇总
  179. RealCountCell := fmt.Sprintf("%s%d", "J", row)
  180. realCountStr := ""
  181. if realCount > 0 {
  182. realCountStr = fmt.Sprintf("%.2f", realCount)
  183. }
  184. f.SetCellValue(sheetName, RealCountCell, realCountStr)
  185. f.SetCellStyle(sheetName, RealCountCell, RealCountCell, alignCenterStyle)
  186. f.SetRowHeight(sheetName, row, 21)
  187. c.Header("Content-Type", "application/octet-stream")
  188. c.Header("Content-Disposition", "attachment; filename="+"reportPurchase.xlsx")
  189. c.Header("Content-Transfer-Encoding", "binary")
  190. err = f.Write(c.Writer)
  191. if err != nil {
  192. return nil, err
  193. }
  194. return nil, nil
  195. }