invoice_payment.go 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945
  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() - 1)
  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. numMonth := yearNum*12 + monthNum //共存在多少个月
  1002. //if req.ListParam == "1" {
  1003. // numMonth = numMonth / 3
  1004. //} else if req.ListParam == "2" {
  1005. // numMonth = numMonth / 6
  1006. //} else if req.ListParam == "3" {
  1007. // numMonth = numMonth / 12
  1008. //}
  1009. // 累计值
  1010. var accumulate float64
  1011. var partAccumulate float64
  1012. var historyAccumulate float64
  1013. var partHistoryAccumulate float64
  1014. //dataList := make([]*fms.IncomeSummaryItem, 0)
  1015. //historydataList := make([]*fms.IncomeSummaryItem, 0)
  1016. fmt.Println("numMonth:", numMonth)
  1017. fmt.Println("InvoiceDate:", invoiceItem.InvoiceDate)
  1018. var j int
  1019. for i := 0; i <= numMonth; i++ {
  1020. //timeNow, _ := time.Parse("2006-01", time.Now().Format("2006-01"))
  1021. var endDateTime time.Time
  1022. var prevEndDateTime time.Time
  1023. var prevStartDate, prevEndDate string
  1024. var startDate, endDate string
  1025. //开始日期
  1026. if req.StartDate != "" && req.EndDate != "" {
  1027. startDate = reqStartDate.AddDate(0, i, 0).Format("2006-01")
  1028. prevStartDate = reqStartDate.AddDate(-1, i, 0).Format("2006-01")
  1029. } else {
  1030. startDate = invoiceItem.InvoiceDate.AddDate(0, i-numMonth, 0).Format("2006-01")
  1031. prevStartDate = invoiceItem.InvoiceDate.AddDate(-1, i-numMonth, 0).Format("2006-01")
  1032. }
  1033. startDate = fmt.Sprint(startDate, "-01")
  1034. prevStartDate = fmt.Sprint(prevStartDate, "-01")
  1035. startDateTime, _ := time.Parse(utils.FormatDate, startDate)
  1036. prevStartDateTime, _ := time.Parse(utils.FormatDate, prevStartDate)
  1037. //结束日期
  1038. if req.StartDate != "" && req.EndDate != "" {
  1039. endDateTime = reqStartDate.AddDate(0, i+1, -1)
  1040. prevEndDateTime = reqStartDate.AddDate(-1, i+1, -1)
  1041. if reqEndDate.After(invoiceItem.InvoiceDate) {
  1042. endDateTime = invoiceItem.InvoiceDate.AddDate(0, i, 0)
  1043. prevEndDateTime = invoiceItem.InvoiceDate.AddDate(-1, i, 0)
  1044. }
  1045. } else {
  1046. endDateTime = invoiceItem.InvoiceDate.AddDate(0, i-numMonth+1, -1)
  1047. prevEndDateTime = invoiceItem.InvoiceDate.AddDate(-1, i-numMonth+1, -1)
  1048. }
  1049. endDate = endDateTime.Format(utils.FormatDate)
  1050. prevEndDate = prevEndDateTime.Format(utils.FormatDate)
  1051. cond := `1 = 1`
  1052. histrtyCond := `1 = 1`
  1053. pars := make([]interface{}, 0)
  1054. historyPars := make([]interface{}, 0)
  1055. prevCond := `1 = 1`
  1056. prevHistoryCond := `1 = 1`
  1057. prevPars := make([]interface{}, 0)
  1058. prevHistoryPars := make([]interface{}, 0)
  1059. if req.CompanyType == 1 {
  1060. cond += ` AND b.new_company = 1 `
  1061. prevCond += ` AND b.new_company = 1 `
  1062. histrtyCond += ` AND new_company = 1 `
  1063. prevHistoryCond += ` AND new_company = 1 `
  1064. } else if req.CompanyType == 2 {
  1065. cond += ` AND b.new_company = 0 `
  1066. prevCond += ` AND b.new_company = 0 `
  1067. histrtyCond += ` AND new_company = 0 `
  1068. prevHistoryCond += ` AND new_company = 0 `
  1069. }
  1070. if req.SellerIds != "" {
  1071. sellerIds := strings.Split(req.SellerIds, ",")
  1072. cond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
  1073. pars = append(pars, sellerIds, sellerIds)
  1074. prevCond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
  1075. prevPars = append(prevPars, sellerIds, sellerIds)
  1076. histrtyCond += ` AND seller_id in ? `
  1077. prevHistoryCond += ` AND seller_id in ? `
  1078. historyPars = append(historyPars, sellerIds)
  1079. prevHistoryPars = append(prevHistoryPars, sellerIds)
  1080. }
  1081. //fmt.Println("i:",i)
  1082. //fmt.Println("j:",j)
  1083. {
  1084. //本期
  1085. st := fmt.Sprint(startDate, " 00:00:00")
  1086. ed := fmt.Sprint(endDate, " 23:59:59")
  1087. //校验日期,分段查询
  1088. if startDateTime.After(historyTime) || startDateTime.Equal(historyTime) {
  1089. //全部走新查询
  1090. //fmt.Println("新查询")
  1091. cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  1092. pars = append(pars, st, ed, st, ed)
  1093. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
  1094. if e != nil {
  1095. return
  1096. }
  1097. // 开票到款金额合计(换算后)
  1098. var amountTotal float64
  1099. if len(summaryIds) > 0 {
  1100. amountCond := `a.id IN ? AND (a.invoice_id <> 0 OR (a.payment_id <> 0 AND a.invoice_id =0))`
  1101. amountPars := make([]interface{}, 0)
  1102. amountPars = append(amountPars, summaryIds)
  1103. amountCond += ` AND (b.invoice_time BETWEEN ? AND ?)`
  1104. amountPars = append(amountPars, st, ed)
  1105. results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  1106. if e != nil {
  1107. err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1108. return
  1109. }
  1110. //dataList = append(dataList, results...)
  1111. var amountSum float64
  1112. for _, result := range results {
  1113. incomeChart.DataList = append(incomeChart.DataList, result)
  1114. amountSum += result.Amount
  1115. }
  1116. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1117. accumulate += amountTotal
  1118. partAccumulate += amountTotal
  1119. }
  1120. if i == j || i == numMonth {
  1121. if req.ListParam == "4" {
  1122. totalMoneySlice = append(totalMoneySlice, accumulate)
  1123. } else if req.ListParam == "0" {
  1124. totalMoneySlice = append(totalMoneySlice, amountTotal)
  1125. } else if i > 0 || i == numMonth {
  1126. totalMoneySlice = append(totalMoneySlice, partAccumulate)
  1127. fmt.Println("partAccumulate:",partAccumulate)
  1128. partAccumulate = 0.0
  1129. }
  1130. }
  1131. } else if endDateTime.Before(historyTime) || endDateTime.Equal(historyTime) {
  1132. //全部走旧查询
  1133. //fmt.Println("旧查询")
  1134. fmt.Println("st:",st)
  1135. fmt.Println("ed:",ed)
  1136. histrtyCond += ` AND (invoice_time BETWEEN ? AND ? )`
  1137. historyPars = append(historyPars, st, ed)
  1138. //fmt.Println("st:",st)
  1139. //fmt.Println("ed:",ed)
  1140. // 开票到款金额合计(换算后)
  1141. var amountTotal float64
  1142. results, e := fms.GetIncomeHistory(histrtyCond, historyPars)
  1143. if e != nil {
  1144. err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1145. return
  1146. }
  1147. var amountSum float64
  1148. //dataList = append(dataList, results...)
  1149. for _, result := range results {
  1150. incomeChart.DataList = append(incomeChart.DataList, result)
  1151. amountSum += result.Amount
  1152. }
  1153. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1154. accumulate += amountTotal
  1155. partAccumulate += amountTotal
  1156. if i == j || i == numMonth {
  1157. if req.ListParam == "4" {
  1158. totalMoneySlice = append(totalMoneySlice, accumulate)
  1159. } else if req.ListParam == "0" {
  1160. totalMoneySlice = append(totalMoneySlice, amountTotal)
  1161. } else if i > 0 || i == numMonth {
  1162. totalMoneySlice = append(totalMoneySlice, partAccumulate)
  1163. partAccumulate = 0.0
  1164. }
  1165. }
  1166. //fmt.Println("partAccumulate:",partAccumulate)
  1167. }
  1168. //else {
  1169. // //刚好跨过20230301的,各查一部分
  1170. // //fmt.Println("查询3")
  1171. // var historyAmountTotal float64
  1172. // histrtyCond += ` AND (invoice_time BETWEEN ? AND '2023-03-01 23:59:59' ) `
  1173. // historyPars = append(historyPars, st)
  1174. // //// 开票到款金额合计(换算后)
  1175. // var amountTotal float64
  1176. // results, e := fms.GetIncomeHistory(histrtyCond, historyPars)
  1177. // if e != nil {
  1178. // err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1179. // return
  1180. // }
  1181. // var amountSum float64
  1182. // for _, result := range results {
  1183. // incomeChart.DataList = append(incomeChart.DataList, result)
  1184. // amountSum += result.Amount
  1185. // }
  1186. // amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1187. // accumulate += amountTotal
  1188. // historyAmountTotal = amountTotal
  1189. //
  1190. // 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 ?))`
  1191. // pars = append(pars, ed, ed)
  1192. // summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
  1193. // if e != nil {
  1194. // return
  1195. // }
  1196. //
  1197. // if len(summaryIds) > 0 {
  1198. // amountCond := `a.id IN ? AND (a.invoice_id <> 0 OR (a.payment_id <> 0 AND a.invoice_id =0))`
  1199. // amountPars := make([]interface{}, 0)
  1200. // amountPars = append(amountPars, summaryIds)
  1201. // results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  1202. // if e != nil {
  1203. // err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1204. // return
  1205. // }
  1206. // var amountSum float64
  1207. // for _, result := range results {
  1208. // incomeChart.DataList = append(incomeChart.DataList, result)
  1209. // amountSum += result.Amount
  1210. // }
  1211. // amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1212. // accumulate += amountTotal
  1213. // }
  1214. // if req.ListParam == "4" {
  1215. // totalMoneySlice = append(totalMoneySlice, accumulate)
  1216. // } else {
  1217. // totalMoneySlice = append(totalMoneySlice, amountTotal+historyAmountTotal)
  1218. // }
  1219. //
  1220. //}
  1221. }
  1222. { //去年同期,用于计算同比值
  1223. prevSt := fmt.Sprint(prevStartDate, " 00:00:00")
  1224. prevEd := fmt.Sprint(prevEndDate, " 23:59:59")
  1225. //校验日期,分段查询
  1226. if prevStartDateTime.After(historyTime) || prevStartDateTime.Equal(historyTime) {
  1227. //全部走新查询
  1228. prevCond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
  1229. prevPars = append(prevPars, prevSt, prevEd, prevSt, prevEd)
  1230. prevSummaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(prevCond, prevPars)
  1231. if e != nil {
  1232. return
  1233. }
  1234. // 开票到款金额合计(换算后)
  1235. var prevAmountTotal float64
  1236. if len(prevSummaryIds) > 0 {
  1237. amountCond := `a.id IN ? AND (a.invoice_id <> 0 OR (a.payment_id <> 0 AND a.invoice_id =0))`
  1238. amountPars := make([]interface{}, 0)
  1239. amountPars = append(amountPars, prevSummaryIds)
  1240. amountCond += ` AND (b.invoice_time BETWEEN ? AND ?)`
  1241. amountPars = append(amountPars, prevSt, prevEd)
  1242. results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  1243. if e != nil {
  1244. err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1245. return
  1246. }
  1247. var amountSum float64
  1248. //historydataList = append(historydataList, results...)
  1249. for _, result := range results {
  1250. amountSum += result.Amount
  1251. }
  1252. prevAmountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1253. historyAccumulate += prevAmountTotal
  1254. partHistoryAccumulate += prevAmountTotal
  1255. }
  1256. if i == j || i == numMonth {
  1257. if req.ListParam == "4" {
  1258. prevTotalMoneySlice = append(prevTotalMoneySlice, historyAccumulate)
  1259. } else if req.ListParam == "0" {
  1260. prevTotalMoneySlice = append(prevTotalMoneySlice, prevAmountTotal)
  1261. } else if i > 0 || i == numMonth {
  1262. prevTotalMoneySlice = append(prevTotalMoneySlice, partHistoryAccumulate)
  1263. fmt.Println("partHistoryAccumulate:",partHistoryAccumulate)
  1264. partHistoryAccumulate = 0.0
  1265. }
  1266. }
  1267. } else if prevEndDateTime.Before(historyTime) || prevEndDateTime.Equal(historyTime) {
  1268. //全部走旧查询
  1269. fmt.Println("prevSt:",prevSt)
  1270. fmt.Println("prevEd:",prevEd)
  1271. prevHistoryCond += ` AND (invoice_time BETWEEN ? AND ?)`
  1272. prevHistoryPars = append(prevHistoryPars, prevSt, prevEd)
  1273. // 开票到款金额合计(换算后)
  1274. var amountTotal float64
  1275. results, e := fms.GetIncomeHistory(prevHistoryCond, prevHistoryPars)
  1276. if e != nil {
  1277. err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1278. return
  1279. }
  1280. //historydataList = append(historydataList, results...)
  1281. var amountSum float64
  1282. for _, result := range results {
  1283. amountSum += result.Amount
  1284. }
  1285. amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1286. historyAccumulate += amountTotal
  1287. partHistoryAccumulate += amountTotal
  1288. if i == j || i == numMonth {
  1289. if req.ListParam == "4" {
  1290. prevTotalMoneySlice = append(prevTotalMoneySlice, historyAccumulate)
  1291. } else if req.ListParam == "0" {
  1292. prevTotalMoneySlice = append(prevTotalMoneySlice, amountTotal)
  1293. } else if i > 0 || i == numMonth {
  1294. prevTotalMoneySlice = append(prevTotalMoneySlice, partHistoryAccumulate)
  1295. fmt.Println("partHistoryAccumulate:",partHistoryAccumulate)
  1296. partHistoryAccumulate = 0.0
  1297. }
  1298. }
  1299. //fmt.Println("partHistoryAccumulate:",partHistoryAccumulate)
  1300. }
  1301. //else {
  1302. // //刚好跨过20230301的,各查一部分
  1303. // var historyAmountTotal float64
  1304. // prevHistoryCond += ` AND (invoice_time BETWEEN ? AND '2023-03-01 23:59:59' ) `
  1305. // prevHistoryPars = append(prevHistoryPars, prevSt)
  1306. // // 开票到款金额合计(换算后)
  1307. // var amountTotal float64
  1308. // results, e := fms.GetIncomeHistory(prevHistoryCond, prevHistoryPars)
  1309. // if e != nil {
  1310. // err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1311. // return
  1312. // }
  1313. // var amountSum float64
  1314. // for _, result := range results {
  1315. // amountSum += result.Amount
  1316. // }
  1317. // amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1318. // historyAccumulate += amountTotal
  1319. //
  1320. //
  1321. // 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 ?))`
  1322. // prevPars = append(prevPars, prevEd, prevEd)
  1323. // prevSummaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(prevCond, prevPars)
  1324. // if e != nil {
  1325. // return
  1326. // }
  1327. //
  1328. // // 开票到款金额合计(换算后)
  1329. // var prevAmountTotal float64
  1330. //
  1331. // if len(prevSummaryIds) > 0 {
  1332. // amountCond := `a.id IN ? AND (a.invoice_id <> 0 OR (a.payment_id <> 0 AND a.invoice_id =0))`
  1333. // amountPars := make([]interface{}, 0)
  1334. // amountPars = append(amountPars, prevSummaryIds)
  1335. // results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
  1336. // if e != nil {
  1337. // err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
  1338. // return
  1339. // }
  1340. // var amountSum float64
  1341. // for _, result := range results {
  1342. // amountSum += result.Amount
  1343. // }
  1344. // prevAmountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
  1345. // historyAccumulate += prevAmountTotal
  1346. // }
  1347. // if req.ListParam == "4" {
  1348. // prevTotalMoneySlice = append(prevTotalMoneySlice, historyAccumulate)
  1349. // } else {
  1350. // prevTotalMoneySlice = append(prevTotalMoneySlice, prevAmountTotal+historyAmountTotal)
  1351. // }
  1352. //}
  1353. if req.ListParam == "1" && i == j {
  1354. if i == 0{
  1355. dateSlice = append(dateSlice, startDateTime.AddDate(0, 2, 0).Format("06/01"))
  1356. j = j + 2
  1357. } else {
  1358. dateSlice = append(dateSlice, startDateTime.AddDate(0, 3, 0).Format("06/01"))
  1359. j = j + 3
  1360. }
  1361. } else if req.ListParam == "2" && i == j {
  1362. if i == 0{
  1363. dateSlice = append(dateSlice, startDateTime.AddDate(0, 5, 0).Format("06/01"))
  1364. j = j + 5
  1365. } else {
  1366. dateSlice = append(dateSlice, startDateTime.AddDate(0, 6, 0).Format("06/01"))
  1367. j = j + 6
  1368. }
  1369. } else if req.ListParam == "3" && i == j {
  1370. if i == 0{
  1371. dateSlice = append(dateSlice, startDateTime.AddDate(0, 11, 0).Format("06/01"))
  1372. j = j + 11
  1373. } else {
  1374. dateSlice = append(dateSlice, startDateTime.AddDate(0, 12, 0).Format("06/01"))
  1375. j = j + 12
  1376. }
  1377. } else if i == j {
  1378. dateSlice = append(dateSlice, startDateTime.Format("06/01"))
  1379. j++
  1380. }
  1381. }
  1382. }
  1383. //for i, v := range dataList {
  1384. // amountTotal, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.Amount), 64)
  1385. // accumulate += amountTotal
  1386. //
  1387. // if req.ListParam == "4" {
  1388. // totalMoneySlice = append(totalMoneySlice, accumulate)
  1389. // } else {
  1390. // totalMoneySlice = append(totalMoneySlice, amountTotal)
  1391. // }
  1392. // if req.ListParam == "1" {
  1393. // i = i + 2
  1394. // } else if req.ListParam == "2" {
  1395. // i = i + 5
  1396. // } else if req.ListParam == "3" {
  1397. // i = i + 11
  1398. // }
  1399. //}
  1400. //
  1401. //for i, v := range historydataList {
  1402. // historyAmountTotal, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.Amount), 64)
  1403. // historyAccumulate += historyAmountTotal
  1404. //
  1405. // if req.ListParam == "4" {
  1406. // prevTotalMoneySlice = append(prevTotalMoneySlice, historyAccumulate)
  1407. // } else {
  1408. // prevTotalMoneySlice = append(prevTotalMoneySlice, historyAmountTotal)
  1409. // }
  1410. // if req.ListParam == "1" {
  1411. // i = i + 2
  1412. // } else if req.ListParam == "2" {
  1413. // i = i + 5
  1414. // } else if req.ListParam == "3" {
  1415. // i = i + 11
  1416. // }
  1417. //}
  1418. fmt.Println("prevTotalMoneySlice:", len(prevTotalMoneySlice))
  1419. fmt.Println("totalMoneySlice:", len(totalMoneySlice))
  1420. //计算同比值
  1421. for i := range prevTotalMoneySlice {
  1422. var yoy float64
  1423. var yoyStr string
  1424. //fmt.Println("1:", prevTotalMoneySlice[i])
  1425. //fmt.Println("2:", totalMoneySlice[i])
  1426. //fmt.Println("3:", totalMoneySlice[i]-prevTotalMoneySlice[i])
  1427. if prevTotalMoneySlice[i] != 0 && totalMoneySlice[i] != 0 {
  1428. yoy = (totalMoneySlice[i] - prevTotalMoneySlice[i]) / prevTotalMoneySlice[i]
  1429. yoyStr = fmt.Sprintf("%.4f", yoy)
  1430. if i == len(prevTotalMoneySlice)-1 && i > 0{
  1431. fmt.Println("totalMoneySlice[i-1]:",totalMoneySlice[i-1])
  1432. yoy = (totalMoneySlice[i] - totalMoneySlice[i-1]) / totalMoneySlice[i-1]
  1433. yoyStr = fmt.Sprintf("%.4f", yoy)
  1434. }
  1435. }
  1436. yoySlice = append(yoySlice, yoyStr)
  1437. }
  1438. incomeChart.Title = "开票到款统计图"
  1439. incomeChart.Date = dateSlice
  1440. incomeChart.TotalMoney = totalMoneySlice
  1441. incomeChart.PrevTotalMoney = prevTotalMoneySlice
  1442. incomeChart.Yoy = yoySlice
  1443. //redisJsonData, err := json.Marshal(incomeChart)
  1444. //if err == nil {
  1445. // global.Redis.SetEX(context.TODO(), key, string(redisJsonData), time.Minute*30)
  1446. //}
  1447. //} else {
  1448. // err = json.Unmarshal([]byte(redisJsonData), &incomeChart)
  1449. // if err != nil {
  1450. // fmt.Println("近两年的收入统计数据,json转换失败")
  1451. // }
  1452. //}
  1453. return
  1454. }
  1455. // ExportIncomeList 导出业务收入统计列表
  1456. func ExportIncomeList(c *gin.Context, list []*fms.IncomeSummaryItem) {
  1457. // 生成Excel文件
  1458. xlsxFile := xlsx.NewFile()
  1459. style := xlsx.NewStyle()
  1460. alignment := xlsx.Alignment{
  1461. Horizontal: "center",
  1462. Vertical: "center",
  1463. WrapText: true,
  1464. }
  1465. style.Alignment = alignment
  1466. style.ApplyAlignment = true
  1467. sheetName := "业务收入统计表"
  1468. sheet, err := xlsxFile.AddSheet(sheetName)
  1469. if err != nil {
  1470. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  1471. return
  1472. }
  1473. // 数据表头
  1474. rowTitle := []string{"开票日期", "新客户(1)", "客户名称", "金额", "销售"}
  1475. titleRow := sheet.AddRow()
  1476. for i := range rowTitle {
  1477. v := titleRow.AddCell()
  1478. v.SetString(rowTitle[i])
  1479. v.SetStyle(style)
  1480. }
  1481. incomeSummaryItemList := make(fms.IncomeSummaryItemList, 0)
  1482. incomeSummaryItemList = list
  1483. sort.Sort(incomeSummaryItemList)
  1484. // 填充数据
  1485. for _, v := range incomeSummaryItemList {
  1486. dataRow := sheet.AddRow()
  1487. dataRow.AddCell().SetString(v.InvoiceDate.Format(utils.FormatDate)) // 开票日期
  1488. dataRow.AddCell().SetString(strconv.Itoa(v.NewCompany)) // 新客户
  1489. dataRow.AddCell().SetString(v.CompanyName) // 客户名称
  1490. dataRow.AddCell().SetString(fmt.Sprint(v.Amount)) // 金额
  1491. dataRow.AddCell().SetString(v.SellerName) // 销售员
  1492. }
  1493. // 输出文件
  1494. var buffer bytes.Buffer
  1495. _ = xlsxFile.Write(&buffer)
  1496. content := bytes.NewReader(buffer.Bytes())
  1497. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1498. fileName := sheetName + randStr + ".xlsx"
  1499. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  1500. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1501. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  1502. }
  1503. // List
  1504. // @Title 未开票统计列表
  1505. // @Description 未开票统计列表
  1506. // @Param Keyword query string false "关键词"
  1507. // @Param SellGroupId query int false "销售组别ID"
  1508. // @Param ServiceType query int false "套餐类型"
  1509. // @Param StartDate query string false "合同开始日期"
  1510. // @Param EndDate query string false "合同结束日期"
  1511. // @Param TimeType query int false "时间类型: 1-开票时间; 2-到款时间"
  1512. // @Param HasInvoice query string false "是否已开票: 0-否; 1-是; 空-全部"
  1513. // @Param HasPayment query string false "是否已到款: 0-否; 1-是; 空-全部"
  1514. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  1515. // @Param ListParam query int false "套餐类型: 0-全部; 1-ficc; 2-权益"
  1516. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'start_date':开票日 、 'end_date':到款日"
  1517. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
  1518. // @Success 200 {object} fms.ContractRegisterItem
  1519. // @router /census/invoice_payment/not_invoice/list [get]
  1520. func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
  1521. var req fms.InvoicePaymentCensusListReq
  1522. if e := c.BindQuery(&req); e != nil {
  1523. err, ok := e.(validator.ValidationErrors)
  1524. if !ok {
  1525. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  1526. return
  1527. }
  1528. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1529. return
  1530. }
  1531. cond := `1 = 1`
  1532. pars := make([]interface{}, 0)
  1533. // 客户姓名/合同编号
  1534. if req.Keyword != "" {
  1535. kw := "%" + req.Keyword + "%"
  1536. cond += ` AND (b.company_name LIKE ? OR b.contract_code LIKE ?)`
  1537. pars = append(pars, kw, kw)
  1538. }
  1539. if req.SellerIds != "" {
  1540. sellerIds := strings.Split(req.SellerIds, ",")
  1541. cond += ` AND (c.seller_id in ?)`
  1542. pars = append(pars, sellerIds)
  1543. }
  1544. // 套餐筛选
  1545. if req.ServiceTypes != "" {
  1546. serviceTypes := strings.Split(req.ServiceTypes, ",")
  1547. tempRegisterIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
  1548. if e != nil {
  1549. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  1550. return
  1551. }
  1552. if len(tempRegisterIds) > 0 {
  1553. cond += ` AND a.register_id IN ?`
  1554. pars = append(pars, tempRegisterIds)
  1555. } else {
  1556. cond += ` AND 1 = 2`
  1557. }
  1558. }
  1559. if req.ListParam == 1{
  1560. cond += ` AND a.service_product_id = 1 `
  1561. }else if req.ListParam == 2 {
  1562. cond += ` AND a.service_product_id = 2 `
  1563. }
  1564. // 开票到款日期
  1565. //if req.TimeType != 0 {
  1566. // if req.TimeType == 1 && req.StartDate != "" && req.EndDate != "" {
  1567. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  1568. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1569. // cond += ` AND (c.invoice_time BETWEEN ? AND ?) `
  1570. // pars = append(pars, st, ed)
  1571. // }else if req.TimeType == 2 && req.StartDate != "" && req.EndDate != "" {
  1572. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  1573. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1574. // cond += ` AND (d.invoice_time BETWEEN ? AND ?) `
  1575. // pars = append(pars, st, ed)
  1576. // }else if req.TimeType == 3 && req.StartDate != "" && req.EndDate != "" {
  1577. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  1578. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1579. // cond += ` AND (c.invoice_time BETWEEN ? AND ?) AND (d.invoice_time BETWEEN ? AND ?) `
  1580. // pars = append(pars, st, ed, st, ed)
  1581. // }
  1582. //} else if 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 ?) or (d.invoice_time BETWEEN ? AND ?))`
  1586. // pars = append(pars, st, ed, st, ed)
  1587. //}
  1588. //if req.HasInvoice == "1" {
  1589. // cond += ` AND a.invoice_id > 0 `
  1590. //}else if req.HasInvoice == "0" {
  1591. // cond += ` AND a.invoice_id = 0 `
  1592. //}
  1593. //
  1594. //if req.HasPayment == "1" {
  1595. // cond += ` AND a.payment_id > 0 `
  1596. //}else if req.HasPayment == "0" {
  1597. // cond += ` AND a.payment_id = 0 `
  1598. //}
  1599. page := new(base.Page)
  1600. page.SetPageSize(req.PageSize)
  1601. page.SetCurrent(req.Current)
  1602. //排序
  1603. if req.SortType == "" {
  1604. req.SortType = "desc"
  1605. }
  1606. if req.SortType != "desc" && req.SortType != "asc" {
  1607. resp.Fail("排序类型不正确", c)
  1608. return
  1609. }
  1610. if req.SortParam == "" {
  1611. page.AddOrderItem(base.OrderItem{Column: "e.start_date", Asc: false})
  1612. }else if req.SortParam == "start_date" {
  1613. if req.SortType == "asc" {
  1614. page.AddOrderItem(base.OrderItem{Column: "e.start_date", Asc: true})
  1615. }else{
  1616. page.AddOrderItem(base.OrderItem{Column: "e.start_date", Asc: false})
  1617. }
  1618. }else if req.SortParam == "end_date" {
  1619. if req.SortType == "asc" {
  1620. page.AddOrderItem(base.OrderItem{Column: "e.end_date", Asc: true})
  1621. }else{
  1622. page.AddOrderItem(base.OrderItem{Column: "e.end_date", Asc: false})
  1623. }
  1624. }else{
  1625. resp.Fail("排序字段不正确", c)
  1626. return
  1627. }
  1628. if req.IsExport == 1 {
  1629. page.SetPageSize(10000)
  1630. page.SetCurrent(1)
  1631. }
  1632. registerList, total, e := fms.GetInvoiceCensusPageList(page, cond, pars)
  1633. if e != nil {
  1634. resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
  1635. return
  1636. }
  1637. queryRegisterIds := make([]int, 0)
  1638. for i := range registerList {
  1639. queryRegisterIds = append(queryRegisterIds, registerList[i].ContractRegisterId)
  1640. }
  1641. //fmt.Println("queryRegisterIds:",queryRegisterIds)
  1642. results := new(fms.NotInvoicePaymentCensusResp)
  1643. if len(queryRegisterIds) > 0 {
  1644. // 获取汇总数据IDs, 用于查询合计数据
  1645. summaryIdsCond := cond
  1646. summaryIdsPars := pars
  1647. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
  1648. if e != nil {
  1649. resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
  1650. return
  1651. }
  1652. //fmt.Println("summaryIds:",summaryIds)
  1653. var listErr, totalErr, totalGroupErr error
  1654. wg := sync.WaitGroup{}
  1655. // 响应列表
  1656. respList := make([]*fms.InvoicePaymentCensusItem, 0)
  1657. summaryList := make([]*fms.InvoicePaymentSummaryItem, 0)
  1658. wg.Add(1)
  1659. go func() {
  1660. defer wg.Done()
  1661. // 获取汇总数据
  1662. summaryCond := cond
  1663. summaryCond += ` AND a.register_id IN ?`
  1664. summaryPars := pars
  1665. summaryPars = append(summaryPars, queryRegisterIds)
  1666. summaryData, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
  1667. if e != nil {
  1668. resp.FailMsg("获取失败", "获取商品到款汇总列表失败, Err: "+e.Error(), c)
  1669. return
  1670. }
  1671. summaryList = summaryData
  1672. //summaryIds := make([]int, 0)
  1673. paymentIds := make([]int, 0)
  1674. for i := range summaryList {
  1675. //summaryIds = append(summaryIds, summaryList[i].SummaryId)
  1676. if summaryList[i].PaymentId > 0 {
  1677. paymentIds = append(paymentIds, summaryList[i].PaymentId)
  1678. }
  1679. }
  1680. // 合同套餐
  1681. /*contractServiceCond := `contract_register_id IN ?`
  1682. contractServicePars := make([]interface{}, 0)
  1683. contractServicePars = append(contractServicePars, queryRegisterIds)
  1684. contractServiceOB := new(fms.ContractService)
  1685. contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
  1686. if e != nil {
  1687. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  1688. return
  1689. }
  1690. contractServiceMap := make(map[int][]*fms.ContractService, 0)
  1691. servicesNameMap := make(map[int][]string, 0)
  1692. for i := range contractServiceList {
  1693. if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
  1694. contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
  1695. }
  1696. contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
  1697. servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
  1698. }
  1699. */
  1700. servicesNameMap, serviceFormatMap, e := fmsService.GetContractServiceNameFormat(queryRegisterIds)
  1701. if e != nil {
  1702. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  1703. return
  1704. }
  1705. // 到款套餐分配
  1706. serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
  1707. if len(paymentIds) > 0 {
  1708. serviceAmountCond := `contract_payment_id IN ?`
  1709. serviceAmountPars := make([]interface{}, 0)
  1710. serviceAmountPars = append(serviceAmountPars, paymentIds)
  1711. serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  1712. serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  1713. if e != nil {
  1714. listErr = fmt.Errorf("获取到款套餐分配列表失败, Err: %s", e.Error())
  1715. return
  1716. }
  1717. for i := range serviceAmountList {
  1718. if serviceAmountMap[serviceAmountList[i].ContractPaymentId] == nil {
  1719. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = make([]*fms.ContractPaymentServiceAmount, 0)
  1720. }
  1721. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = append(serviceAmountMap[serviceAmountList[i].ContractPaymentId], serviceAmountList[i])
  1722. }
  1723. }
  1724. // 重组汇总数据
  1725. summaryMap := make(map[int][]*fms.InvoicePaymentCensusInfo)
  1726. amountMap := make(map[string]*fms.ContractPaymentServiceAmount)
  1727. for i := range summaryList {
  1728. v := new(fms.InvoicePaymentCensusInfo)
  1729. v.InvoiceId = summaryList[i].InvoiceId
  1730. v.InvoiceDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].InvoiceDate)
  1731. v.InvoiceAmount = summaryList[i].InvoiceAmount
  1732. v.SellerId = summaryList[i].SellerId
  1733. v.SellerName = summaryList[i].SellerName
  1734. v.SellerGroupId = summaryList[i].SellerGroupId
  1735. v.SellerGroupName = summaryList[i].SellerGroupName
  1736. v.SellerType = summaryList[i].ServiceProductId
  1737. v.PaymentId = summaryList[i].PaymentId
  1738. v.PaymentDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].PaymentDate)
  1739. v.PaymentAmount = summaryList[i].PaymentAmount
  1740. v.PayType = summaryList[i].PayType
  1741. // 套餐到款分配
  1742. svaList := make([]*fms.ContractPaymentServiceAmountItem, 0)
  1743. amountList := serviceAmountMap[summaryList[i].PaymentId]
  1744. if amountList != nil {
  1745. for i := range amountList {
  1746. k := fmt.Sprintf("%d-%d", amountList[i].ContractPaymentId, amountList[i].ServiceTemplateId)
  1747. amountMap[k] = amountList[i]
  1748. }
  1749. }
  1750. // 合同对应的所有套餐
  1751. svList := serviceFormatMap[summaryList[i].RegisterId]
  1752. if svList != nil {
  1753. for ii := range svList {
  1754. vv := new(fms.ContractPaymentServiceAmountItem)
  1755. vv.ServiceTemplateId = svList[ii].ServiceTemplateId
  1756. vv.ServiceTemplateName = svList[ii].FormatTitle
  1757. vv.ServiceTemplatePid = svList[ii].ServiceTemplatePid
  1758. k2 := fmt.Sprintf("%d-%d", summaryList[i].PaymentId, svList[ii].ServiceTemplateId)
  1759. a := amountMap[k2]
  1760. if a != nil {
  1761. vv.ContractPaymentServiceAmountId = a.ContractPaymentServiceAmountId
  1762. vv.ContractPaymentId = a.ContractPaymentId
  1763. vv.Amount = a.Amount
  1764. }
  1765. svaList = append(svaList, vv)
  1766. }
  1767. }
  1768. v.ServiceAmountList = svaList
  1769. summaryMap[summaryList[i].SummaryId] = append(summaryMap[summaryList[i].SummaryId], v)
  1770. }
  1771. // 货币列表
  1772. currencyOB := new(fms.CurrencyUnit)
  1773. currencyCond := `enable = 1`
  1774. currencyPars := make([]interface{}, 0)
  1775. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  1776. if e != nil {
  1777. resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
  1778. return
  1779. }
  1780. unitMap := make(map[string]string)
  1781. for i := range currencyList {
  1782. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  1783. }
  1784. // 响应列表
  1785. for i := range registerList {
  1786. v := new(fms.InvoicePaymentCensusItem)
  1787. v.SummaryId = registerList[i].SummaryId
  1788. v.ContractRegisterId = registerList[i].ContractRegisterId
  1789. v.CompanyName = registerList[i].CompanyName
  1790. v.NewCompany = registerList[i].NewCompany
  1791. v.StartDate = utils.TimeTransferString(utils.FormatDate, registerList[i].StartDate)
  1792. v.EndDate = utils.TimeTransferString(utils.FormatDate, registerList[i].EndDate)
  1793. v.ServicesName = servicesNameMap[registerList[i].ContractRegisterId]
  1794. v.InvoicePaymentList = summaryMap[registerList[i].SummaryId]
  1795. v.ContractType = registerList[i].ContractType
  1796. v.ContractCode = registerList[i].ContractCode
  1797. v.ContractAmount = registerList[i].ContractAmount
  1798. v.UnitName = unitMap[registerList[i].CurrencyUnit]
  1799. v.ProductIds = registerList[i].ProductIds
  1800. v.NotInvoiceTotal = registerList[i].ContractAmount - registerList[i].InvoicedAmount
  1801. respList = append(respList, v)
  1802. }
  1803. }()
  1804. // 开票到款金额合计(换算后)
  1805. var invoiceTotal, paymentTotal, contractAmount float64
  1806. wg.Add(1)
  1807. go func() {
  1808. defer wg.Done()
  1809. if len(summaryIds) == 0 {
  1810. return
  1811. }
  1812. amountTotalCond := `a.id IN ?`
  1813. amountTotalPars := make([]interface{}, 0)
  1814. amountTotalPars = append(amountTotalPars, summaryIds)
  1815. invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
  1816. if e != nil {
  1817. totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
  1818. return
  1819. }
  1820. invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
  1821. paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
  1822. if e != nil {
  1823. totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
  1824. return
  1825. }
  1826. paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
  1827. }()
  1828. // 分币种金额统计
  1829. invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  1830. paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  1831. contractAmountCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  1832. wg.Add(1)
  1833. go func() {
  1834. defer wg.Done()
  1835. currencyOB := new(fms.CurrencyUnit)
  1836. currencyCond := `enable = 1`
  1837. currencyPars := make([]interface{}, 0)
  1838. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  1839. if e != nil {
  1840. totalGroupErr = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
  1841. return
  1842. }
  1843. unitMap := make(map[string]string)
  1844. for i := range currencyList {
  1845. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  1846. invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  1847. Name: currencyList[i].Name,
  1848. UnitName: currencyList[i].UnitName,
  1849. Code: currencyList[i].Code,
  1850. FlagImg: currencyList[i].FlagImg,
  1851. })
  1852. paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  1853. Name: currencyList[i].Name,
  1854. UnitName: currencyList[i].UnitName,
  1855. Code: currencyList[i].Code,
  1856. FlagImg: currencyList[i].FlagImg,
  1857. })
  1858. contractAmountCurrencyTotals = append(contractAmountCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  1859. Name: currencyList[i].Name,
  1860. UnitName: currencyList[i].UnitName,
  1861. Code: currencyList[i].Code,
  1862. FlagImg: currencyList[i].FlagImg,
  1863. })
  1864. }
  1865. if len(summaryIds) == 0 {
  1866. return
  1867. }
  1868. totalGroupCond := `a.id IN ?`
  1869. totalGroupPars := make([]interface{}, 0)
  1870. totalGroupPars = append(totalGroupPars, summaryIds)
  1871. invoiceSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 1)
  1872. if e != nil {
  1873. totalGroupErr = fmt.Errorf("获取汇总货币合计开票金额失败, Err: %s", e.Error())
  1874. return
  1875. }
  1876. paymentSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 2)
  1877. if e != nil {
  1878. totalGroupErr = fmt.Errorf("获取汇总货币合计到款金额失败, Err: %s", e.Error())
  1879. return
  1880. }
  1881. invoiceSumMap := make(map[string]float64)
  1882. paymentSumMap := make(map[string]float64)
  1883. contractSumMap := make(map[string]float64)
  1884. for i := range invoiceSumGroup {
  1885. invoiceSumMap[invoiceSumGroup[i].CurrencyUnit] = invoiceSumGroup[i].OriginAmountTotal
  1886. continue
  1887. }
  1888. for i := range paymentSumGroup {
  1889. paymentSumMap[paymentSumGroup[i].CurrencyUnit] = paymentSumGroup[i].OriginAmountTotal
  1890. continue
  1891. }
  1892. for i := range invoiceCurrencyTotals {
  1893. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
  1894. invoiceCurrencyTotals[i].Amount = a
  1895. }
  1896. for i := range paymentCurrencyTotals {
  1897. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
  1898. paymentCurrencyTotals[i].Amount = a
  1899. }
  1900. contractRegisters, err := fms.GetContractInfoByRegisterIds(queryRegisterIds)
  1901. if err != nil {
  1902. resp.FailMsg("查询合同信息失败", "查询合同信息失败, Err: "+err.Error(), c)
  1903. return
  1904. }
  1905. for _, v := range contractRegisters {
  1906. if v.RMBRate == 0.0{
  1907. v.RMBRate = 1
  1908. }
  1909. amount,_ := strconv.ParseFloat(fmt.Sprintf("%.2f",v.ContractAmount / v.RMBRate), 64)
  1910. contractAmount += amount
  1911. contractSumMap[v.CurrencyUnit] = v.ContractAmount
  1912. }
  1913. for _, v := range contractAmountCurrencyTotals {
  1914. v.Amount = contractSumMap[v.Code] - invoiceSumMap[v.Code]
  1915. }
  1916. }()
  1917. wg.Wait()
  1918. if listErr != nil {
  1919. resp.FailMsg("获取失败", listErr.Error(), c)
  1920. return
  1921. }
  1922. if totalErr != nil {
  1923. resp.FailMsg("获取失败", totalErr.Error(), c)
  1924. return
  1925. }
  1926. if totalGroupErr != nil {
  1927. resp.FailMsg("获取失败", totalGroupErr.Error(), c)
  1928. return
  1929. }
  1930. results.DataList = respList
  1931. results.InvoiceTotal = invoiceTotal
  1932. results.PaymentTotal = paymentTotal
  1933. results.NotInvoiceTotal = contractAmount-invoiceTotal
  1934. results.InvoiceCurrencyTotal = invoiceCurrencyTotals
  1935. results.PaymentCurrencyTotal = paymentCurrencyTotals
  1936. results.NotInvoiceCurrencyTotal = contractAmountCurrencyTotals
  1937. }
  1938. // 是否导出
  1939. if req.IsExport == 1 {
  1940. ExportNotInvoiceCensusList(c, results)
  1941. return
  1942. }
  1943. page.SetTotal(total)
  1944. baseData := new(base.BaseData)
  1945. baseData.SetPage(page)
  1946. baseData.SetList(results)
  1947. resp.OkData("获取成功", baseData, c)
  1948. }
  1949. // List
  1950. // @Title 开票未到款统计列表
  1951. // @Description 开票未到款统计列表
  1952. // @Param Keyword query string false "关键词"
  1953. // @Param SellGroupId query int false "销售组别ID"
  1954. // @Param ServiceType query int false "套餐类型"
  1955. // @Param StartDate query string false "合同开始日期"
  1956. // @Param EndDate query string false "合同结束日期"
  1957. // @Param TimeType query int false "时间类型: 1-开票时间; 2-到款时间"
  1958. // @Param HasInvoice query string false "是否已开票: 0-否; 1-是; 空-全部"
  1959. // @Param HasPayment query string false "是否已到款: 0-否; 1-是; 空-全部"
  1960. // @Param IsExport query int false "是否导出: 0-否; 1-是"
  1961. // @Param ListParam query int false "套餐类型: 0-全部; 1-ficc; 2-权益"
  1962. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'invoice_time':开票日 、 'payment_date':到款日"
  1963. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
  1964. // @Success 200 {object} fms.ContractRegisterItem
  1965. // @router /census/invoice_payment/not_payment/list [get]
  1966. func (ct *InvoicePaymentController) NotPaymentList(c *gin.Context) {
  1967. var req fms.InvoicePaymentCensusListReq
  1968. if e := c.BindQuery(&req); e != nil {
  1969. err, ok := e.(validator.ValidationErrors)
  1970. if !ok {
  1971. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  1972. return
  1973. }
  1974. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1975. return
  1976. }
  1977. cond := `1 = 1`
  1978. pars := make([]interface{}, 0)
  1979. // 客户姓名/销售
  1980. if req.Keyword != "" {
  1981. kw := "%" + req.Keyword + "%"
  1982. cond += ` AND (b.company_name LIKE ? OR b.contract_code LIKE ?)`
  1983. pars = append(pars, kw, kw)
  1984. }
  1985. if req.SellerIds != "" {
  1986. sellerIds := strings.Split(req.SellerIds, ",")
  1987. cond += ` AND (c.seller_id in ?)`
  1988. pars = append(pars, sellerIds)
  1989. }
  1990. // 套餐筛选
  1991. if req.ServiceTypes != "" {
  1992. serviceTypes := strings.Split(req.ServiceTypes, ",")
  1993. tempRegisterIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
  1994. if e != nil {
  1995. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  1996. return
  1997. }
  1998. if len(tempRegisterIds) > 0 {
  1999. cond += ` AND a.register_id IN ?`
  2000. pars = append(pars, tempRegisterIds)
  2001. } else {
  2002. cond += ` AND 1 = 2`
  2003. }
  2004. }
  2005. if req.ListParam == 1{
  2006. cond += ` AND a.service_product_id = 1 `
  2007. }else if req.ListParam == 2 {
  2008. cond += ` AND a.service_product_id = 2 `
  2009. }
  2010. // 开票到款日期
  2011. //if req.TimeType != 0 {
  2012. // if req.TimeType == 1 && req.StartDate != "" && req.EndDate != "" {
  2013. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  2014. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  2015. // cond += ` AND (c.invoice_time BETWEEN ? AND ?) `
  2016. // pars = append(pars, st, ed)
  2017. // }else if req.TimeType == 2 && req.StartDate != "" && req.EndDate != "" {
  2018. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  2019. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  2020. // cond += ` AND (d.invoice_time BETWEEN ? AND ?) `
  2021. // pars = append(pars, st, ed)
  2022. // }else if req.TimeType == 3 && req.StartDate != "" && req.EndDate != "" {
  2023. // st := fmt.Sprint(req.StartDate, " 00:00:00")
  2024. // ed := fmt.Sprint(req.EndDate, " 23:59:59")
  2025. // cond += ` AND (c.invoice_time BETWEEN ? AND ?) AND (d.invoice_time BETWEEN ? AND ?) `
  2026. // pars = append(pars, st, ed, st, ed)
  2027. // }
  2028. //} else if 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 ?) or (d.invoice_time BETWEEN ? AND ?))`
  2032. // pars = append(pars, st, ed, st, ed)
  2033. //}
  2034. //if req.HasInvoice == "1" {
  2035. // cond += ` AND a.invoice_id > 0 `
  2036. //}else if req.HasInvoice == "0" {
  2037. // cond += ` AND a.invoice_id = 0 `
  2038. //}
  2039. //
  2040. //if req.HasPayment == "1" {
  2041. // cond += ` AND a.payment_id > 0 `
  2042. //}else if req.HasPayment == "0" {
  2043. // cond += ` AND a.payment_id = 0 `
  2044. //}
  2045. page := new(base.Page)
  2046. page.SetPageSize(req.PageSize)
  2047. page.SetCurrent(req.Current)
  2048. //排序
  2049. if req.SortType == "" {
  2050. req.SortType = "desc"
  2051. }
  2052. if req.SortType != "desc" && req.SortType != "asc" {
  2053. resp.Fail("排序类型不正确", c)
  2054. return
  2055. }
  2056. if req.SortType == "asc" {
  2057. page.AddOrderItem(base.OrderItem{Column: "e.invoice_time", Asc: true})
  2058. }else{
  2059. page.AddOrderItem(base.OrderItem{Column: "e.invoice_time", Asc: false})
  2060. }
  2061. if req.IsExport == 1 {
  2062. page.SetPageSize(10000)
  2063. page.SetCurrent(1)
  2064. }
  2065. registerList, total, e := fms.GetNoPaymentCensusPageList(page, cond, pars)
  2066. if e != nil {
  2067. resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
  2068. return
  2069. }
  2070. queryRegisterIds := make([]int, 0)
  2071. for i := range registerList {
  2072. queryRegisterIds = append(queryRegisterIds, registerList[i].ContractRegisterId)
  2073. }
  2074. //fmt.Println("queryRegisterIds:",queryRegisterIds)
  2075. results := new(fms.NotInvoicePaymentCensusResp)
  2076. if len(queryRegisterIds) > 0 {
  2077. // 获取汇总数据IDs, 用于查询合计数据
  2078. summaryIdsCond := cond
  2079. summaryIdsPars := pars
  2080. summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
  2081. if e != nil {
  2082. resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
  2083. return
  2084. }
  2085. //fmt.Println("summaryIds:",summaryIds)
  2086. var listErr, totalErr, totalGroupErr error
  2087. wg := sync.WaitGroup{}
  2088. // 响应列表
  2089. respList := make([]*fms.InvoicePaymentCensusItem, 0)
  2090. summaryList := make([]*fms.InvoicePaymentSummaryItem, 0)
  2091. wg.Add(1)
  2092. go func() {
  2093. defer wg.Done()
  2094. // 获取汇总数据
  2095. summaryCond := cond
  2096. summaryCond += ` AND a.register_id IN ?`
  2097. summaryPars := pars
  2098. summaryPars = append(summaryPars, queryRegisterIds)
  2099. summaryData, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
  2100. if e != nil {
  2101. resp.FailMsg("获取失败", "获取商品到款汇总列表失败, Err: "+e.Error(), c)
  2102. return
  2103. }
  2104. summaryList = summaryData
  2105. //summaryIds := make([]int, 0)
  2106. paymentIds := make([]int, 0)
  2107. for i := range summaryList {
  2108. //summaryIds = append(summaryIds, summaryList[i].SummaryId)
  2109. if summaryList[i].PaymentId > 0 {
  2110. paymentIds = append(paymentIds, summaryList[i].PaymentId)
  2111. }
  2112. }
  2113. // 合同套餐
  2114. /*contractServiceCond := `contract_register_id IN ?`
  2115. contractServicePars := make([]interface{}, 0)
  2116. contractServicePars = append(contractServicePars, queryRegisterIds)
  2117. contractServiceOB := new(fms.ContractService)
  2118. contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
  2119. if e != nil {
  2120. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  2121. return
  2122. }
  2123. contractServiceMap := make(map[int][]*fms.ContractService, 0)
  2124. servicesNameMap := make(map[int][]string, 0)
  2125. for i := range contractServiceList {
  2126. if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
  2127. contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
  2128. }
  2129. contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
  2130. servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
  2131. }
  2132. */
  2133. servicesNameMap, serviceFormatMap, e := fmsService.GetContractServiceNameFormat(queryRegisterIds)
  2134. if e != nil {
  2135. listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
  2136. return
  2137. }
  2138. // 到款套餐分配
  2139. serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
  2140. if len(paymentIds) > 0 {
  2141. serviceAmountCond := `contract_payment_id IN ?`
  2142. serviceAmountPars := make([]interface{}, 0)
  2143. serviceAmountPars = append(serviceAmountPars, paymentIds)
  2144. serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  2145. serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  2146. if e != nil {
  2147. listErr = fmt.Errorf("获取到款套餐分配列表失败, Err: %s", e.Error())
  2148. return
  2149. }
  2150. for i := range serviceAmountList {
  2151. if serviceAmountMap[serviceAmountList[i].ContractPaymentId] == nil {
  2152. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = make([]*fms.ContractPaymentServiceAmount, 0)
  2153. }
  2154. serviceAmountMap[serviceAmountList[i].ContractPaymentId] = append(serviceAmountMap[serviceAmountList[i].ContractPaymentId], serviceAmountList[i])
  2155. }
  2156. }
  2157. // 重组汇总数据
  2158. summaryMap := make(map[int][]*fms.InvoicePaymentCensusInfo)
  2159. amountMap := make(map[string]*fms.ContractPaymentServiceAmount)
  2160. for i := range summaryList {
  2161. v := new(fms.InvoicePaymentCensusInfo)
  2162. v.InvoiceId = summaryList[i].InvoiceId
  2163. v.InvoiceDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].InvoiceDate)
  2164. v.InvoiceAmount = summaryList[i].InvoiceAmount
  2165. v.SellerId = summaryList[i].SellerId
  2166. v.SellerName = summaryList[i].SellerName
  2167. v.SellerGroupId = summaryList[i].SellerGroupId
  2168. v.SellerGroupName = summaryList[i].SellerGroupName
  2169. v.SellerType = summaryList[i].ServiceProductId
  2170. v.PaymentId = summaryList[i].PaymentId
  2171. v.PaymentDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].PaymentDate)
  2172. v.PaymentAmount = summaryList[i].PaymentAmount
  2173. v.PayType = summaryList[i].PayType
  2174. // 套餐到款分配
  2175. svaList := make([]*fms.ContractPaymentServiceAmountItem, 0)
  2176. amountList := serviceAmountMap[summaryList[i].PaymentId]
  2177. if amountList != nil {
  2178. for i := range amountList {
  2179. k := fmt.Sprintf("%d-%d", amountList[i].ContractPaymentId, amountList[i].ServiceTemplateId)
  2180. amountMap[k] = amountList[i]
  2181. }
  2182. }
  2183. // 合同对应的所有套餐
  2184. svList := serviceFormatMap[summaryList[i].RegisterId]
  2185. if svList != nil {
  2186. for ii := range svList {
  2187. vv := new(fms.ContractPaymentServiceAmountItem)
  2188. vv.ServiceTemplateId = svList[ii].ServiceTemplateId
  2189. vv.ServiceTemplateName = svList[ii].FormatTitle
  2190. vv.ServiceTemplatePid = svList[ii].ServiceTemplatePid
  2191. k2 := fmt.Sprintf("%d-%d", summaryList[i].PaymentId, svList[ii].ServiceTemplateId)
  2192. a := amountMap[k2]
  2193. if a != nil {
  2194. vv.ContractPaymentServiceAmountId = a.ContractPaymentServiceAmountId
  2195. vv.ContractPaymentId = a.ContractPaymentId
  2196. vv.Amount = a.Amount
  2197. }
  2198. svaList = append(svaList, vv)
  2199. }
  2200. }
  2201. v.ServiceAmountList = svaList
  2202. summaryMap[summaryList[i].SummaryId] = append(summaryMap[summaryList[i].SummaryId], v)
  2203. }
  2204. // 货币列表
  2205. currencyOB := new(fms.CurrencyUnit)
  2206. currencyCond := `enable = 1`
  2207. currencyPars := make([]interface{}, 0)
  2208. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  2209. if e != nil {
  2210. resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
  2211. return
  2212. }
  2213. unitMap := make(map[string]string)
  2214. for i := range currencyList {
  2215. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  2216. }
  2217. // 响应列表
  2218. for i := range registerList {
  2219. v := new(fms.InvoicePaymentCensusItem)
  2220. v.SummaryId = registerList[i].SummaryId
  2221. v.ContractRegisterId = registerList[i].ContractRegisterId
  2222. v.CompanyName = registerList[i].CompanyName
  2223. v.NewCompany = registerList[i].NewCompany
  2224. v.StartDate = utils.TimeTransferString(utils.FormatDate, registerList[i].StartDate)
  2225. v.EndDate = utils.TimeTransferString(utils.FormatDate, registerList[i].EndDate)
  2226. v.ServicesName = servicesNameMap[registerList[i].ContractRegisterId]
  2227. v.InvoicePaymentList = summaryMap[registerList[i].SummaryId]
  2228. v.ContractType = registerList[i].ContractType
  2229. v.ContractCode = registerList[i].ContractCode
  2230. v.ContractAmount = registerList[i].ContractAmount
  2231. v.UnitName = unitMap[registerList[i].CurrencyUnit]
  2232. v.ProductIds = registerList[i].ProductIds
  2233. respList = append(respList, v)
  2234. }
  2235. }()
  2236. // 开票到款金额合计(换算后)
  2237. // 开票到款金额合计(换算后)
  2238. var invoiceTotal, paymentTotal, contractAmount float64
  2239. wg.Add(1)
  2240. go func() {
  2241. defer wg.Done()
  2242. if len(summaryIds) == 0 {
  2243. return
  2244. }
  2245. amountTotalCond := `a.id IN ?`
  2246. amountTotalPars := make([]interface{}, 0)
  2247. amountTotalPars = append(amountTotalPars, summaryIds)
  2248. invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
  2249. if e != nil {
  2250. totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
  2251. return
  2252. }
  2253. invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
  2254. paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
  2255. if e != nil {
  2256. totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
  2257. return
  2258. }
  2259. paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
  2260. }()
  2261. // 分币种金额统计
  2262. invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  2263. paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  2264. contractAmountCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
  2265. wg.Add(1)
  2266. go func() {
  2267. defer wg.Done()
  2268. currencyOB := new(fms.CurrencyUnit)
  2269. currencyCond := `enable = 1`
  2270. currencyPars := make([]interface{}, 0)
  2271. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  2272. if e != nil {
  2273. totalGroupErr = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
  2274. return
  2275. }
  2276. unitMap := make(map[string]string)
  2277. for i := range currencyList {
  2278. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  2279. invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  2280. Name: currencyList[i].Name,
  2281. UnitName: currencyList[i].UnitName,
  2282. Code: currencyList[i].Code,
  2283. FlagImg: currencyList[i].FlagImg,
  2284. })
  2285. paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  2286. Name: currencyList[i].Name,
  2287. UnitName: currencyList[i].UnitName,
  2288. Code: currencyList[i].Code,
  2289. FlagImg: currencyList[i].FlagImg,
  2290. })
  2291. contractAmountCurrencyTotals = append(contractAmountCurrencyTotals, &fms.InvoiceListCurrencyTotal{
  2292. Name: currencyList[i].Name,
  2293. UnitName: currencyList[i].UnitName,
  2294. Code: currencyList[i].Code,
  2295. FlagImg: currencyList[i].FlagImg,
  2296. })
  2297. }
  2298. if len(summaryIds) == 0 {
  2299. return
  2300. }
  2301. totalGroupCond := `a.id IN ?`
  2302. totalGroupPars := make([]interface{}, 0)
  2303. totalGroupPars = append(totalGroupPars, summaryIds)
  2304. invoiceSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 1)
  2305. if e != nil {
  2306. totalGroupErr = fmt.Errorf("获取汇总货币合计开票金额失败, Err: %s", e.Error())
  2307. return
  2308. }
  2309. paymentSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 2)
  2310. if e != nil {
  2311. totalGroupErr = fmt.Errorf("获取汇总货币合计到款金额失败, Err: %s", e.Error())
  2312. return
  2313. }
  2314. invoiceSumMap := make(map[string]float64)
  2315. paymentSumMap := make(map[string]float64)
  2316. contractSumMap := make(map[string]float64)
  2317. for i := range invoiceSumGroup {
  2318. invoiceSumMap[invoiceSumGroup[i].CurrencyUnit] = invoiceSumGroup[i].OriginAmountTotal
  2319. continue
  2320. }
  2321. for i := range paymentSumGroup {
  2322. paymentSumMap[paymentSumGroup[i].CurrencyUnit] = paymentSumGroup[i].OriginAmountTotal
  2323. continue
  2324. }
  2325. for i := range invoiceCurrencyTotals {
  2326. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
  2327. invoiceCurrencyTotals[i].Amount = a
  2328. }
  2329. for i := range paymentCurrencyTotals {
  2330. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
  2331. paymentCurrencyTotals[i].Amount = a
  2332. }
  2333. contractRegisters, err := fms.GetContractInfoByRegisterIds(queryRegisterIds)
  2334. if err != nil {
  2335. resp.FailMsg("查询合同信息失败", "查询合同信息失败, Err: "+err.Error(), c)
  2336. return
  2337. }
  2338. for _, v := range contractRegisters {
  2339. if v.RMBRate == 0.0 {
  2340. v.RMBRate = 1
  2341. }
  2342. amount, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.ContractAmount/v.RMBRate), 64)
  2343. contractAmount += amount
  2344. contractSumMap[v.CurrencyUnit] = v.ContractAmount
  2345. }
  2346. for _, v := range contractAmountCurrencyTotals {
  2347. v.Amount = contractSumMap[v.Code] - invoiceSumMap[v.Code]
  2348. }
  2349. }()
  2350. wg.Wait()
  2351. if listErr != nil {
  2352. resp.FailMsg("获取失败", listErr.Error(), c)
  2353. return
  2354. }
  2355. if totalErr != nil {
  2356. resp.FailMsg("获取失败", totalErr.Error(), c)
  2357. return
  2358. }
  2359. if totalGroupErr != nil {
  2360. resp.FailMsg("获取失败", totalGroupErr.Error(), c)
  2361. return
  2362. }
  2363. results.DataList = respList
  2364. results.InvoiceTotal = invoiceTotal
  2365. results.PaymentTotal = paymentTotal
  2366. results.NotInvoiceTotal = contractAmount - invoiceTotal
  2367. results.InvoiceCurrencyTotal = invoiceCurrencyTotals
  2368. results.PaymentCurrencyTotal = paymentCurrencyTotals
  2369. results.NotInvoiceCurrencyTotal = contractAmountCurrencyTotals
  2370. }
  2371. // 是否导出
  2372. if req.IsExport == 1 {
  2373. ExportNotPaymentCensusList(c, results)
  2374. return
  2375. }
  2376. page.SetTotal(total)
  2377. baseData := new(base.BaseData)
  2378. baseData.SetPage(page)
  2379. baseData.SetList(results)
  2380. resp.OkData("获取成功", baseData, c)
  2381. }
  2382. // ExportNotInvoiceCensusList 导出未开票统计列表
  2383. func ExportNotInvoiceCensusList(c *gin.Context, results *fms.NotInvoicePaymentCensusResp) {
  2384. list := results.DataList
  2385. if len(list) == 0 {
  2386. resp.Fail("列表数据为空", c)
  2387. return
  2388. }
  2389. // 生成Excel文件
  2390. xlsxFile := xlsx.NewFile()
  2391. style := xlsx.NewStyle()
  2392. alignment := xlsx.Alignment{
  2393. Horizontal: "center",
  2394. Vertical: "center",
  2395. WrapText: true,
  2396. }
  2397. style.Alignment = alignment
  2398. style.ApplyAlignment = true
  2399. sheet, err := xlsxFile.AddSheet("未开票统计")
  2400. if err != nil {
  2401. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  2402. return
  2403. }
  2404. _ = sheet.SetColWidth(1, 1, 30)
  2405. _ = sheet.SetColWidth(3, 3, 30)
  2406. // 前三行-开票金额合计
  2407. rowA := sheet.AddRow()
  2408. cellAA := rowA.AddCell()
  2409. cellAA.SetString(fmt.Sprintf("未开票合计金额(换算后):%.2f(元)", results.NotInvoiceTotal))
  2410. rowBData := "未开票金额:"
  2411. for _, v := range results.NotInvoiceCurrencyTotal {
  2412. rowBData += fmt.Sprintf("%s%.2f(%s) ", v.Name, v.Amount, v.UnitName)
  2413. }
  2414. rowB := sheet.AddRow()
  2415. rowB.AddCell().SetString(rowBData)
  2416. sheet.AddRow()
  2417. // 表头, 套餐动态获取
  2418. rowTitle := []string{"序号", "客户名称", "是否新客户", "合同有效期", "开票日", "开票金额", "到款日", "到款金额", "付款方式", "销售",
  2419. "销售组别", "销售类型"}
  2420. serviceTempCond := ``
  2421. serviceTempPars := make([]interface{}, 0)
  2422. serviceTempOB := new(fms.ContractServiceTemplate)
  2423. serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
  2424. if e != nil {
  2425. resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
  2426. return
  2427. }
  2428. serviceTempListMap := make(map[int]*fms.ContractServiceTemplate)
  2429. var serviceTempShow [] *fms.ContractServiceTemplate
  2430. for i := range serviceTempList {
  2431. serviceTempListMap[serviceTempList[i].ServiceTemplateId] = serviceTempList[i]
  2432. }
  2433. for i := range serviceTempList {
  2434. if serviceTempList[i].ProductId == 1 {
  2435. rowTitle = append(rowTitle, serviceTempList[i].Title)
  2436. serviceTempShow = append(serviceTempShow, serviceTempList[i])
  2437. }else if serviceTempList[i].ProductId == 2{
  2438. if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "权益大套餐" || (serviceTempList[i].Title == "策略" && serviceTempListMap[serviceTempList[i].Pid].Pid==0){
  2439. }else if serviceTempList[i].Title == "医药" || serviceTempList[i].Title == "消费" || serviceTempList[i].Title == "科技" || serviceTempList[i].Title == "智造" {
  2440. }else {
  2441. serviceTempShow = append(serviceTempShow, serviceTempList[i])
  2442. }
  2443. if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "45万" || serviceTempList[i].Title == "70万" {
  2444. continue
  2445. }
  2446. if serviceTempList[i].Pid > 0 && serviceTempListMap[serviceTempList[i].Pid].Pid > 0 {
  2447. continue
  2448. }
  2449. rowTitle = append(rowTitle, serviceTempList[i].Title)
  2450. }
  2451. }
  2452. hi := 0
  2453. insertHi := 0
  2454. titleRow := sheet.AddRow()
  2455. for i := range rowTitle {
  2456. v := titleRow.AddCell()
  2457. v.SetString(rowTitle[i])
  2458. v.SetStyle(style)
  2459. if rowTitle[i] != "权益大套餐" && rowTitle[i] != "医药" && rowTitle[i] != "消费" && rowTitle[i] != "科技" && rowTitle[i] != "智造" {
  2460. v.VMerge = 1
  2461. hi ++
  2462. }else{
  2463. if insertHi == 0 {
  2464. insertHi = hi
  2465. }
  2466. v.HMerge = 1
  2467. titleRow.AddCell().SetString("")
  2468. }
  2469. }
  2470. // 新增一行放主观和客观
  2471. specialRow := sheet.AddRow()
  2472. for i:=0; i< hi; i++ {
  2473. if i == insertHi {
  2474. v1 := specialRow.AddCell()
  2475. v1.SetString("45万")
  2476. v1.SetStyle(style)
  2477. v1 = specialRow.AddCell()
  2478. v1.SetString("70万")
  2479. v1.SetStyle(style)
  2480. for j:=0; j<8;j++ {
  2481. if j%2 ==0 {
  2482. v1 = specialRow.AddCell()
  2483. v1.SetString("主观")
  2484. v1.SetStyle(style)
  2485. }else{
  2486. v1 = specialRow.AddCell()
  2487. v1.SetString("客观")
  2488. v1.SetStyle(style)
  2489. }
  2490. }
  2491. }else{
  2492. v := specialRow.AddCell()
  2493. v.SetString("")
  2494. }
  2495. }
  2496. newCompanyMap := map[int]string{0: "否", 1: "是"}
  2497. sellerTypeMap := map[int]string{1:"FICC销售", 2:"权益销售"}
  2498. for k, v := range list {
  2499. dataRow := sheet.AddRow()
  2500. // 前四个单元格根据每行开票到款条数向下合并
  2501. l := len(v.InvoicePaymentList)
  2502. mergeRowNum := l - 1
  2503. // 序号
  2504. sortNum := k + 1
  2505. colA := dataRow.AddCell()
  2506. colA.VMerge = mergeRowNum
  2507. colA.SetString(fmt.Sprint(sortNum))
  2508. // 客户名称
  2509. colB := dataRow.AddCell()
  2510. colB.VMerge = mergeRowNum
  2511. colB.SetString(v.CompanyName)
  2512. // 是否新客户
  2513. colC := dataRow.AddCell()
  2514. colC.VMerge = mergeRowNum
  2515. colC.SetString(newCompanyMap[v.NewCompany])
  2516. // 合同有效期
  2517. colD := dataRow.AddCell()
  2518. colD.VMerge = mergeRowNum
  2519. colD.SetString(fmt.Sprint(v.StartDate, "至", v.EndDate))
  2520. // 开票到款信息
  2521. for k2, v2 := range v.InvoicePaymentList {
  2522. rowData := []string{
  2523. v2.InvoiceDate, // 开票日
  2524. fmt.Sprint(v2.InvoiceAmount), // 开票金额
  2525. v2.PaymentDate, // 到款日
  2526. fmt.Sprint(v2.PaymentAmount), // 到款金额
  2527. fms.ContractPaymentPayTypeNameMap[v2.PayType], // 付款方式
  2528. v2.SellerName, // 销售
  2529. v2.SellerGroupName, // 组别
  2530. sellerTypeMap[v2.SellerType], // 销售类型
  2531. }
  2532. // 套餐金额信息
  2533. serviceTempShowAmount := make(map[int]string)
  2534. for i := range serviceTempShow {
  2535. for s2 := range v2.ServiceAmountList {
  2536. item := v2.ServiceAmountList[s2]
  2537. if item.ServiceTemplateId == serviceTempShow[i].ServiceTemplateId {
  2538. serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount)
  2539. break
  2540. }else if serviceTempShow[i].Pid == item.ServiceTemplateId {
  2541. serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount/2)
  2542. break
  2543. }
  2544. }
  2545. }
  2546. for i := range serviceTempShow {
  2547. sa := ""
  2548. if am, ok := serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId]; ok {
  2549. sa = am
  2550. }
  2551. rowData = append(rowData, sa)
  2552. }
  2553. // 首行开票到款
  2554. if k2 == 0 {
  2555. for i := range rowData {
  2556. dataRow.AddCell().SetString(rowData[i])
  2557. }
  2558. continue
  2559. }
  2560. // 其他行开票到款, 加四列空的单元格用于合并
  2561. dataRowExtra := sheet.AddRow()
  2562. for i := 0; i < 4; i++ {
  2563. dataRowExtra.AddCell()
  2564. }
  2565. for i := range rowData {
  2566. dataRowExtra.AddCell().SetString(rowData[i])
  2567. }
  2568. }
  2569. }
  2570. // 输出文件
  2571. var buffer bytes.Buffer
  2572. _ = xlsxFile.Write(&buffer)
  2573. content := bytes.NewReader(buffer.Bytes())
  2574. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  2575. fileName := "商品到款统计_" + randStr + ".xlsx"
  2576. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  2577. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  2578. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  2579. }
  2580. // ExportNotPaymentCensusList 导出开票未到款统计列表
  2581. func ExportNotPaymentCensusList(c *gin.Context, results *fms.NotInvoicePaymentCensusResp) {
  2582. list := results.DataList
  2583. if len(list) == 0 {
  2584. resp.Fail("列表数据为空", c)
  2585. return
  2586. }
  2587. // 生成Excel文件
  2588. xlsxFile := xlsx.NewFile()
  2589. style := xlsx.NewStyle()
  2590. alignment := xlsx.Alignment{
  2591. Horizontal: "center",
  2592. Vertical: "center",
  2593. WrapText: true,
  2594. }
  2595. style.Alignment = alignment
  2596. style.ApplyAlignment = true
  2597. sheet, err := xlsxFile.AddSheet("开票未到款统计")
  2598. if err != nil {
  2599. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  2600. return
  2601. }
  2602. _ = sheet.SetColWidth(1, 1, 30)
  2603. _ = sheet.SetColWidth(3, 3, 30)
  2604. // 前三行-开票金额合计
  2605. rowA := sheet.AddRow()
  2606. cellAA := rowA.AddCell()
  2607. cellAA.SetString(fmt.Sprintf("开票未到款合计金额(换算后):%.2f(元)", results.InvoiceTotal))
  2608. rowBData := "已开票金额:"
  2609. for _, v := range results.InvoiceCurrencyTotal {
  2610. rowBData += fmt.Sprintf("%s%.2f(%s) ", v.Name, v.Amount, v.UnitName)
  2611. }
  2612. rowB := sheet.AddRow()
  2613. rowB.AddCell().SetString(rowBData)
  2614. sheet.AddRow()
  2615. // 表头, 套餐动态获取
  2616. rowTitle := []string{"序号", "客户名称", "是否新客户", "合同有效期", "开票日", "开票金额", "到款日", "到款金额", "付款方式", "销售",
  2617. "销售组别", "销售类型"}
  2618. serviceTempCond := ``
  2619. serviceTempPars := make([]interface{}, 0)
  2620. serviceTempOB := new(fms.ContractServiceTemplate)
  2621. serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
  2622. if e != nil {
  2623. resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
  2624. return
  2625. }
  2626. serviceTempListMap := make(map[int]*fms.ContractServiceTemplate)
  2627. var serviceTempShow [] *fms.ContractServiceTemplate
  2628. for i := range serviceTempList {
  2629. serviceTempListMap[serviceTempList[i].ServiceTemplateId] = serviceTempList[i]
  2630. }
  2631. for i := range serviceTempList {
  2632. if serviceTempList[i].ProductId == 1 {
  2633. rowTitle = append(rowTitle, serviceTempList[i].Title)
  2634. serviceTempShow = append(serviceTempShow, serviceTempList[i])
  2635. }else if serviceTempList[i].ProductId == 2{
  2636. if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "权益大套餐" || (serviceTempList[i].Title == "策略" && serviceTempListMap[serviceTempList[i].Pid].Pid==0){
  2637. }else if serviceTempList[i].Title == "医药" || serviceTempList[i].Title == "消费" || serviceTempList[i].Title == "科技" || serviceTempList[i].Title == "智造" {
  2638. }else {
  2639. serviceTempShow = append(serviceTempShow, serviceTempList[i])
  2640. }
  2641. if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "45万" || serviceTempList[i].Title == "70万" {
  2642. continue
  2643. }
  2644. if serviceTempList[i].Pid > 0 && serviceTempListMap[serviceTempList[i].Pid].Pid > 0 {
  2645. continue
  2646. }
  2647. rowTitle = append(rowTitle, serviceTempList[i].Title)
  2648. }
  2649. }
  2650. hi := 0
  2651. insertHi := 0
  2652. titleRow := sheet.AddRow()
  2653. for i := range rowTitle {
  2654. v := titleRow.AddCell()
  2655. v.SetString(rowTitle[i])
  2656. v.SetStyle(style)
  2657. if rowTitle[i] != "权益大套餐" && rowTitle[i] != "医药" && rowTitle[i] != "消费" && rowTitle[i] != "科技" && rowTitle[i] != "智造" {
  2658. v.VMerge = 1
  2659. hi ++
  2660. }else{
  2661. if insertHi == 0 {
  2662. insertHi = hi
  2663. }
  2664. v.HMerge = 1
  2665. titleRow.AddCell().SetString("")
  2666. }
  2667. }
  2668. // 新增一行放主观和客观
  2669. specialRow := sheet.AddRow()
  2670. for i:=0; i< hi; i++ {
  2671. if i == insertHi {
  2672. v1 := specialRow.AddCell()
  2673. v1.SetString("45万")
  2674. v1.SetStyle(style)
  2675. v1 = specialRow.AddCell()
  2676. v1.SetString("70万")
  2677. v1.SetStyle(style)
  2678. for j:=0; j<8;j++ {
  2679. if j%2 ==0 {
  2680. v1 = specialRow.AddCell()
  2681. v1.SetString("主观")
  2682. v1.SetStyle(style)
  2683. }else{
  2684. v1 = specialRow.AddCell()
  2685. v1.SetString("客观")
  2686. v1.SetStyle(style)
  2687. }
  2688. }
  2689. }else{
  2690. v := specialRow.AddCell()
  2691. v.SetString("")
  2692. }
  2693. }
  2694. newCompanyMap := map[int]string{0: "否", 1: "是"}
  2695. sellerTypeMap := map[int]string{1:"FICC销售", 2:"权益销售"}
  2696. for k, v := range list {
  2697. dataRow := sheet.AddRow()
  2698. // 前四个单元格根据每行开票到款条数向下合并
  2699. l := len(v.InvoicePaymentList)
  2700. mergeRowNum := l - 1
  2701. // 序号
  2702. sortNum := k + 1
  2703. colA := dataRow.AddCell()
  2704. colA.VMerge = mergeRowNum
  2705. colA.SetString(fmt.Sprint(sortNum))
  2706. // 客户名称
  2707. colB := dataRow.AddCell()
  2708. colB.VMerge = mergeRowNum
  2709. colB.SetString(v.CompanyName)
  2710. // 是否新客户
  2711. colC := dataRow.AddCell()
  2712. colC.VMerge = mergeRowNum
  2713. colC.SetString(newCompanyMap[v.NewCompany])
  2714. // 合同有效期
  2715. colD := dataRow.AddCell()
  2716. colD.VMerge = mergeRowNum
  2717. colD.SetString(fmt.Sprint(v.StartDate, "至", v.EndDate))
  2718. // 开票到款信息
  2719. for k2, v2 := range v.InvoicePaymentList {
  2720. rowData := []string{
  2721. v2.InvoiceDate, // 开票日
  2722. fmt.Sprint(v2.InvoiceAmount), // 开票金额
  2723. v2.PaymentDate, // 到款日
  2724. fmt.Sprint(v2.PaymentAmount), // 到款金额
  2725. fms.ContractPaymentPayTypeNameMap[v2.PayType], // 付款方式
  2726. v2.SellerName, // 销售
  2727. v2.SellerGroupName, // 组别
  2728. sellerTypeMap[v2.SellerType], // 销售类型
  2729. }
  2730. // 套餐金额信息
  2731. serviceTempShowAmount := make(map[int]string)
  2732. for i := range serviceTempShow {
  2733. for s2 := range v2.ServiceAmountList {
  2734. item := v2.ServiceAmountList[s2]
  2735. if item.ServiceTemplateId == serviceTempShow[i].ServiceTemplateId {
  2736. serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount)
  2737. break
  2738. }else if serviceTempShow[i].Pid == item.ServiceTemplateId {
  2739. serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount/2)
  2740. break
  2741. }
  2742. }
  2743. }
  2744. for i := range serviceTempShow {
  2745. sa := ""
  2746. if am, ok := serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId]; ok {
  2747. sa = am
  2748. }
  2749. rowData = append(rowData, sa)
  2750. }
  2751. // 首行开票到款
  2752. if k2 == 0 {
  2753. for i := range rowData {
  2754. dataRow.AddCell().SetString(rowData[i])
  2755. }
  2756. continue
  2757. }
  2758. // 其他行开票到款, 加四列空的单元格用于合并
  2759. dataRowExtra := sheet.AddRow()
  2760. for i := 0; i < 4; i++ {
  2761. dataRowExtra.AddCell()
  2762. }
  2763. for i := range rowData {
  2764. dataRowExtra.AddCell().SetString(rowData[i])
  2765. }
  2766. }
  2767. }
  2768. // 输出文件
  2769. var buffer bytes.Buffer
  2770. _ = xlsxFile.Write(&buffer)
  2771. content := bytes.NewReader(buffer.Bytes())
  2772. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  2773. fileName := "商品到款统计_" + randStr + ".xlsx"
  2774. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  2775. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  2776. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  2777. }