invoice_payment.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 "已开票"
  30. // @Param HasPayment query int false "已到款"
  31. // @Param IsExport query int false "是否导出"
  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 += ` 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 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 += ` a.invoice_type = ? AND (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. if req.IsExport == 1 {
  91. page.SetPageSize(10000)
  92. page.SetCurrent(1)
  93. }
  94. total, list, e := fms.GetInvoicePaymentCensusPageList(page, cond, pars)
  95. if e != nil {
  96. resp.FailMsg("获取失败", "获取商品到款统计列表失败, Err: "+e.Error(), c)
  97. return
  98. }
  99. registerIds := make([]int, 0)
  100. for i := range list {
  101. registerIds = append(registerIds, list[i].ContractRegisterId)
  102. }
  103. results := new(fms.InvoicePaymentCensusResp)
  104. if len(registerIds) > 0 {
  105. // 获取开票到款列表
  106. ivCond := `contract_register_id IN ?`
  107. ivPars := make([]interface{}, 0)
  108. ivPars = append(ivPars, registerIds)
  109. iv := new(fms.ContractInvoice)
  110. invoiceList, e := iv.List(ivCond, ivPars, "")
  111. if e != nil {
  112. resp.FailMsg("获取失败", "获取开票到款列表失败, Err: "+e.Error(), c)
  113. return
  114. }
  115. // 取出开票、到款
  116. invoiceMap := make(map[int][]*fms.ContractInvoice, 0)
  117. paymentMap := make(map[int][]*fms.ContractInvoice, 0)
  118. paymentIds := make([]int, 0)
  119. for i := range invoiceList {
  120. if invoiceMap[invoiceList[i].ContractRegisterId] == nil {
  121. invoiceMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoice, 0)
  122. }
  123. if paymentMap[invoiceList[i].ContractRegisterId] == nil {
  124. paymentMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoice, 0)
  125. }
  126. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  127. invoiceMap[invoiceList[i].ContractRegisterId] = append(invoiceMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  128. }
  129. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  130. paymentMap[invoiceList[i].ContractRegisterId] = append(paymentMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  131. paymentIds = append(paymentIds, invoiceList[i].ContractInvoiceId)
  132. }
  133. }
  134. // 合同套餐
  135. contractServiceCond := `contract_register_id IN ?`
  136. contractServicePars := make([]interface{}, 0)
  137. contractServicePars = append(contractServicePars, registerIds)
  138. contractServiceOB := new(fms.ContractService)
  139. contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
  140. if e != nil {
  141. resp.FailMsg("获取失败", "获取合同套餐列表失败, Err:"+e.Error(), c)
  142. return
  143. }
  144. contractServiceMap := make(map[int][]*fms.ContractService, 0)
  145. servicesNameMap := make(map[int][]string, 0)
  146. for i := range contractServiceList {
  147. if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
  148. contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
  149. }
  150. contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
  151. servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
  152. }
  153. // 到款套餐分配
  154. serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
  155. if len(paymentIds) > 0 {
  156. serviceAmountCond := `contract_payment_id IN ?`
  157. serviceAmountPars := make([]interface{}, 0)
  158. serviceAmountPars = append(serviceAmountPars, paymentIds)
  159. serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  160. serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  161. if e != nil {
  162. resp.FailMsg("获取失败", "获取到款套餐分配列表失败, Err:"+e.Error(), c)
  163. return
  164. }
  165. for i := range serviceAmountList {
  166. if serviceAmountMap[serviceAmountList[i].ContractRegisterId] == nil {
  167. serviceAmountMap[serviceAmountList[i].ContractRegisterId] = make([]*fms.ContractPaymentServiceAmount, 0)
  168. }
  169. serviceAmountMap[serviceAmountList[i].ContractRegisterId] = append(serviceAmountMap[serviceAmountList[i].ContractRegisterId], serviceAmountList[i])
  170. }
  171. }
  172. // 整合响应数据
  173. respList := make([]*fms.InvoicePaymentCensusItem, 0)
  174. for i := range list {
  175. v := new(fms.InvoicePaymentCensusItem)
  176. v.ContractRegisterId = list[i].ContractRegisterId
  177. v.CompanyName = list[i].CompanyName
  178. v.NewCompany = list[i].NewCompany
  179. v.StartDate = list[i].StartDate.Format(utils.FormatDate)
  180. v.EndDate = list[i].EndDate.Format(utils.FormatDate)
  181. svList := servicesNameMap[list[i].ContractRegisterId]
  182. v.ServicesName = strings.Join(svList, ",")
  183. // 格式化(合并)开票到款数据
  184. v.InvoicePaymentList = fmsService.MergeInvoiceList2InvoicePaymentCensusInfo(invoiceMap[list[i].ContractRegisterId], paymentMap[list[i].ContractRegisterId],
  185. contractServiceMap[list[i].ContractRegisterId], serviceAmountMap[list[i].ContractRegisterId])
  186. respList = append(respList, v)
  187. }
  188. // 开票到款金额合计
  189. amountTotalCond := `contract_register_id IN ?`
  190. amountTotalPars := make([]interface{}, 0)
  191. amountTotalPars = append(amountTotalPars, registerIds)
  192. amountTotalList, e := fms.GetContractInvoiceAmountTotal(amountTotalCond, amountTotalPars)
  193. if e != nil {
  194. resp.FailMsg("获取失败", "获取开票到款金额合计失败, Err:"+e.Error(), c)
  195. return
  196. }
  197. amountTotalMap := make(map[int]float64)
  198. for i := range amountTotalList {
  199. amountTotalMap[amountTotalList[i].InvoiceType] = amountTotalList[i].TotalAmount
  200. }
  201. results.DataList = respList
  202. results.InvoiceTotal = amountTotalMap[fms.ContractInvoiceTypeMake]
  203. results.PaymentTotal = amountTotalMap[fms.ContractInvoiceTypePay]
  204. }
  205. // 是否导出
  206. if req.IsExport == 1 {
  207. ExportInvoicePaymentCensusList(c, results.DataList)
  208. return
  209. }
  210. page.SetTotal(total)
  211. baseData := new(base.BaseData)
  212. baseData.SetPage(page)
  213. baseData.SetList(results)
  214. resp.OkData("获取成功", baseData, c)
  215. }
  216. // ExportInvoicePaymentCensusList 导出商品到款统计列表
  217. func ExportInvoicePaymentCensusList(c *gin.Context, list []*fms.InvoicePaymentCensusItem) {
  218. // 生成Excel文件
  219. xlsxFile := xlsx.NewFile()
  220. style := xlsx.NewStyle()
  221. alignment := xlsx.Alignment{
  222. Horizontal: "center",
  223. Vertical: "center",
  224. WrapText: true,
  225. }
  226. style.Alignment = alignment
  227. style.ApplyAlignment = true
  228. sheet, err := xlsxFile.AddSheet("商品到款统计")
  229. if err != nil {
  230. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  231. return
  232. }
  233. _ = sheet.SetColWidth(1, 1, 30)
  234. _ = sheet.SetColWidth(3, 3, 30)
  235. // 表头, 套餐动态获取
  236. rowTitle := []string{"序号", "客户名称", "是否新客户", "合同有效期", "开票日", "开票金额", "到款日", "到款金额", "付款方式", "销售",
  237. "组别"}
  238. serviceTempCond := ``
  239. serviceTempPars := make([]interface{}, 0)
  240. serviceTempOB := new(fms.ContractServiceTemplate)
  241. serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
  242. if e != nil {
  243. resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
  244. return
  245. }
  246. for i := range serviceTempList {
  247. rowTitle = append(rowTitle, serviceTempList[i].Title)
  248. }
  249. titleRow := sheet.AddRow()
  250. for i := range rowTitle {
  251. v := titleRow.AddCell()
  252. v.SetString(rowTitle[i])
  253. v.SetStyle(style)
  254. }
  255. newCompanyMap := map[int]string{0: "否", 1: "是"}
  256. for k, v := range list {
  257. dataRow := sheet.AddRow()
  258. // 前四个单元格根据每行开票到款条数向下合并
  259. l := len(v.InvoicePaymentList)
  260. mergeRowNum := l - 1
  261. // 序号
  262. sortNum := k + 1
  263. colA := dataRow.AddCell()
  264. colA.VMerge = mergeRowNum
  265. colA.SetString(fmt.Sprint(sortNum))
  266. // 客户名称
  267. colB := dataRow.AddCell()
  268. colB.VMerge = mergeRowNum
  269. colB.SetString(v.CompanyName)
  270. // 是否新客户
  271. colC := dataRow.AddCell()
  272. colC.VMerge = mergeRowNum
  273. colC.SetString(newCompanyMap[v.NewCompany])
  274. // 合同有效期
  275. colD := dataRow.AddCell()
  276. colD.VMerge = mergeRowNum
  277. colD.SetString(fmt.Sprint(v.StartDate, "至", v.EndDate))
  278. // 开票到款信息
  279. for k2, v2 := range v.InvoicePaymentList {
  280. rowData := []string{
  281. v2.InvoiceDate, // 开票日
  282. fmt.Sprint(v2.InvoiceAmount), // 开票金额
  283. v2.PaymentDate, // 到款日
  284. fmt.Sprint(v2.PaymentAmount), // 到款金额
  285. fms.ContractPaymentPayTypeNameMap[v2.PayType], // 付款方式
  286. v2.SellerName, // 销售
  287. v2.SellerGroupName, // 组别
  288. }
  289. // 套餐金额信息
  290. for i := range serviceTempList {
  291. sa := ""
  292. for s2 := range v2.ServiceAmountList {
  293. if v2.ServiceAmountList[s2].ServiceTemplateId == serviceTempList[i].ServiceTemplateId {
  294. sa = fmt.Sprint(v2.ServiceAmountList[s2].Amount)
  295. break
  296. }
  297. }
  298. rowData = append(rowData, sa)
  299. }
  300. // 首行开票到款
  301. if k2 == 0 {
  302. for i := range rowData {
  303. dataRow.AddCell().SetString(rowData[i])
  304. }
  305. continue
  306. }
  307. // 其他行开票到款, 加四列空的单元格用于合并
  308. dataRowExtra := sheet.AddRow()
  309. for i := 0; i < 4; i++ {
  310. dataRowExtra.AddCell()
  311. }
  312. for i := range rowData {
  313. dataRowExtra.AddCell().SetString(rowData[i])
  314. }
  315. }
  316. }
  317. // 输出文件
  318. var buffer bytes.Buffer
  319. _ = xlsxFile.Write(&buffer)
  320. content := bytes.NewReader(buffer.Bytes())
  321. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  322. fileName := "商品到款统计_" + randStr + ".xlsx"
  323. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  324. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  325. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  326. }