report.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. "go.mongodb.org/mongo-driver/bson"
  10. "go.mongodb.org/mongo-driver/bson/primitive"
  11. )
  12. // 统计报表
  13. func Report(r *GinRouter) {
  14. // 加工列表
  15. r.GET("/report/produce/list", ReportProduceList)
  16. // 采购列表
  17. r.GET("/report/purchase/list", ReportPurchaseList)
  18. r.GET("/report/produce/download", ReportProduceDownload)
  19. r.GET("/report/purchase/download", ReportPurchaseDownload)
  20. }
  21. // 加工单
  22. func ReportProduceList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  23. // 财务管理】 添加统计报表功能。按 时间范围, 供应商(单选) 包装(多选) 计划(多选) 四个维度进行过滤,形成报表。可以下载
  24. page, size, query := UtilQueryPageSize(c)
  25. // 条件处理
  26. query["status"] = "complete"
  27. if _supplierId, ok := query["supplierId"]; ok {
  28. delete(query, "supplierId")
  29. supplierId, _ := primitive.ObjectIDFromHex(_supplierId.(string))
  30. if !supplierId.IsZero() {
  31. query["supplierId"] = supplierId
  32. }
  33. }
  34. if _timeRange, ok := query["timeRange"]; ok {
  35. timeRange, _ := _timeRange.([]interface{})
  36. if len(timeRange) == 2 {
  37. start, end := getTimeRange(timeRange[0].(string), timeRange[1].(string))
  38. query["updateTime"] = bson.M{"$gte": start, "$lte": end}
  39. }
  40. delete(query, "timeRange")
  41. }
  42. if _planIds, ok := query["planIds"]; ok {
  43. if len(_planIds.([]interface{})) > 0 {
  44. planQuery := bson.A{}
  45. for _, _planId := range _planIds.([]interface{}) {
  46. planId, _ := primitive.ObjectIDFromHex(_planId.(string))
  47. planQuery = append(planQuery, bson.M{"planId": planId})
  48. }
  49. query["$or"] = planQuery
  50. }
  51. delete(query, "planIds")
  52. }
  53. fmt.Println(query)
  54. // 获取采购单符合条件的信息
  55. return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  56. CollectName: repo.CollectionBillProduce,
  57. Query: query,
  58. Page: page,
  59. Size: size,
  60. })
  61. }
  62. // 采购单
  63. func ReportPurchaseList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  64. // 财务管理】 添加统计报表功能。按 时间范围, 供应商(单选) 计划(多选) 四个维度进行过滤,形成报表。可以下载
  65. page, size, query := UtilQueryPageSize(c)
  66. // 条件处理
  67. query["status"] = "complete"
  68. if _supplierId, ok := query["supplierId"]; ok {
  69. delete(query, "supplierId")
  70. supplierId, _ := primitive.ObjectIDFromHex(_supplierId.(string))
  71. if !supplierId.IsZero() {
  72. query["supplierId"] = supplierId
  73. }
  74. }
  75. if _timeRange, ok := query["timeRange"]; ok {
  76. timeRange, _ := _timeRange.([]interface{})
  77. if len(timeRange) == 2 {
  78. start, end := getTimeRange(timeRange[0].(string), timeRange[1].(string))
  79. query["updateTime"] = bson.M{"$gte": start, "$lte": end}
  80. }
  81. delete(query, "timeRange")
  82. }
  83. if _planIds, ok := query["planIds"]; ok {
  84. if len(_planIds.([]interface{})) > 0 {
  85. planQuery := bson.A{}
  86. for _, _planId := range _planIds.([]interface{}) {
  87. planId, _ := primitive.ObjectIDFromHex(_planId.(string))
  88. planQuery = append(planQuery, bson.M{"planId": planId})
  89. }
  90. query["$or"] = planQuery
  91. }
  92. delete(query, "planIds")
  93. }
  94. // 获取采购单符合条件的信息
  95. return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  96. CollectName: repo.CollectionBillPurchase,
  97. Query: query,
  98. Page: page,
  99. Size: size,
  100. })
  101. }
  102. func ReportProduceDownload(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  103. _, _, query := UtilQueryPageSize(c)
  104. // 条件处理
  105. query["status"] = "complete"
  106. if _supplierId, ok := query["supplierId"]; ok {
  107. delete(query, "supplierId")
  108. supplierId, _ := primitive.ObjectIDFromHex(_supplierId.(string))
  109. if !supplierId.IsZero() {
  110. query["supplierId"] = supplierId
  111. }
  112. }
  113. if _timeRange, ok := query["timeRange"]; ok {
  114. timeRange, _ := _timeRange.([]interface{})
  115. if len(timeRange) == 2 {
  116. start, end := getTimeRange(timeRange[0].(string), timeRange[1].(string))
  117. query["updateTime"] = bson.M{"$gte": start, "$lte": end}
  118. }
  119. delete(query, "timeRange")
  120. }
  121. if _planIds, ok := query["planIds"]; ok {
  122. if len(_planIds.([]interface{})) > 0 {
  123. planQuery := bson.A{}
  124. for _, _planId := range _planIds.([]interface{}) {
  125. planId, _ := primitive.ObjectIDFromHex(_planId.(string))
  126. planQuery = append(planQuery, bson.M{"planId": planId})
  127. }
  128. query["$or"] = planQuery
  129. }
  130. delete(query, "planIds")
  131. }
  132. // 获取采符合条件的信息
  133. produces := []model.ProduceBill{}
  134. err := repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  135. CollectName: repo.CollectionBillProduce,
  136. Query: query,
  137. }, &produces)
  138. if err != nil || len(produces) < 1 {
  139. return nil, errors.New("数据不存在")
  140. }
  141. f := excelize.NewFile()
  142. index := f.NewSheet("Sheet1")
  143. sheetName := f.GetSheetName(index)
  144. f.SetActiveSheet(index)
  145. f.SetDefaultFont("宋体")
  146. border := []excelize.Border{
  147. {Type: "top", Style: 1, Color: "000000"},
  148. {Type: "left", Style: 1, Color: "000000"},
  149. {Type: "right", Style: 1, Color: "000000"},
  150. {Type: "bottom", Style: 1, Color: "000000"},
  151. }
  152. alignCenterStyle, _ := f.NewStyle(&excelize.Style{
  153. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  154. Border: border,
  155. })
  156. offset := 0
  157. info := model.Setting{}
  158. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  159. CollectName: "infos",
  160. }, &info)
  161. var budgetCount float64 = 0
  162. var realCount float64 = 0
  163. var row int = 0
  164. for _, produce := range produces {
  165. produceExcel := NewReportProduceExcel(f)
  166. produceExcel.Content = &produce
  167. produceExcel.Title = fmt.Sprintf("%s加工单", info.CompanyName)
  168. //设置对应的数据
  169. produceExcel.Offset = offset
  170. produceExcel.Draws()
  171. budgetCount += produceExcel.BudgetCount
  172. realCount += produceExcel.RealCount
  173. offset += 15
  174. row = produceExcel.Row
  175. }
  176. row++
  177. startCell := fmt.Sprintf("%s%d", "A", row)
  178. endCell := fmt.Sprintf("%s%d", "H", row)
  179. f.MergeCell(sheetName, startCell, endCell)
  180. f.SetCellStyle(sheetName, startCell, endCell, alignCenterStyle)
  181. f.SetCellValue(sheetName, startCell, "汇总金额")
  182. // 预算金额汇总
  183. budgetCountCell := fmt.Sprintf("%s%d", "I", row)
  184. budgetCountStr := ""
  185. if realCount > 0 {
  186. budgetCountStr = fmt.Sprintf("%.2f", budgetCount)
  187. }
  188. f.SetCellValue(sheetName, budgetCountCell, budgetCountStr)
  189. f.SetCellStyle(sheetName, budgetCountCell, budgetCountCell, alignCenterStyle)
  190. // 实际金额汇总
  191. RealCountCell := fmt.Sprintf("%s%d", "J", row)
  192. realCountStr := ""
  193. if realCount > 0 {
  194. realCountStr = fmt.Sprintf("%.2f", realCount)
  195. }
  196. f.SetCellValue(sheetName, RealCountCell, realCountStr)
  197. f.SetCellStyle(sheetName, RealCountCell, RealCountCell, alignCenterStyle)
  198. f.SetRowHeight(sheetName, row, 21)
  199. c.Header("Content-Type", "application/octet-stream")
  200. c.Header("Content-Disposition", "attachment; filename="+"reportProduce.xlsx")
  201. c.Header("Content-Transfer-Encoding", "binary")
  202. err = f.Write(c.Writer)
  203. if err != nil {
  204. return nil, err
  205. }
  206. return nil, nil
  207. }
  208. func ReportPurchaseDownload(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  209. _, _, query := UtilQueryPageSize(c)
  210. // 条件处理
  211. query["status"] = "complete"
  212. if _supplierId, ok := query["supplierId"]; ok {
  213. delete(query, "supplierId")
  214. supplierId, _ := primitive.ObjectIDFromHex(_supplierId.(string))
  215. if !supplierId.IsZero() {
  216. query["supplierId"] = supplierId
  217. }
  218. }
  219. if _timeRange, ok := query["timeRange"]; ok {
  220. timeRange, _ := _timeRange.([]interface{})
  221. if len(timeRange) == 2 {
  222. start, end := getTimeRange(timeRange[0].(string), timeRange[1].(string))
  223. query["updateTime"] = bson.M{"$gte": start, "$lte": end}
  224. }
  225. delete(query, "timeRange")
  226. }
  227. if _planIds, ok := query["planIds"]; ok {
  228. if len(_planIds.([]interface{})) > 0 {
  229. planQuery := bson.A{}
  230. for _, _planId := range _planIds.([]interface{}) {
  231. planId, _ := primitive.ObjectIDFromHex(_planId.(string))
  232. planQuery = append(planQuery, bson.M{"planId": planId})
  233. }
  234. query["$or"] = planQuery
  235. }
  236. delete(query, "planIds")
  237. }
  238. // 获取符合条件的信息
  239. purchases := []model.PurchaseBill{}
  240. err := repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
  241. CollectName: repo.CollectionBillPurchase,
  242. Query: query,
  243. }, &purchases)
  244. if err != nil || len(purchases) < 1 {
  245. return nil, errors.New("数据不存在")
  246. }
  247. f := excelize.NewFile()
  248. index := f.NewSheet("Sheet1")
  249. sheetName := f.GetSheetName(index)
  250. f.SetActiveSheet(index)
  251. f.SetDefaultFont("宋体")
  252. border := []excelize.Border{
  253. {Type: "top", Style: 1, Color: "000000"},
  254. {Type: "left", Style: 1, Color: "000000"},
  255. {Type: "right", Style: 1, Color: "000000"},
  256. {Type: "bottom", Style: 1, Color: "000000"},
  257. }
  258. alignCenterStyle, _ := f.NewStyle(&excelize.Style{
  259. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  260. Border: border,
  261. })
  262. offset := 0
  263. info := model.Setting{}
  264. repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  265. CollectName: "infos",
  266. }, &info)
  267. var budgetCount float64 = 0
  268. var realCount float64 = 0
  269. var row int = 0
  270. for _, purchase := range purchases {
  271. purchaseExcel := NewReportPurchaseExcel(f)
  272. purchaseExcel.Content = &purchase
  273. purchaseExcel.Title = fmt.Sprintf("%s原材料采购单", info.CompanyName)
  274. //设置对应的数据
  275. purchaseExcel.Offset = offset
  276. purchaseExcel.Draws()
  277. budgetCount += purchaseExcel.BudgetCount
  278. realCount += purchaseExcel.RealCount
  279. offset += 15
  280. row = purchaseExcel.Row
  281. }
  282. row++
  283. startCell := fmt.Sprintf("%s%d", "A", row)
  284. endCell := fmt.Sprintf("%s%d", "H", row)
  285. f.MergeCell(sheetName, startCell, endCell)
  286. f.SetCellStyle(sheetName, startCell, endCell, alignCenterStyle)
  287. f.SetCellValue(sheetName, startCell, "汇总金额")
  288. // 预算金额汇总
  289. budgetCountCell := fmt.Sprintf("%s%d", "I", row)
  290. budgetCountStr := ""
  291. if realCount > 0 {
  292. budgetCountStr = fmt.Sprintf("%.2f", budgetCount)
  293. }
  294. f.SetCellValue(sheetName, budgetCountCell, budgetCountStr)
  295. f.SetCellStyle(sheetName, budgetCountCell, budgetCountCell, alignCenterStyle)
  296. // 实际金额汇总
  297. RealCountCell := fmt.Sprintf("%s%d", "J", row)
  298. realCountStr := ""
  299. if realCount > 0 {
  300. realCountStr = fmt.Sprintf("%.2f", realCount)
  301. }
  302. f.SetCellValue(sheetName, RealCountCell, realCountStr)
  303. f.SetCellStyle(sheetName, RealCountCell, RealCountCell, alignCenterStyle)
  304. f.SetRowHeight(sheetName, row, 21)
  305. c.Header("Content-Type", "application/octet-stream")
  306. c.Header("Content-Disposition", "attachment; filename="+"reportPurchase.xlsx")
  307. c.Header("Content-Transfer-Encoding", "binary")
  308. err = f.Write(c.Writer)
  309. if err != nil {
  310. return nil, err
  311. }
  312. return nil, nil
  313. }