report.go 9.5 KB

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