invoice_payment.go 105 KB

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