report.go 9.6 KB

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