invoice_payment.go 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951
  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"
  11. "hongze/fms_api/models/base"
  12. "hongze/fms_api/models/fms"
  13. "hongze/fms_api/services/alarm_msg"
  14. fmsService "hongze/fms_api/services/fms"
  15. "hongze/fms_api/utils"
  16. "net/http"
  17. "sort"
  18. "strconv"
  19. "strings"
  20. "sync"
  21. "time"
  22. )
  23. // InvoicePaymentController 商品到款统计
  24. type InvoicePaymentController struct{}
  25. // List2 原版(已废弃)
  26. //func (ct *InvoicePaymentController) List2(c *gin.Context) {
  27. // var req fms.InvoicePaymentCensusListReq
  28. // if e := c.BindQuery(&req); e != nil {
  29. // err, ok := e.(validator.ValidationErrors)
  30. // if !ok {
  31. // resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  32. // return
  33. // }
  34. // resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  35. // return
  36. // }
  37. //
  38. // cond := `1 = 1`
  39. // pars := make([]interface{}, 0)
  40. // // 合同编号/客户姓名/销售
  41. // if req.Keyword != "" {
  42. // kw := "%" + req.Keyword + "%"
  43. // cond += ` AND b.company_name LIKE ? OR a.seller_name LIKE ?`
  44. // pars = append(pars, kw, kw)
  45. // }
  46. // if req.SellGroupId > 0 {
  47. // cond += ` AND a.seller_group_id = ?`
  48. // pars = append(pars, req.SellGroupId)
  49. // }
  50. // // 套餐筛选
  51. // if req.ServiceType != 0 {
  52. // registerIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
  53. // if e != nil {
  54. // resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  55. // return
  56. // }
  57. // if len(registerIds) > 0 {
  58. // cond += ` AND b.contract_register_id IN ?`
  59. // pars = append(pars, registerIds)
  60. // } else {
  61. // cond += ` AND 1 = 2`
  62. // }
  63. // }
  64. // // 开票到款日期
  65. // if req.TimeType > 0 && req.StartDate != "" && req.EndDate != "" {
  66. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  67. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  68. // cond += ` AND a.invoice_type = ? AND (a.invoice_time BETWEEN ? AND ?)`
  69. // pars = append(pars, req.TimeType, st, ed)
  70. // }
  71. // // 已开票
  72. // if req.HasInvoice == 1 && req.HasPayment == 0 {
  73. // cond += ` AND a.invoice_type = 1`
  74. // }
  75. // // 已到款
  76. // if req.HasInvoice == 0 && req.HasPayment == 1 {
  77. // cond += ` AND a.invoice_type = 2`
  78. // }
  79. //
  80. // page := new(base.Page)
  81. // page.SetPageSize(req.PageSize)
  82. // page.SetCurrent(req.Current)
  83. // page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
  84. // page.AddOrderItem(base.OrderItem{Column: "b.contract_register_id", Asc: true})
  85. // if req.IsExport == 1 {
  86. // page.SetPageSize(10000)
  87. // page.SetCurrent(1)
  88. // }
  89. //
  90. // list, allRegisterIds, e := fms.GetInvoicePaymentCensusPageList(page, cond, pars)
  91. // if e != nil {
  92. // resp.FailMsg("获取失败", "获取商品到款统计列表失败, Err: "+e.Error(), c)
  93. // return
  94. // }
  95. // total := int64(len(allRegisterIds))
  96. // registerIds := make([]int, 0)
  97. // for i := range list {
  98. // registerIds = append(registerIds, list[i].ContractRegisterId)
  99. // }
  100. //
  101. // results := new(fms.InvoicePaymentCensusResp)
  102. // if len(registerIds) > 0 {
  103. // // 获取开票到款列表
  104. // ivCond := `contract_register_id IN ?`
  105. // ivPars := make([]interface{}, 0)
  106. // ivPars = append(ivPars, registerIds)
  107. // iv := new(fms.ContractInvoice)
  108. // invoiceList, e := iv.List(ivCond, ivPars, "")
  109. // if e != nil {
  110. // resp.FailMsg("获取失败", "获取开票到款列表失败, Err: "+e.Error(), c)
  111. // return
  112. // }
  113. // // 取出开票、到款
  114. // invoiceMap := make(map[int][]*fms.ContractInvoice, 0)
  115. // paymentMap := make(map[int][]*fms.ContractInvoice, 0)
  116. // paymentIds := make([]int, 0)
  117. // for i := range invoiceList {
  118. // if invoiceMap[invoiceList[i].ContractRegisterId] == nil {
  119. // invoiceMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoice, 0)
  120. // }
  121. // if paymentMap[invoiceList[i].ContractRegisterId] == nil {
  122. // paymentMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoice, 0)
  123. // }
  124. // if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  125. // invoiceMap[invoiceList[i].ContractRegisterId] = append(invoiceMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  126. // }
  127. // if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  128. // paymentMap[invoiceList[i].ContractRegisterId] = append(paymentMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  129. // paymentIds = append(paymentIds, invoiceList[i].ContractInvoiceId)
  130. // }
  131. // }
  132. //
  133. // // 合同套餐
  134. // contractServiceCond := `contract_register_id IN ?`
  135. // contractServicePars := make([]interface{}, 0)
  136. // contractServicePars = append(contractServicePars, registerIds)
  137. // contractServiceOB := new(fms.ContractService)
  138. // contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
  139. // if e != nil {
  140. // resp.FailMsg("获取失败", "获取合同套餐列表失败, Err:"+e.Error(), c)
  141. // return
  142. // }
  143. // contractServiceMap := make(map[int][]*fms.ContractService, 0)
  144. // servicesNameMap := make(map[int][]string, 0)
  145. // for i := range contractServiceList {
  146. // if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
  147. // contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
  148. // }
  149. // contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
  150. // servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
  151. // }
  152. //
  153. // // 到款套餐分配
  154. // serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
  155. // if len(paymentIds) > 0 {
  156. // serviceAmountCond := `contract_payment_id IN ?`
  157. // serviceAmountPars := make([]interface{}, 0)
  158. // serviceAmountPars = append(serviceAmountPars, paymentIds)
  159. // serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  160. // serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  161. // if e != nil {
  162. // resp.FailMsg("获取失败", "获取到款套餐分配列表失败, Err:"+e.Error(), c)
  163. // return
  164. // }
  165. // for i := range serviceAmountList {
  166. // if serviceAmountMap[serviceAmountList[i].ContractRegisterId] == nil {
  167. // serviceAmountMap[serviceAmountList[i].ContractRegisterId] = make([]*fms.ContractPaymentServiceAmount, 0)
  168. // }
  169. // serviceAmountMap[serviceAmountList[i].ContractRegisterId] = append(serviceAmountMap[serviceAmountList[i].ContractRegisterId], serviceAmountList[i])
  170. // }
  171. // }
  172. //
  173. // // 整合响应数据
  174. // respList := make([]*fms.InvoicePaymentCensusItem, 0)
  175. // for i := range list {
  176. // v := new(fms.InvoicePaymentCensusItem)
  177. // v.ContractRegisterId = list[i].ContractRegisterId
  178. // v.CompanyName = list[i].CompanyName
  179. // v.NewCompany = list[i].NewCompany
  180. // v.StartDate = list[i].StartDate.Format(utils.FormatDate)
  181. // v.EndDate = list[i].EndDate.Format(utils.FormatDate)
  182. // svList := servicesNameMap[list[i].ContractRegisterId]
  183. // v.ServicesName = strings.Join(svList, ",")
  184. // // 格式化(合并)开票到款数据
  185. // v.InvoicePaymentList = fmsService.MergeInvoiceList2InvoicePaymentCensusInfo(invoiceMap[list[i].ContractRegisterId], paymentMap[list[i].ContractRegisterId],
  186. // contractServiceMap[list[i].ContractRegisterId], serviceAmountMap[list[i].ContractRegisterId])
  187. // respList = append(respList, v)
  188. // }
  189. //
  190. // // 开票到款金额合计(换算后)
  191. // amountTotalCond := `contract_register_id IN ?`
  192. // amountTotalPars := make([]interface{}, 0)
  193. // amountTotalPars = append(amountTotalPars, allRegisterIds)
  194. // amountTotalList, e := fms.GetContractInvoiceAmountTotal(amountTotalCond, amountTotalPars)
  195. // if e != nil {
  196. // resp.FailMsg("获取失败", "获取开票到款金额合计失败, Err:"+e.Error(), c)
  197. // return
  198. // }
  199. // amountTotalMap := make(map[int]float64)
  200. // for i := range amountTotalList {
  201. // amountTotalMap[amountTotalList[i].InvoiceType] = amountTotalList[i].TotalAmount
  202. // }
  203. // invoiceTotal, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", amountTotalMap[fms.ContractInvoiceTypeMake]), 64)
  204. // paymentTotal, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", amountTotalMap[fms.ContractInvoiceTypePay]), 64)
  205. //
  206. // // 分币种金额统计
  207. // currencyOB := new(fms.CurrencyUnit)
  208. // currencyCond := `enable = 1`
  209. // currencyPars := make([]interface{}, 0)
  210. // currencyList, e := currencyOB.List(currencyCond, currencyPars)
  211. // if e != nil {
  212. // resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
  213. // return
  214. // }
  215. // unitMap := make(map[string]string)
  216. // invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  217. // paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  218. // for i := range currencyList {
  219. // unitMap[currencyList[i].Code] = currencyList[i].UnitName
  220. // invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  221. // Name: currencyList[i].Name,
  222. // UnitName: currencyList[i].UnitName,
  223. // Code: currencyList[i].Code,
  224. // FlagImg: currencyList[i].FlagImg,
  225. // })
  226. // paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  227. // Name: currencyList[i].Name,
  228. // UnitName: currencyList[i].UnitName,
  229. // Code: currencyList[i].Code,
  230. // FlagImg: currencyList[i].FlagImg,
  231. // })
  232. // }
  233. // sumList, e := fms.GetInvoiceListCurrencySum(amountTotalCond, amountTotalPars, "currency_unit, invoice_type")
  234. // if e != nil {
  235. // resp.FailMsg("获取失败", "获取商品到款统计货币合计金额失败, Err: "+e.Error(), c)
  236. // return
  237. // }
  238. // invoiceSumMap := make(map[string]float64)
  239. // paymentSumMap := make(map[string]float64)
  240. // for i := range sumList {
  241. // if sumList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  242. // invoiceSumMap[sumList[i].CurrencyUnit] = sumList[i].OriginAmountTotal
  243. // continue
  244. // }
  245. // if sumList[i].InvoiceType == fms.ContractInvoiceTypePay {
  246. // paymentSumMap[sumList[i].CurrencyUnit] = sumList[i].OriginAmountTotal
  247. // }
  248. // }
  249. // for i := range invoiceCurrencyTotals {
  250. // a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
  251. // invoiceCurrencyTotals[i].Amount = a
  252. // }
  253. // for i := range paymentCurrencyTotals {
  254. // a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
  255. // paymentCurrencyTotals[i].Amount = a
  256. // }
  257. //
  258. // results.DataList = respList
  259. // results.InvoiceTotal = invoiceTotal
  260. // results.PaymentTotal = paymentTotal
  261. // results.InvoiceCurrencyTotal = invoiceCurrencyTotals
  262. // results.PaymentCurrencyTotal = paymentCurrencyTotals
  263. // }
  264. //
  265. // // 是否导出
  266. // if req.IsExport == 1 {
  267. // ExportInvoicePaymentCensusList(c, results)
  268. // return
  269. // }
  270. // page.SetTotal(total)
  271. // baseData := new(base.BaseData)
  272. // baseData.SetPage(page)
  273. // baseData.SetList(results)
  274. // resp.OkData("获取成功", baseData, c)
  275. //}
  276. // ExportInvoicePaymentCensusList 导出商品到款统计列表
  277. func ExportInvoicePaymentCensusList(c *gin.Context, results *fms.InvoicePaymentCensusResp) {
  278. list := results.DataList
  279. if len(list) == 0 {
  280. resp.Fail("列表数据为空", c)
  281. return
  282. }
  283. // 生成Excel文件
  284. xlsxFile := xlsx.NewFile()
  285. style := xlsx.NewStyle()
  286. alignment := xlsx.Alignment{
  287. Horizontal: "center",
  288. Vertical: "center",
  289. WrapText: true,
  290. }
  291. style.Alignment = alignment
  292. style.ApplyAlignment = true
  293. sheet, err := xlsxFile.AddSheet("商品到款统计")
  294. if err != nil {
  295. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  296. return
  297. }
  298. _ = sheet.SetColWidth(1, 1, 30)
  299. _ = sheet.SetColWidth(3, 3, 30)
  300. // 前三行-开票金额合计
  301. rowA := sheet.AddRow()
  302. cellAA := rowA.AddCell()
  303. cellAA.SetString(fmt.Sprintf("已开票合计金额(换算后):%.2f(元)", results.InvoiceTotal))
  304. rowBData := "已开票金额:"
  305. for _, v := range results.InvoiceCurrencyTotal {
  306. rowBData += fmt.Sprintf("%s%.2f(%s) ", v.Name, v.Amount, v.UnitName)
  307. }
  308. rowB := sheet.AddRow()
  309. rowB.AddCell().SetString(rowBData)
  310. sheet.AddRow()
  311. // 四至六-到款金额合计
  312. rowD := sheet.AddRow()
  313. cellDA := rowD.AddCell()
  314. cellDA.SetString(fmt.Sprintf("已到款合计金额(换算后):%.2f(元)", results.PaymentTotal))
  315. rowEData := "已到款金额:"
  316. for _, v := range results.PaymentCurrencyTotal {
  317. rowEData += fmt.Sprintf("%s%.2f(%s) ", v.Name, v.Amount, v.UnitName)
  318. }
  319. rowE := sheet.AddRow()
  320. rowE.AddCell().SetString(rowEData)
  321. sheet.AddRow()
  322. // 表头, 套餐动态获取
  323. rowTitle := []string{"序号", "客户名称", "是否新客户", "合同有效期", "开票日", "开票金额", "到款日", "到款金额", "付款方式", "销售",
  324. "销售组别", "销售类型"}
  325. serviceTempCond := ``
  326. serviceTempPars := make([]interface{}, 0)
  327. serviceTempOB := new(fms.ContractServiceTemplate)
  328. serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
  329. if e != nil {
  330. resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
  331. return
  332. }
  333. serviceTempListMap := make(map[int]*fms.ContractServiceTemplate)
  334. var serviceTempShow []*fms.ContractServiceTemplate
  335. for i := range serviceTempList {
  336. serviceTempListMap[serviceTempList[i].ServiceTemplateId] = serviceTempList[i]
  337. }
  338. for i := range serviceTempList {
  339. if serviceTempList[i].ProductId == 1 {
  340. rowTitle = append(rowTitle, serviceTempList[i].Title)
  341. serviceTempShow = append(serviceTempShow, serviceTempList[i])
  342. } else if serviceTempList[i].ProductId == 2 {
  343. if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "权益大套餐" || (serviceTempList[i].Title == "策略" && serviceTempListMap[serviceTempList[i].Pid].Pid == 0) {
  344. } else if serviceTempList[i].Title == "医药" || serviceTempList[i].Title == "消费" || serviceTempList[i].Title == "科技" || serviceTempList[i].Title == "智造" {
  345. } else {
  346. serviceTempShow = append(serviceTempShow, serviceTempList[i])
  347. }
  348. if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "45万" || serviceTempList[i].Title == "70万" {
  349. continue
  350. }
  351. if serviceTempList[i].Pid > 0 && serviceTempListMap[serviceTempList[i].Pid].Pid > 0 {
  352. continue
  353. }
  354. rowTitle = append(rowTitle, serviceTempList[i].Title)
  355. }
  356. }
  357. hi := 0
  358. insertHi := 0
  359. titleRow := sheet.AddRow()
  360. for i := range rowTitle {
  361. v := titleRow.AddCell()
  362. v.SetString(rowTitle[i])
  363. v.SetStyle(style)
  364. if rowTitle[i] != "权益大套餐" && rowTitle[i] != "医药" && rowTitle[i] != "消费" && rowTitle[i] != "科技" && rowTitle[i] != "智造" {
  365. v.VMerge = 1
  366. hi++
  367. } else {
  368. if insertHi == 0 {
  369. insertHi = hi
  370. }
  371. v.HMerge = 1
  372. titleRow.AddCell().SetString("")
  373. }
  374. }
  375. // 新增一行放主观和客观
  376. specialRow := sheet.AddRow()
  377. for i := 0; i < hi; i++ {
  378. if i == insertHi {
  379. v1 := specialRow.AddCell()
  380. v1.SetString("45万")
  381. v1.SetStyle(style)
  382. v1 = specialRow.AddCell()
  383. v1.SetString("70万")
  384. v1.SetStyle(style)
  385. for j := 0; j < 8; j++ {
  386. if j%2 == 0 {
  387. v1 = specialRow.AddCell()
  388. v1.SetString("主观")
  389. v1.SetStyle(style)
  390. } else {
  391. v1 = specialRow.AddCell()
  392. v1.SetString("客观")
  393. v1.SetStyle(style)
  394. }
  395. }
  396. } else {
  397. v := specialRow.AddCell()
  398. v.SetString("")
  399. }
  400. }
  401. newCompanyMap := map[int]string{0: "否", 1: "是"}
  402. sellerTypeMap := map[int]string{1: "FICC销售", 2: "权益销售"}
  403. for k, v := range list {
  404. dataRow := sheet.AddRow()
  405. // 前四个单元格根据每行开票到款条数向下合并
  406. l := len(v.InvoicePaymentList)
  407. mergeRowNum := l - 1
  408. // 序号
  409. sortNum := k + 1
  410. colA := dataRow.AddCell()
  411. colA.VMerge = mergeRowNum
  412. colA.SetString(fmt.Sprint(sortNum))
  413. // 客户名称
  414. colB := dataRow.AddCell()
  415. colB.VMerge = mergeRowNum
  416. colB.SetString(v.CompanyName)
  417. // 是否新客户
  418. colC := dataRow.AddCell()
  419. colC.VMerge = mergeRowNum
  420. colC.SetString(newCompanyMap[v.NewCompany])
  421. // 合同有效期
  422. colD := dataRow.AddCell()
  423. colD.VMerge = mergeRowNum
  424. colD.SetString(fmt.Sprint(v.StartDate, "至", v.EndDate))
  425. // 开票到款信息
  426. for k2, v2 := range v.InvoicePaymentList {
  427. rowData := []string{
  428. v2.InvoiceDate, // 开票日
  429. fmt.Sprint(v2.InvoiceAmount), // 开票金额
  430. v2.PaymentDate, // 到款日
  431. fmt.Sprint(v2.PaymentAmount), // 到款金额
  432. fms.ContractPaymentPayTypeNameMap[v2.PayType], // 付款方式
  433. v2.SellerName, // 销售
  434. v2.SellerGroupName, // 组别
  435. sellerTypeMap[v2.SellerType], // 销售类型
  436. }
  437. // 套餐金额信息
  438. serviceTempShowAmount := make(map[int]string)
  439. for i := range serviceTempShow {
  440. for s2 := range v2.ServiceAmountList {
  441. item := v2.ServiceAmountList[s2]
  442. if item.ServiceTemplateId == serviceTempShow[i].ServiceTemplateId {
  443. serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount)
  444. break
  445. } else if serviceTempShow[i].Pid == item.ServiceTemplateId {
  446. serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount / 2)
  447. break
  448. }
  449. }
  450. }
  451. for i := range serviceTempShow {
  452. sa := ""
  453. if am, ok := serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId]; ok {
  454. sa = am
  455. }
  456. rowData = append(rowData, sa)
  457. }
  458. // 首行开票到款
  459. if k2 == 0 {
  460. for i := range rowData {
  461. dataRow.AddCell().SetString(rowData[i])
  462. }
  463. continue
  464. }
  465. // 其他行开票到款, 加四列空的单元格用于合并
  466. dataRowExtra := sheet.AddRow()
  467. for i := 0; i < 4; i++ {
  468. dataRowExtra.AddCell()
  469. }
  470. for i := range rowData {
  471. dataRowExtra.AddCell().SetString(rowData[i])
  472. }
  473. }
  474. }
  475. // 输出文件
  476. var buffer bytes.Buffer
  477. _ = xlsxFile.Write(&buffer)
  478. content := bytes.NewReader(buffer.Bytes())
  479. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  480. fileName := "商品到款统计_" + randStr + ".xlsx"
  481. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  482. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  483. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  484. }
  485. // List
  486. // @Title 商品到款统计列表
  487. // @Description 商品到款统计列表
  488. // @Param Keyword query string false "关键词"
  489. // @Param SellGroupId query int false "销售组别ID"
  490. // @Param ServiceType query int false "套餐类型"
  491. // @Param StartDate query string false "合同开始日期"
  492. // @Param EndDate query string false "合同结束日期"
  493. // @Param TimeType query int false "时间类型: 1-开票时间; 2-到款时间"
  494. // @Param HasInvoice query string false "是否已开票: 0-否; 1-是; 空-全部"
  495. // @Param HasPayment query string false "是否已到款: 0-否; 1-是; 空-全部"
  496. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  497. // @Param ListParam query int false "套餐类型: 0-全部; 1-ficc; 2-权益"
  498. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'invoice_time':开票日 、 'payment_date':到款日"
  499. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
  500. // @Success 200 {object} fms.ContractRegisterItem
  501. // @router /census/invoice_payment/list [get]
  502. func (ct *InvoicePaymentController) List(c *gin.Context) {
  503. var req fms.InvoicePaymentCensusListReq
  504. if e := c.BindQuery(&req); e != nil {
  505. err, ok := e.(validator.ValidationErrors)
  506. if !ok {
  507. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  508. return
  509. }
  510. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  511. return
  512. }
  513. cond := `1 = 1`
  514. pars := make([]interface{}, 0)
  515. // 客户姓名/销售
  516. if req.Keyword != "" {
  517. kw := "%" + req.Keyword + "%"
  518. cond += ` AND b.company_name LIKE ?`
  519. pars = append(pars, kw)
  520. }
  521. if req.SellerIds != "" {
  522. sellerIds := strings.Split(req.SellerIds, ",")
  523. cond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
  524. pars = append(pars, sellerIds, sellerIds)
  525. }
  526. // 套餐筛选
  527. if req.ServiceTypes != "" {
  528. serviceTypes := strings.Split(req.ServiceTypes, ",")
  529. tempRegisterIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
  530. if e != nil {
  531. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  532. return
  533. }
  534. if len(tempRegisterIds) > 0 {
  535. cond += ` AND a.register_id IN ?`
  536. pars = append(pars, tempRegisterIds)
  537. } else {
  538. cond += ` AND 1 = 2`
  539. }
  540. }
  541. if req.ListParam == 1 {
  542. cond += ` AND a.service_product_id = 1 `
  543. } else if req.ListParam == 2 {
  544. cond += ` AND a.service_product_id = 2 `
  545. }
  546. // 开票到款日期
  547. if req.TimeType != 0 {
  548. if req.TimeType == 1 && req.StartDate != "" && req.EndDate != "" {
  549. st := fmt.Sprint(req.StartDate, " 00:00:00")
  550. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  551. cond += ` AND (c.invoice_time BETWEEN ? AND ?) `
  552. pars = append(pars, st, ed)
  553. } else if req.TimeType == 2 && req.StartDate != "" && req.EndDate != "" {
  554. st := fmt.Sprint(req.StartDate, " 00:00:00")
  555. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  556. cond += ` AND (d.invoice_time BETWEEN ? AND ?) `
  557. pars = append(pars, st, ed)
  558. } else if req.TimeType == 3 && req.StartDate != "" && req.EndDate != "" {
  559. st := fmt.Sprint(req.StartDate, " 00:00:00")
  560. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  561. cond += ` AND (c.invoice_time BETWEEN ? AND ?) AND (d.invoice_time BETWEEN ? AND ?) `
  562. pars = append(pars, st, ed, st, ed)
  563. }
  564. } else if req.StartDate != "" && req.EndDate != "" {
  565. st := fmt.Sprint(req.StartDate, " 00:00:00")
  566. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  567. cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  568. pars = append(pars, st, ed, st, ed)
  569. }
  570. if req.HasInvoice == "1" {
  571. cond += ` AND a.invoice_id > 0 `
  572. } else if req.HasInvoice == "0" {
  573. cond += ` AND a.invoice_id = 0 `
  574. }
  575. if req.HasPayment == "1" {
  576. cond += ` AND a.payment_id > 0 `
  577. } else if req.HasPayment == "0" {
  578. cond += ` AND a.payment_id = 0 `
  579. }
  580. page := new(base.Page)
  581. page.SetPageSize(req.PageSize)
  582. page.SetCurrent(req.Current)
  583. //排序
  584. if req.SortType == "" {
  585. req.SortType = "desc"
  586. }
  587. if req.SortType != "desc" && req.SortType != "asc" {
  588. resp.Fail("排序类型不正确", c)
  589. return
  590. }
  591. if req.SortParam == "" {
  592. page.AddOrderItem(base.OrderItem{Column: "sort_invoice_id", Asc: true})
  593. page.AddOrderItem(base.OrderItem{Column: "sort_payment_id", Asc: false})
  594. page.AddOrderItem(base.OrderItem{Column: "c.invoice_time", Asc: false})
  595. page.AddOrderItem(base.OrderItem{Column: "c.amount", Asc: false})
  596. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
  597. } else if req.SortParam == "invoice_time" {
  598. if req.SortType == "asc" {
  599. page.AddOrderItem(base.OrderItem{Column: "sort_invoice_id", Asc: false})
  600. page.AddOrderItem(base.OrderItem{Column: "c.invoice_time", Asc: true})
  601. page.AddOrderItem(base.OrderItem{Column: "c.amount", Asc: true})
  602. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: true})
  603. } else {
  604. page.AddOrderItem(base.OrderItem{Column: "sort_invoice_id", Asc: true})
  605. page.AddOrderItem(base.OrderItem{Column: "c.invoice_time", Asc: false})
  606. page.AddOrderItem(base.OrderItem{Column: "c.amount", Asc: false})
  607. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
  608. }
  609. } else if req.SortParam == "payment_date" {
  610. if req.SortType == "asc" {
  611. page.AddOrderItem(base.OrderItem{Column: "sort_payment_id", Asc: false})
  612. page.AddOrderItem(base.OrderItem{Column: "d.invoice_time", Asc: true})
  613. page.AddOrderItem(base.OrderItem{Column: "d.amount", Asc: true})
  614. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: true})
  615. } else {
  616. page.AddOrderItem(base.OrderItem{Column: "sort_payment_id", Asc: true})
  617. page.AddOrderItem(base.OrderItem{Column: "d.invoice_time", Asc: false})
  618. page.AddOrderItem(base.OrderItem{Column: "d.amount", Asc: false})
  619. page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
  620. }
  621. } else {
  622. resp.Fail("排序字段不正确", c)
  623. return
  624. }
  625. if req.IsExport == 1 {
  626. page.SetPageSize(10000)
  627. page.SetCurrent(1)
  628. }
  629. registerList, total, e := fms.GetInvoicePaymentCensusPageList(page, cond, pars)
  630. if e != nil {
  631. resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
  632. return
  633. }
  634. queryRegisterIds := make([]int, 0)
  635. for i := range registerList {
  636. queryRegisterIds = append(queryRegisterIds, registerList[i].ContractRegisterId)
  637. }
  638. //fmt.Println("queryRegisterIds:",queryRegisterIds)
  639. results := new(fms.InvoicePaymentCensusResp)
  640. if len(queryRegisterIds) > 0 {
  641. // 获取汇总数据IDs, 用于查询合计数据
  642. summaryIdsCond := cond
  643. summaryIdsPars := pars
  644. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
  645. if e != nil {
  646. resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
  647. return
  648. }
  649. //fmt.Println("summaryIds:",summaryIds)
  650. var listErr, totalErr, totalGroupErr error
  651. wg := sync.WaitGroup{}
  652. // 响应列表
  653. respList := make([]*fms.InvoicePaymentCensusItem, 0)
  654. summaryList := make([]*fms.InvoicePaymentSummaryItem, 0)
  655. wg.Add(1)
  656. go func() {
  657. defer wg.Done()
  658. // 获取汇总数据
  659. summaryCond := cond
  660. summaryCond += ` AND a.register_id IN ?`
  661. summaryPars := pars
  662. summaryPars = append(summaryPars, queryRegisterIds)
  663. summaryData, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
  664. if e != nil {
  665. resp.FailMsg("获取失败", "获取商品到款汇总列表失败, Err: "+e.Error(), c)
  666. return
  667. }
  668. summaryList = summaryData
  669. //summaryIds := make([]int, 0)
  670. paymentIds := make([]int, 0)
  671. for i := range summaryList {
  672. //summaryIds = append(summaryIds, summaryList[i].SummaryId)
  673. if summaryList[i].PaymentId > 0 {
  674. paymentIds = append(paymentIds, summaryList[i].PaymentId)
  675. }
  676. }
  677. // 合同套餐
  678. /*contractServiceCond := `contract_register_id IN ?`
  679. contractServicePars := make([]interface{}, 0)
  680. contractServicePars = append(contractServicePars, queryRegisterIds)
  681. contractServiceOB := new(fms.ContractService)
  682. contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
  683. if e != nil {
  684. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  685. return
  686. }
  687. contractServiceMap := make(map[int][]*fms.ContractService, 0)
  688. servicesNameMap := make(map[int][]string, 0)
  689. for i := range contractServiceList {
  690. if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
  691. contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
  692. }
  693. contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
  694. servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
  695. }
  696. */
  697. servicesNameMap, serviceFormatMap, e := fmsService.GetContractServiceNameFormat(queryRegisterIds)
  698. if e != nil {
  699. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  700. return
  701. }
  702. // 到款套餐分配
  703. serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
  704. if len(paymentIds) > 0 {
  705. serviceAmountCond := `contract_payment_id IN ?`
  706. serviceAmountPars := make([]interface{}, 0)
  707. serviceAmountPars = append(serviceAmountPars, paymentIds)
  708. serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  709. serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  710. if e != nil {
  711. listErr = fmt.Errorf("获取到款套餐分配列表失败, Err: %s", e.Error())
  712. return
  713. }
  714. for i := range serviceAmountList {
  715. if serviceAmountMap[serviceAmountList[i].ContractPaymentId] == nil {
  716. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = make([]*fms.ContractPaymentServiceAmount, 0)
  717. }
  718. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = append(serviceAmountMap[serviceAmountList[i].ContractPaymentId], serviceAmountList[i])
  719. }
  720. }
  721. // 重组汇总数据
  722. summaryMap := make(map[int][]*fms.InvoicePaymentCensusInfo)
  723. amountMap := make(map[string]*fms.ContractPaymentServiceAmount)
  724. for i := range summaryList {
  725. v := new(fms.InvoicePaymentCensusInfo)
  726. v.InvoiceId = summaryList[i].InvoiceId
  727. v.InvoiceDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].InvoiceDate)
  728. v.InvoiceAmount = summaryList[i].InvoiceAmount
  729. v.SellerId = summaryList[i].SellerId
  730. v.SellerName = summaryList[i].SellerName
  731. v.SellerGroupId = summaryList[i].SellerGroupId
  732. v.SellerGroupName = summaryList[i].SellerGroupName
  733. v.SellerType = summaryList[i].ServiceProductId
  734. v.PaymentId = summaryList[i].PaymentId
  735. v.PaymentDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].PaymentDate)
  736. v.PaymentAmount = summaryList[i].PaymentAmount
  737. v.PayType = summaryList[i].PayType
  738. // 套餐到款分配
  739. svaList := make([]*fms.ContractPaymentServiceAmountItem, 0)
  740. amountList := serviceAmountMap[summaryList[i].PaymentId]
  741. if amountList != nil {
  742. for i := range amountList {
  743. k := fmt.Sprintf("%d-%d", amountList[i].ContractPaymentId, amountList[i].ServiceTemplateId)
  744. amountMap[k] = amountList[i]
  745. }
  746. }
  747. // 合同对应的所有套餐
  748. svList := serviceFormatMap[summaryList[i].RegisterId]
  749. if svList != nil {
  750. for ii := range svList {
  751. vv := new(fms.ContractPaymentServiceAmountItem)
  752. vv.ServiceTemplateId = svList[ii].ServiceTemplateId
  753. vv.ServiceTemplateName = svList[ii].FormatTitle
  754. vv.ServiceTemplatePid = svList[ii].ServiceTemplatePid
  755. vv.ServiceProductId = svList[ii].ServiceProductId
  756. k2 := fmt.Sprintf("%d-%d", summaryList[i].PaymentId, svList[ii].ServiceTemplateId)
  757. a := amountMap[k2]
  758. if a != nil {
  759. vv.ContractPaymentServiceAmountId = a.ContractPaymentServiceAmountId
  760. vv.ContractPaymentId = a.ContractPaymentId
  761. vv.Amount = a.Amount
  762. }
  763. svaList = append(svaList, vv)
  764. }
  765. }
  766. v.ServiceAmountList = svaList
  767. summaryMap[summaryList[i].SummaryId] = append(summaryMap[summaryList[i].SummaryId], v)
  768. }
  769. // 响应列表
  770. for i := range registerList {
  771. v := new(fms.InvoicePaymentCensusItem)
  772. v.SummaryId = registerList[i].SummaryId
  773. v.ContractRegisterId = registerList[i].ContractRegisterId
  774. v.CompanyName = registerList[i].CompanyName
  775. v.NewCompany = registerList[i].NewCompany
  776. v.StartDate = utils.TimeTransferString(utils.FormatDate, registerList[i].StartDate)
  777. v.EndDate = utils.TimeTransferString(utils.FormatDate, registerList[i].EndDate)
  778. v.ServicesName = servicesNameMap[registerList[i].ContractRegisterId]
  779. v.InvoicePaymentList = summaryMap[registerList[i].SummaryId]
  780. v.ContractType = registerList[i].ContractType
  781. v.ActualPayCompanies = registerList[i].ActualPayCompanies
  782. respList = append(respList, v)
  783. }
  784. }()
  785. // 开票到款金额合计(换算后)
  786. var invoiceTotal, paymentTotal, amountTotal float64
  787. wg.Add(1)
  788. go func() {
  789. defer wg.Done()
  790. if len(summaryIds) == 0 {
  791. return
  792. }
  793. amountTotalCond := `a.id IN ?`
  794. amountTotalPars := make([]interface{}, 0)
  795. amountTotalPars = append(amountTotalPars, summaryIds)
  796. invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
  797. if e != nil {
  798. totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
  799. return
  800. }
  801. invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
  802. paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
  803. if e != nil {
  804. totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
  805. return
  806. }
  807. paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
  808. amountCond := `a.id IN ? AND (a.invoice_id <> 0 OR (a.payment_id <> 0 AND a.invoice_id =0))`
  809. amountPars := make([]interface{}, 0)
  810. amountPars = append(amountPars, summaryIds)
  811. amountSum, e := fms.GetContractSummaryInvoicePaymentAmount(amountCond, amountPars)
  812. if e != nil {
  813. totalErr = fmt.Errorf("获取汇总金额合计失败, Err: %s", e.Error())
  814. return
  815. }
  816. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  817. }()
  818. // 分币种金额统计
  819. invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  820. paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  821. wg.Add(1)
  822. go func() {
  823. defer wg.Done()
  824. currencyOB := new(fms.CurrencyUnit)
  825. currencyCond := `enable = 1`
  826. currencyPars := make([]interface{}, 0)
  827. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  828. if e != nil {
  829. totalGroupErr = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
  830. return
  831. }
  832. unitMap := make(map[string]string)
  833. for i := range currencyList {
  834. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  835. invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  836. Name: currencyList[i].Name,
  837. UnitName: currencyList[i].UnitName,
  838. Code: currencyList[i].Code,
  839. FlagImg: currencyList[i].FlagImg,
  840. })
  841. paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  842. Name: currencyList[i].Name,
  843. UnitName: currencyList[i].UnitName,
  844. Code: currencyList[i].Code,
  845. FlagImg: currencyList[i].FlagImg,
  846. })
  847. }
  848. if len(summaryIds) == 0 {
  849. return
  850. }
  851. totalGroupCond := `a.id IN ?`
  852. totalGroupPars := make([]interface{}, 0)
  853. totalGroupPars = append(totalGroupPars, summaryIds)
  854. invoiceSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 1)
  855. if e != nil {
  856. totalGroupErr = fmt.Errorf("获取汇总货币合计开票金额失败, Err: %s", e.Error())
  857. return
  858. }
  859. paymentSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 2)
  860. if e != nil {
  861. totalGroupErr = fmt.Errorf("获取汇总货币合计到款金额失败, Err: %s", e.Error())
  862. return
  863. }
  864. invoiceSumMap := make(map[string]float64)
  865. paymentSumMap := make(map[string]float64)
  866. for i := range invoiceSumGroup {
  867. invoiceSumMap[invoiceSumGroup[i].CurrencyUnit] = invoiceSumGroup[i].OriginAmountTotal
  868. continue
  869. }
  870. for i := range paymentSumGroup {
  871. paymentSumMap[paymentSumGroup[i].CurrencyUnit] = paymentSumGroup[i].OriginAmountTotal
  872. continue
  873. }
  874. for i := range invoiceCurrencyTotals {
  875. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
  876. invoiceCurrencyTotals[i].Amount = a
  877. }
  878. for i := range paymentCurrencyTotals {
  879. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
  880. paymentCurrencyTotals[i].Amount = a
  881. }
  882. }()
  883. wg.Wait()
  884. if listErr != nil {
  885. resp.FailMsg("获取失败", listErr.Error(), c)
  886. return
  887. }
  888. if totalErr != nil {
  889. resp.FailMsg("获取失败", totalErr.Error(), c)
  890. return
  891. }
  892. if totalGroupErr != nil {
  893. resp.FailMsg("获取失败", totalGroupErr.Error(), c)
  894. return
  895. }
  896. results.DataList = respList
  897. results.InvoiceTotal = invoiceTotal
  898. results.PaymentTotal = paymentTotal
  899. results.AmountTotal = amountTotal
  900. results.InvoiceCurrencyTotal = invoiceCurrencyTotals
  901. results.PaymentCurrencyTotal = paymentCurrencyTotals
  902. }
  903. // 是否导出
  904. if req.IsExport == 1 {
  905. ExportInvoicePaymentCensusList(c, results)
  906. return
  907. }
  908. page.SetTotal(total)
  909. baseData := new(base.BaseData)
  910. baseData.SetPage(page)
  911. baseData.SetList(results)
  912. resp.OkData("获取成功", baseData, c)
  913. }
  914. // IncomeList
  915. // @Title 业务收入统计表
  916. // @Description 获取业务收入统计表接口
  917. // @Param ListParam query int false "套餐类型: 0-月度; 1-季度; 2-半年度;3-年度;4-月度累计"
  918. // @Success 200 {object} fms.CensusIncomeChartResp
  919. // @router /census/income/list [get]
  920. func (this *InvoicePaymentController) IncomeList(c *gin.Context) {
  921. var req fms.IncomeListReq
  922. if e := c.BindQuery(&req); e != nil {
  923. err, ok := e.(validator.ValidationErrors)
  924. if !ok {
  925. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  926. return
  927. }
  928. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  929. return
  930. }
  931. //收入统计
  932. var incomeList models.CensusIncomeChartResp
  933. ch := make(chan models.CensusIncomeChartResp, 1)
  934. go getCensusIncomeListV2(ch, req)
  935. for v := range ch {
  936. incomeList = v
  937. close(ch)
  938. }
  939. // 是否导出
  940. if req.IsExport == 1 {
  941. ExportIncomeList(c, incomeList.DataList)
  942. return
  943. }
  944. resp.OkData("获取成功", incomeList, c)
  945. }
  946. func getCensusIncomeListV2(ch chan models.CensusIncomeChartResp, req fms.IncomeListReq) (incomeChart models.CensusIncomeChartResp, err error) {
  947. defer func() {
  948. if err != nil {
  949. global.LOG.Error(err)
  950. go alarm_msg.SendAlarmMsg("获取近两年的收入统计数据异常,Err:"+err.Error(), 3)
  951. //go utils.SendEmail(utils.APPNAME+"获取近12个月的收入统计数据异常:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
  952. }
  953. ch <- incomeChart
  954. }()
  955. //todayStr := utils.GetToday("20060102")
  956. //key := "admin:home:fmsIncomeList:" + todayStr
  957. //
  958. //redisJsonData, redisErr := global.Redis.Get(context.TODO(), key).Result()
  959. //if redisErr != nil {
  960. //获取最新的开票到款日期
  961. cond := ``
  962. pars := make([]interface{}, 0)
  963. if req.SellerIds != "" {
  964. sellerIds := strings.Split(req.SellerIds, ",")
  965. cond += ` AND (a.seller_id in ? )`
  966. pars = append(pars, sellerIds, sellerIds)
  967. }
  968. if req.CompanyType == 1 {
  969. cond += ` AND b.new_company = 1 `
  970. } else if req.CompanyType == 2 {
  971. cond += ` AND b.new_company = 0 `
  972. }
  973. invoiceItem, err := fms.GetLatestIncome(cond, pars)
  974. if err != nil {
  975. err = fmt.Errorf("获取最新的开票或到款日期, Err: %s", err.Error())
  976. return
  977. }
  978. invoiceItem.InvoiceDate = invoiceItem.InvoiceDate.AddDate(0,0,-invoiceItem.InvoiceDate.Day()+1)
  979. var dateSlice []string
  980. var totalMoneySlice, prevTotalMoneySlice []float64
  981. var yoySlice []string
  982. var yearNum, monthNum int
  983. var reqStartDate, reqEndDate time.Time
  984. historyTime, _ := time.Parse(utils.FormatDate, "2023-04-01")
  985. if req.StartDate != "" && req.EndDate != "" {
  986. st := fmt.Sprint(req.StartDate, "-01 00:00:00")
  987. ed := fmt.Sprint(req.EndDate, "-01 23:59:59")
  988. reqStartDate, _ = time.Parse(utils.FormatDateTime, st)
  989. reqEndDate, _ = time.Parse(utils.FormatDateTime, ed)
  990. if reqEndDate.After(invoiceItem.InvoiceDate) {
  991. yearNum = invoiceItem.InvoiceDate.Year() - reqStartDate.Year()
  992. monthNum = int(invoiceItem.InvoiceDate.Month() - reqStartDate.Month())
  993. } else {
  994. yearNum = reqEndDate.Year() - reqStartDate.Year()
  995. monthNum = int(reqEndDate.Month() - reqStartDate.Month())
  996. }
  997. } else {
  998. yearNum = invoiceItem.InvoiceDate.Year() - 2020
  999. monthNum = int(invoiceItem.InvoiceDate.Month() - 1)
  1000. }
  1001. if yearNum < 0 {
  1002. yearNum = -yearNum
  1003. }
  1004. if monthNum < 0 {
  1005. monthNum = -monthNum
  1006. }
  1007. numMonth := yearNum*12 + monthNum //共存在多少个月
  1008. //if req.ListParam == "1" {
  1009. // numMonth = numMonth / 3
  1010. //} else if req.ListParam == "2" {
  1011. // numMonth = numMonth / 6
  1012. //} else if req.ListParam == "3" {
  1013. // numMonth = numMonth / 12
  1014. //}
  1015. // 累计值
  1016. var accumulate float64
  1017. var partAccumulate float64
  1018. var historyAccumulate float64
  1019. var partHistoryAccumulate float64
  1020. //dataList := make([]*fms.IncomeSummaryItem, 0)
  1021. //historydataList := make([]*fms.IncomeSummaryItem, 0)
  1022. fmt.Println("numMonth:", numMonth)
  1023. fmt.Println("InvoiceDate:", invoiceItem.InvoiceDate)
  1024. var j int
  1025. for i := 0; i <= numMonth; i++ {
  1026. //timeNow, _ := time.Parse("2006-01", time.Now().Format("2006-01"))
  1027. var endDateTime time.Time
  1028. var prevEndDateTime time.Time
  1029. var prevStartDate, prevEndDate string
  1030. var startDate, endDate string
  1031. //开始日期
  1032. if req.StartDate != "" && req.EndDate != "" {
  1033. startDate = reqStartDate.AddDate(0, i, 0).Format("2006-01")
  1034. prevStartDate = reqStartDate.AddDate(-1, i, 0).Format("2006-01")
  1035. } else {
  1036. startDate = invoiceItem.InvoiceDate.AddDate(0, i-numMonth, 0).Format("2006-01")
  1037. prevStartDate = invoiceItem.InvoiceDate.AddDate(-1, i-numMonth, 0).Format("2006-01")
  1038. }
  1039. startDate = fmt.Sprint(startDate, "-01")
  1040. prevStartDate = fmt.Sprint(prevStartDate, "-01")
  1041. startDateTime, _ := time.Parse(utils.FormatDate, startDate)
  1042. prevStartDateTime, _ := time.Parse(utils.FormatDate, prevStartDate)
  1043. //结束日期
  1044. if req.StartDate != "" && req.EndDate != "" {
  1045. endDateTime = reqStartDate.AddDate(0, i+1, -1)
  1046. prevEndDateTime = reqStartDate.AddDate(-1, i+1, -1)
  1047. if reqEndDate.After(invoiceItem.InvoiceDate) {
  1048. endDateTime = invoiceItem.InvoiceDate.AddDate(0, i-numMonth+1, -1)
  1049. prevEndDateTime = invoiceItem.InvoiceDate.AddDate(-1, i-numMonth+1, -1)
  1050. }
  1051. } else {
  1052. endDateTime = invoiceItem.InvoiceDate.AddDate(0, i-numMonth+1, -1)
  1053. prevEndDateTime = invoiceItem.InvoiceDate.AddDate(-1, i-numMonth+1, -1)
  1054. }
  1055. endDate = endDateTime.Format(utils.FormatDate)
  1056. prevEndDate = prevEndDateTime.Format(utils.FormatDate)
  1057. cond := `1 = 1`
  1058. histrtyCond := `1 = 1`
  1059. pars := make([]interface{}, 0)
  1060. historyPars := make([]interface{}, 0)
  1061. prevCond := `1 = 1`
  1062. prevHistoryCond := `1 = 1`
  1063. prevPars := make([]interface{}, 0)
  1064. prevHistoryPars := make([]interface{}, 0)
  1065. if req.CompanyType == 1 {
  1066. cond += ` AND b.new_company = 1 `
  1067. prevCond += ` AND b.new_company = 1 `
  1068. histrtyCond += ` AND new_company = 1 `
  1069. prevHistoryCond += ` AND new_company = 1 `
  1070. } else if req.CompanyType == 2 {
  1071. cond += ` AND b.new_company = 0 `
  1072. prevCond += ` AND b.new_company = 0 `
  1073. histrtyCond += ` AND new_company = 0 `
  1074. prevHistoryCond += ` AND new_company = 0 `
  1075. }
  1076. if req.SellerIds != "" {
  1077. sellerIds := strings.Split(req.SellerIds, ",")
  1078. cond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
  1079. pars = append(pars, sellerIds, sellerIds)
  1080. prevCond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
  1081. prevPars = append(prevPars, sellerIds, sellerIds)
  1082. histrtyCond += ` AND seller_id in ? `
  1083. prevHistoryCond += ` AND seller_id in ? `
  1084. historyPars = append(historyPars, sellerIds)
  1085. prevHistoryPars = append(prevHistoryPars, sellerIds)
  1086. }
  1087. //fmt.Println("i:",i)
  1088. //fmt.Println("j:",j)
  1089. {
  1090. //本期
  1091. st := fmt.Sprint(startDate, " 00:00:00")
  1092. ed := fmt.Sprint(endDate, " 23:59:59")
  1093. //校验日期,分段查询
  1094. if startDateTime.After(historyTime) || startDateTime.Equal(historyTime) {
  1095. //全部走新查询
  1096. //fmt.Println("新查询")
  1097. cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  1098. pars = append(pars, st, ed, st, ed)
  1099. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
  1100. if e != nil {
  1101. return
  1102. }
  1103. // 开票到款金额合计(换算后)
  1104. var amountTotal float64
  1105. if len(summaryIds) > 0 {
  1106. amountCond := `a.id IN ? AND (a.invoice_id <> 0 OR (a.payment_id <> 0 AND a.invoice_id =0))`
  1107. amountPars := make([]interface{}, 0)
  1108. amountPars = append(amountPars, summaryIds)
  1109. amountCond += ` AND (b.invoice_time BETWEEN ? AND ?)`
  1110. amountPars = append(amountPars, st, ed)
  1111. results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  1112. if e != nil {
  1113. err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1114. return
  1115. }
  1116. //dataList = append(dataList, results...)
  1117. var amountSum float64
  1118. for _, result := range results {
  1119. incomeChart.DataList = append(incomeChart.DataList, result)
  1120. amountSum += result.Amount
  1121. }
  1122. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1123. accumulate += amountTotal
  1124. partAccumulate += amountTotal
  1125. }
  1126. if i == j || i == numMonth {
  1127. if req.ListParam == "4" {
  1128. totalMoneySlice = append(totalMoneySlice, accumulate)
  1129. } else if req.ListParam == "0" {
  1130. totalMoneySlice = append(totalMoneySlice, amountTotal)
  1131. } else if i > 0 || i == numMonth {
  1132. totalMoneySlice = append(totalMoneySlice, partAccumulate)
  1133. fmt.Println("partAccumulate:",partAccumulate)
  1134. partAccumulate = 0.0
  1135. }
  1136. }
  1137. } else if endDateTime.Before(historyTime) || endDateTime.Equal(historyTime) {
  1138. //全部走旧查询
  1139. //fmt.Println("旧查询")
  1140. fmt.Println("st:",st)
  1141. fmt.Println("ed:",ed)
  1142. histrtyCond += ` AND (invoice_time BETWEEN ? AND ? )`
  1143. historyPars = append(historyPars, st, ed)
  1144. //fmt.Println("st:",st)
  1145. //fmt.Println("ed:",ed)
  1146. // 开票到款金额合计(换算后)
  1147. var amountTotal float64
  1148. results, e := fms.GetIncomeHistory(histrtyCond, historyPars)
  1149. if e != nil {
  1150. err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1151. return
  1152. }
  1153. var amountSum float64
  1154. //dataList = append(dataList, results...)
  1155. for _, result := range results {
  1156. incomeChart.DataList = append(incomeChart.DataList, result)
  1157. amountSum += result.Amount
  1158. }
  1159. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1160. accumulate += amountTotal
  1161. partAccumulate += amountTotal
  1162. if i == j || i == numMonth {
  1163. if req.ListParam == "4" {
  1164. totalMoneySlice = append(totalMoneySlice, accumulate)
  1165. } else if req.ListParam == "0" {
  1166. totalMoneySlice = append(totalMoneySlice, amountTotal)
  1167. } else if i > 0 || i == numMonth {
  1168. totalMoneySlice = append(totalMoneySlice, partAccumulate)
  1169. partAccumulate = 0.0
  1170. }
  1171. }
  1172. //fmt.Println("partAccumulate:",partAccumulate)
  1173. }
  1174. //else {
  1175. // //刚好跨过20230301的,各查一部分
  1176. // //fmt.Println("查询3")
  1177. // var historyAmountTotal float64
  1178. // histrtyCond += ` AND (invoice_time BETWEEN ? AND '2023-03-01 23:59:59' ) `
  1179. // historyPars = append(historyPars, st)
  1180. // //// 开票到款金额合计(换算后)
  1181. // var amountTotal float64
  1182. // results, e := fms.GetIncomeHistory(histrtyCond, historyPars)
  1183. // if e != nil {
  1184. // err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1185. // return
  1186. // }
  1187. // var amountSum float64
  1188. // for _, result := range results {
  1189. // incomeChart.DataList = append(incomeChart.DataList, result)
  1190. // amountSum += result.Amount
  1191. // }
  1192. // amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1193. // accumulate += amountTotal
  1194. // historyAmountTotal = amountTotal
  1195. //
  1196. // cond += ` AND ((c.invoice_time BETWEEN '2023-03-02 00:00:00' AND ?) or (d.invoice_time BETWEEN '2023-03-02 00:00:00' AND ?))`
  1197. // pars = append(pars, ed, ed)
  1198. // summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
  1199. // if e != nil {
  1200. // return
  1201. // }
  1202. //
  1203. // if len(summaryIds) > 0 {
  1204. // amountCond := `a.id IN ? AND (a.invoice_id <> 0 OR (a.payment_id <> 0 AND a.invoice_id =0))`
  1205. // amountPars := make([]interface{}, 0)
  1206. // amountPars = append(amountPars, summaryIds)
  1207. // results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  1208. // if e != nil {
  1209. // err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1210. // return
  1211. // }
  1212. // var amountSum float64
  1213. // for _, result := range results {
  1214. // incomeChart.DataList = append(incomeChart.DataList, result)
  1215. // amountSum += result.Amount
  1216. // }
  1217. // amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1218. // accumulate += amountTotal
  1219. // }
  1220. // if req.ListParam == "4" {
  1221. // totalMoneySlice = append(totalMoneySlice, accumulate)
  1222. // } else {
  1223. // totalMoneySlice = append(totalMoneySlice, amountTotal+historyAmountTotal)
  1224. // }
  1225. //
  1226. //}
  1227. }
  1228. { //去年同期,用于计算同比值
  1229. prevSt := fmt.Sprint(prevStartDate, " 00:00:00")
  1230. prevEd := fmt.Sprint(prevEndDate, " 23:59:59")
  1231. //校验日期,分段查询
  1232. if prevStartDateTime.After(historyTime) || prevStartDateTime.Equal(historyTime) {
  1233. //全部走新查询
  1234. prevCond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  1235. prevPars = append(prevPars, prevSt, prevEd, prevSt, prevEd)
  1236. prevSummaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(prevCond, prevPars)
  1237. if e != nil {
  1238. return
  1239. }
  1240. // 开票到款金额合计(换算后)
  1241. var prevAmountTotal float64
  1242. if len(prevSummaryIds) > 0 {
  1243. amountCond := `a.id IN ? AND (a.invoice_id <> 0 OR (a.payment_id <> 0 AND a.invoice_id =0))`
  1244. amountPars := make([]interface{}, 0)
  1245. amountPars = append(amountPars, prevSummaryIds)
  1246. amountCond += ` AND (b.invoice_time BETWEEN ? AND ?)`
  1247. amountPars = append(amountPars, prevSt, prevEd)
  1248. results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  1249. if e != nil {
  1250. err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1251. return
  1252. }
  1253. var amountSum float64
  1254. //historydataList = append(historydataList, results...)
  1255. for _, result := range results {
  1256. amountSum += result.Amount
  1257. }
  1258. prevAmountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1259. historyAccumulate += prevAmountTotal
  1260. partHistoryAccumulate += prevAmountTotal
  1261. }
  1262. if i == j || i == numMonth {
  1263. if req.ListParam == "4" {
  1264. prevTotalMoneySlice = append(prevTotalMoneySlice, historyAccumulate)
  1265. } else if req.ListParam == "0" {
  1266. prevTotalMoneySlice = append(prevTotalMoneySlice, prevAmountTotal)
  1267. } else if i > 0 || i == numMonth {
  1268. prevTotalMoneySlice = append(prevTotalMoneySlice, partHistoryAccumulate)
  1269. fmt.Println("partHistoryAccumulate:",partHistoryAccumulate)
  1270. partHistoryAccumulate = 0.0
  1271. }
  1272. }
  1273. } else if prevEndDateTime.Before(historyTime) || prevEndDateTime.Equal(historyTime) {
  1274. //全部走旧查询
  1275. fmt.Println("prevSt:",prevSt)
  1276. fmt.Println("prevEd:",prevEd)
  1277. prevHistoryCond += ` AND (invoice_time BETWEEN ? AND ?)`
  1278. prevHistoryPars = append(prevHistoryPars, prevSt, prevEd)
  1279. // 开票到款金额合计(换算后)
  1280. var amountTotal float64
  1281. results, e := fms.GetIncomeHistory(prevHistoryCond, prevHistoryPars)
  1282. if e != nil {
  1283. err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1284. return
  1285. }
  1286. //historydataList = append(historydataList, results...)
  1287. var amountSum float64
  1288. for _, result := range results {
  1289. amountSum += result.Amount
  1290. }
  1291. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1292. historyAccumulate += amountTotal
  1293. partHistoryAccumulate += amountTotal
  1294. if i == j || i == numMonth {
  1295. if req.ListParam == "4" {
  1296. prevTotalMoneySlice = append(prevTotalMoneySlice, historyAccumulate)
  1297. } else if req.ListParam == "0" {
  1298. prevTotalMoneySlice = append(prevTotalMoneySlice, amountTotal)
  1299. } else if i > 0 || i == numMonth {
  1300. prevTotalMoneySlice = append(prevTotalMoneySlice, partHistoryAccumulate)
  1301. fmt.Println("partHistoryAccumulate:",partHistoryAccumulate)
  1302. partHistoryAccumulate = 0.0
  1303. }
  1304. }
  1305. //fmt.Println("partHistoryAccumulate:",partHistoryAccumulate)
  1306. }
  1307. //else {
  1308. // //刚好跨过20230301的,各查一部分
  1309. // var historyAmountTotal float64
  1310. // prevHistoryCond += ` AND (invoice_time BETWEEN ? AND '2023-03-01 23:59:59' ) `
  1311. // prevHistoryPars = append(prevHistoryPars, prevSt)
  1312. // // 开票到款金额合计(换算后)
  1313. // var amountTotal float64
  1314. // results, e := fms.GetIncomeHistory(prevHistoryCond, prevHistoryPars)
  1315. // if e != nil {
  1316. // err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1317. // return
  1318. // }
  1319. // var amountSum float64
  1320. // for _, result := range results {
  1321. // amountSum += result.Amount
  1322. // }
  1323. // amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1324. // historyAccumulate += amountTotal
  1325. //
  1326. //
  1327. // prevCond += ` AND ((c.invoice_time BETWEEN '2023-03-02 00:00:00' AND ?) or (d.invoice_time BETWEEN '2023-03-02 00:00:00' AND ?))`
  1328. // prevPars = append(prevPars, prevEd, prevEd)
  1329. // prevSummaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(prevCond, prevPars)
  1330. // if e != nil {
  1331. // return
  1332. // }
  1333. //
  1334. // // 开票到款金额合计(换算后)
  1335. // var prevAmountTotal float64
  1336. //
  1337. // if len(prevSummaryIds) > 0 {
  1338. // amountCond := `a.id IN ? AND (a.invoice_id <> 0 OR (a.payment_id <> 0 AND a.invoice_id =0))`
  1339. // amountPars := make([]interface{}, 0)
  1340. // amountPars = append(amountPars, prevSummaryIds)
  1341. // results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  1342. // if e != nil {
  1343. // err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1344. // return
  1345. // }
  1346. // var amountSum float64
  1347. // for _, result := range results {
  1348. // amountSum += result.Amount
  1349. // }
  1350. // prevAmountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1351. // historyAccumulate += prevAmountTotal
  1352. // }
  1353. // if req.ListParam == "4" {
  1354. // prevTotalMoneySlice = append(prevTotalMoneySlice, historyAccumulate)
  1355. // } else {
  1356. // prevTotalMoneySlice = append(prevTotalMoneySlice, prevAmountTotal+historyAmountTotal)
  1357. // }
  1358. //}
  1359. if req.ListParam == "1" && i == j {
  1360. if i == 0{
  1361. dateSlice = append(dateSlice, startDateTime.AddDate(0, 2, 0).Format("06/01"))
  1362. j = j + 2
  1363. } else {
  1364. dateSlice = append(dateSlice, startDateTime.AddDate(0, 3, 0).Format("06/01"))
  1365. j = j + 3
  1366. }
  1367. } else if req.ListParam == "2" && i == j {
  1368. if i == 0{
  1369. dateSlice = append(dateSlice, startDateTime.AddDate(0, 5, 0).Format("06/01"))
  1370. j = j + 5
  1371. } else {
  1372. dateSlice = append(dateSlice, startDateTime.AddDate(0, 6, 0).Format("06/01"))
  1373. j = j + 6
  1374. }
  1375. } else if req.ListParam == "3" && i == j {
  1376. if i == 0{
  1377. dateSlice = append(dateSlice, startDateTime.AddDate(0, 11, 0).Format("06/01"))
  1378. j = j + 11
  1379. } else {
  1380. dateSlice = append(dateSlice, startDateTime.AddDate(0, 12, 0).Format("06/01"))
  1381. j = j + 12
  1382. }
  1383. } else if i == j {
  1384. dateSlice = append(dateSlice, startDateTime.Format("06/01"))
  1385. j++
  1386. }
  1387. }
  1388. }
  1389. //for i, v := range dataList {
  1390. // amountTotal, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.Amount), 64)
  1391. // accumulate += amountTotal
  1392. //
  1393. // if req.ListParam == "4" {
  1394. // totalMoneySlice = append(totalMoneySlice, accumulate)
  1395. // } else {
  1396. // totalMoneySlice = append(totalMoneySlice, amountTotal)
  1397. // }
  1398. // if req.ListParam == "1" {
  1399. // i = i + 2
  1400. // } else if req.ListParam == "2" {
  1401. // i = i + 5
  1402. // } else if req.ListParam == "3" {
  1403. // i = i + 11
  1404. // }
  1405. //}
  1406. //
  1407. //for i, v := range historydataList {
  1408. // historyAmountTotal, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.Amount), 64)
  1409. // historyAccumulate += historyAmountTotal
  1410. //
  1411. // if req.ListParam == "4" {
  1412. // prevTotalMoneySlice = append(prevTotalMoneySlice, historyAccumulate)
  1413. // } else {
  1414. // prevTotalMoneySlice = append(prevTotalMoneySlice, historyAmountTotal)
  1415. // }
  1416. // if req.ListParam == "1" {
  1417. // i = i + 2
  1418. // } else if req.ListParam == "2" {
  1419. // i = i + 5
  1420. // } else if req.ListParam == "3" {
  1421. // i = i + 11
  1422. // }
  1423. //}
  1424. fmt.Println("prevTotalMoneySlice:", len(prevTotalMoneySlice))
  1425. fmt.Println("totalMoneySlice:", len(totalMoneySlice))
  1426. //计算同比值
  1427. for i := range prevTotalMoneySlice {
  1428. var yoy float64
  1429. var yoyStr string
  1430. //fmt.Println("1:", prevTotalMoneySlice[i])
  1431. //fmt.Println("2:", totalMoneySlice[i])
  1432. //fmt.Println("3:", totalMoneySlice[i]-prevTotalMoneySlice[i])
  1433. if prevTotalMoneySlice[i] != 0 && totalMoneySlice[i] != 0 {
  1434. yoy = (totalMoneySlice[i] - prevTotalMoneySlice[i]) / prevTotalMoneySlice[i]
  1435. yoyStr = fmt.Sprintf("%.4f", yoy)
  1436. if i == len(prevTotalMoneySlice)-1 && i > 0 && req.ListParam == "3"{
  1437. fmt.Println("totalMoneySlice[i-1]:",totalMoneySlice[i-1])
  1438. yoy = (totalMoneySlice[i] - totalMoneySlice[i-1]) / totalMoneySlice[i-1]
  1439. yoyStr = fmt.Sprintf("%.4f", yoy)
  1440. }
  1441. }
  1442. yoySlice = append(yoySlice, yoyStr)
  1443. }
  1444. incomeChart.Title = "开票到款统计图"
  1445. incomeChart.Date = dateSlice
  1446. incomeChart.TotalMoney = totalMoneySlice
  1447. incomeChart.PrevTotalMoney = prevTotalMoneySlice
  1448. incomeChart.Yoy = yoySlice
  1449. //redisJsonData, err := json.Marshal(incomeChart)
  1450. //if err == nil {
  1451. // global.Redis.SetEX(context.TODO(), key, string(redisJsonData), time.Minute*30)
  1452. //}
  1453. //} else {
  1454. // err = json.Unmarshal([]byte(redisJsonData), &incomeChart)
  1455. // if err != nil {
  1456. // fmt.Println("近两年的收入统计数据,json转换失败")
  1457. // }
  1458. //}
  1459. return
  1460. }
  1461. // ExportIncomeList 导出业务收入统计列表
  1462. func ExportIncomeList(c *gin.Context, list []*fms.IncomeSummaryItem) {
  1463. // 生成Excel文件
  1464. xlsxFile := xlsx.NewFile()
  1465. style := xlsx.NewStyle()
  1466. alignment := xlsx.Alignment{
  1467. Horizontal: "center",
  1468. Vertical: "center",
  1469. WrapText: true,
  1470. }
  1471. style.Alignment = alignment
  1472. style.ApplyAlignment = true
  1473. sheetName := "业务收入统计表"
  1474. sheet, err := xlsxFile.AddSheet(sheetName)
  1475. if err != nil {
  1476. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  1477. return
  1478. }
  1479. // 数据表头
  1480. rowTitle := []string{"开票日期", "新客户(1)", "客户名称", "金额", "销售"}
  1481. titleRow := sheet.AddRow()
  1482. for i := range rowTitle {
  1483. v := titleRow.AddCell()
  1484. v.SetString(rowTitle[i])
  1485. v.SetStyle(style)
  1486. }
  1487. incomeSummaryItemList := make(fms.IncomeSummaryItemList, 0)
  1488. incomeSummaryItemList = list
  1489. sort.Sort(incomeSummaryItemList)
  1490. // 填充数据
  1491. for _, v := range incomeSummaryItemList {
  1492. dataRow := sheet.AddRow()
  1493. dataRow.AddCell().SetString(v.InvoiceDate.Format(utils.FormatDate)) // 开票日期
  1494. dataRow.AddCell().SetString(strconv.Itoa(v.NewCompany)) // 新客户
  1495. dataRow.AddCell().SetString(v.CompanyName) // 客户名称
  1496. dataRow.AddCell().SetString(fmt.Sprint(v.Amount)) // 金额
  1497. dataRow.AddCell().SetString(v.SellerName) // 销售员
  1498. }
  1499. // 输出文件
  1500. var buffer bytes.Buffer
  1501. _ = xlsxFile.Write(&buffer)
  1502. content := bytes.NewReader(buffer.Bytes())
  1503. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1504. fileName := sheetName + randStr + ".xlsx"
  1505. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  1506. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1507. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  1508. }
  1509. // List
  1510. // @Title 未开票统计列表
  1511. // @Description 未开票统计列表
  1512. // @Param Keyword query string false "关键词"
  1513. // @Param SellGroupId query int false "销售组别ID"
  1514. // @Param ServiceType query int false "套餐类型"
  1515. // @Param StartDate query string false "合同开始日期"
  1516. // @Param EndDate query string false "合同结束日期"
  1517. // @Param TimeType query int false "时间类型: 1-开票时间; 2-到款时间"
  1518. // @Param HasInvoice query string false "是否已开票: 0-否; 1-是; 空-全部"
  1519. // @Param HasPayment query string false "是否已到款: 0-否; 1-是; 空-全部"
  1520. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  1521. // @Param ListParam query int false "套餐类型: 0-全部; 1-ficc; 2-权益"
  1522. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'start_date':开票日 、 'end_date':到款日"
  1523. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
  1524. // @Success 200 {object} fms.ContractRegisterItem
  1525. // @router /census/invoice_payment/not_invoice/list [get]
  1526. func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
  1527. var req fms.InvoicePaymentCensusListReq
  1528. if e := c.BindQuery(&req); e != nil {
  1529. err, ok := e.(validator.ValidationErrors)
  1530. if !ok {
  1531. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  1532. return
  1533. }
  1534. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1535. return
  1536. }
  1537. cond := `1 = 1`
  1538. pars := make([]interface{}, 0)
  1539. // 客户姓名/合同编号
  1540. if req.Keyword != "" {
  1541. kw := "%" + req.Keyword + "%"
  1542. cond += ` AND (b.company_name LIKE ? OR b.contract_code LIKE ?)`
  1543. pars = append(pars, kw, kw)
  1544. }
  1545. if req.SellerIds != "" {
  1546. sellerIds := strings.Split(req.SellerIds, ",")
  1547. cond += ` AND (c.seller_id in ?)`
  1548. pars = append(pars, sellerIds)
  1549. }
  1550. // 套餐筛选
  1551. if req.ServiceTypes != "" {
  1552. serviceTypes := strings.Split(req.ServiceTypes, ",")
  1553. tempRegisterIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
  1554. if e != nil {
  1555. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  1556. return
  1557. }
  1558. if len(tempRegisterIds) > 0 {
  1559. cond += ` AND a.register_id IN ?`
  1560. pars = append(pars, tempRegisterIds)
  1561. } else {
  1562. cond += ` AND 1 = 2`
  1563. }
  1564. }
  1565. if req.ListParam == 1{
  1566. cond += ` AND a.service_product_id = 1 `
  1567. }else if req.ListParam == 2 {
  1568. cond += ` AND a.service_product_id = 2 `
  1569. }
  1570. // 开票到款日期
  1571. //if req.TimeType != 0 {
  1572. // if req.TimeType == 1 && req.StartDate != "" && req.EndDate != "" {
  1573. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  1574. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1575. // cond += ` AND (c.invoice_time BETWEEN ? AND ?) `
  1576. // pars = append(pars, st, ed)
  1577. // }else if req.TimeType == 2 && req.StartDate != "" && req.EndDate != "" {
  1578. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  1579. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1580. // cond += ` AND (d.invoice_time BETWEEN ? AND ?) `
  1581. // pars = append(pars, st, ed)
  1582. // }else if req.TimeType == 3 && req.StartDate != "" && req.EndDate != "" {
  1583. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  1584. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1585. // cond += ` AND (c.invoice_time BETWEEN ? AND ?) AND (d.invoice_time BETWEEN ? AND ?) `
  1586. // pars = append(pars, st, ed, st, ed)
  1587. // }
  1588. //} else if req.StartDate != "" && req.EndDate != "" {
  1589. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  1590. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1591. // cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  1592. // pars = append(pars, st, ed, st, ed)
  1593. //}
  1594. //if req.HasInvoice == "1" {
  1595. // cond += ` AND a.invoice_id > 0 `
  1596. //}else if req.HasInvoice == "0" {
  1597. // cond += ` AND a.invoice_id = 0 `
  1598. //}
  1599. //
  1600. //if req.HasPayment == "1" {
  1601. // cond += ` AND a.payment_id > 0 `
  1602. //}else if req.HasPayment == "0" {
  1603. // cond += ` AND a.payment_id = 0 `
  1604. //}
  1605. page := new(base.Page)
  1606. page.SetPageSize(req.PageSize)
  1607. page.SetCurrent(req.Current)
  1608. //排序
  1609. if req.SortType == "" {
  1610. req.SortType = "desc"
  1611. }
  1612. if req.SortType != "desc" && req.SortType != "asc" {
  1613. resp.Fail("排序类型不正确", c)
  1614. return
  1615. }
  1616. if req.SortParam == "" {
  1617. page.AddOrderItem(base.OrderItem{Column: "e.start_date", Asc: false})
  1618. }else if req.SortParam == "start_date" {
  1619. if req.SortType == "asc" {
  1620. page.AddOrderItem(base.OrderItem{Column: "e.start_date", Asc: true})
  1621. }else{
  1622. page.AddOrderItem(base.OrderItem{Column: "e.start_date", Asc: false})
  1623. }
  1624. }else if req.SortParam == "end_date" {
  1625. if req.SortType == "asc" {
  1626. page.AddOrderItem(base.OrderItem{Column: "e.end_date", Asc: true})
  1627. }else{
  1628. page.AddOrderItem(base.OrderItem{Column: "e.end_date", Asc: false})
  1629. }
  1630. }else{
  1631. resp.Fail("排序字段不正确", c)
  1632. return
  1633. }
  1634. if req.IsExport == 1 {
  1635. page.SetPageSize(10000)
  1636. page.SetCurrent(1)
  1637. }
  1638. registerList, total, e := fms.GetInvoiceCensusPageList(page, cond, pars)
  1639. if e != nil {
  1640. resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
  1641. return
  1642. }
  1643. queryRegisterIds := make([]int, 0)
  1644. for i := range registerList {
  1645. queryRegisterIds = append(queryRegisterIds, registerList[i].ContractRegisterId)
  1646. }
  1647. //fmt.Println("queryRegisterIds:",queryRegisterIds)
  1648. results := new(fms.NotInvoicePaymentCensusResp)
  1649. if len(queryRegisterIds) > 0 {
  1650. // 获取汇总数据IDs, 用于查询合计数据
  1651. summaryIdsCond := cond
  1652. summaryIdsPars := pars
  1653. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
  1654. if e != nil {
  1655. resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
  1656. return
  1657. }
  1658. //fmt.Println("summaryIds:",summaryIds)
  1659. var listErr, totalErr, totalGroupErr error
  1660. wg := sync.WaitGroup{}
  1661. // 响应列表
  1662. respList := make([]*fms.InvoicePaymentCensusItem, 0)
  1663. summaryList := make([]*fms.InvoicePaymentSummaryItem, 0)
  1664. wg.Add(1)
  1665. go func() {
  1666. defer wg.Done()
  1667. // 获取汇总数据
  1668. summaryCond := cond
  1669. summaryCond += ` AND a.register_id IN ?`
  1670. summaryPars := pars
  1671. summaryPars = append(summaryPars, queryRegisterIds)
  1672. summaryData, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
  1673. if e != nil {
  1674. resp.FailMsg("获取失败", "获取商品到款汇总列表失败, Err: "+e.Error(), c)
  1675. return
  1676. }
  1677. summaryList = summaryData
  1678. //summaryIds := make([]int, 0)
  1679. paymentIds := make([]int, 0)
  1680. for i := range summaryList {
  1681. //summaryIds = append(summaryIds, summaryList[i].SummaryId)
  1682. if summaryList[i].PaymentId > 0 {
  1683. paymentIds = append(paymentIds, summaryList[i].PaymentId)
  1684. }
  1685. }
  1686. // 合同套餐
  1687. /*contractServiceCond := `contract_register_id IN ?`
  1688. contractServicePars := make([]interface{}, 0)
  1689. contractServicePars = append(contractServicePars, queryRegisterIds)
  1690. contractServiceOB := new(fms.ContractService)
  1691. contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
  1692. if e != nil {
  1693. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  1694. return
  1695. }
  1696. contractServiceMap := make(map[int][]*fms.ContractService, 0)
  1697. servicesNameMap := make(map[int][]string, 0)
  1698. for i := range contractServiceList {
  1699. if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
  1700. contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
  1701. }
  1702. contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
  1703. servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
  1704. }
  1705. */
  1706. servicesNameMap, serviceFormatMap, e := fmsService.GetContractServiceNameFormat(queryRegisterIds)
  1707. if e != nil {
  1708. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  1709. return
  1710. }
  1711. // 到款套餐分配
  1712. serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
  1713. if len(paymentIds) > 0 {
  1714. serviceAmountCond := `contract_payment_id IN ?`
  1715. serviceAmountPars := make([]interface{}, 0)
  1716. serviceAmountPars = append(serviceAmountPars, paymentIds)
  1717. serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  1718. serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  1719. if e != nil {
  1720. listErr = fmt.Errorf("获取到款套餐分配列表失败, Err: %s", e.Error())
  1721. return
  1722. }
  1723. for i := range serviceAmountList {
  1724. if serviceAmountMap[serviceAmountList[i].ContractPaymentId] == nil {
  1725. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = make([]*fms.ContractPaymentServiceAmount, 0)
  1726. }
  1727. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = append(serviceAmountMap[serviceAmountList[i].ContractPaymentId], serviceAmountList[i])
  1728. }
  1729. }
  1730. // 重组汇总数据
  1731. summaryMap := make(map[int][]*fms.InvoicePaymentCensusInfo)
  1732. amountMap := make(map[string]*fms.ContractPaymentServiceAmount)
  1733. for i := range summaryList {
  1734. v := new(fms.InvoicePaymentCensusInfo)
  1735. v.InvoiceId = summaryList[i].InvoiceId
  1736. v.InvoiceDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].InvoiceDate)
  1737. v.InvoiceAmount = summaryList[i].InvoiceAmount
  1738. v.SellerId = summaryList[i].SellerId
  1739. v.SellerName = summaryList[i].SellerName
  1740. v.SellerGroupId = summaryList[i].SellerGroupId
  1741. v.SellerGroupName = summaryList[i].SellerGroupName
  1742. v.SellerType = summaryList[i].ServiceProductId
  1743. v.PaymentId = summaryList[i].PaymentId
  1744. v.PaymentDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].PaymentDate)
  1745. v.PaymentAmount = summaryList[i].PaymentAmount
  1746. v.PayType = summaryList[i].PayType
  1747. // 套餐到款分配
  1748. svaList := make([]*fms.ContractPaymentServiceAmountItem, 0)
  1749. amountList := serviceAmountMap[summaryList[i].PaymentId]
  1750. if amountList != nil {
  1751. for i := range amountList {
  1752. k := fmt.Sprintf("%d-%d", amountList[i].ContractPaymentId, amountList[i].ServiceTemplateId)
  1753. amountMap[k] = amountList[i]
  1754. }
  1755. }
  1756. // 合同对应的所有套餐
  1757. svList := serviceFormatMap[summaryList[i].RegisterId]
  1758. if svList != nil {
  1759. for ii := range svList {
  1760. vv := new(fms.ContractPaymentServiceAmountItem)
  1761. vv.ServiceTemplateId = svList[ii].ServiceTemplateId
  1762. vv.ServiceTemplateName = svList[ii].FormatTitle
  1763. vv.ServiceTemplatePid = svList[ii].ServiceTemplatePid
  1764. k2 := fmt.Sprintf("%d-%d", summaryList[i].PaymentId, svList[ii].ServiceTemplateId)
  1765. a := amountMap[k2]
  1766. if a != nil {
  1767. vv.ContractPaymentServiceAmountId = a.ContractPaymentServiceAmountId
  1768. vv.ContractPaymentId = a.ContractPaymentId
  1769. vv.Amount = a.Amount
  1770. }
  1771. svaList = append(svaList, vv)
  1772. }
  1773. }
  1774. v.ServiceAmountList = svaList
  1775. summaryMap[summaryList[i].SummaryId] = append(summaryMap[summaryList[i].SummaryId], v)
  1776. }
  1777. // 货币列表
  1778. currencyOB := new(fms.CurrencyUnit)
  1779. currencyCond := `enable = 1`
  1780. currencyPars := make([]interface{}, 0)
  1781. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  1782. if e != nil {
  1783. resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
  1784. return
  1785. }
  1786. unitMap := make(map[string]string)
  1787. for i := range currencyList {
  1788. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  1789. }
  1790. // 响应列表
  1791. for i := range registerList {
  1792. v := new(fms.InvoicePaymentCensusItem)
  1793. v.SummaryId = registerList[i].SummaryId
  1794. v.ContractRegisterId = registerList[i].ContractRegisterId
  1795. v.CompanyName = registerList[i].CompanyName
  1796. v.NewCompany = registerList[i].NewCompany
  1797. v.StartDate = utils.TimeTransferString(utils.FormatDate, registerList[i].StartDate)
  1798. v.EndDate = utils.TimeTransferString(utils.FormatDate, registerList[i].EndDate)
  1799. v.ServicesName = servicesNameMap[registerList[i].ContractRegisterId]
  1800. v.InvoicePaymentList = summaryMap[registerList[i].SummaryId]
  1801. v.ContractType = registerList[i].ContractType
  1802. v.ContractCode = registerList[i].ContractCode
  1803. v.ContractAmount = registerList[i].ContractAmount
  1804. v.UnitName = unitMap[registerList[i].CurrencyUnit]
  1805. v.ProductIds = registerList[i].ProductIds
  1806. v.NotInvoiceTotal = registerList[i].ContractAmount - registerList[i].InvoicedAmount
  1807. respList = append(respList, v)
  1808. }
  1809. }()
  1810. // 开票到款金额合计(换算后)
  1811. var invoiceTotal, paymentTotal, contractAmount float64
  1812. wg.Add(1)
  1813. go func() {
  1814. defer wg.Done()
  1815. if len(summaryIds) == 0 {
  1816. return
  1817. }
  1818. amountTotalCond := `a.id IN ?`
  1819. amountTotalPars := make([]interface{}, 0)
  1820. amountTotalPars = append(amountTotalPars, summaryIds)
  1821. invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
  1822. if e != nil {
  1823. totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
  1824. return
  1825. }
  1826. invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
  1827. paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
  1828. if e != nil {
  1829. totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
  1830. return
  1831. }
  1832. paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
  1833. }()
  1834. // 分币种金额统计
  1835. invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  1836. paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  1837. contractAmountCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  1838. wg.Add(1)
  1839. go func() {
  1840. defer wg.Done()
  1841. currencyOB := new(fms.CurrencyUnit)
  1842. currencyCond := `enable = 1`
  1843. currencyPars := make([]interface{}, 0)
  1844. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  1845. if e != nil {
  1846. totalGroupErr = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
  1847. return
  1848. }
  1849. unitMap := make(map[string]string)
  1850. for i := range currencyList {
  1851. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  1852. invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  1853. Name: currencyList[i].Name,
  1854. UnitName: currencyList[i].UnitName,
  1855. Code: currencyList[i].Code,
  1856. FlagImg: currencyList[i].FlagImg,
  1857. })
  1858. paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  1859. Name: currencyList[i].Name,
  1860. UnitName: currencyList[i].UnitName,
  1861. Code: currencyList[i].Code,
  1862. FlagImg: currencyList[i].FlagImg,
  1863. })
  1864. contractAmountCurrencyTotals = append(contractAmountCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  1865. Name: currencyList[i].Name,
  1866. UnitName: currencyList[i].UnitName,
  1867. Code: currencyList[i].Code,
  1868. FlagImg: currencyList[i].FlagImg,
  1869. })
  1870. }
  1871. if len(summaryIds) == 0 {
  1872. return
  1873. }
  1874. totalGroupCond := `a.id IN ?`
  1875. totalGroupPars := make([]interface{}, 0)
  1876. totalGroupPars = append(totalGroupPars, summaryIds)
  1877. invoiceSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 1)
  1878. if e != nil {
  1879. totalGroupErr = fmt.Errorf("获取汇总货币合计开票金额失败, Err: %s", e.Error())
  1880. return
  1881. }
  1882. paymentSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 2)
  1883. if e != nil {
  1884. totalGroupErr = fmt.Errorf("获取汇总货币合计到款金额失败, Err: %s", e.Error())
  1885. return
  1886. }
  1887. invoiceSumMap := make(map[string]float64)
  1888. paymentSumMap := make(map[string]float64)
  1889. contractSumMap := make(map[string]float64)
  1890. for i := range invoiceSumGroup {
  1891. invoiceSumMap[invoiceSumGroup[i].CurrencyUnit] = invoiceSumGroup[i].OriginAmountTotal
  1892. continue
  1893. }
  1894. for i := range paymentSumGroup {
  1895. paymentSumMap[paymentSumGroup[i].CurrencyUnit] = paymentSumGroup[i].OriginAmountTotal
  1896. continue
  1897. }
  1898. for i := range invoiceCurrencyTotals {
  1899. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
  1900. invoiceCurrencyTotals[i].Amount = a
  1901. }
  1902. for i := range paymentCurrencyTotals {
  1903. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
  1904. paymentCurrencyTotals[i].Amount = a
  1905. }
  1906. contractRegisters, err := fms.GetContractInfoByRegisterIds(queryRegisterIds)
  1907. if err != nil {
  1908. resp.FailMsg("查询合同信息失败", "查询合同信息失败, Err: "+err.Error(), c)
  1909. return
  1910. }
  1911. for _, v := range contractRegisters {
  1912. if v.RMBRate == 0.0{
  1913. v.RMBRate = 1
  1914. }
  1915. amount,_ := strconv.ParseFloat(fmt.Sprintf("%.2f",v.ContractAmount / v.RMBRate), 64)
  1916. contractAmount += amount
  1917. contractSumMap[v.CurrencyUnit] = v.ContractAmount
  1918. }
  1919. for _, v := range contractAmountCurrencyTotals {
  1920. v.Amount = contractSumMap[v.Code] - invoiceSumMap[v.Code]
  1921. }
  1922. }()
  1923. wg.Wait()
  1924. if listErr != nil {
  1925. resp.FailMsg("获取失败", listErr.Error(), c)
  1926. return
  1927. }
  1928. if totalErr != nil {
  1929. resp.FailMsg("获取失败", totalErr.Error(), c)
  1930. return
  1931. }
  1932. if totalGroupErr != nil {
  1933. resp.FailMsg("获取失败", totalGroupErr.Error(), c)
  1934. return
  1935. }
  1936. results.DataList = respList
  1937. results.InvoiceTotal = invoiceTotal
  1938. results.PaymentTotal = paymentTotal
  1939. results.NotInvoiceTotal = contractAmount-invoiceTotal
  1940. results.InvoiceCurrencyTotal = invoiceCurrencyTotals
  1941. results.PaymentCurrencyTotal = paymentCurrencyTotals
  1942. results.NotInvoiceCurrencyTotal = contractAmountCurrencyTotals
  1943. }
  1944. // 是否导出
  1945. if req.IsExport == 1 {
  1946. ExportNotInvoiceCensusList(c, results)
  1947. return
  1948. }
  1949. page.SetTotal(total)
  1950. baseData := new(base.BaseData)
  1951. baseData.SetPage(page)
  1952. baseData.SetList(results)
  1953. resp.OkData("获取成功", baseData, c)
  1954. }
  1955. // List
  1956. // @Title 开票未到款统计列表
  1957. // @Description 开票未到款统计列表
  1958. // @Param Keyword query string false "关键词"
  1959. // @Param SellGroupId query int false "销售组别ID"
  1960. // @Param ServiceType query int false "套餐类型"
  1961. // @Param StartDate query string false "合同开始日期"
  1962. // @Param EndDate query string false "合同结束日期"
  1963. // @Param TimeType query int false "时间类型: 1-开票时间; 2-到款时间"
  1964. // @Param HasInvoice query string false "是否已开票: 0-否; 1-是; 空-全部"
  1965. // @Param HasPayment query string false "是否已到款: 0-否; 1-是; 空-全部"
  1966. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  1967. // @Param ListParam query int false "套餐类型: 0-全部; 1-ficc; 2-权益"
  1968. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'invoice_time':开票日 、 'payment_date':到款日"
  1969. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
  1970. // @Success 200 {object} fms.ContractRegisterItem
  1971. // @router /census/invoice_payment/not_payment/list [get]
  1972. func (ct *InvoicePaymentController) NotPaymentList(c *gin.Context) {
  1973. var req fms.InvoicePaymentCensusListReq
  1974. if e := c.BindQuery(&req); e != nil {
  1975. err, ok := e.(validator.ValidationErrors)
  1976. if !ok {
  1977. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  1978. return
  1979. }
  1980. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1981. return
  1982. }
  1983. cond := `1 = 1`
  1984. pars := make([]interface{}, 0)
  1985. // 客户姓名/销售
  1986. if req.Keyword != "" {
  1987. kw := "%" + req.Keyword + "%"
  1988. cond += ` AND (b.company_name LIKE ? OR b.contract_code LIKE ?)`
  1989. pars = append(pars, kw, kw)
  1990. }
  1991. if req.SellerIds != "" {
  1992. sellerIds := strings.Split(req.SellerIds, ",")
  1993. cond += ` AND (c.seller_id in ?)`
  1994. pars = append(pars, sellerIds)
  1995. }
  1996. // 套餐筛选
  1997. if req.ServiceTypes != "" {
  1998. serviceTypes := strings.Split(req.ServiceTypes, ",")
  1999. tempRegisterIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
  2000. if e != nil {
  2001. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  2002. return
  2003. }
  2004. if len(tempRegisterIds) > 0 {
  2005. cond += ` AND a.register_id IN ?`
  2006. pars = append(pars, tempRegisterIds)
  2007. } else {
  2008. cond += ` AND 1 = 2`
  2009. }
  2010. }
  2011. if req.ListParam == 1{
  2012. cond += ` AND a.service_product_id = 1 `
  2013. }else if req.ListParam == 2 {
  2014. cond += ` AND a.service_product_id = 2 `
  2015. }
  2016. // 开票到款日期
  2017. //if req.TimeType != 0 {
  2018. // if req.TimeType == 1 && req.StartDate != "" && req.EndDate != "" {
  2019. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  2020. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  2021. // cond += ` AND (c.invoice_time BETWEEN ? AND ?) `
  2022. // pars = append(pars, st, ed)
  2023. // }else if req.TimeType == 2 && req.StartDate != "" && req.EndDate != "" {
  2024. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  2025. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  2026. // cond += ` AND (d.invoice_time BETWEEN ? AND ?) `
  2027. // pars = append(pars, st, ed)
  2028. // }else if req.TimeType == 3 && req.StartDate != "" && req.EndDate != "" {
  2029. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  2030. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  2031. // cond += ` AND (c.invoice_time BETWEEN ? AND ?) AND (d.invoice_time BETWEEN ? AND ?) `
  2032. // pars = append(pars, st, ed, st, ed)
  2033. // }
  2034. //} else if req.StartDate != "" && req.EndDate != "" {
  2035. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  2036. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  2037. // cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  2038. // pars = append(pars, st, ed, st, ed)
  2039. //}
  2040. //if req.HasInvoice == "1" {
  2041. // cond += ` AND a.invoice_id > 0 `
  2042. //}else if req.HasInvoice == "0" {
  2043. // cond += ` AND a.invoice_id = 0 `
  2044. //}
  2045. //
  2046. //if req.HasPayment == "1" {
  2047. // cond += ` AND a.payment_id > 0 `
  2048. //}else if req.HasPayment == "0" {
  2049. // cond += ` AND a.payment_id = 0 `
  2050. //}
  2051. page := new(base.Page)
  2052. page.SetPageSize(req.PageSize)
  2053. page.SetCurrent(req.Current)
  2054. //排序
  2055. if req.SortType == "" {
  2056. req.SortType = "desc"
  2057. }
  2058. if req.SortType != "desc" && req.SortType != "asc" {
  2059. resp.Fail("排序类型不正确", c)
  2060. return
  2061. }
  2062. if req.SortType == "asc" {
  2063. page.AddOrderItem(base.OrderItem{Column: "e.invoice_time", Asc: true})
  2064. }else{
  2065. page.AddOrderItem(base.OrderItem{Column: "e.invoice_time", Asc: false})
  2066. }
  2067. if req.IsExport == 1 {
  2068. page.SetPageSize(10000)
  2069. page.SetCurrent(1)
  2070. }
  2071. registerList, total, e := fms.GetNoPaymentCensusPageList(page, cond, pars)
  2072. if e != nil {
  2073. resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
  2074. return
  2075. }
  2076. queryRegisterIds := make([]int, 0)
  2077. for i := range registerList {
  2078. queryRegisterIds = append(queryRegisterIds, registerList[i].ContractRegisterId)
  2079. }
  2080. //fmt.Println("queryRegisterIds:",queryRegisterIds)
  2081. results := new(fms.NotInvoicePaymentCensusResp)
  2082. if len(queryRegisterIds) > 0 {
  2083. // 获取汇总数据IDs, 用于查询合计数据
  2084. summaryIdsCond := cond
  2085. summaryIdsPars := pars
  2086. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
  2087. if e != nil {
  2088. resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
  2089. return
  2090. }
  2091. //fmt.Println("summaryIds:",summaryIds)
  2092. var listErr, totalErr, totalGroupErr error
  2093. wg := sync.WaitGroup{}
  2094. // 响应列表
  2095. respList := make([]*fms.InvoicePaymentCensusItem, 0)
  2096. summaryList := make([]*fms.InvoicePaymentSummaryItem, 0)
  2097. wg.Add(1)
  2098. go func() {
  2099. defer wg.Done()
  2100. // 获取汇总数据
  2101. summaryCond := cond
  2102. summaryCond += ` AND a.register_id IN ?`
  2103. summaryPars := pars
  2104. summaryPars = append(summaryPars, queryRegisterIds)
  2105. summaryData, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
  2106. if e != nil {
  2107. resp.FailMsg("获取失败", "获取商品到款汇总列表失败, Err: "+e.Error(), c)
  2108. return
  2109. }
  2110. summaryList = summaryData
  2111. //summaryIds := make([]int, 0)
  2112. paymentIds := make([]int, 0)
  2113. for i := range summaryList {
  2114. //summaryIds = append(summaryIds, summaryList[i].SummaryId)
  2115. if summaryList[i].PaymentId > 0 {
  2116. paymentIds = append(paymentIds, summaryList[i].PaymentId)
  2117. }
  2118. }
  2119. // 合同套餐
  2120. /*contractServiceCond := `contract_register_id IN ?`
  2121. contractServicePars := make([]interface{}, 0)
  2122. contractServicePars = append(contractServicePars, queryRegisterIds)
  2123. contractServiceOB := new(fms.ContractService)
  2124. contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
  2125. if e != nil {
  2126. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  2127. return
  2128. }
  2129. contractServiceMap := make(map[int][]*fms.ContractService, 0)
  2130. servicesNameMap := make(map[int][]string, 0)
  2131. for i := range contractServiceList {
  2132. if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
  2133. contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
  2134. }
  2135. contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
  2136. servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
  2137. }
  2138. */
  2139. servicesNameMap, serviceFormatMap, e := fmsService.GetContractServiceNameFormat(queryRegisterIds)
  2140. if e != nil {
  2141. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  2142. return
  2143. }
  2144. // 到款套餐分配
  2145. serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
  2146. if len(paymentIds) > 0 {
  2147. serviceAmountCond := `contract_payment_id IN ?`
  2148. serviceAmountPars := make([]interface{}, 0)
  2149. serviceAmountPars = append(serviceAmountPars, paymentIds)
  2150. serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  2151. serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  2152. if e != nil {
  2153. listErr = fmt.Errorf("获取到款套餐分配列表失败, Err: %s", e.Error())
  2154. return
  2155. }
  2156. for i := range serviceAmountList {
  2157. if serviceAmountMap[serviceAmountList[i].ContractPaymentId] == nil {
  2158. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = make([]*fms.ContractPaymentServiceAmount, 0)
  2159. }
  2160. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = append(serviceAmountMap[serviceAmountList[i].ContractPaymentId], serviceAmountList[i])
  2161. }
  2162. }
  2163. // 重组汇总数据
  2164. summaryMap := make(map[int][]*fms.InvoicePaymentCensusInfo)
  2165. amountMap := make(map[string]*fms.ContractPaymentServiceAmount)
  2166. for i := range summaryList {
  2167. v := new(fms.InvoicePaymentCensusInfo)
  2168. v.InvoiceId = summaryList[i].InvoiceId
  2169. v.InvoiceDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].InvoiceDate)
  2170. v.InvoiceAmount = summaryList[i].InvoiceAmount
  2171. v.SellerId = summaryList[i].SellerId
  2172. v.SellerName = summaryList[i].SellerName
  2173. v.SellerGroupId = summaryList[i].SellerGroupId
  2174. v.SellerGroupName = summaryList[i].SellerGroupName
  2175. v.SellerType = summaryList[i].ServiceProductId
  2176. v.PaymentId = summaryList[i].PaymentId
  2177. v.PaymentDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].PaymentDate)
  2178. v.PaymentAmount = summaryList[i].PaymentAmount
  2179. v.PayType = summaryList[i].PayType
  2180. // 套餐到款分配
  2181. svaList := make([]*fms.ContractPaymentServiceAmountItem, 0)
  2182. amountList := serviceAmountMap[summaryList[i].PaymentId]
  2183. if amountList != nil {
  2184. for i := range amountList {
  2185. k := fmt.Sprintf("%d-%d", amountList[i].ContractPaymentId, amountList[i].ServiceTemplateId)
  2186. amountMap[k] = amountList[i]
  2187. }
  2188. }
  2189. // 合同对应的所有套餐
  2190. svList := serviceFormatMap[summaryList[i].RegisterId]
  2191. if svList != nil {
  2192. for ii := range svList {
  2193. vv := new(fms.ContractPaymentServiceAmountItem)
  2194. vv.ServiceTemplateId = svList[ii].ServiceTemplateId
  2195. vv.ServiceTemplateName = svList[ii].FormatTitle
  2196. vv.ServiceTemplatePid = svList[ii].ServiceTemplatePid
  2197. k2 := fmt.Sprintf("%d-%d", summaryList[i].PaymentId, svList[ii].ServiceTemplateId)
  2198. a := amountMap[k2]
  2199. if a != nil {
  2200. vv.ContractPaymentServiceAmountId = a.ContractPaymentServiceAmountId
  2201. vv.ContractPaymentId = a.ContractPaymentId
  2202. vv.Amount = a.Amount
  2203. }
  2204. svaList = append(svaList, vv)
  2205. }
  2206. }
  2207. v.ServiceAmountList = svaList
  2208. summaryMap[summaryList[i].SummaryId] = append(summaryMap[summaryList[i].SummaryId], v)
  2209. }
  2210. // 货币列表
  2211. currencyOB := new(fms.CurrencyUnit)
  2212. currencyCond := `enable = 1`
  2213. currencyPars := make([]interface{}, 0)
  2214. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  2215. if e != nil {
  2216. resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
  2217. return
  2218. }
  2219. unitMap := make(map[string]string)
  2220. for i := range currencyList {
  2221. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  2222. }
  2223. // 响应列表
  2224. for i := range registerList {
  2225. v := new(fms.InvoicePaymentCensusItem)
  2226. v.SummaryId = registerList[i].SummaryId
  2227. v.ContractRegisterId = registerList[i].ContractRegisterId
  2228. v.CompanyName = registerList[i].CompanyName
  2229. v.NewCompany = registerList[i].NewCompany
  2230. v.StartDate = utils.TimeTransferString(utils.FormatDate, registerList[i].StartDate)
  2231. v.EndDate = utils.TimeTransferString(utils.FormatDate, registerList[i].EndDate)
  2232. v.ServicesName = servicesNameMap[registerList[i].ContractRegisterId]
  2233. v.InvoicePaymentList = summaryMap[registerList[i].SummaryId]
  2234. v.ContractType = registerList[i].ContractType
  2235. v.ContractCode = registerList[i].ContractCode
  2236. v.ContractAmount = registerList[i].ContractAmount
  2237. v.UnitName = unitMap[registerList[i].CurrencyUnit]
  2238. v.ProductIds = registerList[i].ProductIds
  2239. respList = append(respList, v)
  2240. }
  2241. }()
  2242. // 开票到款金额合计(换算后)
  2243. // 开票到款金额合计(换算后)
  2244. var invoiceTotal, paymentTotal, contractAmount float64
  2245. wg.Add(1)
  2246. go func() {
  2247. defer wg.Done()
  2248. if len(summaryIds) == 0 {
  2249. return
  2250. }
  2251. amountTotalCond := `a.id IN ?`
  2252. amountTotalPars := make([]interface{}, 0)
  2253. amountTotalPars = append(amountTotalPars, summaryIds)
  2254. invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
  2255. if e != nil {
  2256. totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
  2257. return
  2258. }
  2259. invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
  2260. paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
  2261. if e != nil {
  2262. totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
  2263. return
  2264. }
  2265. paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
  2266. }()
  2267. // 分币种金额统计
  2268. invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  2269. paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  2270. contractAmountCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  2271. wg.Add(1)
  2272. go func() {
  2273. defer wg.Done()
  2274. currencyOB := new(fms.CurrencyUnit)
  2275. currencyCond := `enable = 1`
  2276. currencyPars := make([]interface{}, 0)
  2277. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  2278. if e != nil {
  2279. totalGroupErr = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
  2280. return
  2281. }
  2282. unitMap := make(map[string]string)
  2283. for i := range currencyList {
  2284. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  2285. invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  2286. Name: currencyList[i].Name,
  2287. UnitName: currencyList[i].UnitName,
  2288. Code: currencyList[i].Code,
  2289. FlagImg: currencyList[i].FlagImg,
  2290. })
  2291. paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  2292. Name: currencyList[i].Name,
  2293. UnitName: currencyList[i].UnitName,
  2294. Code: currencyList[i].Code,
  2295. FlagImg: currencyList[i].FlagImg,
  2296. })
  2297. contractAmountCurrencyTotals = append(contractAmountCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  2298. Name: currencyList[i].Name,
  2299. UnitName: currencyList[i].UnitName,
  2300. Code: currencyList[i].Code,
  2301. FlagImg: currencyList[i].FlagImg,
  2302. })
  2303. }
  2304. if len(summaryIds) == 0 {
  2305. return
  2306. }
  2307. totalGroupCond := `a.id IN ?`
  2308. totalGroupPars := make([]interface{}, 0)
  2309. totalGroupPars = append(totalGroupPars, summaryIds)
  2310. invoiceSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 1)
  2311. if e != nil {
  2312. totalGroupErr = fmt.Errorf("获取汇总货币合计开票金额失败, Err: %s", e.Error())
  2313. return
  2314. }
  2315. paymentSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 2)
  2316. if e != nil {
  2317. totalGroupErr = fmt.Errorf("获取汇总货币合计到款金额失败, Err: %s", e.Error())
  2318. return
  2319. }
  2320. invoiceSumMap := make(map[string]float64)
  2321. paymentSumMap := make(map[string]float64)
  2322. contractSumMap := make(map[string]float64)
  2323. for i := range invoiceSumGroup {
  2324. invoiceSumMap[invoiceSumGroup[i].CurrencyUnit] = invoiceSumGroup[i].OriginAmountTotal
  2325. continue
  2326. }
  2327. for i := range paymentSumGroup {
  2328. paymentSumMap[paymentSumGroup[i].CurrencyUnit] = paymentSumGroup[i].OriginAmountTotal
  2329. continue
  2330. }
  2331. for i := range invoiceCurrencyTotals {
  2332. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
  2333. invoiceCurrencyTotals[i].Amount = a
  2334. }
  2335. for i := range paymentCurrencyTotals {
  2336. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
  2337. paymentCurrencyTotals[i].Amount = a
  2338. }
  2339. contractRegisters, err := fms.GetContractInfoByRegisterIds(queryRegisterIds)
  2340. if err != nil {
  2341. resp.FailMsg("查询合同信息失败", "查询合同信息失败, Err: "+err.Error(), c)
  2342. return
  2343. }
  2344. for _, v := range contractRegisters {
  2345. if v.RMBRate == 0.0 {
  2346. v.RMBRate = 1
  2347. }
  2348. amount, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.ContractAmount/v.RMBRate), 64)
  2349. contractAmount += amount
  2350. contractSumMap[v.CurrencyUnit] = v.ContractAmount
  2351. }
  2352. for _, v := range contractAmountCurrencyTotals {
  2353. v.Amount = contractSumMap[v.Code] - invoiceSumMap[v.Code]
  2354. }
  2355. }()
  2356. wg.Wait()
  2357. if listErr != nil {
  2358. resp.FailMsg("获取失败", listErr.Error(), c)
  2359. return
  2360. }
  2361. if totalErr != nil {
  2362. resp.FailMsg("获取失败", totalErr.Error(), c)
  2363. return
  2364. }
  2365. if totalGroupErr != nil {
  2366. resp.FailMsg("获取失败", totalGroupErr.Error(), c)
  2367. return
  2368. }
  2369. results.DataList = respList
  2370. results.InvoiceTotal = invoiceTotal
  2371. results.PaymentTotal = paymentTotal
  2372. results.NotInvoiceTotal = contractAmount - invoiceTotal
  2373. results.InvoiceCurrencyTotal = invoiceCurrencyTotals
  2374. results.PaymentCurrencyTotal = paymentCurrencyTotals
  2375. results.NotInvoiceCurrencyTotal = contractAmountCurrencyTotals
  2376. }
  2377. // 是否导出
  2378. if req.IsExport == 1 {
  2379. ExportNotPaymentCensusList(c, results)
  2380. return
  2381. }
  2382. page.SetTotal(total)
  2383. baseData := new(base.BaseData)
  2384. baseData.SetPage(page)
  2385. baseData.SetList(results)
  2386. resp.OkData("获取成功", baseData, c)
  2387. }
  2388. // ExportNotInvoiceCensusList 导出未开票统计列表
  2389. func ExportNotInvoiceCensusList(c *gin.Context, results *fms.NotInvoicePaymentCensusResp) {
  2390. list := results.DataList
  2391. if len(list) == 0 {
  2392. resp.Fail("列表数据为空", c)
  2393. return
  2394. }
  2395. // 生成Excel文件
  2396. xlsxFile := xlsx.NewFile()
  2397. style := xlsx.NewStyle()
  2398. alignment := xlsx.Alignment{
  2399. Horizontal: "center",
  2400. Vertical: "center",
  2401. WrapText: true,
  2402. }
  2403. style.Alignment = alignment
  2404. style.ApplyAlignment = true
  2405. sheet, err := xlsxFile.AddSheet("未开票统计")
  2406. if err != nil {
  2407. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  2408. return
  2409. }
  2410. _ = sheet.SetColWidth(1, 1, 30)
  2411. _ = sheet.SetColWidth(3, 3, 30)
  2412. // 前三行-开票金额合计
  2413. rowA := sheet.AddRow()
  2414. cellAA := rowA.AddCell()
  2415. cellAA.SetString(fmt.Sprintf("未开票合计金额(换算后):%.2f(元)", results.NotInvoiceTotal))
  2416. rowBData := "未开票金额:"
  2417. for _, v := range results.NotInvoiceCurrencyTotal {
  2418. rowBData += fmt.Sprintf("%s%.2f(%s) ", v.Name, v.Amount, v.UnitName)
  2419. }
  2420. rowB := sheet.AddRow()
  2421. rowB.AddCell().SetString(rowBData)
  2422. sheet.AddRow()
  2423. // 表头, 套餐动态获取
  2424. rowTitle := []string{"序号", "客户名称", "是否新客户", "合同有效期", "开票日", "开票金额", "到款日", "到款金额", "付款方式", "销售",
  2425. "销售组别", "销售类型"}
  2426. serviceTempCond := ``
  2427. serviceTempPars := make([]interface{}, 0)
  2428. serviceTempOB := new(fms.ContractServiceTemplate)
  2429. serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
  2430. if e != nil {
  2431. resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
  2432. return
  2433. }
  2434. serviceTempListMap := make(map[int]*fms.ContractServiceTemplate)
  2435. var serviceTempShow [] *fms.ContractServiceTemplate
  2436. for i := range serviceTempList {
  2437. serviceTempListMap[serviceTempList[i].ServiceTemplateId] = serviceTempList[i]
  2438. }
  2439. for i := range serviceTempList {
  2440. if serviceTempList[i].ProductId == 1 {
  2441. rowTitle = append(rowTitle, serviceTempList[i].Title)
  2442. serviceTempShow = append(serviceTempShow, serviceTempList[i])
  2443. }else if serviceTempList[i].ProductId == 2{
  2444. if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "权益大套餐" || (serviceTempList[i].Title == "策略" && serviceTempListMap[serviceTempList[i].Pid].Pid==0){
  2445. }else if serviceTempList[i].Title == "医药" || serviceTempList[i].Title == "消费" || serviceTempList[i].Title == "科技" || serviceTempList[i].Title == "智造" {
  2446. }else {
  2447. serviceTempShow = append(serviceTempShow, serviceTempList[i])
  2448. }
  2449. if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "45万" || serviceTempList[i].Title == "70万" {
  2450. continue
  2451. }
  2452. if serviceTempList[i].Pid > 0 && serviceTempListMap[serviceTempList[i].Pid].Pid > 0 {
  2453. continue
  2454. }
  2455. rowTitle = append(rowTitle, serviceTempList[i].Title)
  2456. }
  2457. }
  2458. hi := 0
  2459. insertHi := 0
  2460. titleRow := sheet.AddRow()
  2461. for i := range rowTitle {
  2462. v := titleRow.AddCell()
  2463. v.SetString(rowTitle[i])
  2464. v.SetStyle(style)
  2465. if rowTitle[i] != "权益大套餐" && rowTitle[i] != "医药" && rowTitle[i] != "消费" && rowTitle[i] != "科技" && rowTitle[i] != "智造" {
  2466. v.VMerge = 1
  2467. hi ++
  2468. }else{
  2469. if insertHi == 0 {
  2470. insertHi = hi
  2471. }
  2472. v.HMerge = 1
  2473. titleRow.AddCell().SetString("")
  2474. }
  2475. }
  2476. // 新增一行放主观和客观
  2477. specialRow := sheet.AddRow()
  2478. for i:=0; i< hi; i++ {
  2479. if i == insertHi {
  2480. v1 := specialRow.AddCell()
  2481. v1.SetString("45万")
  2482. v1.SetStyle(style)
  2483. v1 = specialRow.AddCell()
  2484. v1.SetString("70万")
  2485. v1.SetStyle(style)
  2486. for j:=0; j<8;j++ {
  2487. if j%2 ==0 {
  2488. v1 = specialRow.AddCell()
  2489. v1.SetString("主观")
  2490. v1.SetStyle(style)
  2491. }else{
  2492. v1 = specialRow.AddCell()
  2493. v1.SetString("客观")
  2494. v1.SetStyle(style)
  2495. }
  2496. }
  2497. }else{
  2498. v := specialRow.AddCell()
  2499. v.SetString("")
  2500. }
  2501. }
  2502. newCompanyMap := map[int]string{0: "否", 1: "是"}
  2503. sellerTypeMap := map[int]string{1:"FICC销售", 2:"权益销售"}
  2504. for k, v := range list {
  2505. dataRow := sheet.AddRow()
  2506. // 前四个单元格根据每行开票到款条数向下合并
  2507. l := len(v.InvoicePaymentList)
  2508. mergeRowNum := l - 1
  2509. // 序号
  2510. sortNum := k + 1
  2511. colA := dataRow.AddCell()
  2512. colA.VMerge = mergeRowNum
  2513. colA.SetString(fmt.Sprint(sortNum))
  2514. // 客户名称
  2515. colB := dataRow.AddCell()
  2516. colB.VMerge = mergeRowNum
  2517. colB.SetString(v.CompanyName)
  2518. // 是否新客户
  2519. colC := dataRow.AddCell()
  2520. colC.VMerge = mergeRowNum
  2521. colC.SetString(newCompanyMap[v.NewCompany])
  2522. // 合同有效期
  2523. colD := dataRow.AddCell()
  2524. colD.VMerge = mergeRowNum
  2525. colD.SetString(fmt.Sprint(v.StartDate, "至", v.EndDate))
  2526. // 开票到款信息
  2527. for k2, v2 := range v.InvoicePaymentList {
  2528. rowData := []string{
  2529. v2.InvoiceDate, // 开票日
  2530. fmt.Sprint(v2.InvoiceAmount), // 开票金额
  2531. v2.PaymentDate, // 到款日
  2532. fmt.Sprint(v2.PaymentAmount), // 到款金额
  2533. fms.ContractPaymentPayTypeNameMap[v2.PayType], // 付款方式
  2534. v2.SellerName, // 销售
  2535. v2.SellerGroupName, // 组别
  2536. sellerTypeMap[v2.SellerType], // 销售类型
  2537. }
  2538. // 套餐金额信息
  2539. serviceTempShowAmount := make(map[int]string)
  2540. for i := range serviceTempShow {
  2541. for s2 := range v2.ServiceAmountList {
  2542. item := v2.ServiceAmountList[s2]
  2543. if item.ServiceTemplateId == serviceTempShow[i].ServiceTemplateId {
  2544. serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount)
  2545. break
  2546. }else if serviceTempShow[i].Pid == item.ServiceTemplateId {
  2547. serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount/2)
  2548. break
  2549. }
  2550. }
  2551. }
  2552. for i := range serviceTempShow {
  2553. sa := ""
  2554. if am, ok := serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId]; ok {
  2555. sa = am
  2556. }
  2557. rowData = append(rowData, sa)
  2558. }
  2559. // 首行开票到款
  2560. if k2 == 0 {
  2561. for i := range rowData {
  2562. dataRow.AddCell().SetString(rowData[i])
  2563. }
  2564. continue
  2565. }
  2566. // 其他行开票到款, 加四列空的单元格用于合并
  2567. dataRowExtra := sheet.AddRow()
  2568. for i := 0; i < 4; i++ {
  2569. dataRowExtra.AddCell()
  2570. }
  2571. for i := range rowData {
  2572. dataRowExtra.AddCell().SetString(rowData[i])
  2573. }
  2574. }
  2575. }
  2576. // 输出文件
  2577. var buffer bytes.Buffer
  2578. _ = xlsxFile.Write(&buffer)
  2579. content := bytes.NewReader(buffer.Bytes())
  2580. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  2581. fileName := "商品到款统计_" + randStr + ".xlsx"
  2582. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  2583. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  2584. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  2585. }
  2586. // ExportNotPaymentCensusList 导出开票未到款统计列表
  2587. func ExportNotPaymentCensusList(c *gin.Context, results *fms.NotInvoicePaymentCensusResp) {
  2588. list := results.DataList
  2589. if len(list) == 0 {
  2590. resp.Fail("列表数据为空", c)
  2591. return
  2592. }
  2593. // 生成Excel文件
  2594. xlsxFile := xlsx.NewFile()
  2595. style := xlsx.NewStyle()
  2596. alignment := xlsx.Alignment{
  2597. Horizontal: "center",
  2598. Vertical: "center",
  2599. WrapText: true,
  2600. }
  2601. style.Alignment = alignment
  2602. style.ApplyAlignment = true
  2603. sheet, err := xlsxFile.AddSheet("开票未到款统计")
  2604. if err != nil {
  2605. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  2606. return
  2607. }
  2608. _ = sheet.SetColWidth(1, 1, 30)
  2609. _ = sheet.SetColWidth(3, 3, 30)
  2610. // 前三行-开票金额合计
  2611. rowA := sheet.AddRow()
  2612. cellAA := rowA.AddCell()
  2613. cellAA.SetString(fmt.Sprintf("开票未到款合计金额(换算后):%.2f(元)", results.InvoiceTotal))
  2614. rowBData := "已开票金额:"
  2615. for _, v := range results.InvoiceCurrencyTotal {
  2616. rowBData += fmt.Sprintf("%s%.2f(%s) ", v.Name, v.Amount, v.UnitName)
  2617. }
  2618. rowB := sheet.AddRow()
  2619. rowB.AddCell().SetString(rowBData)
  2620. sheet.AddRow()
  2621. // 表头, 套餐动态获取
  2622. rowTitle := []string{"序号", "客户名称", "是否新客户", "合同有效期", "开票日", "开票金额", "到款日", "到款金额", "付款方式", "销售",
  2623. "销售组别", "销售类型"}
  2624. serviceTempCond := ``
  2625. serviceTempPars := make([]interface{}, 0)
  2626. serviceTempOB := new(fms.ContractServiceTemplate)
  2627. serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
  2628. if e != nil {
  2629. resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
  2630. return
  2631. }
  2632. serviceTempListMap := make(map[int]*fms.ContractServiceTemplate)
  2633. var serviceTempShow [] *fms.ContractServiceTemplate
  2634. for i := range serviceTempList {
  2635. serviceTempListMap[serviceTempList[i].ServiceTemplateId] = serviceTempList[i]
  2636. }
  2637. for i := range serviceTempList {
  2638. if serviceTempList[i].ProductId == 1 {
  2639. rowTitle = append(rowTitle, serviceTempList[i].Title)
  2640. serviceTempShow = append(serviceTempShow, serviceTempList[i])
  2641. }else if serviceTempList[i].ProductId == 2{
  2642. if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "权益大套餐" || (serviceTempList[i].Title == "策略" && serviceTempListMap[serviceTempList[i].Pid].Pid==0){
  2643. }else if serviceTempList[i].Title == "医药" || serviceTempList[i].Title == "消费" || serviceTempList[i].Title == "科技" || serviceTempList[i].Title == "智造" {
  2644. }else {
  2645. serviceTempShow = append(serviceTempShow, serviceTempList[i])
  2646. }
  2647. if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "45万" || serviceTempList[i].Title == "70万" {
  2648. continue
  2649. }
  2650. if serviceTempList[i].Pid > 0 && serviceTempListMap[serviceTempList[i].Pid].Pid > 0 {
  2651. continue
  2652. }
  2653. rowTitle = append(rowTitle, serviceTempList[i].Title)
  2654. }
  2655. }
  2656. hi := 0
  2657. insertHi := 0
  2658. titleRow := sheet.AddRow()
  2659. for i := range rowTitle {
  2660. v := titleRow.AddCell()
  2661. v.SetString(rowTitle[i])
  2662. v.SetStyle(style)
  2663. if rowTitle[i] != "权益大套餐" && rowTitle[i] != "医药" && rowTitle[i] != "消费" && rowTitle[i] != "科技" && rowTitle[i] != "智造" {
  2664. v.VMerge = 1
  2665. hi ++
  2666. }else{
  2667. if insertHi == 0 {
  2668. insertHi = hi
  2669. }
  2670. v.HMerge = 1
  2671. titleRow.AddCell().SetString("")
  2672. }
  2673. }
  2674. // 新增一行放主观和客观
  2675. specialRow := sheet.AddRow()
  2676. for i:=0; i< hi; i++ {
  2677. if i == insertHi {
  2678. v1 := specialRow.AddCell()
  2679. v1.SetString("45万")
  2680. v1.SetStyle(style)
  2681. v1 = specialRow.AddCell()
  2682. v1.SetString("70万")
  2683. v1.SetStyle(style)
  2684. for j:=0; j<8;j++ {
  2685. if j%2 ==0 {
  2686. v1 = specialRow.AddCell()
  2687. v1.SetString("主观")
  2688. v1.SetStyle(style)
  2689. }else{
  2690. v1 = specialRow.AddCell()
  2691. v1.SetString("客观")
  2692. v1.SetStyle(style)
  2693. }
  2694. }
  2695. }else{
  2696. v := specialRow.AddCell()
  2697. v.SetString("")
  2698. }
  2699. }
  2700. newCompanyMap := map[int]string{0: "否", 1: "是"}
  2701. sellerTypeMap := map[int]string{1:"FICC销售", 2:"权益销售"}
  2702. for k, v := range list {
  2703. dataRow := sheet.AddRow()
  2704. // 前四个单元格根据每行开票到款条数向下合并
  2705. l := len(v.InvoicePaymentList)
  2706. mergeRowNum := l - 1
  2707. // 序号
  2708. sortNum := k + 1
  2709. colA := dataRow.AddCell()
  2710. colA.VMerge = mergeRowNum
  2711. colA.SetString(fmt.Sprint(sortNum))
  2712. // 客户名称
  2713. colB := dataRow.AddCell()
  2714. colB.VMerge = mergeRowNum
  2715. colB.SetString(v.CompanyName)
  2716. // 是否新客户
  2717. colC := dataRow.AddCell()
  2718. colC.VMerge = mergeRowNum
  2719. colC.SetString(newCompanyMap[v.NewCompany])
  2720. // 合同有效期
  2721. colD := dataRow.AddCell()
  2722. colD.VMerge = mergeRowNum
  2723. colD.SetString(fmt.Sprint(v.StartDate, "至", v.EndDate))
  2724. // 开票到款信息
  2725. for k2, v2 := range v.InvoicePaymentList {
  2726. rowData := []string{
  2727. v2.InvoiceDate, // 开票日
  2728. fmt.Sprint(v2.InvoiceAmount), // 开票金额
  2729. v2.PaymentDate, // 到款日
  2730. fmt.Sprint(v2.PaymentAmount), // 到款金额
  2731. fms.ContractPaymentPayTypeNameMap[v2.PayType], // 付款方式
  2732. v2.SellerName, // 销售
  2733. v2.SellerGroupName, // 组别
  2734. sellerTypeMap[v2.SellerType], // 销售类型
  2735. }
  2736. // 套餐金额信息
  2737. serviceTempShowAmount := make(map[int]string)
  2738. for i := range serviceTempShow {
  2739. for s2 := range v2.ServiceAmountList {
  2740. item := v2.ServiceAmountList[s2]
  2741. if item.ServiceTemplateId == serviceTempShow[i].ServiceTemplateId {
  2742. serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount)
  2743. break
  2744. }else if serviceTempShow[i].Pid == item.ServiceTemplateId {
  2745. serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount/2)
  2746. break
  2747. }
  2748. }
  2749. }
  2750. for i := range serviceTempShow {
  2751. sa := ""
  2752. if am, ok := serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId]; ok {
  2753. sa = am
  2754. }
  2755. rowData = append(rowData, sa)
  2756. }
  2757. // 首行开票到款
  2758. if k2 == 0 {
  2759. for i := range rowData {
  2760. dataRow.AddCell().SetString(rowData[i])
  2761. }
  2762. continue
  2763. }
  2764. // 其他行开票到款, 加四列空的单元格用于合并
  2765. dataRowExtra := sheet.AddRow()
  2766. for i := 0; i < 4; i++ {
  2767. dataRowExtra.AddCell()
  2768. }
  2769. for i := range rowData {
  2770. dataRowExtra.AddCell().SetString(rowData[i])
  2771. }
  2772. }
  2773. }
  2774. // 输出文件
  2775. var buffer bytes.Buffer
  2776. _ = xlsxFile.Write(&buffer)
  2777. content := bytes.NewReader(buffer.Bytes())
  2778. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  2779. fileName := "商品到款统计_" + randStr + ".xlsx"
  2780. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  2781. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  2782. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  2783. }