invoice_payment.go 82 KB

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