invoice_payment.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package census
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "github.com/go-playground/validator/v10"
  6. "hongze/fms_api/controller/resp"
  7. "hongze/fms_api/global"
  8. "hongze/fms_api/models/base"
  9. "hongze/fms_api/models/fms"
  10. fmsService "hongze/fms_api/services/fms"
  11. "hongze/fms_api/utils"
  12. "strings"
  13. )
  14. // InvoicePaymentController 商品到款统计
  15. type InvoicePaymentController struct{}
  16. // List
  17. // @Title 商品到款统计列表
  18. // @Description 商品到款统计列表
  19. // @Param Keyword query string false "关键词"
  20. // @Param SellGroupId query int false "销售组别ID"
  21. // @Param ServiceType query int false "套餐类型"
  22. // @Param StartDate query string false "合同开始日期"
  23. // @Param EndDate query string false "合同结束日期"
  24. // @Param TimeType query int false "时间类型: 1-开票时间; 2-到款时间"
  25. // @Param HasInvoice query int false "已开票"
  26. // @Param HasPayment query int false "已到款"
  27. // @Success 200 {object} fms.ContractRegisterItem
  28. // @router /census/invoice_payment/list [get]
  29. func (ct *InvoicePaymentController) List(c *gin.Context) {
  30. var req fms.InvoicePaymentCensusListReq
  31. if e := c.BindQuery(&req); e != nil {
  32. err, ok := e.(validator.ValidationErrors)
  33. if !ok {
  34. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  35. return
  36. }
  37. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  38. return
  39. }
  40. cond := `1 = 1`
  41. pars := make([]interface{}, 0)
  42. // 合同编号/客户姓名/销售/实际使用方
  43. if req.Keyword != "" {
  44. kw := "%" + req.Keyword + "%"
  45. cond += ` b.company_name LIKE ? OR a.seller_name LIKE ?`
  46. pars = append(pars, kw, kw)
  47. }
  48. if req.SellGroupId > 0 {
  49. cond += ` AND a.seller_group_id = ?`
  50. pars = append(pars, req.SellGroupId)
  51. }
  52. // 套餐筛选
  53. if req.ServiceType != 0 {
  54. registerIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
  55. if e != nil {
  56. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  57. return
  58. }
  59. if len(registerIds) > 0 {
  60. cond += ` AND contract_register_id IN ?`
  61. pars = append(pars, registerIds)
  62. } else {
  63. cond += ` AND 1 = 2`
  64. }
  65. }
  66. // 开票到款日期
  67. if req.TimeType > 0 && req.StartDate != "" && req.EndDate != "" {
  68. st := fmt.Sprint(req.StartDate, " 00:00:00")
  69. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  70. cond += ` a.invoice_type = ? AND (invoice_time BETWEEN ? AND ?)`
  71. pars = append(pars, req.TimeType, st, ed)
  72. }
  73. // 已开票
  74. if req.HasInvoice == 1 && req.HasPayment == 0 {
  75. cond += ` AND a.invoice_type = 1`
  76. }
  77. // 已到款
  78. if req.HasInvoice == 0 && req.HasPayment == 1 {
  79. cond += ` AND a.invoice_type = 2`
  80. }
  81. page := new(base.Page)
  82. page.SetPageSize(req.PageSize)
  83. page.SetCurrent(req.Current)
  84. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
  85. total, list, e := fms.GetInvoicePaymentCensusPageList(page, cond, pars)
  86. if e != nil {
  87. resp.FailMsg("获取失败", "获取商品到款统计列表失败, Err: "+e.Error(), c)
  88. return
  89. }
  90. registerIds := make([]int, 0)
  91. for i := range list {
  92. registerIds = append(registerIds, list[i].ContractRegisterId)
  93. }
  94. results := new(fms.InvoicePaymentCensusResp)
  95. if len(registerIds) > 0 {
  96. // 获取开票到款列表
  97. ivCond := `contract_register_id IN ?`
  98. ivPars := make([]interface{}, 0)
  99. ivPars = append(ivPars, registerIds)
  100. iv := new(fms.ContractInvoice)
  101. invoiceList, e := iv.List(ivCond, ivPars, "")
  102. if e != nil {
  103. resp.FailMsg("获取失败", "获取开票到款列表失败, Err: "+e.Error(), c)
  104. return
  105. }
  106. // 取出开票、到款
  107. invoiceMap := make(map[int][]*fms.ContractInvoice, 0)
  108. paymentMap := make(map[int][]*fms.ContractInvoice, 0)
  109. paymentIds := make([]int, 0)
  110. for i := range invoiceList {
  111. if invoiceMap[invoiceList[i].ContractRegisterId] == nil {
  112. invoiceMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoice, 0)
  113. }
  114. if paymentMap[invoiceList[i].ContractRegisterId] == nil {
  115. paymentMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoice, 0)
  116. }
  117. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  118. invoiceMap[invoiceList[i].ContractRegisterId] = append(invoiceMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  119. }
  120. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  121. paymentMap[invoiceList[i].ContractRegisterId] = append(paymentMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  122. paymentIds = append(paymentIds, invoiceList[i].ContractInvoiceId)
  123. }
  124. }
  125. // 合同套餐
  126. contractServiceCond := `contract_register_id IN ?`
  127. contractServicePars := make([]interface{}, 0)
  128. contractServicePars = append(contractServicePars, registerIds)
  129. contractServiceOB := new(fms.ContractService)
  130. contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
  131. if e != nil {
  132. resp.FailMsg("获取失败", "获取合同套餐列表失败, Err:"+e.Error(), c)
  133. return
  134. }
  135. contractServiceMap := make(map[int][]*fms.ContractService, 0)
  136. servicesNameMap := make(map[int][]string, 0)
  137. for i := range contractServiceList {
  138. if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
  139. contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
  140. }
  141. contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
  142. servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
  143. }
  144. // 到款套餐分配
  145. serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
  146. if len(paymentIds) > 0 {
  147. serviceAmountCond := `contract_invoice_id IN ?`
  148. serviceAmountPars := make([]interface{}, 0)
  149. serviceAmountPars = append(serviceAmountPars, paymentIds)
  150. serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  151. serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  152. if e != nil {
  153. resp.FailMsg("获取失败", "获取到款套餐分配列表失败, Err:"+e.Error(), c)
  154. return
  155. }
  156. for i := range serviceAmountList {
  157. if serviceAmountMap[serviceAmountList[i].ContractRegisterId] == nil {
  158. serviceAmountMap[serviceAmountList[i].ContractRegisterId] = make([]*fms.ContractPaymentServiceAmount, 0)
  159. }
  160. serviceAmountMap[serviceAmountList[i].ContractRegisterId] = append(serviceAmountMap[serviceAmountList[i].ContractRegisterId], serviceAmountList[i])
  161. }
  162. }
  163. // 整合响应数据
  164. respList := make([]*fms.InvoicePaymentCensusItem, 0)
  165. for i := range list {
  166. v := new(fms.InvoicePaymentCensusItem)
  167. v.ContractRegisterId = list[i].ContractRegisterId
  168. v.CompanyName = list[i].CompanyName
  169. v.NewCompany = list[i].NewCompany
  170. v.StartDate = list[i].StartDate.Format(utils.FormatDate)
  171. v.StartDate = list[i].StartDate.Format(utils.FormatDate)
  172. svList := servicesNameMap[list[i].ContractRegisterId]
  173. v.ServicesName = strings.Join(svList, ",")
  174. // 格式化(合并)开票到款数据
  175. v.InvoicePaymentList = fmsService.MergeInvoiceList2InvoicePaymentCensusInfo(invoiceMap[list[i].ContractRegisterId], paymentMap[list[i].ContractRegisterId],
  176. contractServiceMap[list[i].ContractRegisterId], serviceAmountMap[list[i].ContractRegisterId])
  177. respList = append(respList, v)
  178. }
  179. // 开票到款金额合计
  180. amountTotalCond := `contract_register_id IN ?`
  181. amountTotalPars := make([]interface{}, 0)
  182. amountTotalPars = append(amountTotalPars, registerIds)
  183. amountTotalList, e := fms.GetContractInvoiceAmountTotal(amountTotalCond, amountTotalPars)
  184. if e != nil {
  185. resp.FailMsg("获取失败", "获取开票到款金额合计失败, Err:"+e.Error(), c)
  186. return
  187. }
  188. amountTotalMap := make(map[int]float64)
  189. for i := range amountTotalList {
  190. amountTotalMap[amountTotalList[i].InvoiceType] = amountTotalList[i].TotalAmount
  191. }
  192. results.DataList = respList
  193. results.InvoiceTotal = amountTotalMap[fms.ContractInvoiceTypeMake]
  194. results.PaymentTotal = amountTotalMap[fms.ContractInvoiceTypePay]
  195. }
  196. page.SetTotal(total)
  197. baseData := new(base.BaseData)
  198. baseData.SetPage(page)
  199. baseData.SetList(results)
  200. resp.OkData("获取成功", baseData, c)
  201. }