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