invoice_payment.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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. "hongze/fms_api/utils"
  13. "net/http"
  14. "strconv"
  15. "strings"
  16. "sync"
  17. "time"
  18. )
  19. // InvoicePaymentController 商品到款统计
  20. type InvoicePaymentController struct{}
  21. // List2 原版(已废弃)
  22. //func (ct *InvoicePaymentController) List2(c *gin.Context) {
  23. // var req fms.InvoicePaymentCensusListReq
  24. // if e := c.BindQuery(&req); e != nil {
  25. // err, ok := e.(validator.ValidationErrors)
  26. // if !ok {
  27. // resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  28. // return
  29. // }
  30. // resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  31. // return
  32. // }
  33. //
  34. // cond := `1 = 1`
  35. // pars := make([]interface{}, 0)
  36. // // 合同编号/客户姓名/销售
  37. // if req.Keyword != "" {
  38. // kw := "%" + req.Keyword + "%"
  39. // cond += ` AND b.company_name LIKE ? OR a.seller_name LIKE ?`
  40. // pars = append(pars, kw, kw)
  41. // }
  42. // if req.SellGroupId > 0 {
  43. // cond += ` AND a.seller_group_id = ?`
  44. // pars = append(pars, req.SellGroupId)
  45. // }
  46. // // 套餐筛选
  47. // if req.ServiceType != 0 {
  48. // registerIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
  49. // if e != nil {
  50. // resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  51. // return
  52. // }
  53. // if len(registerIds) > 0 {
  54. // cond += ` AND b.contract_register_id IN ?`
  55. // pars = append(pars, registerIds)
  56. // } else {
  57. // cond += ` AND 1 = 2`
  58. // }
  59. // }
  60. // // 开票到款日期
  61. // if req.TimeType > 0 && req.StartDate != "" && req.EndDate != "" {
  62. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  63. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  64. // cond += ` AND a.invoice_type = ? AND (a.invoice_time BETWEEN ? AND ?)`
  65. // pars = append(pars, req.TimeType, st, ed)
  66. // }
  67. // // 已开票
  68. // if req.HasInvoice == 1 && req.HasPayment == 0 {
  69. // cond += ` AND a.invoice_type = 1`
  70. // }
  71. // // 已到款
  72. // if req.HasInvoice == 0 && req.HasPayment == 1 {
  73. // cond += ` AND a.invoice_type = 2`
  74. // }
  75. //
  76. // page := new(base.Page)
  77. // page.SetPageSize(req.PageSize)
  78. // page.SetCurrent(req.Current)
  79. // page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
  80. // page.AddOrderItem(base.OrderItem{Column: "b.contract_register_id", Asc: true})
  81. // if req.IsExport == 1 {
  82. // page.SetPageSize(10000)
  83. // page.SetCurrent(1)
  84. // }
  85. //
  86. // list, allRegisterIds, e := fms.GetInvoicePaymentCensusPageList(page, cond, pars)
  87. // if e != nil {
  88. // resp.FailMsg("获取失败", "获取商品到款统计列表失败, Err: "+e.Error(), c)
  89. // return
  90. // }
  91. // total := int64(len(allRegisterIds))
  92. // registerIds := make([]int, 0)
  93. // for i := range list {
  94. // registerIds = append(registerIds, list[i].ContractRegisterId)
  95. // }
  96. //
  97. // results := new(fms.InvoicePaymentCensusResp)
  98. // if len(registerIds) > 0 {
  99. // // 获取开票到款列表
  100. // ivCond := `contract_register_id IN ?`
  101. // ivPars := make([]interface{}, 0)
  102. // ivPars = append(ivPars, registerIds)
  103. // iv := new(fms.ContractInvoice)
  104. // invoiceList, e := iv.List(ivCond, ivPars, "")
  105. // if e != nil {
  106. // resp.FailMsg("获取失败", "获取开票到款列表失败, Err: "+e.Error(), c)
  107. // return
  108. // }
  109. // // 取出开票、到款
  110. // invoiceMap := make(map[int][]*fms.ContractInvoice, 0)
  111. // paymentMap := make(map[int][]*fms.ContractInvoice, 0)
  112. // paymentIds := make([]int, 0)
  113. // for i := range invoiceList {
  114. // if invoiceMap[invoiceList[i].ContractRegisterId] == nil {
  115. // invoiceMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoice, 0)
  116. // }
  117. // if paymentMap[invoiceList[i].ContractRegisterId] == nil {
  118. // paymentMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoice, 0)
  119. // }
  120. // if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  121. // invoiceMap[invoiceList[i].ContractRegisterId] = append(invoiceMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  122. // }
  123. // if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  124. // paymentMap[invoiceList[i].ContractRegisterId] = append(paymentMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  125. // paymentIds = append(paymentIds, invoiceList[i].ContractInvoiceId)
  126. // }
  127. // }
  128. //
  129. // // 合同套餐
  130. // contractServiceCond := `contract_register_id IN ?`
  131. // contractServicePars := make([]interface{}, 0)
  132. // contractServicePars = append(contractServicePars, registerIds)
  133. // contractServiceOB := new(fms.ContractService)
  134. // contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
  135. // if e != nil {
  136. // resp.FailMsg("获取失败", "获取合同套餐列表失败, Err:"+e.Error(), c)
  137. // return
  138. // }
  139. // contractServiceMap := make(map[int][]*fms.ContractService, 0)
  140. // servicesNameMap := make(map[int][]string, 0)
  141. // for i := range contractServiceList {
  142. // if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
  143. // contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
  144. // }
  145. // contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
  146. // servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
  147. // }
  148. //
  149. // // 到款套餐分配
  150. // serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
  151. // if len(paymentIds) > 0 {
  152. // serviceAmountCond := `contract_payment_id IN ?`
  153. // serviceAmountPars := make([]interface{}, 0)
  154. // serviceAmountPars = append(serviceAmountPars, paymentIds)
  155. // serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  156. // serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  157. // if e != nil {
  158. // resp.FailMsg("获取失败", "获取到款套餐分配列表失败, Err:"+e.Error(), c)
  159. // return
  160. // }
  161. // for i := range serviceAmountList {
  162. // if serviceAmountMap[serviceAmountList[i].ContractRegisterId] == nil {
  163. // serviceAmountMap[serviceAmountList[i].ContractRegisterId] = make([]*fms.ContractPaymentServiceAmount, 0)
  164. // }
  165. // serviceAmountMap[serviceAmountList[i].ContractRegisterId] = append(serviceAmountMap[serviceAmountList[i].ContractRegisterId], serviceAmountList[i])
  166. // }
  167. // }
  168. //
  169. // // 整合响应数据
  170. // respList := make([]*fms.InvoicePaymentCensusItem, 0)
  171. // for i := range list {
  172. // v := new(fms.InvoicePaymentCensusItem)
  173. // v.ContractRegisterId = list[i].ContractRegisterId
  174. // v.CompanyName = list[i].CompanyName
  175. // v.NewCompany = list[i].NewCompany
  176. // v.StartDate = list[i].StartDate.Format(utils.FormatDate)
  177. // v.EndDate = list[i].EndDate.Format(utils.FormatDate)
  178. // svList := servicesNameMap[list[i].ContractRegisterId]
  179. // v.ServicesName = strings.Join(svList, ",")
  180. // // 格式化(合并)开票到款数据
  181. // v.InvoicePaymentList = fmsService.MergeInvoiceList2InvoicePaymentCensusInfo(invoiceMap[list[i].ContractRegisterId], paymentMap[list[i].ContractRegisterId],
  182. // contractServiceMap[list[i].ContractRegisterId], serviceAmountMap[list[i].ContractRegisterId])
  183. // respList = append(respList, v)
  184. // }
  185. //
  186. // // 开票到款金额合计(换算后)
  187. // amountTotalCond := `contract_register_id IN ?`
  188. // amountTotalPars := make([]interface{}, 0)
  189. // amountTotalPars = append(amountTotalPars, allRegisterIds)
  190. // amountTotalList, e := fms.GetContractInvoiceAmountTotal(amountTotalCond, amountTotalPars)
  191. // if e != nil {
  192. // resp.FailMsg("获取失败", "获取开票到款金额合计失败, Err:"+e.Error(), c)
  193. // return
  194. // }
  195. // amountTotalMap := make(map[int]float64)
  196. // for i := range amountTotalList {
  197. // amountTotalMap[amountTotalList[i].InvoiceType] = amountTotalList[i].TotalAmount
  198. // }
  199. // invoiceTotal, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", amountTotalMap[fms.ContractInvoiceTypeMake]), 64)
  200. // paymentTotal, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", amountTotalMap[fms.ContractInvoiceTypePay]), 64)
  201. //
  202. // // 分币种金额统计
  203. // currencyOB := new(fms.CurrencyUnit)
  204. // currencyCond := `enable = 1`
  205. // currencyPars := make([]interface{}, 0)
  206. // currencyList, e := currencyOB.List(currencyCond, currencyPars)
  207. // if e != nil {
  208. // resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
  209. // return
  210. // }
  211. // unitMap := make(map[string]string)
  212. // invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  213. // paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  214. // for i := range currencyList {
  215. // unitMap[currencyList[i].Code] = currencyList[i].UnitName
  216. // invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  217. // Name: currencyList[i].Name,
  218. // UnitName: currencyList[i].UnitName,
  219. // Code: currencyList[i].Code,
  220. // FlagImg: currencyList[i].FlagImg,
  221. // })
  222. // paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  223. // Name: currencyList[i].Name,
  224. // UnitName: currencyList[i].UnitName,
  225. // Code: currencyList[i].Code,
  226. // FlagImg: currencyList[i].FlagImg,
  227. // })
  228. // }
  229. // sumList, e := fms.GetInvoiceListCurrencySum(amountTotalCond, amountTotalPars, "currency_unit, invoice_type")
  230. // if e != nil {
  231. // resp.FailMsg("获取失败", "获取商品到款统计货币合计金额失败, Err: "+e.Error(), c)
  232. // return
  233. // }
  234. // invoiceSumMap := make(map[string]float64)
  235. // paymentSumMap := make(map[string]float64)
  236. // for i := range sumList {
  237. // if sumList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  238. // invoiceSumMap[sumList[i].CurrencyUnit] = sumList[i].OriginAmountTotal
  239. // continue
  240. // }
  241. // if sumList[i].InvoiceType == fms.ContractInvoiceTypePay {
  242. // paymentSumMap[sumList[i].CurrencyUnit] = sumList[i].OriginAmountTotal
  243. // }
  244. // }
  245. // for i := range invoiceCurrencyTotals {
  246. // a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
  247. // invoiceCurrencyTotals[i].Amount = a
  248. // }
  249. // for i := range paymentCurrencyTotals {
  250. // a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
  251. // paymentCurrencyTotals[i].Amount = a
  252. // }
  253. //
  254. // results.DataList = respList
  255. // results.InvoiceTotal = invoiceTotal
  256. // results.PaymentTotal = paymentTotal
  257. // results.InvoiceCurrencyTotal = invoiceCurrencyTotals
  258. // results.PaymentCurrencyTotal = paymentCurrencyTotals
  259. // }
  260. //
  261. // // 是否导出
  262. // if req.IsExport == 1 {
  263. // ExportInvoicePaymentCensusList(c, results)
  264. // return
  265. // }
  266. // page.SetTotal(total)
  267. // baseData := new(base.BaseData)
  268. // baseData.SetPage(page)
  269. // baseData.SetList(results)
  270. // resp.OkData("获取成功", baseData, c)
  271. //}
  272. // ExportInvoicePaymentCensusList 导出商品到款统计列表
  273. func ExportInvoicePaymentCensusList(c *gin.Context, results *fms.InvoicePaymentCensusResp) {
  274. list := results.DataList
  275. if len(list) == 0 {
  276. resp.Fail("列表数据为空", c)
  277. return
  278. }
  279. // 生成Excel文件
  280. xlsxFile := xlsx.NewFile()
  281. style := xlsx.NewStyle()
  282. alignment := xlsx.Alignment{
  283. Horizontal: "center",
  284. Vertical: "center",
  285. WrapText: true,
  286. }
  287. style.Alignment = alignment
  288. style.ApplyAlignment = true
  289. sheet, err := xlsxFile.AddSheet("商品到款统计")
  290. if err != nil {
  291. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  292. return
  293. }
  294. _ = sheet.SetColWidth(1, 1, 30)
  295. _ = sheet.SetColWidth(3, 3, 30)
  296. // 前三行-开票金额合计
  297. rowA := sheet.AddRow()
  298. cellAA := rowA.AddCell()
  299. cellAA.SetString(fmt.Sprintf("已开票合计金额(换算后):%.2f(元)", results.InvoiceTotal))
  300. rowBData := "已开票金额:"
  301. for _, v := range results.InvoiceCurrencyTotal {
  302. rowBData += fmt.Sprintf("%s%.2f(%s) ", v.Name, v.Amount, v.UnitName)
  303. }
  304. rowB := sheet.AddRow()
  305. rowB.AddCell().SetString(rowBData)
  306. sheet.AddRow()
  307. // 四至六-到款金额合计
  308. rowD := sheet.AddRow()
  309. cellDA := rowD.AddCell()
  310. cellDA.SetString(fmt.Sprintf("已到款合计金额(换算后):%.2f(元)", results.PaymentTotal))
  311. rowEData := "已到款金额:"
  312. for _, v := range results.PaymentCurrencyTotal {
  313. rowEData += fmt.Sprintf("%s%.2f(%s) ", v.Name, v.Amount, v.UnitName)
  314. }
  315. rowE := sheet.AddRow()
  316. rowE.AddCell().SetString(rowEData)
  317. sheet.AddRow()
  318. // 表头, 套餐动态获取
  319. rowTitle := []string{"序号", "客户名称", "是否新客户", "合同有效期", "开票日", "开票金额", "到款日", "到款金额", "付款方式", "销售",
  320. "组别"}
  321. serviceTempCond := ``
  322. serviceTempPars := make([]interface{}, 0)
  323. serviceTempOB := new(fms.ContractServiceTemplate)
  324. serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
  325. if e != nil {
  326. resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
  327. return
  328. }
  329. for i := range serviceTempList {
  330. rowTitle = append(rowTitle, serviceTempList[i].Title)
  331. }
  332. titleRow := sheet.AddRow()
  333. for i := range rowTitle {
  334. v := titleRow.AddCell()
  335. v.SetString(rowTitle[i])
  336. v.SetStyle(style)
  337. }
  338. newCompanyMap := map[int]string{0: "否", 1: "是"}
  339. for k, v := range list {
  340. dataRow := sheet.AddRow()
  341. // 前四个单元格根据每行开票到款条数向下合并
  342. l := len(v.InvoicePaymentList)
  343. mergeRowNum := l - 1
  344. // 序号
  345. sortNum := k + 1
  346. colA := dataRow.AddCell()
  347. colA.VMerge = mergeRowNum
  348. colA.SetString(fmt.Sprint(sortNum))
  349. // 客户名称
  350. colB := dataRow.AddCell()
  351. colB.VMerge = mergeRowNum
  352. colB.SetString(v.CompanyName)
  353. // 是否新客户
  354. colC := dataRow.AddCell()
  355. colC.VMerge = mergeRowNum
  356. colC.SetString(newCompanyMap[v.NewCompany])
  357. // 合同有效期
  358. colD := dataRow.AddCell()
  359. colD.VMerge = mergeRowNum
  360. colD.SetString(fmt.Sprint(v.StartDate, "至", v.EndDate))
  361. // 开票到款信息
  362. for k2, v2 := range v.InvoicePaymentList {
  363. rowData := []string{
  364. v2.InvoiceDate, // 开票日
  365. fmt.Sprint(v2.InvoiceAmount), // 开票金额
  366. v2.PaymentDate, // 到款日
  367. fmt.Sprint(v2.PaymentAmount), // 到款金额
  368. fms.ContractPaymentPayTypeNameMap[v2.PayType], // 付款方式
  369. v2.SellerName, // 销售
  370. v2.SellerGroupName, // 组别
  371. }
  372. // 套餐金额信息
  373. for i := range serviceTempList {
  374. sa := ""
  375. for s2 := range v2.ServiceAmountList {
  376. if v2.ServiceAmountList[s2].ServiceTemplateId == serviceTempList[i].ServiceTemplateId {
  377. sa = fmt.Sprint(v2.ServiceAmountList[s2].Amount)
  378. break
  379. }
  380. }
  381. rowData = append(rowData, sa)
  382. }
  383. // 首行开票到款
  384. if k2 == 0 {
  385. for i := range rowData {
  386. dataRow.AddCell().SetString(rowData[i])
  387. }
  388. continue
  389. }
  390. // 其他行开票到款, 加四列空的单元格用于合并
  391. dataRowExtra := sheet.AddRow()
  392. for i := 0; i < 4; i++ {
  393. dataRowExtra.AddCell()
  394. }
  395. for i := range rowData {
  396. dataRowExtra.AddCell().SetString(rowData[i])
  397. }
  398. }
  399. }
  400. // 输出文件
  401. var buffer bytes.Buffer
  402. _ = xlsxFile.Write(&buffer)
  403. content := bytes.NewReader(buffer.Bytes())
  404. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  405. fileName := "商品到款统计_" + randStr + ".xlsx"
  406. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  407. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  408. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  409. }
  410. // List
  411. // @Title 商品到款统计列表
  412. // @Description 商品到款统计列表
  413. // @Param Keyword query string false "关键词"
  414. // @Param SellGroupId query int false "销售组别ID"
  415. // @Param ServiceType query int false "套餐类型"
  416. // @Param StartDate query string false "合同开始日期"
  417. // @Param EndDate query string false "合同结束日期"
  418. // @Param TimeType query int false "时间类型: 1-开票时间; 2-到款时间"
  419. // @Param HasInvoice query int false "是否已开票: 0-否; 1-是"
  420. // @Param HasPayment query int false "是否已到款: 0-否; 1-是"
  421. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  422. // @Success 200 {object} fms.ContractRegisterItem
  423. // @router /census/invoice_payment/list [get]
  424. func (ct *InvoicePaymentController) List(c *gin.Context) {
  425. var req fms.InvoicePaymentCensusListReq
  426. if e := c.BindQuery(&req); e != nil {
  427. err, ok := e.(validator.ValidationErrors)
  428. if !ok {
  429. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  430. return
  431. }
  432. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  433. return
  434. }
  435. cond := `1 = 1`
  436. pars := make([]interface{}, 0)
  437. // 客户姓名/销售
  438. if req.Keyword != "" {
  439. kw := "%" + req.Keyword + "%"
  440. cond += ` AND b.company_name LIKE ? OR c.seller_name LIKE ?`
  441. pars = append(pars, kw, kw)
  442. }
  443. if req.SellGroupId > 0 {
  444. cond += ` AND c.seller_group_id = ?`
  445. pars = append(pars, req.SellGroupId)
  446. }
  447. // 套餐筛选
  448. if req.ServiceType != 0 {
  449. tempRegisterIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
  450. if e != nil {
  451. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  452. return
  453. }
  454. if len(tempRegisterIds) > 0 {
  455. cond += ` AND a.register_id IN ?`
  456. pars = append(pars, tempRegisterIds)
  457. } else {
  458. cond += ` AND 1 = 2`
  459. }
  460. }
  461. // 开票到款日期
  462. if req.TimeType > 0 && req.StartDate != "" && req.EndDate != "" {
  463. st := fmt.Sprint(req.StartDate, " 00:00:00")
  464. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  465. if req.TimeType == 1 {
  466. cond += ` AND (c.invoice_time BETWEEN ? AND ?)`
  467. }
  468. if req.TimeType == 2 {
  469. cond += ` AND (d.invoice_time BETWEEN ? AND ?)`
  470. }
  471. pars = append(pars, st, ed)
  472. }
  473. // 已开票未到款
  474. if req.HasInvoice == 1 && req.HasPayment == 0 {
  475. cond += ` AND a.invoice_id > 0 AND a.payment_id = 0 `
  476. }
  477. // 已到款未开票
  478. if req.HasPayment == 1 && req.HasInvoice == 0 {
  479. cond += ` AND a.payment_id > 0 AND a.invoice_id = 0 `
  480. }
  481. // 已开票且已到款
  482. if req.HasInvoice == 1 && req.HasPayment == 1 {
  483. cond += ` AND a.invoice_id > 0 AND a.payment_id > 0 `
  484. }
  485. page := new(base.Page)
  486. page.SetPageSize(req.PageSize)
  487. page.SetCurrent(req.Current)
  488. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
  489. if req.IsExport == 1 {
  490. page.SetPageSize(10000)
  491. page.SetCurrent(1)
  492. }
  493. registerList, registerIds, e := fms.GetInvoicePaymentCensusPageList(page, cond, pars)
  494. if e != nil {
  495. resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
  496. return
  497. }
  498. total := int64(len(registerIds))
  499. queryRegisterIds := make([]int, 0)
  500. for i := range registerList {
  501. queryRegisterIds = append(queryRegisterIds, registerList[i].ContractRegisterId)
  502. }
  503. results := new(fms.InvoicePaymentCensusResp)
  504. if len(queryRegisterIds) > 0 {
  505. // 获取汇总数据IDs, 用于查询合计数据
  506. summaryIdsCond := cond
  507. summaryIdsPars := pars
  508. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
  509. if e != nil {
  510. resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
  511. return
  512. }
  513. var listErr, totalErr, totalGroupErr error
  514. wg := sync.WaitGroup{}
  515. // 响应列表
  516. respList := make([]*fms.InvoicePaymentCensusItem, 0)
  517. summaryList := make([]*fms.InvoicePaymentSummaryItem, 0)
  518. wg.Add(1)
  519. go func() {
  520. defer wg.Done()
  521. // 获取汇总数据
  522. summaryCond := cond
  523. summaryCond += ` AND a.register_id IN ?`
  524. summaryPars := pars
  525. summaryPars = append(summaryPars, queryRegisterIds)
  526. summaryData, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
  527. if e != nil {
  528. resp.FailMsg("获取失败", "获取商品到款汇总列表失败, Err: "+e.Error(), c)
  529. return
  530. }
  531. summaryList = summaryData
  532. //summaryIds := make([]int, 0)
  533. paymentIds := make([]int, 0)
  534. for i := range summaryList {
  535. //summaryIds = append(summaryIds, summaryList[i].SummaryId)
  536. if summaryList[i].PaymentId > 0 {
  537. paymentIds = append(paymentIds, summaryList[i].PaymentId)
  538. }
  539. }
  540. // 合同套餐
  541. contractServiceCond := `contract_register_id IN ?`
  542. contractServicePars := make([]interface{}, 0)
  543. contractServicePars = append(contractServicePars, queryRegisterIds)
  544. contractServiceOB := new(fms.ContractService)
  545. contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
  546. if e != nil {
  547. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  548. return
  549. }
  550. contractServiceMap := make(map[int][]*fms.ContractService, 0)
  551. servicesNameMap := make(map[int][]string, 0)
  552. for i := range contractServiceList {
  553. if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
  554. contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
  555. }
  556. contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
  557. servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
  558. }
  559. // 到款套餐分配
  560. serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
  561. if len(paymentIds) > 0 {
  562. serviceAmountCond := `contract_payment_id IN ?`
  563. serviceAmountPars := make([]interface{}, 0)
  564. serviceAmountPars = append(serviceAmountPars, paymentIds)
  565. serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  566. serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  567. if e != nil {
  568. listErr = fmt.Errorf("获取到款套餐分配列表失败, Err: %s", e.Error())
  569. return
  570. }
  571. for i := range serviceAmountList {
  572. if serviceAmountMap[serviceAmountList[i].ContractPaymentId] == nil {
  573. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = make([]*fms.ContractPaymentServiceAmount, 0)
  574. }
  575. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = append(serviceAmountMap[serviceAmountList[i].ContractPaymentId], serviceAmountList[i])
  576. }
  577. }
  578. // 重组汇总数据
  579. summaryMap := make(map[int][]*fms.InvoicePaymentCensusInfo)
  580. amountMap := make(map[string]*fms.ContractPaymentServiceAmount)
  581. for i := range summaryList {
  582. if summaryMap[summaryList[i].RegisterId] == nil {
  583. summaryMap[summaryList[i].RegisterId] = make([]*fms.InvoicePaymentCensusInfo, 0)
  584. }
  585. v := new(fms.InvoicePaymentCensusInfo)
  586. v.InvoiceId = summaryList[i].InvoiceId
  587. v.InvoiceDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].InvoiceDate)
  588. v.InvoiceAmount = summaryList[i].InvoiceAmount
  589. v.SellerId = summaryList[i].SellerId
  590. v.SellerName = summaryList[i].SellerName
  591. v.SellerGroupId = summaryList[i].SellerGroupId
  592. v.SellerGroupName = summaryList[i].SellerGroupName
  593. v.PaymentId = summaryList[i].PaymentId
  594. v.PaymentDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].PaymentDate)
  595. v.PaymentAmount = summaryList[i].PaymentAmount
  596. v.PayType = summaryList[i].PayType
  597. // 套餐到款分配
  598. svaList := make([]*fms.ContractPaymentServiceAmountItem, 0)
  599. amountList := serviceAmountMap[summaryList[i].PaymentId]
  600. if amountList != nil {
  601. for i := range amountList {
  602. k := fmt.Sprintf("%d-%d", amountList[i].ContractPaymentId, amountList[i].ServiceTemplateId)
  603. amountMap[k] = amountList[i]
  604. }
  605. }
  606. // 合同对应的所有套餐
  607. svList := contractServiceMap[summaryList[i].RegisterId]
  608. if svList != nil {
  609. for ii := range svList {
  610. vv := new(fms.ContractPaymentServiceAmountItem)
  611. vv.ServiceTemplateId = svList[ii].ServiceTemplateId
  612. vv.ServiceTemplateName = svList[ii].Title
  613. k2 := fmt.Sprintf("%d-%d", summaryList[i].PaymentId, svList[ii].ServiceTemplateId)
  614. a := amountMap[k2]
  615. if a != nil {
  616. vv.ContractPaymentServiceAmountId = a.ContractPaymentServiceAmountId
  617. vv.ContractPaymentId = a.ContractPaymentId
  618. vv.Amount = a.Amount
  619. }
  620. svaList = append(svaList, vv)
  621. }
  622. }
  623. v.ServiceAmountList = svaList
  624. summaryMap[summaryList[i].RegisterId] = append(summaryMap[summaryList[i].RegisterId], v)
  625. }
  626. // 响应列表
  627. for i := range registerList {
  628. v := new(fms.InvoicePaymentCensusItem)
  629. v.ContractRegisterId = registerList[i].ContractRegisterId
  630. v.CompanyName = registerList[i].CompanyName
  631. v.NewCompany = registerList[i].NewCompany
  632. v.StartDate = utils.TimeTransferString(utils.FormatDate, registerList[i].StartDate)
  633. v.EndDate = utils.TimeTransferString(utils.FormatDate, registerList[i].EndDate)
  634. svList := servicesNameMap[registerList[i].ContractRegisterId]
  635. v.ServicesName = strings.Join(svList, ",")
  636. v.InvoicePaymentList = summaryMap[registerList[i].ContractRegisterId]
  637. respList = append(respList, v)
  638. }
  639. }()
  640. // 开票到款金额合计(换算后)
  641. var invoiceTotal, paymentTotal float64
  642. wg.Add(1)
  643. go func() {
  644. defer wg.Done()
  645. if len(summaryIds) == 0 {
  646. return
  647. }
  648. amountTotalCond := `a.id IN ?`
  649. amountTotalPars := make([]interface{}, 0)
  650. amountTotalPars = append(amountTotalPars, summaryIds)
  651. invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
  652. if e != nil {
  653. totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
  654. return
  655. }
  656. invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
  657. paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
  658. if e != nil {
  659. totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
  660. return
  661. }
  662. paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
  663. }()
  664. // 分币种金额统计
  665. invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  666. paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  667. wg.Add(1)
  668. go func() {
  669. defer wg.Done()
  670. currencyOB := new(fms.CurrencyUnit)
  671. currencyCond := `enable = 1`
  672. currencyPars := make([]interface{}, 0)
  673. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  674. if e != nil {
  675. totalGroupErr = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
  676. return
  677. }
  678. unitMap := make(map[string]string)
  679. for i := range currencyList {
  680. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  681. invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  682. Name: currencyList[i].Name,
  683. UnitName: currencyList[i].UnitName,
  684. Code: currencyList[i].Code,
  685. FlagImg: currencyList[i].FlagImg,
  686. })
  687. paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  688. Name: currencyList[i].Name,
  689. UnitName: currencyList[i].UnitName,
  690. Code: currencyList[i].Code,
  691. FlagImg: currencyList[i].FlagImg,
  692. })
  693. }
  694. if len(summaryIds) == 0 {
  695. return
  696. }
  697. totalGroupCond := `a.id IN ?`
  698. totalGroupPars := make([]interface{}, 0)
  699. totalGroupPars = append(totalGroupPars, summaryIds)
  700. invoiceSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 1)
  701. if e != nil {
  702. totalGroupErr = fmt.Errorf("获取汇总货币合计开票金额失败, Err: %s", e.Error())
  703. return
  704. }
  705. paymentSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 2)
  706. if e != nil {
  707. totalGroupErr = fmt.Errorf("获取汇总货币合计到款金额失败, Err: %s", e.Error())
  708. return
  709. }
  710. invoiceSumMap := make(map[string]float64)
  711. paymentSumMap := make(map[string]float64)
  712. for i := range invoiceSumGroup {
  713. invoiceSumMap[invoiceSumGroup[i].CurrencyUnit] = invoiceSumGroup[i].OriginAmountTotal
  714. continue
  715. }
  716. for i := range paymentSumGroup {
  717. paymentSumMap[paymentSumGroup[i].CurrencyUnit] = paymentSumGroup[i].OriginAmountTotal
  718. continue
  719. }
  720. for i := range invoiceCurrencyTotals {
  721. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
  722. invoiceCurrencyTotals[i].Amount = a
  723. }
  724. for i := range paymentCurrencyTotals {
  725. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
  726. paymentCurrencyTotals[i].Amount = a
  727. }
  728. }()
  729. wg.Wait()
  730. if listErr != nil {
  731. resp.FailMsg("获取失败", listErr.Error(), c)
  732. return
  733. }
  734. if totalErr != nil {
  735. resp.FailMsg("获取失败", totalErr.Error(), c)
  736. return
  737. }
  738. if totalGroupErr != nil {
  739. resp.FailMsg("获取失败", totalGroupErr.Error(), c)
  740. return
  741. }
  742. results.DataList = respList
  743. results.InvoiceTotal = invoiceTotal
  744. results.PaymentTotal = paymentTotal
  745. results.InvoiceCurrencyTotal = invoiceCurrencyTotals
  746. results.PaymentCurrencyTotal = paymentCurrencyTotals
  747. }
  748. // 是否导出
  749. if req.IsExport == 1 {
  750. ExportInvoicePaymentCensusList(c, results)
  751. return
  752. }
  753. page.SetTotal(total)
  754. baseData := new(base.BaseData)
  755. baseData.SetPage(page)
  756. baseData.SetList(results)
  757. resp.OkData("获取成功", baseData, c)
  758. }