invoice_payment.go 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321
  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 ? OR d.seller_id in ?)`
  521. pars = append(pars, sellerIds, 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. v.ActualPayCompanies = registerList[i].ActualPayCompanies
  778. respList = append(respList, v)
  779. }
  780. }()
  781. // 开票到款金额合计(换算后)
  782. var invoiceTotal, paymentTotal,amountTotal float64
  783. wg.Add(1)
  784. go func() {
  785. defer wg.Done()
  786. if len(summaryIds) == 0 {
  787. return
  788. }
  789. amountTotalCond := `a.id IN ?`
  790. amountTotalPars := make([]interface{}, 0)
  791. amountTotalPars = append(amountTotalPars, summaryIds)
  792. invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
  793. if e != nil {
  794. totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
  795. return
  796. }
  797. invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
  798. paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
  799. if e != nil {
  800. totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
  801. return
  802. }
  803. paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
  804. amountCond := `a.id IN ? AND (a.invoice_id <> 0 OR (a.payment_id <> 0 AND a.invoice_id =0))`
  805. amountPars := make([]interface{}, 0)
  806. amountPars = append(amountTotalPars, summaryIds)
  807. amountSum, e := fms.GetContractSummaryInvoicePaymentAmount(amountCond, amountPars)
  808. if e != nil {
  809. totalErr = fmt.Errorf("获取汇总金额合计失败, Err: %s", e.Error())
  810. return
  811. }
  812. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  813. }()
  814. // 分币种金额统计
  815. invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  816. paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  817. wg.Add(1)
  818. go func() {
  819. defer wg.Done()
  820. currencyOB := new(fms.CurrencyUnit)
  821. currencyCond := `enable = 1`
  822. currencyPars := make([]interface{}, 0)
  823. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  824. if e != nil {
  825. totalGroupErr = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
  826. return
  827. }
  828. unitMap := make(map[string]string)
  829. for i := range currencyList {
  830. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  831. invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  832. Name: currencyList[i].Name,
  833. UnitName: currencyList[i].UnitName,
  834. Code: currencyList[i].Code,
  835. FlagImg: currencyList[i].FlagImg,
  836. })
  837. paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  838. Name: currencyList[i].Name,
  839. UnitName: currencyList[i].UnitName,
  840. Code: currencyList[i].Code,
  841. FlagImg: currencyList[i].FlagImg,
  842. })
  843. }
  844. if len(summaryIds) == 0 {
  845. return
  846. }
  847. totalGroupCond := `a.id IN ?`
  848. totalGroupPars := make([]interface{}, 0)
  849. totalGroupPars = append(totalGroupPars, summaryIds)
  850. invoiceSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 1)
  851. if e != nil {
  852. totalGroupErr = fmt.Errorf("获取汇总货币合计开票金额失败, Err: %s", e.Error())
  853. return
  854. }
  855. paymentSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 2)
  856. if e != nil {
  857. totalGroupErr = fmt.Errorf("获取汇总货币合计到款金额失败, Err: %s", e.Error())
  858. return
  859. }
  860. invoiceSumMap := make(map[string]float64)
  861. paymentSumMap := make(map[string]float64)
  862. for i := range invoiceSumGroup {
  863. invoiceSumMap[invoiceSumGroup[i].CurrencyUnit] = invoiceSumGroup[i].OriginAmountTotal
  864. continue
  865. }
  866. for i := range paymentSumGroup {
  867. paymentSumMap[paymentSumGroup[i].CurrencyUnit] = paymentSumGroup[i].OriginAmountTotal
  868. continue
  869. }
  870. for i := range invoiceCurrencyTotals {
  871. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
  872. invoiceCurrencyTotals[i].Amount = a
  873. }
  874. for i := range paymentCurrencyTotals {
  875. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
  876. paymentCurrencyTotals[i].Amount = a
  877. }
  878. }()
  879. wg.Wait()
  880. if listErr != nil {
  881. resp.FailMsg("获取失败", listErr.Error(), c)
  882. return
  883. }
  884. if totalErr != nil {
  885. resp.FailMsg("获取失败", totalErr.Error(), c)
  886. return
  887. }
  888. if totalGroupErr != nil {
  889. resp.FailMsg("获取失败", totalGroupErr.Error(), c)
  890. return
  891. }
  892. results.DataList = respList
  893. results.InvoiceTotal = invoiceTotal
  894. results.PaymentTotal = paymentTotal
  895. results.AmountTotal = amountTotal
  896. results.InvoiceCurrencyTotal = invoiceCurrencyTotals
  897. results.PaymentCurrencyTotal = paymentCurrencyTotals
  898. }
  899. // 是否导出
  900. if req.IsExport == 1 {
  901. ExportInvoicePaymentCensusList(c, results)
  902. return
  903. }
  904. page.SetTotal(total)
  905. baseData := new(base.BaseData)
  906. baseData.SetPage(page)
  907. baseData.SetList(results)
  908. resp.OkData("获取成功", baseData, c)
  909. }
  910. // List
  911. // @Title 未开票统计列表
  912. // @Description 未开票统计列表
  913. // @Param Keyword query string false "关键词"
  914. // @Param SellGroupId query int false "销售组别ID"
  915. // @Param ServiceType query int false "套餐类型"
  916. // @Param StartDate query string false "合同开始日期"
  917. // @Param EndDate query string false "合同结束日期"
  918. // @Param TimeType query int false "时间类型: 1-开票时间; 2-到款时间"
  919. // @Param HasInvoice query string false "是否已开票: 0-否; 1-是; 空-全部"
  920. // @Param HasPayment query string false "是否已到款: 0-否; 1-是; 空-全部"
  921. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  922. // @Param ListParam query int false "套餐类型: 0-全部; 1-ficc; 2-权益"
  923. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'start_date':开票日 、 'end_date':到款日"
  924. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
  925. // @Success 200 {object} fms.ContractRegisterItem
  926. // @router /census/invoice_payment/not_invoice/list [get]
  927. func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
  928. var req fms.InvoicePaymentCensusListReq
  929. if e := c.BindQuery(&req); e != nil {
  930. err, ok := e.(validator.ValidationErrors)
  931. if !ok {
  932. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  933. return
  934. }
  935. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  936. return
  937. }
  938. cond := `1 = 1`
  939. pars := make([]interface{}, 0)
  940. // 客户姓名/合同编号
  941. if req.Keyword != "" {
  942. kw := "%" + req.Keyword + "%"
  943. cond += ` AND (b.company_name LIKE ? OR b.contract_code LIKE ?)`
  944. pars = append(pars, kw, kw)
  945. }
  946. if req.SellerIds != "" {
  947. sellerIds := strings.Split(req.SellerIds, ",")
  948. cond += ` AND (c.seller_id in ?)`
  949. pars = append(pars, sellerIds)
  950. }
  951. // 套餐筛选
  952. if req.ServiceTypes != "" {
  953. serviceTypes := strings.Split(req.ServiceTypes, ",")
  954. tempRegisterIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
  955. if e != nil {
  956. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  957. return
  958. }
  959. if len(tempRegisterIds) > 0 {
  960. cond += ` AND a.register_id IN ?`
  961. pars = append(pars, tempRegisterIds)
  962. } else {
  963. cond += ` AND 1 = 2`
  964. }
  965. }
  966. if req.ListParam == 1{
  967. cond += ` AND a.service_product_id = 1 `
  968. }else if req.ListParam == 2 {
  969. cond += ` AND a.service_product_id = 2 `
  970. }
  971. // 开票到款日期
  972. //if req.TimeType != 0 {
  973. // if req.TimeType == 1 && req.StartDate != "" && req.EndDate != "" {
  974. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  975. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  976. // cond += ` AND (c.invoice_time BETWEEN ? AND ?) `
  977. // pars = append(pars, st, ed)
  978. // }else if req.TimeType == 2 && 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 (d.invoice_time BETWEEN ? AND ?) `
  982. // pars = append(pars, st, ed)
  983. // }else if req.TimeType == 3 && req.StartDate != "" && req.EndDate != "" {
  984. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  985. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  986. // cond += ` AND (c.invoice_time BETWEEN ? AND ?) AND (d.invoice_time BETWEEN ? AND ?) `
  987. // pars = append(pars, st, ed, st, ed)
  988. // }
  989. //} else if req.StartDate != "" && req.EndDate != "" {
  990. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  991. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  992. // cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  993. // pars = append(pars, st, ed, st, ed)
  994. //}
  995. //if req.HasInvoice == "1" {
  996. // cond += ` AND a.invoice_id > 0 `
  997. //}else if req.HasInvoice == "0" {
  998. // cond += ` AND a.invoice_id = 0 `
  999. //}
  1000. //
  1001. //if req.HasPayment == "1" {
  1002. // cond += ` AND a.payment_id > 0 `
  1003. //}else if req.HasPayment == "0" {
  1004. // cond += ` AND a.payment_id = 0 `
  1005. //}
  1006. page := new(base.Page)
  1007. page.SetPageSize(req.PageSize)
  1008. page.SetCurrent(req.Current)
  1009. //排序
  1010. if req.SortType == "" {
  1011. req.SortType = "desc"
  1012. }
  1013. if req.SortType != "desc" && req.SortType != "asc" {
  1014. resp.Fail("排序类型不正确", c)
  1015. return
  1016. }
  1017. if req.SortParam == "" {
  1018. page.AddOrderItem(base.OrderItem{Column: "e.start_date", Asc: false})
  1019. }else if req.SortParam == "start_date" {
  1020. if req.SortType == "asc" {
  1021. page.AddOrderItem(base.OrderItem{Column: "e.start_date", Asc: true})
  1022. }else{
  1023. page.AddOrderItem(base.OrderItem{Column: "e.start_date", Asc: false})
  1024. }
  1025. }else if req.SortParam == "end_date" {
  1026. if req.SortType == "asc" {
  1027. page.AddOrderItem(base.OrderItem{Column: "e.end_date", Asc: true})
  1028. }else{
  1029. page.AddOrderItem(base.OrderItem{Column: "e.end_date", Asc: false})
  1030. }
  1031. }else{
  1032. resp.Fail("排序字段不正确", c)
  1033. return
  1034. }
  1035. if req.IsExport == 1 {
  1036. page.SetPageSize(10000)
  1037. page.SetCurrent(1)
  1038. }
  1039. registerList, total, e := fms.GetInvoiceCensusPageList(page, cond, pars)
  1040. if e != nil {
  1041. resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
  1042. return
  1043. }
  1044. queryRegisterIds := make([]int, 0)
  1045. for i := range registerList {
  1046. queryRegisterIds = append(queryRegisterIds, registerList[i].ContractRegisterId)
  1047. }
  1048. //fmt.Println("queryRegisterIds:",queryRegisterIds)
  1049. results := new(fms.NotInvoicePaymentCensusResp)
  1050. if len(queryRegisterIds) > 0 {
  1051. // 获取汇总数据IDs, 用于查询合计数据
  1052. summaryIdsCond := cond
  1053. summaryIdsPars := pars
  1054. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
  1055. if e != nil {
  1056. resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
  1057. return
  1058. }
  1059. //fmt.Println("summaryIds:",summaryIds)
  1060. var listErr, totalErr, totalGroupErr error
  1061. wg := sync.WaitGroup{}
  1062. // 响应列表
  1063. respList := make([]*fms.InvoicePaymentCensusItem, 0)
  1064. summaryList := make([]*fms.InvoicePaymentSummaryItem, 0)
  1065. wg.Add(1)
  1066. go func() {
  1067. defer wg.Done()
  1068. // 获取汇总数据
  1069. summaryCond := cond
  1070. summaryCond += ` AND a.register_id IN ?`
  1071. summaryPars := pars
  1072. summaryPars = append(summaryPars, queryRegisterIds)
  1073. summaryData, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
  1074. if e != nil {
  1075. resp.FailMsg("获取失败", "获取商品到款汇总列表失败, Err: "+e.Error(), c)
  1076. return
  1077. }
  1078. summaryList = summaryData
  1079. //summaryIds := make([]int, 0)
  1080. paymentIds := make([]int, 0)
  1081. for i := range summaryList {
  1082. //summaryIds = append(summaryIds, summaryList[i].SummaryId)
  1083. if summaryList[i].PaymentId > 0 {
  1084. paymentIds = append(paymentIds, summaryList[i].PaymentId)
  1085. }
  1086. }
  1087. // 合同套餐
  1088. /*contractServiceCond := `contract_register_id IN ?`
  1089. contractServicePars := make([]interface{}, 0)
  1090. contractServicePars = append(contractServicePars, queryRegisterIds)
  1091. contractServiceOB := new(fms.ContractService)
  1092. contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
  1093. if e != nil {
  1094. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  1095. return
  1096. }
  1097. contractServiceMap := make(map[int][]*fms.ContractService, 0)
  1098. servicesNameMap := make(map[int][]string, 0)
  1099. for i := range contractServiceList {
  1100. if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
  1101. contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
  1102. }
  1103. contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
  1104. servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
  1105. }
  1106. */
  1107. servicesNameMap, serviceFormatMap, e := fmsService.GetContractServiceNameFormat(queryRegisterIds)
  1108. if e != nil {
  1109. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  1110. return
  1111. }
  1112. // 到款套餐分配
  1113. serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
  1114. if len(paymentIds) > 0 {
  1115. serviceAmountCond := `contract_payment_id IN ?`
  1116. serviceAmountPars := make([]interface{}, 0)
  1117. serviceAmountPars = append(serviceAmountPars, paymentIds)
  1118. serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  1119. serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  1120. if e != nil {
  1121. listErr = fmt.Errorf("获取到款套餐分配列表失败, Err: %s", e.Error())
  1122. return
  1123. }
  1124. for i := range serviceAmountList {
  1125. if serviceAmountMap[serviceAmountList[i].ContractPaymentId] == nil {
  1126. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = make([]*fms.ContractPaymentServiceAmount, 0)
  1127. }
  1128. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = append(serviceAmountMap[serviceAmountList[i].ContractPaymentId], serviceAmountList[i])
  1129. }
  1130. }
  1131. // 重组汇总数据
  1132. summaryMap := make(map[int][]*fms.InvoicePaymentCensusInfo)
  1133. amountMap := make(map[string]*fms.ContractPaymentServiceAmount)
  1134. for i := range summaryList {
  1135. v := new(fms.InvoicePaymentCensusInfo)
  1136. v.InvoiceId = summaryList[i].InvoiceId
  1137. v.InvoiceDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].InvoiceDate)
  1138. v.InvoiceAmount = summaryList[i].InvoiceAmount
  1139. v.SellerId = summaryList[i].SellerId
  1140. v.SellerName = summaryList[i].SellerName
  1141. v.SellerGroupId = summaryList[i].SellerGroupId
  1142. v.SellerGroupName = summaryList[i].SellerGroupName
  1143. v.SellerType = summaryList[i].ServiceProductId
  1144. v.PaymentId = summaryList[i].PaymentId
  1145. v.PaymentDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].PaymentDate)
  1146. v.PaymentAmount = summaryList[i].PaymentAmount
  1147. v.PayType = summaryList[i].PayType
  1148. // 套餐到款分配
  1149. svaList := make([]*fms.ContractPaymentServiceAmountItem, 0)
  1150. amountList := serviceAmountMap[summaryList[i].PaymentId]
  1151. if amountList != nil {
  1152. for i := range amountList {
  1153. k := fmt.Sprintf("%d-%d", amountList[i].ContractPaymentId, amountList[i].ServiceTemplateId)
  1154. amountMap[k] = amountList[i]
  1155. }
  1156. }
  1157. // 合同对应的所有套餐
  1158. svList := serviceFormatMap[summaryList[i].RegisterId]
  1159. if svList != nil {
  1160. for ii := range svList {
  1161. vv := new(fms.ContractPaymentServiceAmountItem)
  1162. vv.ServiceTemplateId = svList[ii].ServiceTemplateId
  1163. vv.ServiceTemplateName = svList[ii].FormatTitle
  1164. vv.ServiceTemplatePid = svList[ii].ServiceTemplatePid
  1165. k2 := fmt.Sprintf("%d-%d", summaryList[i].PaymentId, svList[ii].ServiceTemplateId)
  1166. a := amountMap[k2]
  1167. if a != nil {
  1168. vv.ContractPaymentServiceAmountId = a.ContractPaymentServiceAmountId
  1169. vv.ContractPaymentId = a.ContractPaymentId
  1170. vv.Amount = a.Amount
  1171. }
  1172. svaList = append(svaList, vv)
  1173. }
  1174. }
  1175. v.ServiceAmountList = svaList
  1176. summaryMap[summaryList[i].SummaryId] = append(summaryMap[summaryList[i].SummaryId], v)
  1177. }
  1178. // 货币列表
  1179. currencyOB := new(fms.CurrencyUnit)
  1180. currencyCond := `enable = 1`
  1181. currencyPars := make([]interface{}, 0)
  1182. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  1183. if e != nil {
  1184. resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
  1185. return
  1186. }
  1187. unitMap := make(map[string]string)
  1188. for i := range currencyList {
  1189. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  1190. }
  1191. // 响应列表
  1192. for i := range registerList {
  1193. v := new(fms.InvoicePaymentCensusItem)
  1194. v.SummaryId = registerList[i].SummaryId
  1195. v.ContractRegisterId = registerList[i].ContractRegisterId
  1196. v.CompanyName = registerList[i].CompanyName
  1197. v.NewCompany = registerList[i].NewCompany
  1198. v.StartDate = utils.TimeTransferString(utils.FormatDate, registerList[i].StartDate)
  1199. v.EndDate = utils.TimeTransferString(utils.FormatDate, registerList[i].EndDate)
  1200. v.ServicesName = servicesNameMap[registerList[i].ContractRegisterId]
  1201. v.InvoicePaymentList = summaryMap[registerList[i].SummaryId]
  1202. v.ContractType = registerList[i].ContractType
  1203. v.ContractCode = registerList[i].ContractCode
  1204. v.ContractAmount = registerList[i].ContractAmount
  1205. v.UnitName = unitMap[registerList[i].CurrencyUnit]
  1206. v.ProductIds = registerList[i].ProductIds
  1207. v.NotInvoiceTotal = registerList[i].ContractAmount - registerList[i].InvoicedAmount
  1208. respList = append(respList, v)
  1209. }
  1210. }()
  1211. // 开票到款金额合计(换算后)
  1212. var invoiceTotal, paymentTotal, contractAmount float64
  1213. wg.Add(1)
  1214. go func() {
  1215. defer wg.Done()
  1216. if len(summaryIds) == 0 {
  1217. return
  1218. }
  1219. amountTotalCond := `a.id IN ?`
  1220. amountTotalPars := make([]interface{}, 0)
  1221. amountTotalPars = append(amountTotalPars, summaryIds)
  1222. invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
  1223. if e != nil {
  1224. totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
  1225. return
  1226. }
  1227. invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
  1228. paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
  1229. if e != nil {
  1230. totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
  1231. return
  1232. }
  1233. paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
  1234. }()
  1235. // 分币种金额统计
  1236. invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  1237. paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  1238. contractAmountCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  1239. wg.Add(1)
  1240. go func() {
  1241. defer wg.Done()
  1242. currencyOB := new(fms.CurrencyUnit)
  1243. currencyCond := `enable = 1`
  1244. currencyPars := make([]interface{}, 0)
  1245. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  1246. if e != nil {
  1247. totalGroupErr = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
  1248. return
  1249. }
  1250. unitMap := make(map[string]string)
  1251. for i := range currencyList {
  1252. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  1253. invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  1254. Name: currencyList[i].Name,
  1255. UnitName: currencyList[i].UnitName,
  1256. Code: currencyList[i].Code,
  1257. FlagImg: currencyList[i].FlagImg,
  1258. })
  1259. paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  1260. Name: currencyList[i].Name,
  1261. UnitName: currencyList[i].UnitName,
  1262. Code: currencyList[i].Code,
  1263. FlagImg: currencyList[i].FlagImg,
  1264. })
  1265. contractAmountCurrencyTotals = append(contractAmountCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  1266. Name: currencyList[i].Name,
  1267. UnitName: currencyList[i].UnitName,
  1268. Code: currencyList[i].Code,
  1269. FlagImg: currencyList[i].FlagImg,
  1270. })
  1271. }
  1272. if len(summaryIds) == 0 {
  1273. return
  1274. }
  1275. totalGroupCond := `a.id IN ?`
  1276. totalGroupPars := make([]interface{}, 0)
  1277. totalGroupPars = append(totalGroupPars, summaryIds)
  1278. invoiceSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 1)
  1279. if e != nil {
  1280. totalGroupErr = fmt.Errorf("获取汇总货币合计开票金额失败, Err: %s", e.Error())
  1281. return
  1282. }
  1283. paymentSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 2)
  1284. if e != nil {
  1285. totalGroupErr = fmt.Errorf("获取汇总货币合计到款金额失败, Err: %s", e.Error())
  1286. return
  1287. }
  1288. invoiceSumMap := make(map[string]float64)
  1289. paymentSumMap := make(map[string]float64)
  1290. contractSumMap := make(map[string]float64)
  1291. for i := range invoiceSumGroup {
  1292. invoiceSumMap[invoiceSumGroup[i].CurrencyUnit] = invoiceSumGroup[i].OriginAmountTotal
  1293. continue
  1294. }
  1295. for i := range paymentSumGroup {
  1296. paymentSumMap[paymentSumGroup[i].CurrencyUnit] = paymentSumGroup[i].OriginAmountTotal
  1297. continue
  1298. }
  1299. for i := range invoiceCurrencyTotals {
  1300. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
  1301. invoiceCurrencyTotals[i].Amount = a
  1302. }
  1303. for i := range paymentCurrencyTotals {
  1304. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
  1305. paymentCurrencyTotals[i].Amount = a
  1306. }
  1307. contractRegisters, err := fms.GetContractInfoByRegisterIds(queryRegisterIds)
  1308. if err != nil {
  1309. resp.FailMsg("查询合同信息失败", "查询合同信息失败, Err: "+err.Error(), c)
  1310. return
  1311. }
  1312. for _, v := range contractRegisters {
  1313. if v.RMBRate == 0.0{
  1314. v.RMBRate = 1
  1315. }
  1316. amount,_ := strconv.ParseFloat(fmt.Sprintf("%.2f",v.ContractAmount / v.RMBRate), 64)
  1317. contractAmount += amount
  1318. contractSumMap[v.CurrencyUnit] = v.ContractAmount
  1319. }
  1320. for _, v := range contractAmountCurrencyTotals {
  1321. v.Amount = contractSumMap[v.Code] - invoiceSumMap[v.Code]
  1322. }
  1323. }()
  1324. wg.Wait()
  1325. if listErr != nil {
  1326. resp.FailMsg("获取失败", listErr.Error(), c)
  1327. return
  1328. }
  1329. if totalErr != nil {
  1330. resp.FailMsg("获取失败", totalErr.Error(), c)
  1331. return
  1332. }
  1333. if totalGroupErr != nil {
  1334. resp.FailMsg("获取失败", totalGroupErr.Error(), c)
  1335. return
  1336. }
  1337. results.DataList = respList
  1338. results.InvoiceTotal = invoiceTotal
  1339. results.PaymentTotal = paymentTotal
  1340. results.NotInvoiceTotal = contractAmount-invoiceTotal
  1341. results.InvoiceCurrencyTotal = invoiceCurrencyTotals
  1342. results.PaymentCurrencyTotal = paymentCurrencyTotals
  1343. results.NotInvoiceCurrencyTotal = contractAmountCurrencyTotals
  1344. }
  1345. // 是否导出
  1346. if req.IsExport == 1 {
  1347. ExportNotInvoiceCensusList(c, results)
  1348. return
  1349. }
  1350. page.SetTotal(total)
  1351. baseData := new(base.BaseData)
  1352. baseData.SetPage(page)
  1353. baseData.SetList(results)
  1354. resp.OkData("获取成功", baseData, c)
  1355. }
  1356. // List
  1357. // @Title 开票未到款统计列表
  1358. // @Description 开票未到款统计列表
  1359. // @Param Keyword query string false "关键词"
  1360. // @Param SellGroupId query int false "销售组别ID"
  1361. // @Param ServiceType query int false "套餐类型"
  1362. // @Param StartDate query string false "合同开始日期"
  1363. // @Param EndDate query string false "合同结束日期"
  1364. // @Param TimeType query int false "时间类型: 1-开票时间; 2-到款时间"
  1365. // @Param HasInvoice query string false "是否已开票: 0-否; 1-是; 空-全部"
  1366. // @Param HasPayment query string false "是否已到款: 0-否; 1-是; 空-全部"
  1367. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  1368. // @Param ListParam query int false "套餐类型: 0-全部; 1-ficc; 2-权益"
  1369. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'invoice_time':开票日 、 'payment_date':到款日"
  1370. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
  1371. // @Success 200 {object} fms.ContractRegisterItem
  1372. // @router /census/invoice_payment/not_payment/list [get]
  1373. func (ct *InvoicePaymentController) NotPaymentList(c *gin.Context) {
  1374. var req fms.InvoicePaymentCensusListReq
  1375. if e := c.BindQuery(&req); e != nil {
  1376. err, ok := e.(validator.ValidationErrors)
  1377. if !ok {
  1378. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  1379. return
  1380. }
  1381. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1382. return
  1383. }
  1384. cond := `1 = 1`
  1385. pars := make([]interface{}, 0)
  1386. // 客户姓名/销售
  1387. if req.Keyword != "" {
  1388. kw := "%" + req.Keyword + "%"
  1389. cond += ` AND (b.company_name LIKE ? OR b.contract_code LIKE ?)`
  1390. pars = append(pars, kw, kw)
  1391. }
  1392. if req.SellerIds != "" {
  1393. sellerIds := strings.Split(req.SellerIds, ",")
  1394. cond += ` AND (c.seller_id in ?)`
  1395. pars = append(pars, sellerIds)
  1396. }
  1397. // 套餐筛选
  1398. if req.ServiceTypes != "" {
  1399. serviceTypes := strings.Split(req.ServiceTypes, ",")
  1400. tempRegisterIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
  1401. if e != nil {
  1402. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  1403. return
  1404. }
  1405. if len(tempRegisterIds) > 0 {
  1406. cond += ` AND a.register_id IN ?`
  1407. pars = append(pars, tempRegisterIds)
  1408. } else {
  1409. cond += ` AND 1 = 2`
  1410. }
  1411. }
  1412. if req.ListParam == 1{
  1413. cond += ` AND a.service_product_id = 1 `
  1414. }else if req.ListParam == 2 {
  1415. cond += ` AND a.service_product_id = 2 `
  1416. }
  1417. // 开票到款日期
  1418. //if req.TimeType != 0 {
  1419. // if req.TimeType == 1 && req.StartDate != "" && req.EndDate != "" {
  1420. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  1421. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1422. // cond += ` AND (c.invoice_time BETWEEN ? AND ?) `
  1423. // pars = append(pars, st, ed)
  1424. // }else if req.TimeType == 2 && req.StartDate != "" && req.EndDate != "" {
  1425. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  1426. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1427. // cond += ` AND (d.invoice_time BETWEEN ? AND ?) `
  1428. // pars = append(pars, st, ed)
  1429. // }else if req.TimeType == 3 && req.StartDate != "" && req.EndDate != "" {
  1430. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  1431. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1432. // cond += ` AND (c.invoice_time BETWEEN ? AND ?) AND (d.invoice_time BETWEEN ? AND ?) `
  1433. // pars = append(pars, st, ed, st, ed)
  1434. // }
  1435. //} else if req.StartDate != "" && req.EndDate != "" {
  1436. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  1437. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1438. // cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  1439. // pars = append(pars, st, ed, st, ed)
  1440. //}
  1441. //if req.HasInvoice == "1" {
  1442. // cond += ` AND a.invoice_id > 0 `
  1443. //}else if req.HasInvoice == "0" {
  1444. // cond += ` AND a.invoice_id = 0 `
  1445. //}
  1446. //
  1447. //if req.HasPayment == "1" {
  1448. // cond += ` AND a.payment_id > 0 `
  1449. //}else if req.HasPayment == "0" {
  1450. // cond += ` AND a.payment_id = 0 `
  1451. //}
  1452. page := new(base.Page)
  1453. page.SetPageSize(req.PageSize)
  1454. page.SetCurrent(req.Current)
  1455. //排序
  1456. if req.SortType == "" {
  1457. req.SortType = "desc"
  1458. }
  1459. if req.SortType != "desc" && req.SortType != "asc" {
  1460. resp.Fail("排序类型不正确", c)
  1461. return
  1462. }
  1463. if req.SortType == "asc" {
  1464. page.AddOrderItem(base.OrderItem{Column: "e.invoice_time", Asc: true})
  1465. }else{
  1466. page.AddOrderItem(base.OrderItem{Column: "e.invoice_time", Asc: false})
  1467. }
  1468. if req.IsExport == 1 {
  1469. page.SetPageSize(10000)
  1470. page.SetCurrent(1)
  1471. }
  1472. registerList, total, e := fms.GetNoPaymentCensusPageList(page, cond, pars)
  1473. if e != nil {
  1474. resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
  1475. return
  1476. }
  1477. queryRegisterIds := make([]int, 0)
  1478. for i := range registerList {
  1479. queryRegisterIds = append(queryRegisterIds, registerList[i].ContractRegisterId)
  1480. }
  1481. //fmt.Println("queryRegisterIds:",queryRegisterIds)
  1482. results := new(fms.NotInvoicePaymentCensusResp)
  1483. if len(queryRegisterIds) > 0 {
  1484. // 获取汇总数据IDs, 用于查询合计数据
  1485. summaryIdsCond := cond
  1486. summaryIdsPars := pars
  1487. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
  1488. if e != nil {
  1489. resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
  1490. return
  1491. }
  1492. //fmt.Println("summaryIds:",summaryIds)
  1493. var listErr, totalErr, totalGroupErr error
  1494. wg := sync.WaitGroup{}
  1495. // 响应列表
  1496. respList := make([]*fms.InvoicePaymentCensusItem, 0)
  1497. summaryList := make([]*fms.InvoicePaymentSummaryItem, 0)
  1498. wg.Add(1)
  1499. go func() {
  1500. defer wg.Done()
  1501. // 获取汇总数据
  1502. summaryCond := cond
  1503. summaryCond += ` AND a.register_id IN ?`
  1504. summaryPars := pars
  1505. summaryPars = append(summaryPars, queryRegisterIds)
  1506. summaryData, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
  1507. if e != nil {
  1508. resp.FailMsg("获取失败", "获取商品到款汇总列表失败, Err: "+e.Error(), c)
  1509. return
  1510. }
  1511. summaryList = summaryData
  1512. //summaryIds := make([]int, 0)
  1513. paymentIds := make([]int, 0)
  1514. for i := range summaryList {
  1515. //summaryIds = append(summaryIds, summaryList[i].SummaryId)
  1516. if summaryList[i].PaymentId > 0 {
  1517. paymentIds = append(paymentIds, summaryList[i].PaymentId)
  1518. }
  1519. }
  1520. // 合同套餐
  1521. /*contractServiceCond := `contract_register_id IN ?`
  1522. contractServicePars := make([]interface{}, 0)
  1523. contractServicePars = append(contractServicePars, queryRegisterIds)
  1524. contractServiceOB := new(fms.ContractService)
  1525. contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
  1526. if e != nil {
  1527. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  1528. return
  1529. }
  1530. contractServiceMap := make(map[int][]*fms.ContractService, 0)
  1531. servicesNameMap := make(map[int][]string, 0)
  1532. for i := range contractServiceList {
  1533. if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
  1534. contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
  1535. }
  1536. contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
  1537. servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
  1538. }
  1539. */
  1540. servicesNameMap, serviceFormatMap, e := fmsService.GetContractServiceNameFormat(queryRegisterIds)
  1541. if e != nil {
  1542. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  1543. return
  1544. }
  1545. // 到款套餐分配
  1546. serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
  1547. if len(paymentIds) > 0 {
  1548. serviceAmountCond := `contract_payment_id IN ?`
  1549. serviceAmountPars := make([]interface{}, 0)
  1550. serviceAmountPars = append(serviceAmountPars, paymentIds)
  1551. serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  1552. serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  1553. if e != nil {
  1554. listErr = fmt.Errorf("获取到款套餐分配列表失败, Err: %s", e.Error())
  1555. return
  1556. }
  1557. for i := range serviceAmountList {
  1558. if serviceAmountMap[serviceAmountList[i].ContractPaymentId] == nil {
  1559. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = make([]*fms.ContractPaymentServiceAmount, 0)
  1560. }
  1561. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = append(serviceAmountMap[serviceAmountList[i].ContractPaymentId], serviceAmountList[i])
  1562. }
  1563. }
  1564. // 重组汇总数据
  1565. summaryMap := make(map[int][]*fms.InvoicePaymentCensusInfo)
  1566. amountMap := make(map[string]*fms.ContractPaymentServiceAmount)
  1567. for i := range summaryList {
  1568. v := new(fms.InvoicePaymentCensusInfo)
  1569. v.InvoiceId = summaryList[i].InvoiceId
  1570. v.InvoiceDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].InvoiceDate)
  1571. v.InvoiceAmount = summaryList[i].InvoiceAmount
  1572. v.SellerId = summaryList[i].SellerId
  1573. v.SellerName = summaryList[i].SellerName
  1574. v.SellerGroupId = summaryList[i].SellerGroupId
  1575. v.SellerGroupName = summaryList[i].SellerGroupName
  1576. v.SellerType = summaryList[i].ServiceProductId
  1577. v.PaymentId = summaryList[i].PaymentId
  1578. v.PaymentDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].PaymentDate)
  1579. v.PaymentAmount = summaryList[i].PaymentAmount
  1580. v.PayType = summaryList[i].PayType
  1581. // 套餐到款分配
  1582. svaList := make([]*fms.ContractPaymentServiceAmountItem, 0)
  1583. amountList := serviceAmountMap[summaryList[i].PaymentId]
  1584. if amountList != nil {
  1585. for i := range amountList {
  1586. k := fmt.Sprintf("%d-%d", amountList[i].ContractPaymentId, amountList[i].ServiceTemplateId)
  1587. amountMap[k] = amountList[i]
  1588. }
  1589. }
  1590. // 合同对应的所有套餐
  1591. svList := serviceFormatMap[summaryList[i].RegisterId]
  1592. if svList != nil {
  1593. for ii := range svList {
  1594. vv := new(fms.ContractPaymentServiceAmountItem)
  1595. vv.ServiceTemplateId = svList[ii].ServiceTemplateId
  1596. vv.ServiceTemplateName = svList[ii].FormatTitle
  1597. vv.ServiceTemplatePid = svList[ii].ServiceTemplatePid
  1598. k2 := fmt.Sprintf("%d-%d", summaryList[i].PaymentId, svList[ii].ServiceTemplateId)
  1599. a := amountMap[k2]
  1600. if a != nil {
  1601. vv.ContractPaymentServiceAmountId = a.ContractPaymentServiceAmountId
  1602. vv.ContractPaymentId = a.ContractPaymentId
  1603. vv.Amount = a.Amount
  1604. }
  1605. svaList = append(svaList, vv)
  1606. }
  1607. }
  1608. v.ServiceAmountList = svaList
  1609. summaryMap[summaryList[i].SummaryId] = append(summaryMap[summaryList[i].SummaryId], v)
  1610. }
  1611. // 货币列表
  1612. currencyOB := new(fms.CurrencyUnit)
  1613. currencyCond := `enable = 1`
  1614. currencyPars := make([]interface{}, 0)
  1615. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  1616. if e != nil {
  1617. resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
  1618. return
  1619. }
  1620. unitMap := make(map[string]string)
  1621. for i := range currencyList {
  1622. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  1623. }
  1624. // 响应列表
  1625. for i := range registerList {
  1626. v := new(fms.InvoicePaymentCensusItem)
  1627. v.SummaryId = registerList[i].SummaryId
  1628. v.ContractRegisterId = registerList[i].ContractRegisterId
  1629. v.CompanyName = registerList[i].CompanyName
  1630. v.NewCompany = registerList[i].NewCompany
  1631. v.StartDate = utils.TimeTransferString(utils.FormatDate, registerList[i].StartDate)
  1632. v.EndDate = utils.TimeTransferString(utils.FormatDate, registerList[i].EndDate)
  1633. v.ServicesName = servicesNameMap[registerList[i].ContractRegisterId]
  1634. v.InvoicePaymentList = summaryMap[registerList[i].SummaryId]
  1635. v.ContractType = registerList[i].ContractType
  1636. v.ContractCode = registerList[i].ContractCode
  1637. v.ContractAmount = registerList[i].ContractAmount
  1638. v.UnitName = unitMap[registerList[i].CurrencyUnit]
  1639. v.ProductIds = registerList[i].ProductIds
  1640. respList = append(respList, v)
  1641. }
  1642. }()
  1643. // 开票到款金额合计(换算后)
  1644. // 开票到款金额合计(换算后)
  1645. var invoiceTotal, paymentTotal, contractAmount float64
  1646. wg.Add(1)
  1647. go func() {
  1648. defer wg.Done()
  1649. if len(summaryIds) == 0 {
  1650. return
  1651. }
  1652. amountTotalCond := `a.id IN ?`
  1653. amountTotalPars := make([]interface{}, 0)
  1654. amountTotalPars = append(amountTotalPars, summaryIds)
  1655. invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
  1656. if e != nil {
  1657. totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
  1658. return
  1659. }
  1660. invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
  1661. paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
  1662. if e != nil {
  1663. totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
  1664. return
  1665. }
  1666. paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
  1667. }()
  1668. // 分币种金额统计
  1669. invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  1670. paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  1671. contractAmountCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  1672. wg.Add(1)
  1673. go func() {
  1674. defer wg.Done()
  1675. currencyOB := new(fms.CurrencyUnit)
  1676. currencyCond := `enable = 1`
  1677. currencyPars := make([]interface{}, 0)
  1678. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  1679. if e != nil {
  1680. totalGroupErr = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
  1681. return
  1682. }
  1683. unitMap := make(map[string]string)
  1684. for i := range currencyList {
  1685. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  1686. invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  1687. Name: currencyList[i].Name,
  1688. UnitName: currencyList[i].UnitName,
  1689. Code: currencyList[i].Code,
  1690. FlagImg: currencyList[i].FlagImg,
  1691. })
  1692. paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  1693. Name: currencyList[i].Name,
  1694. UnitName: currencyList[i].UnitName,
  1695. Code: currencyList[i].Code,
  1696. FlagImg: currencyList[i].FlagImg,
  1697. })
  1698. contractAmountCurrencyTotals = append(contractAmountCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  1699. Name: currencyList[i].Name,
  1700. UnitName: currencyList[i].UnitName,
  1701. Code: currencyList[i].Code,
  1702. FlagImg: currencyList[i].FlagImg,
  1703. })
  1704. }
  1705. if len(summaryIds) == 0 {
  1706. return
  1707. }
  1708. totalGroupCond := `a.id IN ?`
  1709. totalGroupPars := make([]interface{}, 0)
  1710. totalGroupPars = append(totalGroupPars, summaryIds)
  1711. invoiceSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 1)
  1712. if e != nil {
  1713. totalGroupErr = fmt.Errorf("获取汇总货币合计开票金额失败, Err: %s", e.Error())
  1714. return
  1715. }
  1716. paymentSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 2)
  1717. if e != nil {
  1718. totalGroupErr = fmt.Errorf("获取汇总货币合计到款金额失败, Err: %s", e.Error())
  1719. return
  1720. }
  1721. invoiceSumMap := make(map[string]float64)
  1722. paymentSumMap := make(map[string]float64)
  1723. contractSumMap := make(map[string]float64)
  1724. for i := range invoiceSumGroup {
  1725. invoiceSumMap[invoiceSumGroup[i].CurrencyUnit] = invoiceSumGroup[i].OriginAmountTotal
  1726. continue
  1727. }
  1728. for i := range paymentSumGroup {
  1729. paymentSumMap[paymentSumGroup[i].CurrencyUnit] = paymentSumGroup[i].OriginAmountTotal
  1730. continue
  1731. }
  1732. for i := range invoiceCurrencyTotals {
  1733. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
  1734. invoiceCurrencyTotals[i].Amount = a
  1735. }
  1736. for i := range paymentCurrencyTotals {
  1737. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
  1738. paymentCurrencyTotals[i].Amount = a
  1739. }
  1740. contractRegisters, err := fms.GetContractInfoByRegisterIds(queryRegisterIds)
  1741. if err != nil {
  1742. resp.FailMsg("查询合同信息失败", "查询合同信息失败, Err: "+err.Error(), c)
  1743. return
  1744. }
  1745. for _, v := range contractRegisters {
  1746. if v.RMBRate == 0.0 {
  1747. v.RMBRate = 1
  1748. }
  1749. amount, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.ContractAmount/v.RMBRate), 64)
  1750. contractAmount += amount
  1751. contractSumMap[v.CurrencyUnit] = v.ContractAmount
  1752. }
  1753. for _, v := range contractAmountCurrencyTotals {
  1754. v.Amount = contractSumMap[v.Code] - invoiceSumMap[v.Code]
  1755. }
  1756. }()
  1757. wg.Wait()
  1758. if listErr != nil {
  1759. resp.FailMsg("获取失败", listErr.Error(), c)
  1760. return
  1761. }
  1762. if totalErr != nil {
  1763. resp.FailMsg("获取失败", totalErr.Error(), c)
  1764. return
  1765. }
  1766. if totalGroupErr != nil {
  1767. resp.FailMsg("获取失败", totalGroupErr.Error(), c)
  1768. return
  1769. }
  1770. results.DataList = respList
  1771. results.InvoiceTotal = invoiceTotal
  1772. results.PaymentTotal = paymentTotal
  1773. results.NotInvoiceTotal = contractAmount - invoiceTotal
  1774. results.InvoiceCurrencyTotal = invoiceCurrencyTotals
  1775. results.PaymentCurrencyTotal = paymentCurrencyTotals
  1776. results.NotInvoiceCurrencyTotal = contractAmountCurrencyTotals
  1777. }
  1778. // 是否导出
  1779. if req.IsExport == 1 {
  1780. ExportNotPaymentCensusList(c, results)
  1781. return
  1782. }
  1783. page.SetTotal(total)
  1784. baseData := new(base.BaseData)
  1785. baseData.SetPage(page)
  1786. baseData.SetList(results)
  1787. resp.OkData("获取成功", baseData, c)
  1788. }
  1789. // ExportNotInvoiceCensusList 导出未开票统计列表
  1790. func ExportNotInvoiceCensusList(c *gin.Context, results *fms.NotInvoicePaymentCensusResp) {
  1791. list := results.DataList
  1792. if len(list) == 0 {
  1793. resp.Fail("列表数据为空", c)
  1794. return
  1795. }
  1796. // 生成Excel文件
  1797. xlsxFile := xlsx.NewFile()
  1798. style := xlsx.NewStyle()
  1799. alignment := xlsx.Alignment{
  1800. Horizontal: "center",
  1801. Vertical: "center",
  1802. WrapText: true,
  1803. }
  1804. style.Alignment = alignment
  1805. style.ApplyAlignment = true
  1806. sheet, err := xlsxFile.AddSheet("未开票统计")
  1807. if err != nil {
  1808. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  1809. return
  1810. }
  1811. _ = sheet.SetColWidth(1, 1, 30)
  1812. _ = sheet.SetColWidth(3, 3, 30)
  1813. // 前三行-开票金额合计
  1814. rowA := sheet.AddRow()
  1815. cellAA := rowA.AddCell()
  1816. cellAA.SetString(fmt.Sprintf("未开票合计金额(换算后):%.2f(元)", results.NotInvoiceTotal))
  1817. rowBData := "未开票金额:"
  1818. for _, v := range results.NotInvoiceCurrencyTotal {
  1819. rowBData += fmt.Sprintf("%s%.2f(%s) ", v.Name, v.Amount, v.UnitName)
  1820. }
  1821. rowB := sheet.AddRow()
  1822. rowB.AddCell().SetString(rowBData)
  1823. sheet.AddRow()
  1824. // 表头, 套餐动态获取
  1825. rowTitle := []string{"序号", "客户名称", "是否新客户", "合同有效期", "开票日", "开票金额", "到款日", "到款金额", "付款方式", "销售",
  1826. "销售组别", "销售类型"}
  1827. serviceTempCond := ``
  1828. serviceTempPars := make([]interface{}, 0)
  1829. serviceTempOB := new(fms.ContractServiceTemplate)
  1830. serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
  1831. if e != nil {
  1832. resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
  1833. return
  1834. }
  1835. serviceTempListMap := make(map[int]*fms.ContractServiceTemplate)
  1836. var serviceTempShow [] *fms.ContractServiceTemplate
  1837. for i := range serviceTempList {
  1838. serviceTempListMap[serviceTempList[i].ServiceTemplateId] = serviceTempList[i]
  1839. }
  1840. for i := range serviceTempList {
  1841. if serviceTempList[i].ProductId == 1 {
  1842. rowTitle = append(rowTitle, serviceTempList[i].Title)
  1843. serviceTempShow = append(serviceTempShow, serviceTempList[i])
  1844. }else if serviceTempList[i].ProductId == 2{
  1845. if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "权益大套餐" || (serviceTempList[i].Title == "策略" && serviceTempListMap[serviceTempList[i].Pid].Pid==0){
  1846. }else if serviceTempList[i].Title == "医药" || serviceTempList[i].Title == "消费" || serviceTempList[i].Title == "科技" || serviceTempList[i].Title == "智造" {
  1847. }else {
  1848. serviceTempShow = append(serviceTempShow, serviceTempList[i])
  1849. }
  1850. if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "45万" || serviceTempList[i].Title == "70万" {
  1851. continue
  1852. }
  1853. if serviceTempList[i].Pid > 0 && serviceTempListMap[serviceTempList[i].Pid].Pid > 0 {
  1854. continue
  1855. }
  1856. rowTitle = append(rowTitle, serviceTempList[i].Title)
  1857. }
  1858. }
  1859. hi := 0
  1860. insertHi := 0
  1861. titleRow := sheet.AddRow()
  1862. for i := range rowTitle {
  1863. v := titleRow.AddCell()
  1864. v.SetString(rowTitle[i])
  1865. v.SetStyle(style)
  1866. if rowTitle[i] != "权益大套餐" && rowTitle[i] != "医药" && rowTitle[i] != "消费" && rowTitle[i] != "科技" && rowTitle[i] != "智造" {
  1867. v.VMerge = 1
  1868. hi ++
  1869. }else{
  1870. if insertHi == 0 {
  1871. insertHi = hi
  1872. }
  1873. v.HMerge = 1
  1874. titleRow.AddCell().SetString("")
  1875. }
  1876. }
  1877. // 新增一行放主观和客观
  1878. specialRow := sheet.AddRow()
  1879. for i:=0; i< hi; i++ {
  1880. if i == insertHi {
  1881. v1 := specialRow.AddCell()
  1882. v1.SetString("45万")
  1883. v1.SetStyle(style)
  1884. v1 = specialRow.AddCell()
  1885. v1.SetString("70万")
  1886. v1.SetStyle(style)
  1887. for j:=0; j<8;j++ {
  1888. if j%2 ==0 {
  1889. v1 = specialRow.AddCell()
  1890. v1.SetString("主观")
  1891. v1.SetStyle(style)
  1892. }else{
  1893. v1 = specialRow.AddCell()
  1894. v1.SetString("客观")
  1895. v1.SetStyle(style)
  1896. }
  1897. }
  1898. }else{
  1899. v := specialRow.AddCell()
  1900. v.SetString("")
  1901. }
  1902. }
  1903. newCompanyMap := map[int]string{0: "否", 1: "是"}
  1904. sellerTypeMap := map[int]string{1:"FICC销售", 2:"权益销售"}
  1905. for k, v := range list {
  1906. dataRow := sheet.AddRow()
  1907. // 前四个单元格根据每行开票到款条数向下合并
  1908. l := len(v.InvoicePaymentList)
  1909. mergeRowNum := l - 1
  1910. // 序号
  1911. sortNum := k + 1
  1912. colA := dataRow.AddCell()
  1913. colA.VMerge = mergeRowNum
  1914. colA.SetString(fmt.Sprint(sortNum))
  1915. // 客户名称
  1916. colB := dataRow.AddCell()
  1917. colB.VMerge = mergeRowNum
  1918. colB.SetString(v.CompanyName)
  1919. // 是否新客户
  1920. colC := dataRow.AddCell()
  1921. colC.VMerge = mergeRowNum
  1922. colC.SetString(newCompanyMap[v.NewCompany])
  1923. // 合同有效期
  1924. colD := dataRow.AddCell()
  1925. colD.VMerge = mergeRowNum
  1926. colD.SetString(fmt.Sprint(v.StartDate, "至", v.EndDate))
  1927. // 开票到款信息
  1928. for k2, v2 := range v.InvoicePaymentList {
  1929. rowData := []string{
  1930. v2.InvoiceDate, // 开票日
  1931. fmt.Sprint(v2.InvoiceAmount), // 开票金额
  1932. v2.PaymentDate, // 到款日
  1933. fmt.Sprint(v2.PaymentAmount), // 到款金额
  1934. fms.ContractPaymentPayTypeNameMap[v2.PayType], // 付款方式
  1935. v2.SellerName, // 销售
  1936. v2.SellerGroupName, // 组别
  1937. sellerTypeMap[v2.SellerType], // 销售类型
  1938. }
  1939. // 套餐金额信息
  1940. serviceTempShowAmount := make(map[int]string)
  1941. for i := range serviceTempShow {
  1942. for s2 := range v2.ServiceAmountList {
  1943. item := v2.ServiceAmountList[s2]
  1944. if item.ServiceTemplateId == serviceTempShow[i].ServiceTemplateId {
  1945. serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount)
  1946. break
  1947. }else if serviceTempShow[i].Pid == item.ServiceTemplateId {
  1948. serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount/2)
  1949. break
  1950. }
  1951. }
  1952. }
  1953. for i := range serviceTempShow {
  1954. sa := ""
  1955. if am, ok := serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId]; ok {
  1956. sa = am
  1957. }
  1958. rowData = append(rowData, sa)
  1959. }
  1960. // 首行开票到款
  1961. if k2 == 0 {
  1962. for i := range rowData {
  1963. dataRow.AddCell().SetString(rowData[i])
  1964. }
  1965. continue
  1966. }
  1967. // 其他行开票到款, 加四列空的单元格用于合并
  1968. dataRowExtra := sheet.AddRow()
  1969. for i := 0; i < 4; i++ {
  1970. dataRowExtra.AddCell()
  1971. }
  1972. for i := range rowData {
  1973. dataRowExtra.AddCell().SetString(rowData[i])
  1974. }
  1975. }
  1976. }
  1977. // 输出文件
  1978. var buffer bytes.Buffer
  1979. _ = xlsxFile.Write(&buffer)
  1980. content := bytes.NewReader(buffer.Bytes())
  1981. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1982. fileName := "商品到款统计_" + randStr + ".xlsx"
  1983. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  1984. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1985. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  1986. }
  1987. // ExportNotPaymentCensusList 导出开票未到款统计列表
  1988. func ExportNotPaymentCensusList(c *gin.Context, results *fms.NotInvoicePaymentCensusResp) {
  1989. list := results.DataList
  1990. if len(list) == 0 {
  1991. resp.Fail("列表数据为空", c)
  1992. return
  1993. }
  1994. // 生成Excel文件
  1995. xlsxFile := xlsx.NewFile()
  1996. style := xlsx.NewStyle()
  1997. alignment := xlsx.Alignment{
  1998. Horizontal: "center",
  1999. Vertical: "center",
  2000. WrapText: true,
  2001. }
  2002. style.Alignment = alignment
  2003. style.ApplyAlignment = true
  2004. sheet, err := xlsxFile.AddSheet("开票未到款统计")
  2005. if err != nil {
  2006. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  2007. return
  2008. }
  2009. _ = sheet.SetColWidth(1, 1, 30)
  2010. _ = sheet.SetColWidth(3, 3, 30)
  2011. // 前三行-开票金额合计
  2012. rowA := sheet.AddRow()
  2013. cellAA := rowA.AddCell()
  2014. cellAA.SetString(fmt.Sprintf("开票未到款合计金额(换算后):%.2f(元)", results.InvoiceTotal))
  2015. rowBData := "已开票金额:"
  2016. for _, v := range results.InvoiceCurrencyTotal {
  2017. rowBData += fmt.Sprintf("%s%.2f(%s) ", v.Name, v.Amount, v.UnitName)
  2018. }
  2019. rowB := sheet.AddRow()
  2020. rowB.AddCell().SetString(rowBData)
  2021. sheet.AddRow()
  2022. // 表头, 套餐动态获取
  2023. rowTitle := []string{"序号", "客户名称", "是否新客户", "合同有效期", "开票日", "开票金额", "到款日", "到款金额", "付款方式", "销售",
  2024. "销售组别", "销售类型"}
  2025. serviceTempCond := ``
  2026. serviceTempPars := make([]interface{}, 0)
  2027. serviceTempOB := new(fms.ContractServiceTemplate)
  2028. serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
  2029. if e != nil {
  2030. resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
  2031. return
  2032. }
  2033. serviceTempListMap := make(map[int]*fms.ContractServiceTemplate)
  2034. var serviceTempShow [] *fms.ContractServiceTemplate
  2035. for i := range serviceTempList {
  2036. serviceTempListMap[serviceTempList[i].ServiceTemplateId] = serviceTempList[i]
  2037. }
  2038. for i := range serviceTempList {
  2039. if serviceTempList[i].ProductId == 1 {
  2040. rowTitle = append(rowTitle, serviceTempList[i].Title)
  2041. serviceTempShow = append(serviceTempShow, serviceTempList[i])
  2042. }else if serviceTempList[i].ProductId == 2{
  2043. if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "权益大套餐" || (serviceTempList[i].Title == "策略" && serviceTempListMap[serviceTempList[i].Pid].Pid==0){
  2044. }else if serviceTempList[i].Title == "医药" || serviceTempList[i].Title == "消费" || serviceTempList[i].Title == "科技" || serviceTempList[i].Title == "智造" {
  2045. }else {
  2046. serviceTempShow = append(serviceTempShow, serviceTempList[i])
  2047. }
  2048. if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "45万" || serviceTempList[i].Title == "70万" {
  2049. continue
  2050. }
  2051. if serviceTempList[i].Pid > 0 && serviceTempListMap[serviceTempList[i].Pid].Pid > 0 {
  2052. continue
  2053. }
  2054. rowTitle = append(rowTitle, serviceTempList[i].Title)
  2055. }
  2056. }
  2057. hi := 0
  2058. insertHi := 0
  2059. titleRow := sheet.AddRow()
  2060. for i := range rowTitle {
  2061. v := titleRow.AddCell()
  2062. v.SetString(rowTitle[i])
  2063. v.SetStyle(style)
  2064. if rowTitle[i] != "权益大套餐" && rowTitle[i] != "医药" && rowTitle[i] != "消费" && rowTitle[i] != "科技" && rowTitle[i] != "智造" {
  2065. v.VMerge = 1
  2066. hi ++
  2067. }else{
  2068. if insertHi == 0 {
  2069. insertHi = hi
  2070. }
  2071. v.HMerge = 1
  2072. titleRow.AddCell().SetString("")
  2073. }
  2074. }
  2075. // 新增一行放主观和客观
  2076. specialRow := sheet.AddRow()
  2077. for i:=0; i< hi; i++ {
  2078. if i == insertHi {
  2079. v1 := specialRow.AddCell()
  2080. v1.SetString("45万")
  2081. v1.SetStyle(style)
  2082. v1 = specialRow.AddCell()
  2083. v1.SetString("70万")
  2084. v1.SetStyle(style)
  2085. for j:=0; j<8;j++ {
  2086. if j%2 ==0 {
  2087. v1 = specialRow.AddCell()
  2088. v1.SetString("主观")
  2089. v1.SetStyle(style)
  2090. }else{
  2091. v1 = specialRow.AddCell()
  2092. v1.SetString("客观")
  2093. v1.SetStyle(style)
  2094. }
  2095. }
  2096. }else{
  2097. v := specialRow.AddCell()
  2098. v.SetString("")
  2099. }
  2100. }
  2101. newCompanyMap := map[int]string{0: "否", 1: "是"}
  2102. sellerTypeMap := map[int]string{1:"FICC销售", 2:"权益销售"}
  2103. for k, v := range list {
  2104. dataRow := sheet.AddRow()
  2105. // 前四个单元格根据每行开票到款条数向下合并
  2106. l := len(v.InvoicePaymentList)
  2107. mergeRowNum := l - 1
  2108. // 序号
  2109. sortNum := k + 1
  2110. colA := dataRow.AddCell()
  2111. colA.VMerge = mergeRowNum
  2112. colA.SetString(fmt.Sprint(sortNum))
  2113. // 客户名称
  2114. colB := dataRow.AddCell()
  2115. colB.VMerge = mergeRowNum
  2116. colB.SetString(v.CompanyName)
  2117. // 是否新客户
  2118. colC := dataRow.AddCell()
  2119. colC.VMerge = mergeRowNum
  2120. colC.SetString(newCompanyMap[v.NewCompany])
  2121. // 合同有效期
  2122. colD := dataRow.AddCell()
  2123. colD.VMerge = mergeRowNum
  2124. colD.SetString(fmt.Sprint(v.StartDate, "至", v.EndDate))
  2125. // 开票到款信息
  2126. for k2, v2 := range v.InvoicePaymentList {
  2127. rowData := []string{
  2128. v2.InvoiceDate, // 开票日
  2129. fmt.Sprint(v2.InvoiceAmount), // 开票金额
  2130. v2.PaymentDate, // 到款日
  2131. fmt.Sprint(v2.PaymentAmount), // 到款金额
  2132. fms.ContractPaymentPayTypeNameMap[v2.PayType], // 付款方式
  2133. v2.SellerName, // 销售
  2134. v2.SellerGroupName, // 组别
  2135. sellerTypeMap[v2.SellerType], // 销售类型
  2136. }
  2137. // 套餐金额信息
  2138. serviceTempShowAmount := make(map[int]string)
  2139. for i := range serviceTempShow {
  2140. for s2 := range v2.ServiceAmountList {
  2141. item := v2.ServiceAmountList[s2]
  2142. if item.ServiceTemplateId == serviceTempShow[i].ServiceTemplateId {
  2143. serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount)
  2144. break
  2145. }else if serviceTempShow[i].Pid == item.ServiceTemplateId {
  2146. serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount/2)
  2147. break
  2148. }
  2149. }
  2150. }
  2151. for i := range serviceTempShow {
  2152. sa := ""
  2153. if am, ok := serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId]; ok {
  2154. sa = am
  2155. }
  2156. rowData = append(rowData, sa)
  2157. }
  2158. // 首行开票到款
  2159. if k2 == 0 {
  2160. for i := range rowData {
  2161. dataRow.AddCell().SetString(rowData[i])
  2162. }
  2163. continue
  2164. }
  2165. // 其他行开票到款, 加四列空的单元格用于合并
  2166. dataRowExtra := sheet.AddRow()
  2167. for i := 0; i < 4; i++ {
  2168. dataRowExtra.AddCell()
  2169. }
  2170. for i := range rowData {
  2171. dataRowExtra.AddCell().SetString(rowData[i])
  2172. }
  2173. }
  2174. }
  2175. // 输出文件
  2176. var buffer bytes.Buffer
  2177. _ = xlsxFile.Write(&buffer)
  2178. content := bytes.NewReader(buffer.Bytes())
  2179. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  2180. fileName := "商品到款统计_" + randStr + ".xlsx"
  2181. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  2182. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  2183. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  2184. }