report.go 9.5 KB

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