report.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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/product/list", ReportProductList)
  17. r.GET("/report/produce/download", ReportProduceDownload)
  18. r.GET("/report/purchase/download", ReportPurchaseDownload)
  19. r.GET("/report/product/download", ReportProductDownload)
  20. }
  21. // 加工单
  22. func ReportProduceList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  23. page, size, query := UtilQueryPageSize(c)
  24. // 条件处理
  25. // 获取采购单符合条件的信息
  26. return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  27. CollectName: repo.CollectionBillProduce,
  28. Query: handleReportQuery(query),
  29. Page: page,
  30. Size: size,
  31. })
  32. }
  33. // 采购单
  34. func ReportPurchaseList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  35. page, size, query := UtilQueryPageSize(c)
  36. return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  37. CollectName: repo.CollectionBillPurchase,
  38. Query: handleReportQuery(query),
  39. Page: page,
  40. Size: size,
  41. })
  42. }
  43. func ReportProductList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  44. page, size, query := UtilQueryPageSize(c)
  45. return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  46. CollectName: repo.CollectionBillProduct,
  47. Query: handleReportQuery(query),
  48. Page: page,
  49. Size: size,
  50. })
  51. }
  52. func ReportProduceDownload(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  53. _, _, query := UtilQueryPageSize(c)
  54. // 获取采符合条件的信息
  55. produces := []*model.ProduceBill{}
  56. err := repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  57. CollectName: repo.CollectionBillProduce,
  58. Query: handleReportQuery(query),
  59. }, &produces)
  60. if err != nil || len(produces) < 1 {
  61. return nil, errors.New("数据不存在")
  62. }
  63. f := excelize.NewFile()
  64. index := f.NewSheet("Sheet1")
  65. sheetName := f.GetSheetName(index)
  66. f.SetActiveSheet(index)
  67. f.SetDefaultFont("宋体")
  68. border := []excelize.Border{
  69. {Type: "top", Style: 1, Color: "000000"},
  70. {Type: "left", Style: 1, Color: "000000"},
  71. {Type: "right", Style: 1, Color: "000000"},
  72. {Type: "bottom", Style: 1, Color: "000000"},
  73. }
  74. alignCenterStyle, _ := f.NewStyle(&excelize.Style{
  75. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  76. Border: border,
  77. })
  78. offset := 0
  79. companyName := getCompanyName(apictx)
  80. var budgetCount float64 = 0
  81. var realCount float64 = 0
  82. var row int = 0
  83. for _, produce := range produces {
  84. produceExcel := NewReportProduceExcel(f)
  85. produceExcel.Content = produce
  86. produceExcel.Title = fmt.Sprintf("%s加工单", companyName)
  87. //设置对应的数据
  88. produceExcel.Offset = offset
  89. produceExcel.Draws()
  90. budgetCount += produceExcel.BudgetCount
  91. realCount += produceExcel.RealCount
  92. offset += 15
  93. row = produceExcel.Row
  94. }
  95. row++
  96. startCell := fmt.Sprintf("%s%d", "A", row)
  97. endCell := fmt.Sprintf("%s%d", "H", row)
  98. f.MergeCell(sheetName, startCell, endCell)
  99. f.SetCellStyle(sheetName, startCell, endCell, alignCenterStyle)
  100. f.SetCellValue(sheetName, startCell, "汇总金额")
  101. // 预算金额汇总
  102. budgetCountCell := fmt.Sprintf("%s%d", "I", row)
  103. budgetCountStr := ""
  104. if realCount > 0 {
  105. budgetCountStr = fmt.Sprintf("%.3f", budgetCount)
  106. }
  107. f.SetCellValue(sheetName, budgetCountCell, budgetCountStr)
  108. f.SetCellStyle(sheetName, budgetCountCell, budgetCountCell, alignCenterStyle)
  109. // 实际金额汇总
  110. RealCountCell := fmt.Sprintf("%s%d", "J", row)
  111. realCountStr := ""
  112. if realCount > 0 {
  113. realCountStr = fmt.Sprintf("%.3f", realCount)
  114. }
  115. f.SetCellValue(sheetName, RealCountCell, realCountStr)
  116. f.SetCellStyle(sheetName, RealCountCell, RealCountCell, alignCenterStyle)
  117. f.SetRowHeight(sheetName, row, 21)
  118. c.Header("Content-Type", "application/octet-stream")
  119. c.Header("Content-Disposition", "attachment; filename="+"reportProduce.xlsx")
  120. c.Header("Content-Transfer-Encoding", "binary")
  121. err = f.Write(c.Writer)
  122. if err != nil {
  123. return nil, err
  124. }
  125. return nil, nil
  126. }
  127. func ReportPurchaseDownload(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  128. _, _, query := UtilQueryPageSize(c)
  129. purchases := []model.PurchaseBill{}
  130. err := repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  131. CollectName: repo.CollectionBillPurchase,
  132. Query: handleReportQuery(query),
  133. }, &purchases)
  134. if err != nil || len(purchases) < 1 {
  135. return nil, errors.New("数据不存在")
  136. }
  137. f := excelize.NewFile()
  138. index := f.NewSheet("Sheet1")
  139. sheetName := f.GetSheetName(index)
  140. f.SetActiveSheet(index)
  141. f.SetDefaultFont("宋体")
  142. border := []excelize.Border{
  143. {Type: "top", Style: 1, Color: "000000"},
  144. {Type: "left", Style: 1, Color: "000000"},
  145. {Type: "right", Style: 1, Color: "000000"},
  146. {Type: "bottom", Style: 1, Color: "000000"},
  147. }
  148. alignCenterStyle, _ := f.NewStyle(&excelize.Style{
  149. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  150. Border: border,
  151. })
  152. companyName := getCompanyName(apictx)
  153. var budgetCount float64 = 0
  154. var realCount float64 = 0
  155. var row int = 0
  156. for _, purchase := range purchases {
  157. var reportBill *ReportPurchaseExcel
  158. if len(purchase.Paper) > 0 {
  159. reportBill = NewReportPurchaseExcel(f)
  160. }
  161. if reportBill == nil {
  162. return nil, errors.New("数据不存在")
  163. }
  164. reportBill.Row = row
  165. reportBill.Content = &purchase
  166. reportBill.Title = fmt.Sprintf("%s原材料采购单", companyName)
  167. reportBill.Draws()
  168. budgetCount += purchase.BudgetAmount
  169. realCount += purchase.RealAmount
  170. row = reportBill.Row + 3
  171. }
  172. row = row - 2
  173. startCell := fmt.Sprintf("%s%d", "A", row)
  174. endCell := fmt.Sprintf("%s%d", "H", row)
  175. f.MergeCell(sheetName, startCell, endCell)
  176. f.SetCellStyle(sheetName, startCell, endCell, alignCenterStyle)
  177. f.SetCellValue(sheetName, startCell, "汇总金额")
  178. // 预算金额汇总
  179. budgetCountCell := fmt.Sprintf("%s%d", "I", row)
  180. budgetCountStr := ""
  181. if realCount > 0 {
  182. budgetCountStr = fmt.Sprintf("%.3f", budgetCount)
  183. }
  184. f.SetCellValue(sheetName, budgetCountCell, budgetCountStr)
  185. f.SetCellStyle(sheetName, budgetCountCell, budgetCountCell, alignCenterStyle)
  186. // 实际金额汇总
  187. RealCountCell := fmt.Sprintf("%s%d", "J", row)
  188. realCountStr := ""
  189. if realCount > 0 {
  190. realCountStr = fmt.Sprintf("%.3f", realCount)
  191. }
  192. f.SetCellValue(sheetName, RealCountCell, realCountStr)
  193. f.SetCellStyle(sheetName, RealCountCell, RealCountCell, alignCenterStyle)
  194. f.SetRowHeight(sheetName, row, 21)
  195. c.Header("Content-Type", "application/octet-stream")
  196. c.Header("Content-Disposition", "attachment; filename="+"reportPurchase.xlsx")
  197. c.Header("Content-Transfer-Encoding", "binary")
  198. err = f.Write(c.Writer)
  199. if err != nil {
  200. return nil, err
  201. }
  202. return nil, nil
  203. }
  204. func ReportProductDownload(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  205. _, _, query := UtilQueryPageSize(c)
  206. // 获取采符合条件的信息
  207. products := []*model.ProductBill{}
  208. err := repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  209. CollectName: repo.CollectionBillProduct,
  210. Query: handleReportQuery(query),
  211. }, &products)
  212. if err != nil || len(products) < 1 {
  213. return nil, errors.New("数据不存在")
  214. }
  215. f := excelize.NewFile()
  216. index := f.NewSheet("Sheet1")
  217. sheetName := f.GetSheetName(index)
  218. f.SetActiveSheet(index)
  219. f.SetDefaultFont("宋体")
  220. border := []excelize.Border{
  221. {Type: "top", Style: 1, Color: "000000"},
  222. {Type: "left", Style: 1, Color: "000000"},
  223. {Type: "right", Style: 1, Color: "000000"},
  224. {Type: "bottom", Style: 1, Color: "000000"},
  225. }
  226. alignCenterStyle, _ := f.NewStyle(&excelize.Style{
  227. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  228. Border: border,
  229. })
  230. offset := 0
  231. companyName := getCompanyName(apictx)
  232. var budgetCount float64 = 0
  233. var realCount float64 = 0
  234. var row int = 0
  235. for _, product := range products {
  236. produceExcel := NewReportProductBill(f)
  237. produceExcel.Content = product
  238. produceExcel.Title = fmt.Sprintf("%s成品采购单", companyName)
  239. //设置对应的数据
  240. produceExcel.Offset = offset
  241. produceExcel.Draws()
  242. budgetCount += produceExcel.BudgetCount
  243. realCount += produceExcel.RealCount
  244. offset += 15
  245. row = produceExcel.Row
  246. }
  247. row++
  248. startCell := fmt.Sprintf("%s%d", "A", row)
  249. endCell := fmt.Sprintf("%s%d", "H", row)
  250. f.MergeCell(sheetName, startCell, endCell)
  251. f.SetCellStyle(sheetName, startCell, endCell, alignCenterStyle)
  252. f.SetCellValue(sheetName, startCell, "汇总金额")
  253. // 预算金额汇总
  254. budgetCountCell := fmt.Sprintf("%s%d", "I", row)
  255. budgetCountStr := ""
  256. if realCount > 0 {
  257. budgetCountStr = fmt.Sprintf("%.3f", budgetCount)
  258. }
  259. f.SetCellValue(sheetName, budgetCountCell, budgetCountStr)
  260. f.SetCellStyle(sheetName, budgetCountCell, budgetCountCell, alignCenterStyle)
  261. // 实际金额汇总
  262. RealCountCell := fmt.Sprintf("%s%d", "J", row)
  263. realCountStr := ""
  264. if realCount > 0 {
  265. realCountStr = fmt.Sprintf("%.3f", realCount)
  266. }
  267. f.SetCellValue(sheetName, RealCountCell, realCountStr)
  268. f.SetCellStyle(sheetName, RealCountCell, RealCountCell, alignCenterStyle)
  269. f.SetRowHeight(sheetName, row, 21)
  270. c.Header("Content-Type", "application/octet-stream")
  271. c.Header("Content-Disposition", "attachment; filename="+"reportProduce.xlsx")
  272. c.Header("Content-Transfer-Encoding", "binary")
  273. err = f.Write(c.Writer)
  274. if err != nil {
  275. return nil, err
  276. }
  277. return nil, nil
  278. }