invoice_payment.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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 string false "是否已开票: 0-否; 1-是; 空-全部"
  420. // @Param HasPayment query string false "是否已到款: 0-否; 1-是; 空-全部"
  421. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  422. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'invoice_time':开票日 、 'payment_date':到款日"
  423. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
  424. // @Success 200 {object} fms.ContractRegisterItem
  425. // @router /census/invoice_payment/list [get]
  426. func (ct *InvoicePaymentController) List(c *gin.Context) {
  427. var req fms.InvoicePaymentCensusListReq
  428. if e := c.BindQuery(&req); e != nil {
  429. err, ok := e.(validator.ValidationErrors)
  430. if !ok {
  431. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  432. return
  433. }
  434. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  435. return
  436. }
  437. cond := `1 = 1`
  438. pars := make([]interface{}, 0)
  439. // 客户姓名/销售
  440. if req.Keyword != "" {
  441. kw := "%" + req.Keyword + "%"
  442. cond += ` AND b.company_name LIKE ? OR c.seller_name LIKE ?`
  443. pars = append(pars, kw, kw)
  444. }
  445. if req.SellGroupId > 0 {
  446. cond += ` AND c.seller_group_id = ?`
  447. pars = append(pars, req.SellGroupId)
  448. }
  449. // 套餐筛选
  450. if req.ServiceType != 0 {
  451. tempRegisterIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
  452. if e != nil {
  453. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  454. return
  455. }
  456. if len(tempRegisterIds) > 0 {
  457. cond += ` AND a.register_id IN ?`
  458. pars = append(pars, tempRegisterIds)
  459. } else {
  460. cond += ` AND 1 = 2`
  461. }
  462. }
  463. // 开票到款日期
  464. if req.TimeType != 0 {
  465. if req.TimeType == 1 && req.StartDate != "" && req.EndDate != "" {
  466. st := fmt.Sprint(req.StartDate, " 00:00:00")
  467. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  468. cond += ` AND (c.invoice_time BETWEEN ? AND ?) `
  469. pars = append(pars, st, ed)
  470. }else if req.TimeType == 2 && req.StartDate != "" && req.EndDate != "" {
  471. st := fmt.Sprint(req.StartDate, " 00:00:00")
  472. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  473. cond += ` AND (d.invoice_time BETWEEN ? AND ?) `
  474. pars = append(pars, st, ed)
  475. }else if req.TimeType == 3 && req.StartDate != "" && req.EndDate != "" {
  476. st := fmt.Sprint(req.StartDate, " 00:00:00")
  477. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  478. cond += ` AND (c.invoice_time BETWEEN ? AND ?) AND (d.invoice_time BETWEEN ? AND ?) `
  479. pars = append(pars, st, ed, st, ed)
  480. }
  481. }else if req.StartDate != "" && req.EndDate != "" {
  482. st := fmt.Sprint(req.StartDate, " 00:00:00")
  483. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  484. cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  485. pars = append(pars, st, ed, st, ed)
  486. }
  487. if req.HasInvoice == "1" {
  488. cond += ` AND a.invoice_id > 0 `
  489. }else if req.HasInvoice == "0" {
  490. cond += ` AND a.invoice_id = 0 `
  491. }
  492. if req.HasPayment == "1" {
  493. cond += ` AND a.payment_id > 0 `
  494. }else if req.HasPayment == "0" {
  495. cond += ` AND a.payment_id = 0 `
  496. }
  497. page := new(base.Page)
  498. page.SetPageSize(req.PageSize)
  499. page.SetCurrent(req.Current)
  500. //排序
  501. if req.SortType == "" {
  502. req.SortType = "desc"
  503. }
  504. if req.SortType != "desc" && req.SortType != "asc" {
  505. resp.Fail("排序类型不正确", c)
  506. return
  507. }
  508. if req.SortParam == "" {
  509. page.AddOrderItem(base.OrderItem{Column: "sort_invoice_id", Asc: true})
  510. page.AddOrderItem(base.OrderItem{Column: "sort_payment_id", Asc: false})
  511. page.AddOrderItem(base.OrderItem{Column: "c.invoice_time", Asc: false})
  512. page.AddOrderItem(base.OrderItem{Column: "c.amount", Asc: false})
  513. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
  514. }else if req.SortParam == "invoice_time" {
  515. if req.SortType == "asc" {
  516. page.AddOrderItem(base.OrderItem{Column: "sort_invoice_id", Asc: false})
  517. page.AddOrderItem(base.OrderItem{Column: "c.invoice_time", Asc: true})
  518. page.AddOrderItem(base.OrderItem{Column: "c.amount", Asc: true})
  519. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: true})
  520. }else{
  521. page.AddOrderItem(base.OrderItem{Column: "sort_invoice_id", Asc: true})
  522. page.AddOrderItem(base.OrderItem{Column: "c.invoice_time", Asc: false})
  523. page.AddOrderItem(base.OrderItem{Column: "c.amount", Asc: false})
  524. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
  525. }
  526. }else if req.SortParam == "payment_date" {
  527. if req.SortType == "asc" {
  528. page.AddOrderItem(base.OrderItem{Column: "sort_payment_id", Asc: false})
  529. page.AddOrderItem(base.OrderItem{Column: "d.invoice_time", Asc: true})
  530. page.AddOrderItem(base.OrderItem{Column: "d.amount", Asc: true})
  531. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: true})
  532. }else{
  533. page.AddOrderItem(base.OrderItem{Column: "sort_payment_id", Asc: true})
  534. page.AddOrderItem(base.OrderItem{Column: "d.invoice_time", Asc: false})
  535. page.AddOrderItem(base.OrderItem{Column: "d.amount", Asc: false})
  536. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
  537. }
  538. }else{
  539. resp.Fail("排序字段不正确", c)
  540. return
  541. }
  542. if req.IsExport == 1 {
  543. page.SetPageSize(10000)
  544. page.SetCurrent(1)
  545. }
  546. registerList, total, e := fms.GetInvoicePaymentCensusPageList(page, cond, pars)
  547. if e != nil {
  548. resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
  549. return
  550. }
  551. queryRegisterIds := make([]int, 0)
  552. for i := range registerList {
  553. queryRegisterIds = append(queryRegisterIds, registerList[i].ContractRegisterId)
  554. }
  555. results := new(fms.InvoicePaymentCensusResp)
  556. if len(queryRegisterIds) > 0 {
  557. // 获取汇总数据IDs, 用于查询合计数据
  558. summaryIdsCond := cond
  559. summaryIdsPars := pars
  560. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
  561. if e != nil {
  562. resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
  563. return
  564. }
  565. var listErr, totalErr, totalGroupErr error
  566. wg := sync.WaitGroup{}
  567. // 响应列表
  568. respList := make([]*fms.InvoicePaymentCensusItem, 0)
  569. summaryList := make([]*fms.InvoicePaymentSummaryItem, 0)
  570. wg.Add(1)
  571. go func() {
  572. defer wg.Done()
  573. // 获取汇总数据
  574. summaryCond := cond
  575. summaryCond += ` AND a.register_id IN ?`
  576. summaryPars := pars
  577. summaryPars = append(summaryPars, queryRegisterIds)
  578. summaryData, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
  579. if e != nil {
  580. resp.FailMsg("获取失败", "获取商品到款汇总列表失败, Err: "+e.Error(), c)
  581. return
  582. }
  583. summaryList = summaryData
  584. //summaryIds := make([]int, 0)
  585. paymentIds := make([]int, 0)
  586. for i := range summaryList {
  587. //summaryIds = append(summaryIds, summaryList[i].SummaryId)
  588. if summaryList[i].PaymentId > 0 {
  589. paymentIds = append(paymentIds, summaryList[i].PaymentId)
  590. }
  591. }
  592. // 合同套餐
  593. contractServiceCond := `contract_register_id IN ?`
  594. contractServicePars := make([]interface{}, 0)
  595. contractServicePars = append(contractServicePars, queryRegisterIds)
  596. contractServiceOB := new(fms.ContractService)
  597. contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
  598. if e != nil {
  599. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  600. return
  601. }
  602. contractServiceMap := make(map[int][]*fms.ContractService, 0)
  603. servicesNameMap := make(map[int][]string, 0)
  604. for i := range contractServiceList {
  605. if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
  606. contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
  607. }
  608. contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
  609. servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
  610. }
  611. // 到款套餐分配
  612. serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
  613. if len(paymentIds) > 0 {
  614. serviceAmountCond := `contract_payment_id IN ?`
  615. serviceAmountPars := make([]interface{}, 0)
  616. serviceAmountPars = append(serviceAmountPars, paymentIds)
  617. serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  618. serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  619. if e != nil {
  620. listErr = fmt.Errorf("获取到款套餐分配列表失败, Err: %s", e.Error())
  621. return
  622. }
  623. for i := range serviceAmountList {
  624. if serviceAmountMap[serviceAmountList[i].ContractPaymentId] == nil {
  625. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = make([]*fms.ContractPaymentServiceAmount, 0)
  626. }
  627. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = append(serviceAmountMap[serviceAmountList[i].ContractPaymentId], serviceAmountList[i])
  628. }
  629. }
  630. // 重组汇总数据
  631. summaryMap := make(map[int][]*fms.InvoicePaymentCensusInfo)
  632. amountMap := make(map[string]*fms.ContractPaymentServiceAmount)
  633. for i := range summaryList {
  634. v := new(fms.InvoicePaymentCensusInfo)
  635. v.InvoiceId = summaryList[i].InvoiceId
  636. v.InvoiceDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].InvoiceDate)
  637. v.InvoiceAmount = summaryList[i].InvoiceAmount
  638. v.SellerId = summaryList[i].SellerId
  639. v.SellerName = summaryList[i].SellerName
  640. v.SellerGroupId = summaryList[i].SellerGroupId
  641. v.SellerGroupName = summaryList[i].SellerGroupName
  642. v.PaymentId = summaryList[i].PaymentId
  643. v.PaymentDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].PaymentDate)
  644. v.PaymentAmount = summaryList[i].PaymentAmount
  645. v.PayType = summaryList[i].PayType
  646. // 套餐到款分配
  647. svaList := make([]*fms.ContractPaymentServiceAmountItem, 0)
  648. amountList := serviceAmountMap[summaryList[i].PaymentId]
  649. if amountList != nil {
  650. for i := range amountList {
  651. k := fmt.Sprintf("%d-%d", amountList[i].ContractPaymentId, amountList[i].ServiceTemplateId)
  652. amountMap[k] = amountList[i]
  653. }
  654. }
  655. // 合同对应的所有套餐
  656. svList := contractServiceMap[summaryList[i].RegisterId]
  657. if svList != nil {
  658. for ii := range svList {
  659. vv := new(fms.ContractPaymentServiceAmountItem)
  660. vv.ServiceTemplateId = svList[ii].ServiceTemplateId
  661. vv.ServiceTemplateName = svList[ii].Title
  662. k2 := fmt.Sprintf("%d-%d", summaryList[i].PaymentId, svList[ii].ServiceTemplateId)
  663. a := amountMap[k2]
  664. if a != nil {
  665. vv.ContractPaymentServiceAmountId = a.ContractPaymentServiceAmountId
  666. vv.ContractPaymentId = a.ContractPaymentId
  667. vv.Amount = a.Amount
  668. }
  669. svaList = append(svaList, vv)
  670. }
  671. }
  672. v.ServiceAmountList = svaList
  673. summaryMap[summaryList[i].SummaryId] = append(summaryMap[summaryList[i].SummaryId], v)
  674. }
  675. // 响应列表
  676. for i := range registerList {
  677. v := new(fms.InvoicePaymentCensusItem)
  678. v.SummaryId = registerList[i].SummaryId
  679. v.ContractRegisterId = registerList[i].ContractRegisterId
  680. v.CompanyName = registerList[i].CompanyName
  681. v.NewCompany = registerList[i].NewCompany
  682. v.StartDate = utils.TimeTransferString(utils.FormatDate, registerList[i].StartDate)
  683. v.EndDate = utils.TimeTransferString(utils.FormatDate, registerList[i].EndDate)
  684. svList := servicesNameMap[registerList[i].ContractRegisterId]
  685. v.ServicesName = strings.Join(svList, ",")
  686. v.InvoicePaymentList = summaryMap[registerList[i].SummaryId]
  687. respList = append(respList, v)
  688. }
  689. }()
  690. // 开票到款金额合计(换算后)
  691. var invoiceTotal, paymentTotal float64
  692. wg.Add(1)
  693. go func() {
  694. defer wg.Done()
  695. if len(summaryIds) == 0 {
  696. return
  697. }
  698. amountTotalCond := `a.id IN ?`
  699. amountTotalPars := make([]interface{}, 0)
  700. amountTotalPars = append(amountTotalPars, summaryIds)
  701. invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
  702. if e != nil {
  703. totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
  704. return
  705. }
  706. invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
  707. paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
  708. if e != nil {
  709. totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
  710. return
  711. }
  712. paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
  713. }()
  714. // 分币种金额统计
  715. invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  716. paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  717. wg.Add(1)
  718. go func() {
  719. defer wg.Done()
  720. currencyOB := new(fms.CurrencyUnit)
  721. currencyCond := `enable = 1`
  722. currencyPars := make([]interface{}, 0)
  723. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  724. if e != nil {
  725. totalGroupErr = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
  726. return
  727. }
  728. unitMap := make(map[string]string)
  729. for i := range currencyList {
  730. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  731. invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  732. Name: currencyList[i].Name,
  733. UnitName: currencyList[i].UnitName,
  734. Code: currencyList[i].Code,
  735. FlagImg: currencyList[i].FlagImg,
  736. })
  737. paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  738. Name: currencyList[i].Name,
  739. UnitName: currencyList[i].UnitName,
  740. Code: currencyList[i].Code,
  741. FlagImg: currencyList[i].FlagImg,
  742. })
  743. }
  744. if len(summaryIds) == 0 {
  745. return
  746. }
  747. totalGroupCond := `a.id IN ?`
  748. totalGroupPars := make([]interface{}, 0)
  749. totalGroupPars = append(totalGroupPars, summaryIds)
  750. invoiceSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 1)
  751. if e != nil {
  752. totalGroupErr = fmt.Errorf("获取汇总货币合计开票金额失败, Err: %s", e.Error())
  753. return
  754. }
  755. paymentSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 2)
  756. if e != nil {
  757. totalGroupErr = fmt.Errorf("获取汇总货币合计到款金额失败, Err: %s", e.Error())
  758. return
  759. }
  760. invoiceSumMap := make(map[string]float64)
  761. paymentSumMap := make(map[string]float64)
  762. for i := range invoiceSumGroup {
  763. invoiceSumMap[invoiceSumGroup[i].CurrencyUnit] = invoiceSumGroup[i].OriginAmountTotal
  764. continue
  765. }
  766. for i := range paymentSumGroup {
  767. paymentSumMap[paymentSumGroup[i].CurrencyUnit] = paymentSumGroup[i].OriginAmountTotal
  768. continue
  769. }
  770. for i := range invoiceCurrencyTotals {
  771. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
  772. invoiceCurrencyTotals[i].Amount = a
  773. }
  774. for i := range paymentCurrencyTotals {
  775. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
  776. paymentCurrencyTotals[i].Amount = a
  777. }
  778. }()
  779. wg.Wait()
  780. if listErr != nil {
  781. resp.FailMsg("获取失败", listErr.Error(), c)
  782. return
  783. }
  784. if totalErr != nil {
  785. resp.FailMsg("获取失败", totalErr.Error(), c)
  786. return
  787. }
  788. if totalGroupErr != nil {
  789. resp.FailMsg("获取失败", totalGroupErr.Error(), c)
  790. return
  791. }
  792. results.DataList = respList
  793. results.InvoiceTotal = invoiceTotal
  794. results.PaymentTotal = paymentTotal
  795. results.InvoiceCurrencyTotal = invoiceCurrencyTotals
  796. results.PaymentCurrencyTotal = paymentCurrencyTotals
  797. }
  798. // 是否导出
  799. if req.IsExport == 1 {
  800. ExportInvoicePaymentCensusList(c, results)
  801. return
  802. }
  803. page.SetTotal(total)
  804. baseData := new(base.BaseData)
  805. baseData.SetPage(page)
  806. baseData.SetList(results)
  807. resp.OkData("获取成功", baseData, c)
  808. }