invoice_payment.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. package census
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "github.com/go-playground/validator/v10"
  7. "github.com/tealeg/xlsx"
  8. "hongze/fms_api/controller/resp"
  9. "hongze/fms_api/global"
  10. "hongze/fms_api/models/base"
  11. "hongze/fms_api/models/fms"
  12. fmsService "hongze/fms_api/services/fms"
  13. "hongze/fms_api/utils"
  14. "net/http"
  15. "strings"
  16. "time"
  17. )
  18. // InvoicePaymentController 商品到款统计
  19. type InvoicePaymentController struct{}
  20. // List
  21. // @Title 商品到款统计列表
  22. // @Description 商品到款统计列表
  23. // @Param Keyword query string false "关键词"
  24. // @Param SellGroupId query int false "销售组别ID"
  25. // @Param ServiceType query int false "套餐类型"
  26. // @Param StartDate query string false "合同开始日期"
  27. // @Param EndDate query string false "合同结束日期"
  28. // @Param TimeType query int false "时间类型: 1-开票时间; 2-到款时间"
  29. // @Param HasInvoice query int false "是否已开票: 0-否; 1-是"
  30. // @Param HasPayment query int false "是否已到款: 0-否; 1-是"
  31. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  32. // @Success 200 {object} fms.ContractRegisterItem
  33. // @router /census/invoice_payment/list [get]
  34. func (ct *InvoicePaymentController) List(c *gin.Context) {
  35. var req fms.InvoicePaymentCensusListReq
  36. if e := c.BindQuery(&req); e != nil {
  37. err, ok := e.(validator.ValidationErrors)
  38. if !ok {
  39. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  40. return
  41. }
  42. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  43. return
  44. }
  45. cond := `1 = 1`
  46. pars := make([]interface{}, 0)
  47. // 合同编号/客户姓名/销售
  48. if req.Keyword != "" {
  49. kw := "%" + req.Keyword + "%"
  50. cond += ` AND b.company_name LIKE ? OR a.seller_name LIKE ?`
  51. pars = append(pars, kw, kw)
  52. }
  53. if req.SellGroupId > 0 {
  54. cond += ` AND a.seller_group_id = ?`
  55. pars = append(pars, req.SellGroupId)
  56. }
  57. // 套餐筛选
  58. if req.ServiceType != 0 {
  59. registerIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
  60. if e != nil {
  61. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  62. return
  63. }
  64. if len(registerIds) > 0 {
  65. cond += ` AND b.contract_register_id IN ?`
  66. pars = append(pars, registerIds)
  67. } else {
  68. cond += ` AND 1 = 2`
  69. }
  70. }
  71. // 开票到款日期
  72. if req.TimeType > 0 && req.StartDate != "" && req.EndDate != "" {
  73. st := fmt.Sprint(req.StartDate, " 00:00:00")
  74. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  75. cond += ` AND a.invoice_type = ? AND (a.invoice_time BETWEEN ? AND ?)`
  76. pars = append(pars, req.TimeType, st, ed)
  77. }
  78. // 已开票
  79. if req.HasInvoice == 1 && req.HasPayment == 0 {
  80. cond += ` AND a.invoice_type = 1`
  81. }
  82. // 已到款
  83. if req.HasInvoice == 0 && req.HasPayment == 1 {
  84. cond += ` AND a.invoice_type = 2`
  85. }
  86. page := new(base.Page)
  87. page.SetPageSize(req.PageSize)
  88. page.SetCurrent(req.Current)
  89. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
  90. page.AddOrderItem(base.OrderItem{Column: "b.contract_register_id", Asc: true})
  91. if req.IsExport == 1 {
  92. page.SetPageSize(10000)
  93. page.SetCurrent(1)
  94. }
  95. total, list, e := fms.GetInvoicePaymentCensusPageList(page, cond, pars)
  96. if e != nil {
  97. resp.FailMsg("获取失败", "获取商品到款统计列表失败, Err: "+e.Error(), c)
  98. return
  99. }
  100. registerIds := make([]int, 0)
  101. for i := range list {
  102. registerIds = append(registerIds, list[i].ContractRegisterId)
  103. }
  104. results := new(fms.InvoicePaymentCensusResp)
  105. if len(registerIds) > 0 {
  106. // 获取开票到款列表
  107. ivCond := `contract_register_id IN ?`
  108. ivPars := make([]interface{}, 0)
  109. ivPars = append(ivPars, registerIds)
  110. iv := new(fms.ContractInvoice)
  111. invoiceList, e := iv.List(ivCond, ivPars, "")
  112. if e != nil {
  113. resp.FailMsg("获取失败", "获取开票到款列表失败, Err: "+e.Error(), c)
  114. return
  115. }
  116. // 取出开票、到款
  117. invoiceMap := make(map[int][]*fms.ContractInvoice, 0)
  118. paymentMap := make(map[int][]*fms.ContractInvoice, 0)
  119. paymentIds := make([]int, 0)
  120. for i := range invoiceList {
  121. if invoiceMap[invoiceList[i].ContractRegisterId] == nil {
  122. invoiceMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoice, 0)
  123. }
  124. if paymentMap[invoiceList[i].ContractRegisterId] == nil {
  125. paymentMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoice, 0)
  126. }
  127. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  128. invoiceMap[invoiceList[i].ContractRegisterId] = append(invoiceMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  129. }
  130. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  131. paymentMap[invoiceList[i].ContractRegisterId] = append(paymentMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  132. paymentIds = append(paymentIds, invoiceList[i].ContractInvoiceId)
  133. }
  134. }
  135. // 合同套餐
  136. contractServiceCond := `contract_register_id IN ?`
  137. contractServicePars := make([]interface{}, 0)
  138. contractServicePars = append(contractServicePars, registerIds)
  139. contractServiceOB := new(fms.ContractService)
  140. contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
  141. if e != nil {
  142. resp.FailMsg("获取失败", "获取合同套餐列表失败, Err:"+e.Error(), c)
  143. return
  144. }
  145. contractServiceMap := make(map[int][]*fms.ContractService, 0)
  146. servicesNameMap := make(map[int][]string, 0)
  147. for i := range contractServiceList {
  148. if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
  149. contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
  150. }
  151. contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
  152. servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
  153. }
  154. // 到款套餐分配
  155. serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
  156. if len(paymentIds) > 0 {
  157. serviceAmountCond := `contract_payment_id IN ?`
  158. serviceAmountPars := make([]interface{}, 0)
  159. serviceAmountPars = append(serviceAmountPars, paymentIds)
  160. serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  161. serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  162. if e != nil {
  163. resp.FailMsg("获取失败", "获取到款套餐分配列表失败, Err:"+e.Error(), c)
  164. return
  165. }
  166. for i := range serviceAmountList {
  167. if serviceAmountMap[serviceAmountList[i].ContractRegisterId] == nil {
  168. serviceAmountMap[serviceAmountList[i].ContractRegisterId] = make([]*fms.ContractPaymentServiceAmount, 0)
  169. }
  170. serviceAmountMap[serviceAmountList[i].ContractRegisterId] = append(serviceAmountMap[serviceAmountList[i].ContractRegisterId], serviceAmountList[i])
  171. }
  172. }
  173. // 整合响应数据
  174. respList := make([]*fms.InvoicePaymentCensusItem, 0)
  175. for i := range list {
  176. v := new(fms.InvoicePaymentCensusItem)
  177. v.ContractRegisterId = list[i].ContractRegisterId
  178. v.CompanyName = list[i].CompanyName
  179. v.NewCompany = list[i].NewCompany
  180. v.StartDate = list[i].StartDate.Format(utils.FormatDate)
  181. v.EndDate = list[i].EndDate.Format(utils.FormatDate)
  182. svList := servicesNameMap[list[i].ContractRegisterId]
  183. v.ServicesName = strings.Join(svList, ",")
  184. // 格式化(合并)开票到款数据
  185. v.InvoicePaymentList = fmsService.MergeInvoiceList2InvoicePaymentCensusInfo(invoiceMap[list[i].ContractRegisterId], paymentMap[list[i].ContractRegisterId],
  186. contractServiceMap[list[i].ContractRegisterId], serviceAmountMap[list[i].ContractRegisterId])
  187. respList = append(respList, v)
  188. }
  189. // 开票到款金额合计
  190. amountTotalCond := `contract_register_id IN ?`
  191. amountTotalPars := make([]interface{}, 0)
  192. amountTotalPars = append(amountTotalPars, registerIds)
  193. amountTotalList, e := fms.GetContractInvoiceAmountTotal(amountTotalCond, amountTotalPars)
  194. if e != nil {
  195. resp.FailMsg("获取失败", "获取开票到款金额合计失败, Err:"+e.Error(), c)
  196. return
  197. }
  198. amountTotalMap := make(map[int]float64)
  199. for i := range amountTotalList {
  200. amountTotalMap[amountTotalList[i].InvoiceType] = amountTotalList[i].TotalAmount
  201. }
  202. results.DataList = respList
  203. results.InvoiceTotal = amountTotalMap[fms.ContractInvoiceTypeMake]
  204. results.PaymentTotal = amountTotalMap[fms.ContractInvoiceTypePay]
  205. }
  206. // 是否导出
  207. if req.IsExport == 1 {
  208. ExportInvoicePaymentCensusList(c, results.DataList)
  209. return
  210. }
  211. page.SetTotal(total)
  212. baseData := new(base.BaseData)
  213. baseData.SetPage(page)
  214. baseData.SetList(results)
  215. resp.OkData("获取成功", baseData, c)
  216. }
  217. // ExportInvoicePaymentCensusList 导出商品到款统计列表
  218. func ExportInvoicePaymentCensusList(c *gin.Context, list []*fms.InvoicePaymentCensusItem) {
  219. // 生成Excel文件
  220. xlsxFile := xlsx.NewFile()
  221. style := xlsx.NewStyle()
  222. alignment := xlsx.Alignment{
  223. Horizontal: "center",
  224. Vertical: "center",
  225. WrapText: true,
  226. }
  227. style.Alignment = alignment
  228. style.ApplyAlignment = true
  229. sheet, err := xlsxFile.AddSheet("商品到款统计")
  230. if err != nil {
  231. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  232. return
  233. }
  234. _ = sheet.SetColWidth(1, 1, 30)
  235. _ = sheet.SetColWidth(3, 3, 30)
  236. // 表头, 套餐动态获取
  237. rowTitle := []string{"序号", "客户名称", "是否新客户", "合同有效期", "开票日", "开票金额", "到款日", "到款金额", "付款方式", "销售",
  238. "组别"}
  239. serviceTempCond := ``
  240. serviceTempPars := make([]interface{}, 0)
  241. serviceTempOB := new(fms.ContractServiceTemplate)
  242. serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
  243. if e != nil {
  244. resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
  245. return
  246. }
  247. for i := range serviceTempList {
  248. rowTitle = append(rowTitle, serviceTempList[i].Title)
  249. }
  250. titleRow := sheet.AddRow()
  251. for i := range rowTitle {
  252. v := titleRow.AddCell()
  253. v.SetString(rowTitle[i])
  254. v.SetStyle(style)
  255. }
  256. newCompanyMap := map[int]string{0: "否", 1: "是"}
  257. for k, v := range list {
  258. dataRow := sheet.AddRow()
  259. // 前四个单元格根据每行开票到款条数向下合并
  260. l := len(v.InvoicePaymentList)
  261. mergeRowNum := l - 1
  262. // 序号
  263. sortNum := k + 1
  264. colA := dataRow.AddCell()
  265. colA.VMerge = mergeRowNum
  266. colA.SetString(fmt.Sprint(sortNum))
  267. // 客户名称
  268. colB := dataRow.AddCell()
  269. colB.VMerge = mergeRowNum
  270. colB.SetString(v.CompanyName)
  271. // 是否新客户
  272. colC := dataRow.AddCell()
  273. colC.VMerge = mergeRowNum
  274. colC.SetString(newCompanyMap[v.NewCompany])
  275. // 合同有效期
  276. colD := dataRow.AddCell()
  277. colD.VMerge = mergeRowNum
  278. colD.SetString(fmt.Sprint(v.StartDate, "至", v.EndDate))
  279. // 开票到款信息
  280. for k2, v2 := range v.InvoicePaymentList {
  281. rowData := []string{
  282. v2.InvoiceDate, // 开票日
  283. fmt.Sprint(v2.InvoiceAmount), // 开票金额
  284. v2.PaymentDate, // 到款日
  285. fmt.Sprint(v2.PaymentAmount), // 到款金额
  286. fms.ContractPaymentPayTypeNameMap[v2.PayType], // 付款方式
  287. v2.SellerName, // 销售
  288. v2.SellerGroupName, // 组别
  289. }
  290. // 套餐金额信息
  291. for i := range serviceTempList {
  292. sa := ""
  293. for s2 := range v2.ServiceAmountList {
  294. if v2.ServiceAmountList[s2].ServiceTemplateId == serviceTempList[i].ServiceTemplateId {
  295. sa = fmt.Sprint(v2.ServiceAmountList[s2].Amount)
  296. break
  297. }
  298. }
  299. rowData = append(rowData, sa)
  300. }
  301. // 首行开票到款
  302. if k2 == 0 {
  303. for i := range rowData {
  304. dataRow.AddCell().SetString(rowData[i])
  305. }
  306. continue
  307. }
  308. // 其他行开票到款, 加四列空的单元格用于合并
  309. dataRowExtra := sheet.AddRow()
  310. for i := 0; i < 4; i++ {
  311. dataRowExtra.AddCell()
  312. }
  313. for i := range rowData {
  314. dataRowExtra.AddCell().SetString(rowData[i])
  315. }
  316. }
  317. }
  318. // 输出文件
  319. var buffer bytes.Buffer
  320. _ = xlsxFile.Write(&buffer)
  321. content := bytes.NewReader(buffer.Bytes())
  322. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  323. fileName := "商品到款统计_" + randStr + ".xlsx"
  324. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  325. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  326. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  327. }