invoice_payment.go 83 KB

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