invoice_payment.go 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794
  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. "strconv"
  16. "strings"
  17. "sync"
  18. "time"
  19. )
  20. // InvoicePaymentController 商品到款统计
  21. type InvoicePaymentController struct{}
  22. // List2 原版(已废弃)
  23. //func (ct *InvoicePaymentController) List2(c *gin.Context) {
  24. // var req fms.InvoicePaymentCensusListReq
  25. // if e := c.BindQuery(&req); e != nil {
  26. // err, ok := e.(validator.ValidationErrors)
  27. // if !ok {
  28. // resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  29. // return
  30. // }
  31. // resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  32. // return
  33. // }
  34. //
  35. // cond := `1 = 1`
  36. // pars := make([]interface{}, 0)
  37. // // 合同编号/客户姓名/销售
  38. // if req.Keyword != "" {
  39. // kw := "%" + req.Keyword + "%"
  40. // cond += ` AND b.company_name LIKE ? OR a.seller_name LIKE ?`
  41. // pars = append(pars, kw, kw)
  42. // }
  43. // if req.SellGroupId > 0 {
  44. // cond += ` AND a.seller_group_id = ?`
  45. // pars = append(pars, req.SellGroupId)
  46. // }
  47. // // 套餐筛选
  48. // if req.ServiceType != 0 {
  49. // registerIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
  50. // if e != nil {
  51. // resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  52. // return
  53. // }
  54. // if len(registerIds) > 0 {
  55. // cond += ` AND b.contract_register_id IN ?`
  56. // pars = append(pars, registerIds)
  57. // } else {
  58. // cond += ` AND 1 = 2`
  59. // }
  60. // }
  61. // // 开票到款日期
  62. // if req.TimeType > 0 && req.StartDate != "" && req.EndDate != "" {
  63. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  64. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  65. // cond += ` AND a.invoice_type = ? AND (a.invoice_time BETWEEN ? AND ?)`
  66. // pars = append(pars, req.TimeType, st, ed)
  67. // }
  68. // // 已开票
  69. // if req.HasInvoice == 1 && req.HasPayment == 0 {
  70. // cond += ` AND a.invoice_type = 1`
  71. // }
  72. // // 已到款
  73. // if req.HasInvoice == 0 && req.HasPayment == 1 {
  74. // cond += ` AND a.invoice_type = 2`
  75. // }
  76. //
  77. // page := new(base.Page)
  78. // page.SetPageSize(req.PageSize)
  79. // page.SetCurrent(req.Current)
  80. // page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
  81. // page.AddOrderItem(base.OrderItem{Column: "b.contract_register_id", Asc: true})
  82. // if req.IsExport == 1 {
  83. // page.SetPageSize(10000)
  84. // page.SetCurrent(1)
  85. // }
  86. //
  87. // list, allRegisterIds, e := fms.GetInvoicePaymentCensusPageList(page, cond, pars)
  88. // if e != nil {
  89. // resp.FailMsg("获取失败", "获取商品到款统计列表失败, Err: "+e.Error(), c)
  90. // return
  91. // }
  92. // total := int64(len(allRegisterIds))
  93. // registerIds := make([]int, 0)
  94. // for i := range list {
  95. // registerIds = append(registerIds, list[i].ContractRegisterId)
  96. // }
  97. //
  98. // results := new(fms.InvoicePaymentCensusResp)
  99. // if len(registerIds) > 0 {
  100. // // 获取开票到款列表
  101. // ivCond := `contract_register_id IN ?`
  102. // ivPars := make([]interface{}, 0)
  103. // ivPars = append(ivPars, registerIds)
  104. // iv := new(fms.ContractInvoice)
  105. // invoiceList, e := iv.List(ivCond, ivPars, "")
  106. // if e != nil {
  107. // resp.FailMsg("获取失败", "获取开票到款列表失败, Err: "+e.Error(), c)
  108. // return
  109. // }
  110. // // 取出开票、到款
  111. // invoiceMap := make(map[int][]*fms.ContractInvoice, 0)
  112. // paymentMap := make(map[int][]*fms.ContractInvoice, 0)
  113. // paymentIds := make([]int, 0)
  114. // for i := range invoiceList {
  115. // if invoiceMap[invoiceList[i].ContractRegisterId] == nil {
  116. // invoiceMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoice, 0)
  117. // }
  118. // if paymentMap[invoiceList[i].ContractRegisterId] == nil {
  119. // paymentMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoice, 0)
  120. // }
  121. // if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  122. // invoiceMap[invoiceList[i].ContractRegisterId] = append(invoiceMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  123. // }
  124. // if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  125. // paymentMap[invoiceList[i].ContractRegisterId] = append(paymentMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  126. // paymentIds = append(paymentIds, invoiceList[i].ContractInvoiceId)
  127. // }
  128. // }
  129. //
  130. // // 合同套餐
  131. // contractServiceCond := `contract_register_id IN ?`
  132. // contractServicePars := make([]interface{}, 0)
  133. // contractServicePars = append(contractServicePars, registerIds)
  134. // contractServiceOB := new(fms.ContractService)
  135. // contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
  136. // if e != nil {
  137. // resp.FailMsg("获取失败", "获取合同套餐列表失败, Err:"+e.Error(), c)
  138. // return
  139. // }
  140. // contractServiceMap := make(map[int][]*fms.ContractService, 0)
  141. // servicesNameMap := make(map[int][]string, 0)
  142. // for i := range contractServiceList {
  143. // if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
  144. // contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
  145. // }
  146. // contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
  147. // servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
  148. // }
  149. //
  150. // // 到款套餐分配
  151. // serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
  152. // if len(paymentIds) > 0 {
  153. // serviceAmountCond := `contract_payment_id IN ?`
  154. // serviceAmountPars := make([]interface{}, 0)
  155. // serviceAmountPars = append(serviceAmountPars, paymentIds)
  156. // serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  157. // serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  158. // if e != nil {
  159. // resp.FailMsg("获取失败", "获取到款套餐分配列表失败, Err:"+e.Error(), c)
  160. // return
  161. // }
  162. // for i := range serviceAmountList {
  163. // if serviceAmountMap[serviceAmountList[i].ContractRegisterId] == nil {
  164. // serviceAmountMap[serviceAmountList[i].ContractRegisterId] = make([]*fms.ContractPaymentServiceAmount, 0)
  165. // }
  166. // serviceAmountMap[serviceAmountList[i].ContractRegisterId] = append(serviceAmountMap[serviceAmountList[i].ContractRegisterId], serviceAmountList[i])
  167. // }
  168. // }
  169. //
  170. // // 整合响应数据
  171. // respList := make([]*fms.InvoicePaymentCensusItem, 0)
  172. // for i := range list {
  173. // v := new(fms.InvoicePaymentCensusItem)
  174. // v.ContractRegisterId = list[i].ContractRegisterId
  175. // v.CompanyName = list[i].CompanyName
  176. // v.NewCompany = list[i].NewCompany
  177. // v.StartDate = list[i].StartDate.Format(utils.FormatDate)
  178. // v.EndDate = list[i].EndDate.Format(utils.FormatDate)
  179. // svList := servicesNameMap[list[i].ContractRegisterId]
  180. // v.ServicesName = strings.Join(svList, ",")
  181. // // 格式化(合并)开票到款数据
  182. // v.InvoicePaymentList = fmsService.MergeInvoiceList2InvoicePaymentCensusInfo(invoiceMap[list[i].ContractRegisterId], paymentMap[list[i].ContractRegisterId],
  183. // contractServiceMap[list[i].ContractRegisterId], serviceAmountMap[list[i].ContractRegisterId])
  184. // respList = append(respList, v)
  185. // }
  186. //
  187. // // 开票到款金额合计(换算后)
  188. // amountTotalCond := `contract_register_id IN ?`
  189. // amountTotalPars := make([]interface{}, 0)
  190. // amountTotalPars = append(amountTotalPars, allRegisterIds)
  191. // amountTotalList, e := fms.GetContractInvoiceAmountTotal(amountTotalCond, amountTotalPars)
  192. // if e != nil {
  193. // resp.FailMsg("获取失败", "获取开票到款金额合计失败, Err:"+e.Error(), c)
  194. // return
  195. // }
  196. // amountTotalMap := make(map[int]float64)
  197. // for i := range amountTotalList {
  198. // amountTotalMap[amountTotalList[i].InvoiceType] = amountTotalList[i].TotalAmount
  199. // }
  200. // invoiceTotal, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", amountTotalMap[fms.ContractInvoiceTypeMake]), 64)
  201. // paymentTotal, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", amountTotalMap[fms.ContractInvoiceTypePay]), 64)
  202. //
  203. // // 分币种金额统计
  204. // currencyOB := new(fms.CurrencyUnit)
  205. // currencyCond := `enable = 1`
  206. // currencyPars := make([]interface{}, 0)
  207. // currencyList, e := currencyOB.List(currencyCond, currencyPars)
  208. // if e != nil {
  209. // resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
  210. // return
  211. // }
  212. // unitMap := make(map[string]string)
  213. // invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  214. // paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  215. // for i := range currencyList {
  216. // unitMap[currencyList[i].Code] = currencyList[i].UnitName
  217. // invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  218. // Name: currencyList[i].Name,
  219. // UnitName: currencyList[i].UnitName,
  220. // Code: currencyList[i].Code,
  221. // FlagImg: currencyList[i].FlagImg,
  222. // })
  223. // paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  224. // Name: currencyList[i].Name,
  225. // UnitName: currencyList[i].UnitName,
  226. // Code: currencyList[i].Code,
  227. // FlagImg: currencyList[i].FlagImg,
  228. // })
  229. // }
  230. // sumList, e := fms.GetInvoiceListCurrencySum(amountTotalCond, amountTotalPars, "currency_unit, invoice_type")
  231. // if e != nil {
  232. // resp.FailMsg("获取失败", "获取商品到款统计货币合计金额失败, Err: "+e.Error(), c)
  233. // return
  234. // }
  235. // invoiceSumMap := make(map[string]float64)
  236. // paymentSumMap := make(map[string]float64)
  237. // for i := range sumList {
  238. // if sumList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  239. // invoiceSumMap[sumList[i].CurrencyUnit] = sumList[i].OriginAmountTotal
  240. // continue
  241. // }
  242. // if sumList[i].InvoiceType == fms.ContractInvoiceTypePay {
  243. // paymentSumMap[sumList[i].CurrencyUnit] = sumList[i].OriginAmountTotal
  244. // }
  245. // }
  246. // for i := range invoiceCurrencyTotals {
  247. // a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
  248. // invoiceCurrencyTotals[i].Amount = a
  249. // }
  250. // for i := range paymentCurrencyTotals {
  251. // a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
  252. // paymentCurrencyTotals[i].Amount = a
  253. // }
  254. //
  255. // results.DataList = respList
  256. // results.InvoiceTotal = invoiceTotal
  257. // results.PaymentTotal = paymentTotal
  258. // results.InvoiceCurrencyTotal = invoiceCurrencyTotals
  259. // results.PaymentCurrencyTotal = paymentCurrencyTotals
  260. // }
  261. //
  262. // // 是否导出
  263. // if req.IsExport == 1 {
  264. // ExportInvoicePaymentCensusList(c, results)
  265. // return
  266. // }
  267. // page.SetTotal(total)
  268. // baseData := new(base.BaseData)
  269. // baseData.SetPage(page)
  270. // baseData.SetList(results)
  271. // resp.OkData("获取成功", baseData, c)
  272. //}
  273. // ExportInvoicePaymentCensusList 导出商品到款统计列表
  274. func ExportInvoicePaymentCensusList(c *gin.Context, results *fms.InvoicePaymentCensusResp) {
  275. list := results.DataList
  276. if len(list) == 0 {
  277. resp.Fail("列表数据为空", c)
  278. return
  279. }
  280. // 生成Excel文件
  281. xlsxFile := xlsx.NewFile()
  282. style := xlsx.NewStyle()
  283. alignment := xlsx.Alignment{
  284. Horizontal: "center",
  285. Vertical: "center",
  286. WrapText: true,
  287. }
  288. style.Alignment = alignment
  289. style.ApplyAlignment = true
  290. sheet, err := xlsxFile.AddSheet("商品到款统计")
  291. if err != nil {
  292. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  293. return
  294. }
  295. _ = sheet.SetColWidth(1, 1, 30)
  296. _ = sheet.SetColWidth(3, 3, 30)
  297. // 前三行-开票金额合计
  298. rowA := sheet.AddRow()
  299. cellAA := rowA.AddCell()
  300. cellAA.SetString(fmt.Sprintf("已开票合计金额(换算后):%.2f(元)", results.InvoiceTotal))
  301. rowBData := "已开票金额:"
  302. for _, v := range results.InvoiceCurrencyTotal {
  303. rowBData += fmt.Sprintf("%s%.2f(%s) ", v.Name, v.Amount, v.UnitName)
  304. }
  305. rowB := sheet.AddRow()
  306. rowB.AddCell().SetString(rowBData)
  307. sheet.AddRow()
  308. // 四至六-到款金额合计
  309. rowD := sheet.AddRow()
  310. cellDA := rowD.AddCell()
  311. cellDA.SetString(fmt.Sprintf("已到款合计金额(换算后):%.2f(元)", results.PaymentTotal))
  312. rowEData := "已到款金额:"
  313. for _, v := range results.PaymentCurrencyTotal {
  314. rowEData += fmt.Sprintf("%s%.2f(%s) ", v.Name, v.Amount, v.UnitName)
  315. }
  316. rowE := sheet.AddRow()
  317. rowE.AddCell().SetString(rowEData)
  318. sheet.AddRow()
  319. // 表头, 套餐动态获取
  320. rowTitle := []string{"序号", "客户名称", "是否新客户", "合同有效期", "开票日", "开票金额", "到款日", "到款金额", "付款方式", "销售",
  321. "销售组别", "销售类型"}
  322. serviceTempCond := ``
  323. serviceTempPars := make([]interface{}, 0)
  324. serviceTempOB := new(fms.ContractServiceTemplate)
  325. serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
  326. if e != nil {
  327. resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
  328. return
  329. }
  330. serviceTempListMap := make(map[int]*fms.ContractServiceTemplate)
  331. var serviceTempShow [] *fms.ContractServiceTemplate
  332. for i := range serviceTempList {
  333. serviceTempListMap[serviceTempList[i].ServiceTemplateId] = serviceTempList[i]
  334. }
  335. for i := range serviceTempList {
  336. if serviceTempList[i].ProductId == 1 {
  337. rowTitle = append(rowTitle, serviceTempList[i].Title)
  338. serviceTempShow = append(serviceTempShow, serviceTempList[i])
  339. }else if serviceTempList[i].ProductId == 2{
  340. if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "权益大套餐" || (serviceTempList[i].Title == "策略" && serviceTempListMap[serviceTempList[i].Pid].Pid==0){
  341. }else if serviceTempList[i].Title == "医药" || serviceTempList[i].Title == "消费" || serviceTempList[i].Title == "科技" || serviceTempList[i].Title == "智造" {
  342. }else {
  343. serviceTempShow = append(serviceTempShow, serviceTempList[i])
  344. }
  345. if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "45万" || serviceTempList[i].Title == "70万" {
  346. continue
  347. }
  348. if serviceTempList[i].Pid > 0 && serviceTempListMap[serviceTempList[i].Pid].Pid > 0 {
  349. continue
  350. }
  351. rowTitle = append(rowTitle, serviceTempList[i].Title)
  352. }
  353. }
  354. hi := 0
  355. insertHi := 0
  356. titleRow := sheet.AddRow()
  357. for i := range rowTitle {
  358. v := titleRow.AddCell()
  359. v.SetString(rowTitle[i])
  360. v.SetStyle(style)
  361. if rowTitle[i] != "权益大套餐" && rowTitle[i] != "医药" && rowTitle[i] != "消费" && rowTitle[i] != "科技" && rowTitle[i] != "智造" {
  362. v.VMerge = 1
  363. hi ++
  364. }else{
  365. if insertHi == 0 {
  366. insertHi = hi
  367. }
  368. v.HMerge = 1
  369. titleRow.AddCell().SetString("")
  370. }
  371. }
  372. // 新增一行放主观和客观
  373. specialRow := sheet.AddRow()
  374. for i:=0; i< hi; i++ {
  375. if i == insertHi {
  376. v1 := specialRow.AddCell()
  377. v1.SetString("45万")
  378. v1.SetStyle(style)
  379. v1 = specialRow.AddCell()
  380. v1.SetString("70万")
  381. v1.SetStyle(style)
  382. for j:=0; j<8;j++ {
  383. if j%2 ==0 {
  384. v1 = specialRow.AddCell()
  385. v1.SetString("主观")
  386. v1.SetStyle(style)
  387. }else{
  388. v1 = specialRow.AddCell()
  389. v1.SetString("客观")
  390. v1.SetStyle(style)
  391. }
  392. }
  393. }else{
  394. v := specialRow.AddCell()
  395. v.SetString("")
  396. }
  397. }
  398. newCompanyMap := map[int]string{0: "否", 1: "是"}
  399. sellerTypeMap := map[int]string{1:"FICC销售", 2:"权益销售"}
  400. for k, v := range list {
  401. dataRow := sheet.AddRow()
  402. // 前四个单元格根据每行开票到款条数向下合并
  403. l := len(v.InvoicePaymentList)
  404. mergeRowNum := l - 1
  405. // 序号
  406. sortNum := k + 1
  407. colA := dataRow.AddCell()
  408. colA.VMerge = mergeRowNum
  409. colA.SetString(fmt.Sprint(sortNum))
  410. // 客户名称
  411. colB := dataRow.AddCell()
  412. colB.VMerge = mergeRowNum
  413. colB.SetString(v.CompanyName)
  414. // 是否新客户
  415. colC := dataRow.AddCell()
  416. colC.VMerge = mergeRowNum
  417. colC.SetString(newCompanyMap[v.NewCompany])
  418. // 合同有效期
  419. colD := dataRow.AddCell()
  420. colD.VMerge = mergeRowNum
  421. colD.SetString(fmt.Sprint(v.StartDate, "至", v.EndDate))
  422. // 开票到款信息
  423. for k2, v2 := range v.InvoicePaymentList {
  424. rowData := []string{
  425. v2.InvoiceDate, // 开票日
  426. fmt.Sprint(v2.InvoiceAmount), // 开票金额
  427. v2.PaymentDate, // 到款日
  428. fmt.Sprint(v2.PaymentAmount), // 到款金额
  429. fms.ContractPaymentPayTypeNameMap[v2.PayType], // 付款方式
  430. v2.SellerName, // 销售
  431. v2.SellerGroupName, // 组别
  432. sellerTypeMap[v2.SellerType], // 销售类型
  433. }
  434. // 套餐金额信息
  435. serviceTempShowAmount := make(map[int]string)
  436. for i := range serviceTempShow {
  437. for s2 := range v2.ServiceAmountList {
  438. item := v2.ServiceAmountList[s2]
  439. if item.ServiceTemplateId == serviceTempShow[i].ServiceTemplateId {
  440. serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount)
  441. break
  442. }else if serviceTempShow[i].Pid == item.ServiceTemplateId {
  443. serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount/2)
  444. break
  445. }
  446. }
  447. }
  448. for i := range serviceTempShow {
  449. sa := ""
  450. if am, ok := serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId]; ok {
  451. sa = am
  452. }
  453. rowData = append(rowData, sa)
  454. }
  455. // 首行开票到款
  456. if k2 == 0 {
  457. for i := range rowData {
  458. dataRow.AddCell().SetString(rowData[i])
  459. }
  460. continue
  461. }
  462. // 其他行开票到款, 加四列空的单元格用于合并
  463. dataRowExtra := sheet.AddRow()
  464. for i := 0; i < 4; i++ {
  465. dataRowExtra.AddCell()
  466. }
  467. for i := range rowData {
  468. dataRowExtra.AddCell().SetString(rowData[i])
  469. }
  470. }
  471. }
  472. // 输出文件
  473. var buffer bytes.Buffer
  474. _ = xlsxFile.Write(&buffer)
  475. content := bytes.NewReader(buffer.Bytes())
  476. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  477. fileName := "商品到款统计_" + randStr + ".xlsx"
  478. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  479. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  480. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  481. }
  482. // List
  483. // @Title 商品到款统计列表
  484. // @Description 商品到款统计列表
  485. // @Param Keyword query string false "关键词"
  486. // @Param SellGroupId query int false "销售组别ID"
  487. // @Param ServiceType query int false "套餐类型"
  488. // @Param StartDate query string false "合同开始日期"
  489. // @Param EndDate query string false "合同结束日期"
  490. // @Param TimeType query int false "时间类型: 1-开票时间; 2-到款时间"
  491. // @Param HasInvoice query string false "是否已开票: 0-否; 1-是; 空-全部"
  492. // @Param HasPayment query string false "是否已到款: 0-否; 1-是; 空-全部"
  493. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  494. // @Param ListParam query int false "套餐类型: 0-全部; 1-ficc; 2-权益"
  495. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'invoice_time':开票日 、 'payment_date':到款日"
  496. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
  497. // @Success 200 {object} fms.ContractRegisterItem
  498. // @router /census/invoice_payment/list [get]
  499. func (ct *InvoicePaymentController) List(c *gin.Context) {
  500. var req fms.InvoicePaymentCensusListReq
  501. if e := c.BindQuery(&req); e != nil {
  502. err, ok := e.(validator.ValidationErrors)
  503. if !ok {
  504. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  505. return
  506. }
  507. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  508. return
  509. }
  510. cond := `1 = 1`
  511. pars := make([]interface{}, 0)
  512. // 客户姓名/销售
  513. if req.Keyword != "" {
  514. kw := "%" + req.Keyword + "%"
  515. cond += ` AND b.company_name LIKE ?`
  516. pars = append(pars, kw)
  517. }
  518. if req.SellerIds != "" {
  519. sellerIds := strings.Split(req.SellerIds, ",")
  520. cond += ` AND (c.seller_id in ?)`
  521. pars = append(pars, sellerIds)
  522. }
  523. // 套餐筛选
  524. if req.ServiceTypes != "" {
  525. serviceTypes := strings.Split(req.ServiceTypes, ",")
  526. tempRegisterIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
  527. if e != nil {
  528. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  529. return
  530. }
  531. if len(tempRegisterIds) > 0 {
  532. cond += ` AND a.register_id IN ?`
  533. pars = append(pars, tempRegisterIds)
  534. } else {
  535. cond += ` AND 1 = 2`
  536. }
  537. }
  538. if req.ListParam == 1{
  539. cond += ` AND a.service_product_id = 1 `
  540. }else if req.ListParam == 2 {
  541. cond += ` AND a.service_product_id = 2 `
  542. }
  543. // 开票到款日期
  544. if req.TimeType != 0 {
  545. if req.TimeType == 1 && req.StartDate != "" && req.EndDate != "" {
  546. st := fmt.Sprint(req.StartDate, " 00:00:00")
  547. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  548. cond += ` AND (c.invoice_time BETWEEN ? AND ?) `
  549. pars = append(pars, st, ed)
  550. }else if req.TimeType == 2 && req.StartDate != "" && req.EndDate != "" {
  551. st := fmt.Sprint(req.StartDate, " 00:00:00")
  552. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  553. cond += ` AND (d.invoice_time BETWEEN ? AND ?) `
  554. pars = append(pars, st, ed)
  555. }else if req.TimeType == 3 && req.StartDate != "" && req.EndDate != "" {
  556. st := fmt.Sprint(req.StartDate, " 00:00:00")
  557. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  558. cond += ` AND (c.invoice_time BETWEEN ? AND ?) AND (d.invoice_time BETWEEN ? AND ?) `
  559. pars = append(pars, st, ed, st, ed)
  560. }
  561. } else if req.StartDate != "" && req.EndDate != "" {
  562. st := fmt.Sprint(req.StartDate, " 00:00:00")
  563. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  564. cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  565. pars = append(pars, st, ed, st, ed)
  566. }
  567. if req.HasInvoice == "1" {
  568. cond += ` AND a.invoice_id > 0 `
  569. }else if req.HasInvoice == "0" {
  570. cond += ` AND a.invoice_id = 0 `
  571. }
  572. if req.HasPayment == "1" {
  573. cond += ` AND a.payment_id > 0 `
  574. }else if req.HasPayment == "0" {
  575. cond += ` AND a.payment_id = 0 `
  576. }
  577. page := new(base.Page)
  578. page.SetPageSize(req.PageSize)
  579. page.SetCurrent(req.Current)
  580. //排序
  581. if req.SortType == "" {
  582. req.SortType = "desc"
  583. }
  584. if req.SortType != "desc" && req.SortType != "asc" {
  585. resp.Fail("排序类型不正确", c)
  586. return
  587. }
  588. if req.SortParam == "" {
  589. page.AddOrderItem(base.OrderItem{Column: "sort_invoice_id", Asc: true})
  590. page.AddOrderItem(base.OrderItem{Column: "sort_payment_id", Asc: false})
  591. page.AddOrderItem(base.OrderItem{Column: "c.invoice_time", Asc: false})
  592. page.AddOrderItem(base.OrderItem{Column: "c.amount", Asc: false})
  593. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
  594. }else if req.SortParam == "invoice_time" {
  595. if req.SortType == "asc" {
  596. page.AddOrderItem(base.OrderItem{Column: "sort_invoice_id", Asc: false})
  597. page.AddOrderItem(base.OrderItem{Column: "c.invoice_time", Asc: true})
  598. page.AddOrderItem(base.OrderItem{Column: "c.amount", Asc: true})
  599. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: true})
  600. }else{
  601. page.AddOrderItem(base.OrderItem{Column: "sort_invoice_id", Asc: true})
  602. page.AddOrderItem(base.OrderItem{Column: "c.invoice_time", Asc: false})
  603. page.AddOrderItem(base.OrderItem{Column: "c.amount", Asc: false})
  604. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
  605. }
  606. }else if req.SortParam == "payment_date" {
  607. if req.SortType == "asc" {
  608. page.AddOrderItem(base.OrderItem{Column: "sort_payment_id", Asc: false})
  609. page.AddOrderItem(base.OrderItem{Column: "d.invoice_time", Asc: true})
  610. page.AddOrderItem(base.OrderItem{Column: "d.amount", Asc: true})
  611. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: true})
  612. }else{
  613. page.AddOrderItem(base.OrderItem{Column: "sort_payment_id", Asc: true})
  614. page.AddOrderItem(base.OrderItem{Column: "d.invoice_time", Asc: false})
  615. page.AddOrderItem(base.OrderItem{Column: "d.amount", Asc: false})
  616. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
  617. }
  618. }else{
  619. resp.Fail("排序字段不正确", c)
  620. return
  621. }
  622. if req.IsExport == 1 {
  623. page.SetPageSize(10000)
  624. page.SetCurrent(1)
  625. }
  626. registerList, total, e := fms.GetInvoicePaymentCensusPageList(page, cond, pars)
  627. if e != nil {
  628. resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
  629. return
  630. }
  631. queryRegisterIds := make([]int, 0)
  632. for i := range registerList {
  633. queryRegisterIds = append(queryRegisterIds, registerList[i].ContractRegisterId)
  634. }
  635. //fmt.Println("queryRegisterIds:",queryRegisterIds)
  636. results := new(fms.InvoicePaymentCensusResp)
  637. if len(queryRegisterIds) > 0 {
  638. // 获取汇总数据IDs, 用于查询合计数据
  639. summaryIdsCond := cond
  640. summaryIdsPars := pars
  641. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
  642. if e != nil {
  643. resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
  644. return
  645. }
  646. //fmt.Println("summaryIds:",summaryIds)
  647. var listErr, totalErr, totalGroupErr error
  648. wg := sync.WaitGroup{}
  649. // 响应列表
  650. respList := make([]*fms.InvoicePaymentCensusItem, 0)
  651. summaryList := make([]*fms.InvoicePaymentSummaryItem, 0)
  652. wg.Add(1)
  653. go func() {
  654. defer wg.Done()
  655. // 获取汇总数据
  656. summaryCond := cond
  657. summaryCond += ` AND a.register_id IN ?`
  658. summaryPars := pars
  659. summaryPars = append(summaryPars, queryRegisterIds)
  660. summaryData, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
  661. if e != nil {
  662. resp.FailMsg("获取失败", "获取商品到款汇总列表失败, Err: "+e.Error(), c)
  663. return
  664. }
  665. summaryList = summaryData
  666. //summaryIds := make([]int, 0)
  667. paymentIds := make([]int, 0)
  668. for i := range summaryList {
  669. //summaryIds = append(summaryIds, summaryList[i].SummaryId)
  670. if summaryList[i].PaymentId > 0 {
  671. paymentIds = append(paymentIds, summaryList[i].PaymentId)
  672. }
  673. }
  674. // 合同套餐
  675. /*contractServiceCond := `contract_register_id IN ?`
  676. contractServicePars := make([]interface{}, 0)
  677. contractServicePars = append(contractServicePars, queryRegisterIds)
  678. contractServiceOB := new(fms.ContractService)
  679. contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
  680. if e != nil {
  681. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  682. return
  683. }
  684. contractServiceMap := make(map[int][]*fms.ContractService, 0)
  685. servicesNameMap := make(map[int][]string, 0)
  686. for i := range contractServiceList {
  687. if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
  688. contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
  689. }
  690. contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
  691. servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
  692. }
  693. */
  694. servicesNameMap, serviceFormatMap, e := fmsService.GetContractServiceNameFormat(queryRegisterIds)
  695. if e != nil {
  696. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  697. return
  698. }
  699. // 到款套餐分配
  700. serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
  701. if len(paymentIds) > 0 {
  702. serviceAmountCond := `contract_payment_id IN ?`
  703. serviceAmountPars := make([]interface{}, 0)
  704. serviceAmountPars = append(serviceAmountPars, paymentIds)
  705. serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  706. serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  707. if e != nil {
  708. listErr = fmt.Errorf("获取到款套餐分配列表失败, Err: %s", e.Error())
  709. return
  710. }
  711. for i := range serviceAmountList {
  712. if serviceAmountMap[serviceAmountList[i].ContractPaymentId] == nil {
  713. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = make([]*fms.ContractPaymentServiceAmount, 0)
  714. }
  715. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = append(serviceAmountMap[serviceAmountList[i].ContractPaymentId], serviceAmountList[i])
  716. }
  717. }
  718. // 重组汇总数据
  719. summaryMap := make(map[int][]*fms.InvoicePaymentCensusInfo)
  720. amountMap := make(map[string]*fms.ContractPaymentServiceAmount)
  721. for i := range summaryList {
  722. v := new(fms.InvoicePaymentCensusInfo)
  723. v.InvoiceId = summaryList[i].InvoiceId
  724. v.InvoiceDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].InvoiceDate)
  725. v.InvoiceAmount = summaryList[i].InvoiceAmount
  726. v.SellerId = summaryList[i].SellerId
  727. v.SellerName = summaryList[i].SellerName
  728. v.SellerGroupId = summaryList[i].SellerGroupId
  729. v.SellerGroupName = summaryList[i].SellerGroupName
  730. v.SellerType = summaryList[i].ServiceProductId
  731. v.PaymentId = summaryList[i].PaymentId
  732. v.PaymentDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].PaymentDate)
  733. v.PaymentAmount = summaryList[i].PaymentAmount
  734. v.PayType = summaryList[i].PayType
  735. // 套餐到款分配
  736. svaList := make([]*fms.ContractPaymentServiceAmountItem, 0)
  737. amountList := serviceAmountMap[summaryList[i].PaymentId]
  738. if amountList != nil {
  739. for i := range amountList {
  740. k := fmt.Sprintf("%d-%d", amountList[i].ContractPaymentId, amountList[i].ServiceTemplateId)
  741. amountMap[k] = amountList[i]
  742. }
  743. }
  744. // 合同对应的所有套餐
  745. svList := serviceFormatMap[summaryList[i].RegisterId]
  746. if svList != nil {
  747. for ii := range svList {
  748. vv := new(fms.ContractPaymentServiceAmountItem)
  749. vv.ServiceTemplateId = svList[ii].ServiceTemplateId
  750. vv.ServiceTemplateName = svList[ii].FormatTitle
  751. vv.ServiceTemplatePid = svList[ii].ServiceTemplatePid
  752. k2 := fmt.Sprintf("%d-%d", summaryList[i].PaymentId, svList[ii].ServiceTemplateId)
  753. a := amountMap[k2]
  754. if a != nil {
  755. vv.ContractPaymentServiceAmountId = a.ContractPaymentServiceAmountId
  756. vv.ContractPaymentId = a.ContractPaymentId
  757. vv.Amount = a.Amount
  758. }
  759. svaList = append(svaList, vv)
  760. }
  761. }
  762. v.ServiceAmountList = svaList
  763. summaryMap[summaryList[i].SummaryId] = append(summaryMap[summaryList[i].SummaryId], v)
  764. }
  765. // 响应列表
  766. for i := range registerList {
  767. v := new(fms.InvoicePaymentCensusItem)
  768. v.SummaryId = registerList[i].SummaryId
  769. v.ContractRegisterId = registerList[i].ContractRegisterId
  770. v.CompanyName = registerList[i].CompanyName
  771. v.NewCompany = registerList[i].NewCompany
  772. v.StartDate = utils.TimeTransferString(utils.FormatDate, registerList[i].StartDate)
  773. v.EndDate = utils.TimeTransferString(utils.FormatDate, registerList[i].EndDate)
  774. v.ServicesName = servicesNameMap[registerList[i].ContractRegisterId]
  775. v.InvoicePaymentList = summaryMap[registerList[i].SummaryId]
  776. v.ContractType = registerList[i].ContractType
  777. respList = append(respList, v)
  778. }
  779. }()
  780. // 开票到款金额合计(换算后)
  781. var invoiceTotal, paymentTotal float64
  782. wg.Add(1)
  783. go func() {
  784. defer wg.Done()
  785. if len(summaryIds) == 0 {
  786. return
  787. }
  788. amountTotalCond := `a.id IN ?`
  789. amountTotalPars := make([]interface{}, 0)
  790. amountTotalPars = append(amountTotalPars, summaryIds)
  791. invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
  792. if e != nil {
  793. totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
  794. return
  795. }
  796. invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
  797. paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
  798. if e != nil {
  799. totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
  800. return
  801. }
  802. paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
  803. }()
  804. // 分币种金额统计
  805. invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  806. paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  807. wg.Add(1)
  808. go func() {
  809. defer wg.Done()
  810. currencyOB := new(fms.CurrencyUnit)
  811. currencyCond := `enable = 1`
  812. currencyPars := make([]interface{}, 0)
  813. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  814. if e != nil {
  815. totalGroupErr = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
  816. return
  817. }
  818. unitMap := make(map[string]string)
  819. for i := range currencyList {
  820. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  821. invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  822. Name: currencyList[i].Name,
  823. UnitName: currencyList[i].UnitName,
  824. Code: currencyList[i].Code,
  825. FlagImg: currencyList[i].FlagImg,
  826. })
  827. paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  828. Name: currencyList[i].Name,
  829. UnitName: currencyList[i].UnitName,
  830. Code: currencyList[i].Code,
  831. FlagImg: currencyList[i].FlagImg,
  832. })
  833. }
  834. if len(summaryIds) == 0 {
  835. return
  836. }
  837. totalGroupCond := `a.id IN ?`
  838. totalGroupPars := make([]interface{}, 0)
  839. totalGroupPars = append(totalGroupPars, summaryIds)
  840. invoiceSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 1)
  841. if e != nil {
  842. totalGroupErr = fmt.Errorf("获取汇总货币合计开票金额失败, Err: %s", e.Error())
  843. return
  844. }
  845. paymentSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 2)
  846. if e != nil {
  847. totalGroupErr = fmt.Errorf("获取汇总货币合计到款金额失败, Err: %s", e.Error())
  848. return
  849. }
  850. invoiceSumMap := make(map[string]float64)
  851. paymentSumMap := make(map[string]float64)
  852. for i := range invoiceSumGroup {
  853. invoiceSumMap[invoiceSumGroup[i].CurrencyUnit] = invoiceSumGroup[i].OriginAmountTotal
  854. continue
  855. }
  856. for i := range paymentSumGroup {
  857. paymentSumMap[paymentSumGroup[i].CurrencyUnit] = paymentSumGroup[i].OriginAmountTotal
  858. continue
  859. }
  860. for i := range invoiceCurrencyTotals {
  861. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
  862. invoiceCurrencyTotals[i].Amount = a
  863. }
  864. for i := range paymentCurrencyTotals {
  865. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
  866. paymentCurrencyTotals[i].Amount = a
  867. }
  868. }()
  869. wg.Wait()
  870. if listErr != nil {
  871. resp.FailMsg("获取失败", listErr.Error(), c)
  872. return
  873. }
  874. if totalErr != nil {
  875. resp.FailMsg("获取失败", totalErr.Error(), c)
  876. return
  877. }
  878. if totalGroupErr != nil {
  879. resp.FailMsg("获取失败", totalGroupErr.Error(), c)
  880. return
  881. }
  882. results.DataList = respList
  883. results.InvoiceTotal = invoiceTotal
  884. results.PaymentTotal = paymentTotal
  885. results.InvoiceCurrencyTotal = invoiceCurrencyTotals
  886. results.PaymentCurrencyTotal = paymentCurrencyTotals
  887. }
  888. // 是否导出
  889. if req.IsExport == 1 {
  890. ExportInvoicePaymentCensusList(c, results)
  891. return
  892. }
  893. page.SetTotal(total)
  894. baseData := new(base.BaseData)
  895. baseData.SetPage(page)
  896. baseData.SetList(results)
  897. resp.OkData("获取成功", baseData, c)
  898. }
  899. // List
  900. // @Title 商品到款统计列表
  901. // @Description 商品到款统计列表
  902. // @Param Keyword query string false "关键词"
  903. // @Param SellGroupId query int false "销售组别ID"
  904. // @Param ServiceType query int false "套餐类型"
  905. // @Param StartDate query string false "合同开始日期"
  906. // @Param EndDate query string false "合同结束日期"
  907. // @Param TimeType query int false "时间类型: 1-开票时间; 2-到款时间"
  908. // @Param HasInvoice query string false "是否已开票: 0-否; 1-是; 空-全部"
  909. // @Param HasPayment query string false "是否已到款: 0-否; 1-是; 空-全部"
  910. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  911. // @Param ListParam query int false "套餐类型: 0-全部; 1-ficc; 2-权益"
  912. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'invoice_time':开票日 、 'payment_date':到款日"
  913. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
  914. // @Success 200 {object} fms.ContractRegisterItem
  915. // @router /census/invoice_payment/not_invoice/list [get]
  916. func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
  917. var req fms.InvoicePaymentCensusListReq
  918. if e := c.BindQuery(&req); e != nil {
  919. err, ok := e.(validator.ValidationErrors)
  920. if !ok {
  921. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  922. return
  923. }
  924. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  925. return
  926. }
  927. cond := `1 = 1`
  928. pars := make([]interface{}, 0)
  929. // 客户姓名/销售
  930. if req.Keyword != "" {
  931. kw := "%" + req.Keyword + "%"
  932. cond += ` AND b.company_name LIKE ?`
  933. pars = append(pars, kw)
  934. }
  935. if req.SellerIds != "" {
  936. sellerIds := strings.Split(req.SellerIds, ",")
  937. cond += ` AND (c.seller_id in ?)`
  938. pars = append(pars, sellerIds)
  939. }
  940. // 套餐筛选
  941. if req.ServiceTypes != "" {
  942. serviceTypes := strings.Split(req.ServiceTypes, ",")
  943. tempRegisterIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
  944. if e != nil {
  945. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  946. return
  947. }
  948. if len(tempRegisterIds) > 0 {
  949. cond += ` AND a.register_id IN ?`
  950. pars = append(pars, tempRegisterIds)
  951. } else {
  952. cond += ` AND 1 = 2`
  953. }
  954. }
  955. if req.ListParam == 1{
  956. cond += ` AND a.service_product_id = 1 `
  957. }else if req.ListParam == 2 {
  958. cond += ` AND a.service_product_id = 2 `
  959. }
  960. // 开票到款日期
  961. //if req.TimeType != 0 {
  962. // if req.TimeType == 1 && req.StartDate != "" && req.EndDate != "" {
  963. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  964. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  965. // cond += ` AND (c.invoice_time BETWEEN ? AND ?) `
  966. // pars = append(pars, st, ed)
  967. // }else if req.TimeType == 2 && req.StartDate != "" && req.EndDate != "" {
  968. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  969. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  970. // cond += ` AND (d.invoice_time BETWEEN ? AND ?) `
  971. // pars = append(pars, st, ed)
  972. // }else if req.TimeType == 3 && req.StartDate != "" && req.EndDate != "" {
  973. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  974. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  975. // cond += ` AND (c.invoice_time BETWEEN ? AND ?) AND (d.invoice_time BETWEEN ? AND ?) `
  976. // pars = append(pars, st, ed, st, ed)
  977. // }
  978. //} else if req.StartDate != "" && req.EndDate != "" {
  979. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  980. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  981. // cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  982. // pars = append(pars, st, ed, st, ed)
  983. //}
  984. //if req.HasInvoice == "1" {
  985. // cond += ` AND a.invoice_id > 0 `
  986. //}else if req.HasInvoice == "0" {
  987. // cond += ` AND a.invoice_id = 0 `
  988. //}
  989. //
  990. //if req.HasPayment == "1" {
  991. // cond += ` AND a.payment_id > 0 `
  992. //}else if req.HasPayment == "0" {
  993. // cond += ` AND a.payment_id = 0 `
  994. //}
  995. page := new(base.Page)
  996. page.SetPageSize(req.PageSize)
  997. page.SetCurrent(req.Current)
  998. //排序
  999. if req.SortType == "" {
  1000. req.SortType = "desc"
  1001. }
  1002. if req.SortType != "desc" && req.SortType != "asc" {
  1003. resp.Fail("排序类型不正确", c)
  1004. return
  1005. }
  1006. if req.SortParam == "" {
  1007. page.AddOrderItem(base.OrderItem{Column: "b.start_date", Asc: false})
  1008. }else if req.SortParam == "start_date" {
  1009. if req.SortType == "asc" {
  1010. page.AddOrderItem(base.OrderItem{Column: "b.start_date", Asc: true})
  1011. }else{
  1012. page.AddOrderItem(base.OrderItem{Column: "b.start_date", Asc: false})
  1013. }
  1014. }else if req.SortParam == "end_date" {
  1015. if req.SortType == "asc" {
  1016. page.AddOrderItem(base.OrderItem{Column: "b.end_date", Asc: true})
  1017. }else{
  1018. page.AddOrderItem(base.OrderItem{Column: "b.end_date", Asc: false})
  1019. }
  1020. }else{
  1021. resp.Fail("排序字段不正确", c)
  1022. return
  1023. }
  1024. if req.IsExport == 1 {
  1025. page.SetPageSize(10000)
  1026. page.SetCurrent(1)
  1027. }
  1028. registerList, total, e := fms.GetInvoiceCensusPageList(page, cond, pars)
  1029. if e != nil {
  1030. resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
  1031. return
  1032. }
  1033. queryRegisterIds := make([]int, 0)
  1034. for i := range registerList {
  1035. queryRegisterIds = append(queryRegisterIds, registerList[i].ContractRegisterId)
  1036. }
  1037. //fmt.Println("queryRegisterIds:",queryRegisterIds)
  1038. results := new(fms.InvoicePaymentCensusResp)
  1039. if len(queryRegisterIds) > 0 {
  1040. // 获取汇总数据IDs, 用于查询合计数据
  1041. summaryIdsCond := cond
  1042. summaryIdsPars := pars
  1043. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
  1044. if e != nil {
  1045. resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
  1046. return
  1047. }
  1048. //fmt.Println("summaryIds:",summaryIds)
  1049. var listErr, totalErr, totalGroupErr error
  1050. wg := sync.WaitGroup{}
  1051. // 响应列表
  1052. respList := make([]*fms.InvoicePaymentCensusItem, 0)
  1053. summaryList := make([]*fms.InvoicePaymentSummaryItem, 0)
  1054. wg.Add(1)
  1055. go func() {
  1056. defer wg.Done()
  1057. // 获取汇总数据
  1058. summaryCond := cond
  1059. summaryCond += ` AND a.register_id IN ?`
  1060. summaryPars := pars
  1061. summaryPars = append(summaryPars, queryRegisterIds)
  1062. summaryData, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
  1063. if e != nil {
  1064. resp.FailMsg("获取失败", "获取商品到款汇总列表失败, Err: "+e.Error(), c)
  1065. return
  1066. }
  1067. summaryList = summaryData
  1068. //summaryIds := make([]int, 0)
  1069. paymentIds := make([]int, 0)
  1070. for i := range summaryList {
  1071. //summaryIds = append(summaryIds, summaryList[i].SummaryId)
  1072. if summaryList[i].PaymentId > 0 {
  1073. paymentIds = append(paymentIds, summaryList[i].PaymentId)
  1074. }
  1075. }
  1076. // 合同套餐
  1077. /*contractServiceCond := `contract_register_id IN ?`
  1078. contractServicePars := make([]interface{}, 0)
  1079. contractServicePars = append(contractServicePars, queryRegisterIds)
  1080. contractServiceOB := new(fms.ContractService)
  1081. contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
  1082. if e != nil {
  1083. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  1084. return
  1085. }
  1086. contractServiceMap := make(map[int][]*fms.ContractService, 0)
  1087. servicesNameMap := make(map[int][]string, 0)
  1088. for i := range contractServiceList {
  1089. if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
  1090. contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
  1091. }
  1092. contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
  1093. servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
  1094. }
  1095. */
  1096. servicesNameMap, serviceFormatMap, e := fmsService.GetContractServiceNameFormat(queryRegisterIds)
  1097. if e != nil {
  1098. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  1099. return
  1100. }
  1101. // 到款套餐分配
  1102. serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
  1103. if len(paymentIds) > 0 {
  1104. serviceAmountCond := `contract_payment_id IN ?`
  1105. serviceAmountPars := make([]interface{}, 0)
  1106. serviceAmountPars = append(serviceAmountPars, paymentIds)
  1107. serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  1108. serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  1109. if e != nil {
  1110. listErr = fmt.Errorf("获取到款套餐分配列表失败, Err: %s", e.Error())
  1111. return
  1112. }
  1113. for i := range serviceAmountList {
  1114. if serviceAmountMap[serviceAmountList[i].ContractPaymentId] == nil {
  1115. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = make([]*fms.ContractPaymentServiceAmount, 0)
  1116. }
  1117. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = append(serviceAmountMap[serviceAmountList[i].ContractPaymentId], serviceAmountList[i])
  1118. }
  1119. }
  1120. // 重组汇总数据
  1121. summaryMap := make(map[int][]*fms.InvoicePaymentCensusInfo)
  1122. amountMap := make(map[string]*fms.ContractPaymentServiceAmount)
  1123. for i := range summaryList {
  1124. v := new(fms.InvoicePaymentCensusInfo)
  1125. v.InvoiceId = summaryList[i].InvoiceId
  1126. v.InvoiceDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].InvoiceDate)
  1127. v.InvoiceAmount = summaryList[i].InvoiceAmount
  1128. v.SellerId = summaryList[i].SellerId
  1129. v.SellerName = summaryList[i].SellerName
  1130. v.SellerGroupId = summaryList[i].SellerGroupId
  1131. v.SellerGroupName = summaryList[i].SellerGroupName
  1132. v.SellerType = summaryList[i].ServiceProductId
  1133. v.PaymentId = summaryList[i].PaymentId
  1134. v.PaymentDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].PaymentDate)
  1135. v.PaymentAmount = summaryList[i].PaymentAmount
  1136. v.PayType = summaryList[i].PayType
  1137. // 套餐到款分配
  1138. svaList := make([]*fms.ContractPaymentServiceAmountItem, 0)
  1139. amountList := serviceAmountMap[summaryList[i].PaymentId]
  1140. if amountList != nil {
  1141. for i := range amountList {
  1142. k := fmt.Sprintf("%d-%d", amountList[i].ContractPaymentId, amountList[i].ServiceTemplateId)
  1143. amountMap[k] = amountList[i]
  1144. }
  1145. }
  1146. // 合同对应的所有套餐
  1147. svList := serviceFormatMap[summaryList[i].RegisterId]
  1148. if svList != nil {
  1149. for ii := range svList {
  1150. vv := new(fms.ContractPaymentServiceAmountItem)
  1151. vv.ServiceTemplateId = svList[ii].ServiceTemplateId
  1152. vv.ServiceTemplateName = svList[ii].FormatTitle
  1153. vv.ServiceTemplatePid = svList[ii].ServiceTemplatePid
  1154. k2 := fmt.Sprintf("%d-%d", summaryList[i].PaymentId, svList[ii].ServiceTemplateId)
  1155. a := amountMap[k2]
  1156. if a != nil {
  1157. vv.ContractPaymentServiceAmountId = a.ContractPaymentServiceAmountId
  1158. vv.ContractPaymentId = a.ContractPaymentId
  1159. vv.Amount = a.Amount
  1160. }
  1161. svaList = append(svaList, vv)
  1162. }
  1163. }
  1164. v.ServiceAmountList = svaList
  1165. summaryMap[summaryList[i].SummaryId] = append(summaryMap[summaryList[i].SummaryId], v)
  1166. }
  1167. // 响应列表
  1168. for i := range registerList {
  1169. v := new(fms.InvoicePaymentCensusItem)
  1170. v.SummaryId = registerList[i].SummaryId
  1171. v.ContractRegisterId = registerList[i].ContractRegisterId
  1172. v.CompanyName = registerList[i].CompanyName
  1173. v.NewCompany = registerList[i].NewCompany
  1174. v.StartDate = utils.TimeTransferString(utils.FormatDate, registerList[i].StartDate)
  1175. v.EndDate = utils.TimeTransferString(utils.FormatDate, registerList[i].EndDate)
  1176. v.ServicesName = servicesNameMap[registerList[i].ContractRegisterId]
  1177. v.InvoicePaymentList = summaryMap[registerList[i].SummaryId]
  1178. v.ContractType = registerList[i].ContractType
  1179. respList = append(respList, v)
  1180. }
  1181. }()
  1182. // 开票到款金额合计(换算后)
  1183. var invoiceTotal, paymentTotal float64
  1184. wg.Add(1)
  1185. go func() {
  1186. defer wg.Done()
  1187. if len(summaryIds) == 0 {
  1188. return
  1189. }
  1190. amountTotalCond := `a.id IN ?`
  1191. amountTotalPars := make([]interface{}, 0)
  1192. amountTotalPars = append(amountTotalPars, summaryIds)
  1193. invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
  1194. if e != nil {
  1195. totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
  1196. return
  1197. }
  1198. invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
  1199. paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
  1200. if e != nil {
  1201. totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
  1202. return
  1203. }
  1204. paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
  1205. }()
  1206. // 分币种金额统计
  1207. invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  1208. paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  1209. wg.Add(1)
  1210. go func() {
  1211. defer wg.Done()
  1212. currencyOB := new(fms.CurrencyUnit)
  1213. currencyCond := `enable = 1`
  1214. currencyPars := make([]interface{}, 0)
  1215. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  1216. if e != nil {
  1217. totalGroupErr = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
  1218. return
  1219. }
  1220. unitMap := make(map[string]string)
  1221. for i := range currencyList {
  1222. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  1223. invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  1224. Name: currencyList[i].Name,
  1225. UnitName: currencyList[i].UnitName,
  1226. Code: currencyList[i].Code,
  1227. FlagImg: currencyList[i].FlagImg,
  1228. })
  1229. paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  1230. Name: currencyList[i].Name,
  1231. UnitName: currencyList[i].UnitName,
  1232. Code: currencyList[i].Code,
  1233. FlagImg: currencyList[i].FlagImg,
  1234. })
  1235. }
  1236. if len(summaryIds) == 0 {
  1237. return
  1238. }
  1239. totalGroupCond := `a.id IN ?`
  1240. totalGroupPars := make([]interface{}, 0)
  1241. totalGroupPars = append(totalGroupPars, summaryIds)
  1242. invoiceSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 1)
  1243. if e != nil {
  1244. totalGroupErr = fmt.Errorf("获取汇总货币合计开票金额失败, Err: %s", e.Error())
  1245. return
  1246. }
  1247. paymentSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 2)
  1248. if e != nil {
  1249. totalGroupErr = fmt.Errorf("获取汇总货币合计到款金额失败, Err: %s", e.Error())
  1250. return
  1251. }
  1252. invoiceSumMap := make(map[string]float64)
  1253. paymentSumMap := make(map[string]float64)
  1254. for i := range invoiceSumGroup {
  1255. invoiceSumMap[invoiceSumGroup[i].CurrencyUnit] = invoiceSumGroup[i].OriginAmountTotal
  1256. continue
  1257. }
  1258. for i := range paymentSumGroup {
  1259. paymentSumMap[paymentSumGroup[i].CurrencyUnit] = paymentSumGroup[i].OriginAmountTotal
  1260. continue
  1261. }
  1262. for i := range invoiceCurrencyTotals {
  1263. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
  1264. invoiceCurrencyTotals[i].Amount = a
  1265. }
  1266. for i := range paymentCurrencyTotals {
  1267. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
  1268. paymentCurrencyTotals[i].Amount = a
  1269. }
  1270. }()
  1271. wg.Wait()
  1272. if listErr != nil {
  1273. resp.FailMsg("获取失败", listErr.Error(), c)
  1274. return
  1275. }
  1276. if totalErr != nil {
  1277. resp.FailMsg("获取失败", totalErr.Error(), c)
  1278. return
  1279. }
  1280. if totalGroupErr != nil {
  1281. resp.FailMsg("获取失败", totalGroupErr.Error(), c)
  1282. return
  1283. }
  1284. results.DataList = respList
  1285. results.InvoiceTotal = invoiceTotal
  1286. results.PaymentTotal = paymentTotal
  1287. results.InvoiceCurrencyTotal = invoiceCurrencyTotals
  1288. results.PaymentCurrencyTotal = paymentCurrencyTotals
  1289. }
  1290. // 是否导出
  1291. if req.IsExport == 1 {
  1292. ExportInvoicePaymentCensusList(c, results)
  1293. return
  1294. }
  1295. page.SetTotal(total)
  1296. baseData := new(base.BaseData)
  1297. baseData.SetPage(page)
  1298. baseData.SetList(results)
  1299. resp.OkData("获取成功", baseData, c)
  1300. }
  1301. // List
  1302. // @Title 商品到款统计列表
  1303. // @Description 商品到款统计列表
  1304. // @Param Keyword query string false "关键词"
  1305. // @Param SellGroupId query int false "销售组别ID"
  1306. // @Param ServiceType query int false "套餐类型"
  1307. // @Param StartDate query string false "合同开始日期"
  1308. // @Param EndDate query string false "合同结束日期"
  1309. // @Param TimeType query int false "时间类型: 1-开票时间; 2-到款时间"
  1310. // @Param HasInvoice query string false "是否已开票: 0-否; 1-是; 空-全部"
  1311. // @Param HasPayment query string false "是否已到款: 0-否; 1-是; 空-全部"
  1312. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  1313. // @Param ListParam query int false "套餐类型: 0-全部; 1-ficc; 2-权益"
  1314. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'invoice_time':开票日 、 'payment_date':到款日"
  1315. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
  1316. // @Success 200 {object} fms.ContractRegisterItem
  1317. // @router /census/invoice_payment/not_payment/list [get]
  1318. func (ct *InvoicePaymentController) NotPaymentList(c *gin.Context) {
  1319. var req fms.InvoicePaymentCensusListReq
  1320. if e := c.BindQuery(&req); e != nil {
  1321. err, ok := e.(validator.ValidationErrors)
  1322. if !ok {
  1323. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  1324. return
  1325. }
  1326. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1327. return
  1328. }
  1329. cond := `1 = 1`
  1330. pars := make([]interface{}, 0)
  1331. // 客户姓名/销售
  1332. if req.Keyword != "" {
  1333. kw := "%" + req.Keyword + "%"
  1334. cond += ` AND b.company_name LIKE ?`
  1335. pars = append(pars, kw)
  1336. }
  1337. if req.SellerIds != "" {
  1338. sellerIds := strings.Split(req.SellerIds, ",")
  1339. cond += ` AND (c.seller_id in ?)`
  1340. pars = append(pars, sellerIds)
  1341. }
  1342. // 套餐筛选
  1343. if req.ServiceTypes != "" {
  1344. serviceTypes := strings.Split(req.ServiceTypes, ",")
  1345. tempRegisterIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
  1346. if e != nil {
  1347. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  1348. return
  1349. }
  1350. if len(tempRegisterIds) > 0 {
  1351. cond += ` AND a.register_id IN ?`
  1352. pars = append(pars, tempRegisterIds)
  1353. } else {
  1354. cond += ` AND 1 = 2`
  1355. }
  1356. }
  1357. if req.ListParam == 1{
  1358. cond += ` AND a.service_product_id = 1 `
  1359. }else if req.ListParam == 2 {
  1360. cond += ` AND a.service_product_id = 2 `
  1361. }
  1362. // 开票到款日期
  1363. //if req.TimeType != 0 {
  1364. // if req.TimeType == 1 && req.StartDate != "" && req.EndDate != "" {
  1365. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  1366. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1367. // cond += ` AND (c.invoice_time BETWEEN ? AND ?) `
  1368. // pars = append(pars, st, ed)
  1369. // }else if req.TimeType == 2 && req.StartDate != "" && req.EndDate != "" {
  1370. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  1371. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1372. // cond += ` AND (d.invoice_time BETWEEN ? AND ?) `
  1373. // pars = append(pars, st, ed)
  1374. // }else if req.TimeType == 3 && req.StartDate != "" && req.EndDate != "" {
  1375. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  1376. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1377. // cond += ` AND (c.invoice_time BETWEEN ? AND ?) AND (d.invoice_time BETWEEN ? AND ?) `
  1378. // pars = append(pars, st, ed, st, ed)
  1379. // }
  1380. //} else if req.StartDate != "" && req.EndDate != "" {
  1381. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  1382. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1383. // cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  1384. // pars = append(pars, st, ed, st, ed)
  1385. //}
  1386. //if req.HasInvoice == "1" {
  1387. // cond += ` AND a.invoice_id > 0 `
  1388. //}else if req.HasInvoice == "0" {
  1389. // cond += ` AND a.invoice_id = 0 `
  1390. //}
  1391. //
  1392. //if req.HasPayment == "1" {
  1393. // cond += ` AND a.payment_id > 0 `
  1394. //}else if req.HasPayment == "0" {
  1395. // cond += ` AND a.payment_id = 0 `
  1396. //}
  1397. page := new(base.Page)
  1398. page.SetPageSize(req.PageSize)
  1399. page.SetCurrent(req.Current)
  1400. //排序
  1401. if req.SortType == "" {
  1402. req.SortType = "desc"
  1403. }
  1404. if req.SortType != "desc" && req.SortType != "asc" {
  1405. resp.Fail("排序类型不正确", c)
  1406. return
  1407. }
  1408. if req.SortType == "asc" {
  1409. page.AddOrderItem(base.OrderItem{Column: "c.invoice_time", Asc: true})
  1410. }else{
  1411. page.AddOrderItem(base.OrderItem{Column: "c.invoice_time", Asc: false})
  1412. }
  1413. if req.IsExport == 1 {
  1414. page.SetPageSize(10000)
  1415. page.SetCurrent(1)
  1416. }
  1417. registerList, total, e := fms.GetNoPaymentCensusPageList(page, cond, pars)
  1418. if e != nil {
  1419. resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
  1420. return
  1421. }
  1422. queryRegisterIds := make([]int, 0)
  1423. for i := range registerList {
  1424. queryRegisterIds = append(queryRegisterIds, registerList[i].ContractRegisterId)
  1425. }
  1426. //fmt.Println("queryRegisterIds:",queryRegisterIds)
  1427. results := new(fms.InvoicePaymentCensusResp)
  1428. if len(queryRegisterIds) > 0 {
  1429. // 获取汇总数据IDs, 用于查询合计数据
  1430. summaryIdsCond := cond
  1431. summaryIdsPars := pars
  1432. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
  1433. if e != nil {
  1434. resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
  1435. return
  1436. }
  1437. //fmt.Println("summaryIds:",summaryIds)
  1438. var listErr, totalErr, totalGroupErr error
  1439. wg := sync.WaitGroup{}
  1440. // 响应列表
  1441. respList := make([]*fms.InvoicePaymentCensusItem, 0)
  1442. summaryList := make([]*fms.InvoicePaymentSummaryItem, 0)
  1443. wg.Add(1)
  1444. go func() {
  1445. defer wg.Done()
  1446. // 获取汇总数据
  1447. summaryCond := cond
  1448. summaryCond += ` AND a.register_id IN ?`
  1449. summaryPars := pars
  1450. summaryPars = append(summaryPars, queryRegisterIds)
  1451. summaryData, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
  1452. if e != nil {
  1453. resp.FailMsg("获取失败", "获取商品到款汇总列表失败, Err: "+e.Error(), c)
  1454. return
  1455. }
  1456. summaryList = summaryData
  1457. //summaryIds := make([]int, 0)
  1458. paymentIds := make([]int, 0)
  1459. for i := range summaryList {
  1460. //summaryIds = append(summaryIds, summaryList[i].SummaryId)
  1461. if summaryList[i].PaymentId > 0 {
  1462. paymentIds = append(paymentIds, summaryList[i].PaymentId)
  1463. }
  1464. }
  1465. // 合同套餐
  1466. /*contractServiceCond := `contract_register_id IN ?`
  1467. contractServicePars := make([]interface{}, 0)
  1468. contractServicePars = append(contractServicePars, queryRegisterIds)
  1469. contractServiceOB := new(fms.ContractService)
  1470. contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
  1471. if e != nil {
  1472. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  1473. return
  1474. }
  1475. contractServiceMap := make(map[int][]*fms.ContractService, 0)
  1476. servicesNameMap := make(map[int][]string, 0)
  1477. for i := range contractServiceList {
  1478. if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
  1479. contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
  1480. }
  1481. contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
  1482. servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
  1483. }
  1484. */
  1485. servicesNameMap, serviceFormatMap, e := fmsService.GetContractServiceNameFormat(queryRegisterIds)
  1486. if e != nil {
  1487. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  1488. return
  1489. }
  1490. // 到款套餐分配
  1491. serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
  1492. if len(paymentIds) > 0 {
  1493. serviceAmountCond := `contract_payment_id IN ?`
  1494. serviceAmountPars := make([]interface{}, 0)
  1495. serviceAmountPars = append(serviceAmountPars, paymentIds)
  1496. serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  1497. serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  1498. if e != nil {
  1499. listErr = fmt.Errorf("获取到款套餐分配列表失败, Err: %s", e.Error())
  1500. return
  1501. }
  1502. for i := range serviceAmountList {
  1503. if serviceAmountMap[serviceAmountList[i].ContractPaymentId] == nil {
  1504. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = make([]*fms.ContractPaymentServiceAmount, 0)
  1505. }
  1506. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = append(serviceAmountMap[serviceAmountList[i].ContractPaymentId], serviceAmountList[i])
  1507. }
  1508. }
  1509. // 重组汇总数据
  1510. summaryMap := make(map[int][]*fms.InvoicePaymentCensusInfo)
  1511. amountMap := make(map[string]*fms.ContractPaymentServiceAmount)
  1512. for i := range summaryList {
  1513. v := new(fms.InvoicePaymentCensusInfo)
  1514. v.InvoiceId = summaryList[i].InvoiceId
  1515. v.InvoiceDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].InvoiceDate)
  1516. v.InvoiceAmount = summaryList[i].InvoiceAmount
  1517. v.SellerId = summaryList[i].SellerId
  1518. v.SellerName = summaryList[i].SellerName
  1519. v.SellerGroupId = summaryList[i].SellerGroupId
  1520. v.SellerGroupName = summaryList[i].SellerGroupName
  1521. v.SellerType = summaryList[i].ServiceProductId
  1522. v.PaymentId = summaryList[i].PaymentId
  1523. v.PaymentDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].PaymentDate)
  1524. v.PaymentAmount = summaryList[i].PaymentAmount
  1525. v.PayType = summaryList[i].PayType
  1526. // 套餐到款分配
  1527. svaList := make([]*fms.ContractPaymentServiceAmountItem, 0)
  1528. amountList := serviceAmountMap[summaryList[i].PaymentId]
  1529. if amountList != nil {
  1530. for i := range amountList {
  1531. k := fmt.Sprintf("%d-%d", amountList[i].ContractPaymentId, amountList[i].ServiceTemplateId)
  1532. amountMap[k] = amountList[i]
  1533. }
  1534. }
  1535. // 合同对应的所有套餐
  1536. svList := serviceFormatMap[summaryList[i].RegisterId]
  1537. if svList != nil {
  1538. for ii := range svList {
  1539. vv := new(fms.ContractPaymentServiceAmountItem)
  1540. vv.ServiceTemplateId = svList[ii].ServiceTemplateId
  1541. vv.ServiceTemplateName = svList[ii].FormatTitle
  1542. vv.ServiceTemplatePid = svList[ii].ServiceTemplatePid
  1543. k2 := fmt.Sprintf("%d-%d", summaryList[i].PaymentId, svList[ii].ServiceTemplateId)
  1544. a := amountMap[k2]
  1545. if a != nil {
  1546. vv.ContractPaymentServiceAmountId = a.ContractPaymentServiceAmountId
  1547. vv.ContractPaymentId = a.ContractPaymentId
  1548. vv.Amount = a.Amount
  1549. }
  1550. svaList = append(svaList, vv)
  1551. }
  1552. }
  1553. v.ServiceAmountList = svaList
  1554. summaryMap[summaryList[i].SummaryId] = append(summaryMap[summaryList[i].SummaryId], v)
  1555. }
  1556. // 响应列表
  1557. for i := range registerList {
  1558. v := new(fms.InvoicePaymentCensusItem)
  1559. v.SummaryId = registerList[i].SummaryId
  1560. v.ContractRegisterId = registerList[i].ContractRegisterId
  1561. v.CompanyName = registerList[i].CompanyName
  1562. v.NewCompany = registerList[i].NewCompany
  1563. v.StartDate = utils.TimeTransferString(utils.FormatDate, registerList[i].StartDate)
  1564. v.EndDate = utils.TimeTransferString(utils.FormatDate, registerList[i].EndDate)
  1565. v.ServicesName = servicesNameMap[registerList[i].ContractRegisterId]
  1566. v.InvoicePaymentList = summaryMap[registerList[i].SummaryId]
  1567. v.ContractType = registerList[i].ContractType
  1568. respList = append(respList, v)
  1569. }
  1570. }()
  1571. // 开票到款金额合计(换算后)
  1572. var invoiceTotal, paymentTotal float64
  1573. wg.Add(1)
  1574. go func() {
  1575. defer wg.Done()
  1576. if len(summaryIds) == 0 {
  1577. return
  1578. }
  1579. amountTotalCond := `a.id IN ?`
  1580. amountTotalPars := make([]interface{}, 0)
  1581. amountTotalPars = append(amountTotalPars, summaryIds)
  1582. invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
  1583. if e != nil {
  1584. totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
  1585. return
  1586. }
  1587. invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
  1588. paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
  1589. if e != nil {
  1590. totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
  1591. return
  1592. }
  1593. paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
  1594. }()
  1595. // 分币种金额统计
  1596. invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  1597. paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  1598. wg.Add(1)
  1599. go func() {
  1600. defer wg.Done()
  1601. currencyOB := new(fms.CurrencyUnit)
  1602. currencyCond := `enable = 1`
  1603. currencyPars := make([]interface{}, 0)
  1604. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  1605. if e != nil {
  1606. totalGroupErr = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
  1607. return
  1608. }
  1609. unitMap := make(map[string]string)
  1610. for i := range currencyList {
  1611. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  1612. invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  1613. Name: currencyList[i].Name,
  1614. UnitName: currencyList[i].UnitName,
  1615. Code: currencyList[i].Code,
  1616. FlagImg: currencyList[i].FlagImg,
  1617. })
  1618. paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  1619. Name: currencyList[i].Name,
  1620. UnitName: currencyList[i].UnitName,
  1621. Code: currencyList[i].Code,
  1622. FlagImg: currencyList[i].FlagImg,
  1623. })
  1624. }
  1625. if len(summaryIds) == 0 {
  1626. return
  1627. }
  1628. totalGroupCond := `a.id IN ?`
  1629. totalGroupPars := make([]interface{}, 0)
  1630. totalGroupPars = append(totalGroupPars, summaryIds)
  1631. invoiceSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 1)
  1632. if e != nil {
  1633. totalGroupErr = fmt.Errorf("获取汇总货币合计开票金额失败, Err: %s", e.Error())
  1634. return
  1635. }
  1636. paymentSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 2)
  1637. if e != nil {
  1638. totalGroupErr = fmt.Errorf("获取汇总货币合计到款金额失败, Err: %s", e.Error())
  1639. return
  1640. }
  1641. invoiceSumMap := make(map[string]float64)
  1642. paymentSumMap := make(map[string]float64)
  1643. for i := range invoiceSumGroup {
  1644. invoiceSumMap[invoiceSumGroup[i].CurrencyUnit] = invoiceSumGroup[i].OriginAmountTotal
  1645. continue
  1646. }
  1647. for i := range paymentSumGroup {
  1648. paymentSumMap[paymentSumGroup[i].CurrencyUnit] = paymentSumGroup[i].OriginAmountTotal
  1649. continue
  1650. }
  1651. for i := range invoiceCurrencyTotals {
  1652. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
  1653. invoiceCurrencyTotals[i].Amount = a
  1654. }
  1655. for i := range paymentCurrencyTotals {
  1656. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
  1657. paymentCurrencyTotals[i].Amount = a
  1658. }
  1659. }()
  1660. wg.Wait()
  1661. if listErr != nil {
  1662. resp.FailMsg("获取失败", listErr.Error(), c)
  1663. return
  1664. }
  1665. if totalErr != nil {
  1666. resp.FailMsg("获取失败", totalErr.Error(), c)
  1667. return
  1668. }
  1669. if totalGroupErr != nil {
  1670. resp.FailMsg("获取失败", totalGroupErr.Error(), c)
  1671. return
  1672. }
  1673. results.DataList = respList
  1674. results.InvoiceTotal = invoiceTotal
  1675. results.PaymentTotal = paymentTotal
  1676. results.InvoiceCurrencyTotal = invoiceCurrencyTotals
  1677. results.PaymentCurrencyTotal = paymentCurrencyTotals
  1678. }
  1679. // 是否导出
  1680. if req.IsExport == 1 {
  1681. ExportInvoicePaymentCensusList(c, results)
  1682. return
  1683. }
  1684. page.SetTotal(total)
  1685. baseData := new(base.BaseData)
  1686. baseData.SetPage(page)
  1687. baseData.SetList(results)
  1688. resp.OkData("获取成功", baseData, c)
  1689. }