invoice_payment.go 103 KB

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