12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309 |
- package census
- import (
- "bytes"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/go-playground/validator/v10"
- "github.com/tealeg/xlsx"
- "hongze/fms_api/controller/resp"
- "hongze/fms_api/global"
- "hongze/fms_api/models/base"
- "hongze/fms_api/models/fms"
- fmsService "hongze/fms_api/services/fms"
- "hongze/fms_api/utils"
- "net/http"
- "strconv"
- "strings"
- "sync"
- "time"
- )
- // InvoicePaymentController 商品到款统计
- type InvoicePaymentController struct{}
- // List2 原版(已废弃)
- //func (ct *InvoicePaymentController) List2(c *gin.Context) {
- // var req fms.InvoicePaymentCensusListReq
- // if e := c.BindQuery(&req); e != nil {
- // err, ok := e.(validator.ValidationErrors)
- // if !ok {
- // resp.FailData("参数解析失败", "Err:"+e.Error(), c)
- // return
- // }
- // resp.FailData("参数解析失败", err.Translate(global.Trans), c)
- // return
- // }
- //
- // cond := `1 = 1`
- // pars := make([]interface{}, 0)
- // // 合同编号/客户姓名/销售
- // if req.Keyword != "" {
- // kw := "%" + req.Keyword + "%"
- // cond += ` AND b.company_name LIKE ? OR a.seller_name LIKE ?`
- // pars = append(pars, kw, kw)
- // }
- // if req.SellGroupId > 0 {
- // cond += ` AND a.seller_group_id = ?`
- // pars = append(pars, req.SellGroupId)
- // }
- // // 套餐筛选
- // if req.ServiceType != 0 {
- // registerIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
- // if e != nil {
- // resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
- // return
- // }
- // if len(registerIds) > 0 {
- // cond += ` AND b.contract_register_id IN ?`
- // pars = append(pars, registerIds)
- // } else {
- // cond += ` AND 1 = 2`
- // }
- // }
- // // 开票到款日期
- // if req.TimeType > 0 && req.StartDate != "" && req.EndDate != "" {
- // st := fmt.Sprint(req.StartDate, " 00:00:00")
- // ed := fmt.Sprint(req.EndDate, " 23:59:59")
- // cond += ` AND a.invoice_type = ? AND (a.invoice_time BETWEEN ? AND ?)`
- // pars = append(pars, req.TimeType, st, ed)
- // }
- // // 已开票
- // if req.HasInvoice == 1 && req.HasPayment == 0 {
- // cond += ` AND a.invoice_type = 1`
- // }
- // // 已到款
- // if req.HasInvoice == 0 && req.HasPayment == 1 {
- // cond += ` AND a.invoice_type = 2`
- // }
- //
- // page := new(base.Page)
- // page.SetPageSize(req.PageSize)
- // page.SetCurrent(req.Current)
- // page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
- // page.AddOrderItem(base.OrderItem{Column: "b.contract_register_id", Asc: true})
- // if req.IsExport == 1 {
- // page.SetPageSize(10000)
- // page.SetCurrent(1)
- // }
- //
- // list, allRegisterIds, e := fms.GetInvoicePaymentCensusPageList(page, cond, pars)
- // if e != nil {
- // resp.FailMsg("获取失败", "获取商品到款统计列表失败, Err: "+e.Error(), c)
- // return
- // }
- // total := int64(len(allRegisterIds))
- // registerIds := make([]int, 0)
- // for i := range list {
- // registerIds = append(registerIds, list[i].ContractRegisterId)
- // }
- //
- // results := new(fms.InvoicePaymentCensusResp)
- // if len(registerIds) > 0 {
- // // 获取开票到款列表
- // ivCond := `contract_register_id IN ?`
- // ivPars := make([]interface{}, 0)
- // ivPars = append(ivPars, registerIds)
- // iv := new(fms.ContractInvoice)
- // invoiceList, e := iv.List(ivCond, ivPars, "")
- // if e != nil {
- // resp.FailMsg("获取失败", "获取开票到款列表失败, Err: "+e.Error(), c)
- // return
- // }
- // // 取出开票、到款
- // invoiceMap := make(map[int][]*fms.ContractInvoice, 0)
- // paymentMap := make(map[int][]*fms.ContractInvoice, 0)
- // paymentIds := make([]int, 0)
- // for i := range invoiceList {
- // if invoiceMap[invoiceList[i].ContractRegisterId] == nil {
- // invoiceMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoice, 0)
- // }
- // if paymentMap[invoiceList[i].ContractRegisterId] == nil {
- // paymentMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoice, 0)
- // }
- // if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
- // invoiceMap[invoiceList[i].ContractRegisterId] = append(invoiceMap[invoiceList[i].ContractRegisterId], invoiceList[i])
- // }
- // if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
- // paymentMap[invoiceList[i].ContractRegisterId] = append(paymentMap[invoiceList[i].ContractRegisterId], invoiceList[i])
- // paymentIds = append(paymentIds, invoiceList[i].ContractInvoiceId)
- // }
- // }
- //
- // // 合同套餐
- // contractServiceCond := `contract_register_id IN ?`
- // contractServicePars := make([]interface{}, 0)
- // contractServicePars = append(contractServicePars, registerIds)
- // contractServiceOB := new(fms.ContractService)
- // contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
- // if e != nil {
- // resp.FailMsg("获取失败", "获取合同套餐列表失败, Err:"+e.Error(), c)
- // return
- // }
- // contractServiceMap := make(map[int][]*fms.ContractService, 0)
- // servicesNameMap := make(map[int][]string, 0)
- // for i := range contractServiceList {
- // if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
- // contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
- // }
- // contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
- // servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
- // }
- //
- // // 到款套餐分配
- // serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
- // if len(paymentIds) > 0 {
- // serviceAmountCond := `contract_payment_id IN ?`
- // serviceAmountPars := make([]interface{}, 0)
- // serviceAmountPars = append(serviceAmountPars, paymentIds)
- // serviceAmountOB := new(fms.ContractPaymentServiceAmount)
- // serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
- // if e != nil {
- // resp.FailMsg("获取失败", "获取到款套餐分配列表失败, Err:"+e.Error(), c)
- // return
- // }
- // for i := range serviceAmountList {
- // if serviceAmountMap[serviceAmountList[i].ContractRegisterId] == nil {
- // serviceAmountMap[serviceAmountList[i].ContractRegisterId] = make([]*fms.ContractPaymentServiceAmount, 0)
- // }
- // serviceAmountMap[serviceAmountList[i].ContractRegisterId] = append(serviceAmountMap[serviceAmountList[i].ContractRegisterId], serviceAmountList[i])
- // }
- // }
- //
- // // 整合响应数据
- // respList := make([]*fms.InvoicePaymentCensusItem, 0)
- // for i := range list {
- // v := new(fms.InvoicePaymentCensusItem)
- // v.ContractRegisterId = list[i].ContractRegisterId
- // v.CompanyName = list[i].CompanyName
- // v.NewCompany = list[i].NewCompany
- // v.StartDate = list[i].StartDate.Format(utils.FormatDate)
- // v.EndDate = list[i].EndDate.Format(utils.FormatDate)
- // svList := servicesNameMap[list[i].ContractRegisterId]
- // v.ServicesName = strings.Join(svList, ",")
- // // 格式化(合并)开票到款数据
- // v.InvoicePaymentList = fmsService.MergeInvoiceList2InvoicePaymentCensusInfo(invoiceMap[list[i].ContractRegisterId], paymentMap[list[i].ContractRegisterId],
- // contractServiceMap[list[i].ContractRegisterId], serviceAmountMap[list[i].ContractRegisterId])
- // respList = append(respList, v)
- // }
- //
- // // 开票到款金额合计(换算后)
- // amountTotalCond := `contract_register_id IN ?`
- // amountTotalPars := make([]interface{}, 0)
- // amountTotalPars = append(amountTotalPars, allRegisterIds)
- // amountTotalList, e := fms.GetContractInvoiceAmountTotal(amountTotalCond, amountTotalPars)
- // if e != nil {
- // resp.FailMsg("获取失败", "获取开票到款金额合计失败, Err:"+e.Error(), c)
- // return
- // }
- // amountTotalMap := make(map[int]float64)
- // for i := range amountTotalList {
- // amountTotalMap[amountTotalList[i].InvoiceType] = amountTotalList[i].TotalAmount
- // }
- // invoiceTotal, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", amountTotalMap[fms.ContractInvoiceTypeMake]), 64)
- // paymentTotal, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", amountTotalMap[fms.ContractInvoiceTypePay]), 64)
- //
- // // 分币种金额统计
- // currencyOB := new(fms.CurrencyUnit)
- // currencyCond := `enable = 1`
- // currencyPars := make([]interface{}, 0)
- // currencyList, e := currencyOB.List(currencyCond, currencyPars)
- // if e != nil {
- // resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
- // return
- // }
- // unitMap := make(map[string]string)
- // invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
- // paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
- // for i := range currencyList {
- // unitMap[currencyList[i].Code] = currencyList[i].UnitName
- // invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
- // Name: currencyList[i].Name,
- // UnitName: currencyList[i].UnitName,
- // Code: currencyList[i].Code,
- // FlagImg: currencyList[i].FlagImg,
- // })
- // paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
- // Name: currencyList[i].Name,
- // UnitName: currencyList[i].UnitName,
- // Code: currencyList[i].Code,
- // FlagImg: currencyList[i].FlagImg,
- // })
- // }
- // sumList, e := fms.GetInvoiceListCurrencySum(amountTotalCond, amountTotalPars, "currency_unit, invoice_type")
- // if e != nil {
- // resp.FailMsg("获取失败", "获取商品到款统计货币合计金额失败, Err: "+e.Error(), c)
- // return
- // }
- // invoiceSumMap := make(map[string]float64)
- // paymentSumMap := make(map[string]float64)
- // for i := range sumList {
- // if sumList[i].InvoiceType == fms.ContractInvoiceTypeMake {
- // invoiceSumMap[sumList[i].CurrencyUnit] = sumList[i].OriginAmountTotal
- // continue
- // }
- // if sumList[i].InvoiceType == fms.ContractInvoiceTypePay {
- // paymentSumMap[sumList[i].CurrencyUnit] = sumList[i].OriginAmountTotal
- // }
- // }
- // for i := range invoiceCurrencyTotals {
- // a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
- // invoiceCurrencyTotals[i].Amount = a
- // }
- // for i := range paymentCurrencyTotals {
- // a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
- // paymentCurrencyTotals[i].Amount = a
- // }
- //
- // results.DataList = respList
- // results.InvoiceTotal = invoiceTotal
- // results.PaymentTotal = paymentTotal
- // results.InvoiceCurrencyTotal = invoiceCurrencyTotals
- // results.PaymentCurrencyTotal = paymentCurrencyTotals
- // }
- //
- // // 是否导出
- // if req.IsExport == 1 {
- // ExportInvoicePaymentCensusList(c, results)
- // return
- // }
- // page.SetTotal(total)
- // baseData := new(base.BaseData)
- // baseData.SetPage(page)
- // baseData.SetList(results)
- // resp.OkData("获取成功", baseData, c)
- //}
- // ExportInvoicePaymentCensusList 导出商品到款统计列表
- func ExportInvoicePaymentCensusList(c *gin.Context, results *fms.InvoicePaymentCensusResp) {
- list := results.DataList
- if len(list) == 0 {
- resp.Fail("列表数据为空", c)
- return
- }
- // 生成Excel文件
- xlsxFile := xlsx.NewFile()
- style := xlsx.NewStyle()
- alignment := xlsx.Alignment{
- Horizontal: "center",
- Vertical: "center",
- WrapText: true,
- }
- style.Alignment = alignment
- style.ApplyAlignment = true
- sheet, err := xlsxFile.AddSheet("商品到款统计")
- if err != nil {
- resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
- return
- }
- _ = sheet.SetColWidth(1, 1, 30)
- _ = sheet.SetColWidth(3, 3, 30)
- // 前三行-开票金额合计
- rowA := sheet.AddRow()
- cellAA := rowA.AddCell()
- cellAA.SetString(fmt.Sprintf("已开票合计金额(换算后):%.2f(元)", results.InvoiceTotal))
- rowBData := "已开票金额:"
- for _, v := range results.InvoiceCurrencyTotal {
- rowBData += fmt.Sprintf("%s%.2f(%s) ", v.Name, v.Amount, v.UnitName)
- }
- rowB := sheet.AddRow()
- rowB.AddCell().SetString(rowBData)
- sheet.AddRow()
- // 四至六-到款金额合计
- rowD := sheet.AddRow()
- cellDA := rowD.AddCell()
- cellDA.SetString(fmt.Sprintf("已到款合计金额(换算后):%.2f(元)", results.PaymentTotal))
- rowEData := "已到款金额:"
- for _, v := range results.PaymentCurrencyTotal {
- rowEData += fmt.Sprintf("%s%.2f(%s) ", v.Name, v.Amount, v.UnitName)
- }
- rowE := sheet.AddRow()
- rowE.AddCell().SetString(rowEData)
- sheet.AddRow()
- // 表头, 套餐动态获取
- rowTitle := []string{"序号", "客户名称", "是否新客户", "合同有效期", "开票日", "开票金额", "到款日", "到款金额", "付款方式", "销售",
- "销售组别", "销售类型"}
- serviceTempCond := ``
- serviceTempPars := make([]interface{}, 0)
- serviceTempOB := new(fms.ContractServiceTemplate)
- serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
- if e != nil {
- resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
- return
- }
- serviceTempListMap := make(map[int]*fms.ContractServiceTemplate)
- var serviceTempShow [] *fms.ContractServiceTemplate
- for i := range serviceTempList {
- serviceTempListMap[serviceTempList[i].ServiceTemplateId] = serviceTempList[i]
- }
- for i := range serviceTempList {
- if serviceTempList[i].ProductId == 1 {
- rowTitle = append(rowTitle, serviceTempList[i].Title)
- serviceTempShow = append(serviceTempShow, serviceTempList[i])
- }else if serviceTempList[i].ProductId == 2{
- if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "权益大套餐" || (serviceTempList[i].Title == "策略" && serviceTempListMap[serviceTempList[i].Pid].Pid==0){
- }else if serviceTempList[i].Title == "医药" || serviceTempList[i].Title == "消费" || serviceTempList[i].Title == "科技" || serviceTempList[i].Title == "智造" {
- }else {
- serviceTempShow = append(serviceTempShow, serviceTempList[i])
- }
- if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "45万" || serviceTempList[i].Title == "70万" {
- continue
- }
- if serviceTempList[i].Pid > 0 && serviceTempListMap[serviceTempList[i].Pid].Pid > 0 {
- continue
- }
- rowTitle = append(rowTitle, serviceTempList[i].Title)
- }
- }
- hi := 0
- insertHi := 0
- titleRow := sheet.AddRow()
- for i := range rowTitle {
- v := titleRow.AddCell()
- v.SetString(rowTitle[i])
- v.SetStyle(style)
- if rowTitle[i] != "权益大套餐" && rowTitle[i] != "医药" && rowTitle[i] != "消费" && rowTitle[i] != "科技" && rowTitle[i] != "智造" {
- v.VMerge = 1
- hi ++
- }else{
- if insertHi == 0 {
- insertHi = hi
- }
- v.HMerge = 1
- titleRow.AddCell().SetString("")
- }
- }
- // 新增一行放主观和客观
- specialRow := sheet.AddRow()
- for i:=0; i< hi; i++ {
- if i == insertHi {
- v1 := specialRow.AddCell()
- v1.SetString("45万")
- v1.SetStyle(style)
- v1 = specialRow.AddCell()
- v1.SetString("70万")
- v1.SetStyle(style)
- for j:=0; j<8;j++ {
- if j%2 ==0 {
- v1 = specialRow.AddCell()
- v1.SetString("主观")
- v1.SetStyle(style)
- }else{
- v1 = specialRow.AddCell()
- v1.SetString("客观")
- v1.SetStyle(style)
- }
- }
- }else{
- v := specialRow.AddCell()
- v.SetString("")
- }
- }
- newCompanyMap := map[int]string{0: "否", 1: "是"}
- sellerTypeMap := map[int]string{1:"FICC销售", 2:"权益销售"}
- for k, v := range list {
- dataRow := sheet.AddRow()
- // 前四个单元格根据每行开票到款条数向下合并
- l := len(v.InvoicePaymentList)
- mergeRowNum := l - 1
- // 序号
- sortNum := k + 1
- colA := dataRow.AddCell()
- colA.VMerge = mergeRowNum
- colA.SetString(fmt.Sprint(sortNum))
- // 客户名称
- colB := dataRow.AddCell()
- colB.VMerge = mergeRowNum
- colB.SetString(v.CompanyName)
- // 是否新客户
- colC := dataRow.AddCell()
- colC.VMerge = mergeRowNum
- colC.SetString(newCompanyMap[v.NewCompany])
- // 合同有效期
- colD := dataRow.AddCell()
- colD.VMerge = mergeRowNum
- colD.SetString(fmt.Sprint(v.StartDate, "至", v.EndDate))
- // 开票到款信息
- for k2, v2 := range v.InvoicePaymentList {
- rowData := []string{
- v2.InvoiceDate, // 开票日
- fmt.Sprint(v2.InvoiceAmount), // 开票金额
- v2.PaymentDate, // 到款日
- fmt.Sprint(v2.PaymentAmount), // 到款金额
- fms.ContractPaymentPayTypeNameMap[v2.PayType], // 付款方式
- v2.SellerName, // 销售
- v2.SellerGroupName, // 组别
- sellerTypeMap[v2.SellerType], // 销售类型
- }
- // 套餐金额信息
- serviceTempShowAmount := make(map[int]string)
- for i := range serviceTempShow {
- for s2 := range v2.ServiceAmountList {
- item := v2.ServiceAmountList[s2]
- if item.ServiceTemplateId == serviceTempShow[i].ServiceTemplateId {
- serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount)
- break
- }else if serviceTempShow[i].Pid == item.ServiceTemplateId {
- serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount/2)
- break
- }
- }
- }
- for i := range serviceTempShow {
- sa := ""
- if am, ok := serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId]; ok {
- sa = am
- }
- rowData = append(rowData, sa)
- }
- // 首行开票到款
- if k2 == 0 {
- for i := range rowData {
- dataRow.AddCell().SetString(rowData[i])
- }
- continue
- }
- // 其他行开票到款, 加四列空的单元格用于合并
- dataRowExtra := sheet.AddRow()
- for i := 0; i < 4; i++ {
- dataRowExtra.AddCell()
- }
- for i := range rowData {
- dataRowExtra.AddCell().SetString(rowData[i])
- }
- }
- }
- // 输出文件
- var buffer bytes.Buffer
- _ = xlsxFile.Write(&buffer)
- content := bytes.NewReader(buffer.Bytes())
- randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
- fileName := "商品到款统计_" + randStr + ".xlsx"
- c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
- c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
- http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
- }
- // List
- // @Title 商品到款统计列表
- // @Description 商品到款统计列表
- // @Param Keyword query string false "关键词"
- // @Param SellGroupId query int false "销售组别ID"
- // @Param ServiceType query int false "套餐类型"
- // @Param StartDate query string false "合同开始日期"
- // @Param EndDate query string false "合同结束日期"
- // @Param TimeType query int false "时间类型: 1-开票时间; 2-到款时间"
- // @Param HasInvoice query string false "是否已开票: 0-否; 1-是; 空-全部"
- // @Param HasPayment query string false "是否已到款: 0-否; 1-是; 空-全部"
- // @Param IsExport query int false "是否导出: 0-否; 1-是"
- // @Param ListParam query int false "套餐类型: 0-全部; 1-ficc; 2-权益"
- // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'invoice_time':开票日 、 'payment_date':到款日"
- // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
- // @Success 200 {object} fms.ContractRegisterItem
- // @router /census/invoice_payment/list [get]
- func (ct *InvoicePaymentController) List(c *gin.Context) {
- var req fms.InvoicePaymentCensusListReq
- if e := c.BindQuery(&req); e != nil {
- err, ok := e.(validator.ValidationErrors)
- if !ok {
- resp.FailData("参数解析失败", "Err:"+e.Error(), c)
- return
- }
- resp.FailData("参数解析失败", err.Translate(global.Trans), c)
- return
- }
- cond := `1 = 1`
- pars := make([]interface{}, 0)
- // 客户姓名/销售
- if req.Keyword != "" {
- kw := "%" + req.Keyword + "%"
- cond += ` AND b.company_name LIKE ?`
- pars = append(pars, kw)
- }
- if req.SellerIds != "" {
- sellerIds := strings.Split(req.SellerIds, ",")
- cond += ` AND (c.seller_id in ?)`
- pars = append(pars, sellerIds)
- }
- // 套餐筛选
- if req.ServiceTypes != "" {
- serviceTypes := strings.Split(req.ServiceTypes, ",")
- tempRegisterIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
- if e != nil {
- resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
- return
- }
- if len(tempRegisterIds) > 0 {
- cond += ` AND a.register_id IN ?`
- pars = append(pars, tempRegisterIds)
- } else {
- cond += ` AND 1 = 2`
- }
- }
- if req.ListParam == 1{
- cond += ` AND a.service_product_id = 1 `
- }else if req.ListParam == 2 {
- cond += ` AND a.service_product_id = 2 `
- }
- // 开票到款日期
- if req.TimeType != 0 {
- if req.TimeType == 1 && req.StartDate != "" && req.EndDate != "" {
- st := fmt.Sprint(req.StartDate, " 00:00:00")
- ed := fmt.Sprint(req.EndDate, " 23:59:59")
- cond += ` AND (c.invoice_time BETWEEN ? AND ?) `
- pars = append(pars, st, ed)
- }else if req.TimeType == 2 && req.StartDate != "" && req.EndDate != "" {
- st := fmt.Sprint(req.StartDate, " 00:00:00")
- ed := fmt.Sprint(req.EndDate, " 23:59:59")
- cond += ` AND (d.invoice_time BETWEEN ? AND ?) `
- pars = append(pars, st, ed)
- }else if req.TimeType == 3 && req.StartDate != "" && req.EndDate != "" {
- st := fmt.Sprint(req.StartDate, " 00:00:00")
- ed := fmt.Sprint(req.EndDate, " 23:59:59")
- cond += ` AND (c.invoice_time BETWEEN ? AND ?) AND (d.invoice_time BETWEEN ? AND ?) `
- pars = append(pars, st, ed, st, ed)
- }
- } else if req.StartDate != "" && req.EndDate != "" {
- st := fmt.Sprint(req.StartDate, " 00:00:00")
- ed := fmt.Sprint(req.EndDate, " 23:59:59")
- cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
- pars = append(pars, st, ed, st, ed)
- }
- if req.HasInvoice == "1" {
- cond += ` AND a.invoice_id > 0 `
- }else if req.HasInvoice == "0" {
- cond += ` AND a.invoice_id = 0 `
- }
- if req.HasPayment == "1" {
- cond += ` AND a.payment_id > 0 `
- }else if req.HasPayment == "0" {
- cond += ` AND a.payment_id = 0 `
- }
- page := new(base.Page)
- page.SetPageSize(req.PageSize)
- page.SetCurrent(req.Current)
- //排序
- if req.SortType == "" {
- req.SortType = "desc"
- }
- if req.SortType != "desc" && req.SortType != "asc" {
- resp.Fail("排序类型不正确", c)
- return
- }
- if req.SortParam == "" {
- page.AddOrderItem(base.OrderItem{Column: "sort_invoice_id", Asc: true})
- page.AddOrderItem(base.OrderItem{Column: "sort_payment_id", Asc: false})
- page.AddOrderItem(base.OrderItem{Column: "c.invoice_time", Asc: false})
- page.AddOrderItem(base.OrderItem{Column: "c.amount", Asc: false})
- page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
- }else if req.SortParam == "invoice_time" {
- if req.SortType == "asc" {
- page.AddOrderItem(base.OrderItem{Column: "sort_invoice_id", Asc: false})
- page.AddOrderItem(base.OrderItem{Column: "c.invoice_time", Asc: true})
- page.AddOrderItem(base.OrderItem{Column: "c.amount", Asc: true})
- page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: true})
- }else{
- page.AddOrderItem(base.OrderItem{Column: "sort_invoice_id", Asc: true})
- page.AddOrderItem(base.OrderItem{Column: "c.invoice_time", Asc: false})
- page.AddOrderItem(base.OrderItem{Column: "c.amount", Asc: false})
- page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
- }
- }else if req.SortParam == "payment_date" {
- if req.SortType == "asc" {
- page.AddOrderItem(base.OrderItem{Column: "sort_payment_id", Asc: false})
- page.AddOrderItem(base.OrderItem{Column: "d.invoice_time", Asc: true})
- page.AddOrderItem(base.OrderItem{Column: "d.amount", Asc: true})
- page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: true})
- }else{
- page.AddOrderItem(base.OrderItem{Column: "sort_payment_id", Asc: true})
- page.AddOrderItem(base.OrderItem{Column: "d.invoice_time", Asc: false})
- page.AddOrderItem(base.OrderItem{Column: "d.amount", Asc: false})
- page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
- }
- }else{
- resp.Fail("排序字段不正确", c)
- return
- }
- if req.IsExport == 1 {
- page.SetPageSize(10000)
- page.SetCurrent(1)
- }
- registerList, total, e := fms.GetInvoicePaymentCensusPageList(page, cond, pars)
- if e != nil {
- resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
- return
- }
- queryRegisterIds := make([]int, 0)
- for i := range registerList {
- queryRegisterIds = append(queryRegisterIds, registerList[i].ContractRegisterId)
- }
- //fmt.Println("queryRegisterIds:",queryRegisterIds)
- results := new(fms.InvoicePaymentCensusResp)
- if len(queryRegisterIds) > 0 {
- // 获取汇总数据IDs, 用于查询合计数据
- summaryIdsCond := cond
- summaryIdsPars := pars
- summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
- if e != nil {
- resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
- return
- }
- //fmt.Println("summaryIds:",summaryIds)
- var listErr, totalErr, totalGroupErr error
- wg := sync.WaitGroup{}
- // 响应列表
- respList := make([]*fms.InvoicePaymentCensusItem, 0)
- summaryList := make([]*fms.InvoicePaymentSummaryItem, 0)
- wg.Add(1)
- go func() {
- defer wg.Done()
- // 获取汇总数据
- summaryCond := cond
- summaryCond += ` AND a.register_id IN ?`
- summaryPars := pars
- summaryPars = append(summaryPars, queryRegisterIds)
- summaryData, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
- if e != nil {
- resp.FailMsg("获取失败", "获取商品到款汇总列表失败, Err: "+e.Error(), c)
- return
- }
- summaryList = summaryData
- //summaryIds := make([]int, 0)
- paymentIds := make([]int, 0)
- for i := range summaryList {
- //summaryIds = append(summaryIds, summaryList[i].SummaryId)
- if summaryList[i].PaymentId > 0 {
- paymentIds = append(paymentIds, summaryList[i].PaymentId)
- }
- }
- // 合同套餐
- /*contractServiceCond := `contract_register_id IN ?`
- contractServicePars := make([]interface{}, 0)
- contractServicePars = append(contractServicePars, queryRegisterIds)
- contractServiceOB := new(fms.ContractService)
- contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
- if e != nil {
- listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
- return
- }
- contractServiceMap := make(map[int][]*fms.ContractService, 0)
- servicesNameMap := make(map[int][]string, 0)
- for i := range contractServiceList {
- if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
- contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
- }
- contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
- servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
- }
- */
- servicesNameMap, serviceFormatMap, e := fmsService.GetContractServiceNameFormat(queryRegisterIds)
- if e != nil {
- listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
- return
- }
- // 到款套餐分配
- serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
- if len(paymentIds) > 0 {
- serviceAmountCond := `contract_payment_id IN ?`
- serviceAmountPars := make([]interface{}, 0)
- serviceAmountPars = append(serviceAmountPars, paymentIds)
- serviceAmountOB := new(fms.ContractPaymentServiceAmount)
- serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
- if e != nil {
- listErr = fmt.Errorf("获取到款套餐分配列表失败, Err: %s", e.Error())
- return
- }
- for i := range serviceAmountList {
- if serviceAmountMap[serviceAmountList[i].ContractPaymentId] == nil {
- serviceAmountMap[serviceAmountList[i].ContractPaymentId] = make([]*fms.ContractPaymentServiceAmount, 0)
- }
- serviceAmountMap[serviceAmountList[i].ContractPaymentId] = append(serviceAmountMap[serviceAmountList[i].ContractPaymentId], serviceAmountList[i])
- }
- }
- // 重组汇总数据
- summaryMap := make(map[int][]*fms.InvoicePaymentCensusInfo)
- amountMap := make(map[string]*fms.ContractPaymentServiceAmount)
- for i := range summaryList {
- v := new(fms.InvoicePaymentCensusInfo)
- v.InvoiceId = summaryList[i].InvoiceId
- v.InvoiceDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].InvoiceDate)
- v.InvoiceAmount = summaryList[i].InvoiceAmount
- v.SellerId = summaryList[i].SellerId
- v.SellerName = summaryList[i].SellerName
- v.SellerGroupId = summaryList[i].SellerGroupId
- v.SellerGroupName = summaryList[i].SellerGroupName
- v.SellerType = summaryList[i].ServiceProductId
- v.PaymentId = summaryList[i].PaymentId
- v.PaymentDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].PaymentDate)
- v.PaymentAmount = summaryList[i].PaymentAmount
- v.PayType = summaryList[i].PayType
- // 套餐到款分配
- svaList := make([]*fms.ContractPaymentServiceAmountItem, 0)
- amountList := serviceAmountMap[summaryList[i].PaymentId]
- if amountList != nil {
- for i := range amountList {
- k := fmt.Sprintf("%d-%d", amountList[i].ContractPaymentId, amountList[i].ServiceTemplateId)
- amountMap[k] = amountList[i]
- }
- }
- // 合同对应的所有套餐
- svList := serviceFormatMap[summaryList[i].RegisterId]
- if svList != nil {
- for ii := range svList {
- vv := new(fms.ContractPaymentServiceAmountItem)
- vv.ServiceTemplateId = svList[ii].ServiceTemplateId
- vv.ServiceTemplateName = svList[ii].FormatTitle
- vv.ServiceTemplatePid = svList[ii].ServiceTemplatePid
- k2 := fmt.Sprintf("%d-%d", summaryList[i].PaymentId, svList[ii].ServiceTemplateId)
- a := amountMap[k2]
- if a != nil {
- vv.ContractPaymentServiceAmountId = a.ContractPaymentServiceAmountId
- vv.ContractPaymentId = a.ContractPaymentId
- vv.Amount = a.Amount
- }
- svaList = append(svaList, vv)
- }
- }
- v.ServiceAmountList = svaList
- summaryMap[summaryList[i].SummaryId] = append(summaryMap[summaryList[i].SummaryId], v)
- }
- // 响应列表
- for i := range registerList {
- v := new(fms.InvoicePaymentCensusItem)
- v.SummaryId = registerList[i].SummaryId
- v.ContractRegisterId = registerList[i].ContractRegisterId
- v.CompanyName = registerList[i].CompanyName
- v.NewCompany = registerList[i].NewCompany
- v.StartDate = utils.TimeTransferString(utils.FormatDate, registerList[i].StartDate)
- v.EndDate = utils.TimeTransferString(utils.FormatDate, registerList[i].EndDate)
- v.ServicesName = servicesNameMap[registerList[i].ContractRegisterId]
- v.InvoicePaymentList = summaryMap[registerList[i].SummaryId]
- v.ContractType = registerList[i].ContractType
- respList = append(respList, v)
- }
- }()
- // 开票到款金额合计(换算后)
- var invoiceTotal, paymentTotal float64
- wg.Add(1)
- go func() {
- defer wg.Done()
- if len(summaryIds) == 0 {
- return
- }
- amountTotalCond := `a.id IN ?`
- amountTotalPars := make([]interface{}, 0)
- amountTotalPars = append(amountTotalPars, summaryIds)
- invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
- if e != nil {
- totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
- return
- }
- invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
- paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
- if e != nil {
- totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
- return
- }
- paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
- }()
- // 分币种金额统计
- invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
- paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
- wg.Add(1)
- go func() {
- defer wg.Done()
- currencyOB := new(fms.CurrencyUnit)
- currencyCond := `enable = 1`
- currencyPars := make([]interface{}, 0)
- currencyList, e := currencyOB.List(currencyCond, currencyPars)
- if e != nil {
- totalGroupErr = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
- return
- }
- unitMap := make(map[string]string)
- for i := range currencyList {
- unitMap[currencyList[i].Code] = currencyList[i].UnitName
- invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
- Name: currencyList[i].Name,
- UnitName: currencyList[i].UnitName,
- Code: currencyList[i].Code,
- FlagImg: currencyList[i].FlagImg,
- })
- paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
- Name: currencyList[i].Name,
- UnitName: currencyList[i].UnitName,
- Code: currencyList[i].Code,
- FlagImg: currencyList[i].FlagImg,
- })
- }
- if len(summaryIds) == 0 {
- return
- }
- totalGroupCond := `a.id IN ?`
- totalGroupPars := make([]interface{}, 0)
- totalGroupPars = append(totalGroupPars, summaryIds)
- invoiceSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 1)
- if e != nil {
- totalGroupErr = fmt.Errorf("获取汇总货币合计开票金额失败, Err: %s", e.Error())
- return
- }
- paymentSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 2)
- if e != nil {
- totalGroupErr = fmt.Errorf("获取汇总货币合计到款金额失败, Err: %s", e.Error())
- return
- }
- invoiceSumMap := make(map[string]float64)
- paymentSumMap := make(map[string]float64)
- for i := range invoiceSumGroup {
- invoiceSumMap[invoiceSumGroup[i].CurrencyUnit] = invoiceSumGroup[i].OriginAmountTotal
- continue
- }
- for i := range paymentSumGroup {
- paymentSumMap[paymentSumGroup[i].CurrencyUnit] = paymentSumGroup[i].OriginAmountTotal
- continue
- }
- for i := range invoiceCurrencyTotals {
- a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
- invoiceCurrencyTotals[i].Amount = a
- }
- for i := range paymentCurrencyTotals {
- a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
- paymentCurrencyTotals[i].Amount = a
- }
- }()
- wg.Wait()
- if listErr != nil {
- resp.FailMsg("获取失败", listErr.Error(), c)
- return
- }
- if totalErr != nil {
- resp.FailMsg("获取失败", totalErr.Error(), c)
- return
- }
- if totalGroupErr != nil {
- resp.FailMsg("获取失败", totalGroupErr.Error(), c)
- return
- }
- results.DataList = respList
- results.InvoiceTotal = invoiceTotal
- results.PaymentTotal = paymentTotal
- results.InvoiceCurrencyTotal = invoiceCurrencyTotals
- results.PaymentCurrencyTotal = paymentCurrencyTotals
- }
- // 是否导出
- if req.IsExport == 1 {
- ExportInvoicePaymentCensusList(c, results)
- return
- }
- page.SetTotal(total)
- baseData := new(base.BaseData)
- baseData.SetPage(page)
- baseData.SetList(results)
- resp.OkData("获取成功", baseData, c)
- }
- // List
- // @Title 未开票统计列表
- // @Description 未开票统计列表
- // @Param Keyword query string false "关键词"
- // @Param SellGroupId query int false "销售组别ID"
- // @Param ServiceType query int false "套餐类型"
- // @Param StartDate query string false "合同开始日期"
- // @Param EndDate query string false "合同结束日期"
- // @Param TimeType query int false "时间类型: 1-开票时间; 2-到款时间"
- // @Param HasInvoice query string false "是否已开票: 0-否; 1-是; 空-全部"
- // @Param HasPayment query string false "是否已到款: 0-否; 1-是; 空-全部"
- // @Param IsExport query int false "是否导出: 0-否; 1-是"
- // @Param ListParam query int false "套餐类型: 0-全部; 1-ficc; 2-权益"
- // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'start_date':开票日 、 'end_date':到款日"
- // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
- // @Success 200 {object} fms.ContractRegisterItem
- // @router /census/invoice_payment/not_invoice/list [get]
- func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
- var req fms.InvoicePaymentCensusListReq
- if e := c.BindQuery(&req); e != nil {
- err, ok := e.(validator.ValidationErrors)
- if !ok {
- resp.FailData("参数解析失败", "Err:"+e.Error(), c)
- return
- }
- resp.FailData("参数解析失败", err.Translate(global.Trans), c)
- return
- }
- cond := `1 = 1`
- pars := make([]interface{}, 0)
- // 客户姓名/合同编号
- if req.Keyword != "" {
- kw := "%" + req.Keyword + "%"
- cond += ` AND (b.company_name LIKE ? OR b.contract_code LIKE ?)`
- pars = append(pars, kw, kw)
- }
- if req.SellerIds != "" {
- sellerIds := strings.Split(req.SellerIds, ",")
- cond += ` AND (c.seller_id in ?)`
- pars = append(pars, sellerIds)
- }
- // 套餐筛选
- if req.ServiceTypes != "" {
- serviceTypes := strings.Split(req.ServiceTypes, ",")
- tempRegisterIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
- if e != nil {
- resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
- return
- }
- if len(tempRegisterIds) > 0 {
- cond += ` AND a.register_id IN ?`
- pars = append(pars, tempRegisterIds)
- } else {
- cond += ` AND 1 = 2`
- }
- }
- if req.ListParam == 1{
- cond += ` AND a.service_product_id = 1 `
- }else if req.ListParam == 2 {
- cond += ` AND a.service_product_id = 2 `
- }
- // 开票到款日期
- //if req.TimeType != 0 {
- // if req.TimeType == 1 && req.StartDate != "" && req.EndDate != "" {
- // st := fmt.Sprint(req.StartDate, " 00:00:00")
- // ed := fmt.Sprint(req.EndDate, " 23:59:59")
- // cond += ` AND (c.invoice_time BETWEEN ? AND ?) `
- // pars = append(pars, st, ed)
- // }else if req.TimeType == 2 && req.StartDate != "" && req.EndDate != "" {
- // st := fmt.Sprint(req.StartDate, " 00:00:00")
- // ed := fmt.Sprint(req.EndDate, " 23:59:59")
- // cond += ` AND (d.invoice_time BETWEEN ? AND ?) `
- // pars = append(pars, st, ed)
- // }else if req.TimeType == 3 && req.StartDate != "" && req.EndDate != "" {
- // st := fmt.Sprint(req.StartDate, " 00:00:00")
- // ed := fmt.Sprint(req.EndDate, " 23:59:59")
- // cond += ` AND (c.invoice_time BETWEEN ? AND ?) AND (d.invoice_time BETWEEN ? AND ?) `
- // pars = append(pars, st, ed, st, ed)
- // }
- //} else if req.StartDate != "" && req.EndDate != "" {
- // st := fmt.Sprint(req.StartDate, " 00:00:00")
- // ed := fmt.Sprint(req.EndDate, " 23:59:59")
- // cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
- // pars = append(pars, st, ed, st, ed)
- //}
- //if req.HasInvoice == "1" {
- // cond += ` AND a.invoice_id > 0 `
- //}else if req.HasInvoice == "0" {
- // cond += ` AND a.invoice_id = 0 `
- //}
- //
- //if req.HasPayment == "1" {
- // cond += ` AND a.payment_id > 0 `
- //}else if req.HasPayment == "0" {
- // cond += ` AND a.payment_id = 0 `
- //}
- page := new(base.Page)
- page.SetPageSize(req.PageSize)
- page.SetCurrent(req.Current)
- //排序
- if req.SortType == "" {
- req.SortType = "desc"
- }
- if req.SortType != "desc" && req.SortType != "asc" {
- resp.Fail("排序类型不正确", c)
- return
- }
- if req.SortParam == "" {
- page.AddOrderItem(base.OrderItem{Column: "e.start_date", Asc: false})
- }else if req.SortParam == "start_date" {
- if req.SortType == "asc" {
- page.AddOrderItem(base.OrderItem{Column: "e.start_date", Asc: true})
- }else{
- page.AddOrderItem(base.OrderItem{Column: "e.start_date", Asc: false})
- }
- }else if req.SortParam == "end_date" {
- if req.SortType == "asc" {
- page.AddOrderItem(base.OrderItem{Column: "e.end_date", Asc: true})
- }else{
- page.AddOrderItem(base.OrderItem{Column: "e.end_date", Asc: false})
- }
- }else{
- resp.Fail("排序字段不正确", c)
- return
- }
- if req.IsExport == 1 {
- page.SetPageSize(10000)
- page.SetCurrent(1)
- }
- registerList, total, e := fms.GetInvoiceCensusPageList(page, cond, pars)
- if e != nil {
- resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
- return
- }
- queryRegisterIds := make([]int, 0)
- for i := range registerList {
- queryRegisterIds = append(queryRegisterIds, registerList[i].ContractRegisterId)
- }
- //fmt.Println("queryRegisterIds:",queryRegisterIds)
- results := new(fms.NotInvoicePaymentCensusResp)
- if len(queryRegisterIds) > 0 {
- // 获取汇总数据IDs, 用于查询合计数据
- summaryIdsCond := cond
- summaryIdsPars := pars
- summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
- if e != nil {
- resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
- return
- }
- //fmt.Println("summaryIds:",summaryIds)
- var listErr, totalErr, totalGroupErr error
- wg := sync.WaitGroup{}
- // 响应列表
- respList := make([]*fms.InvoicePaymentCensusItem, 0)
- summaryList := make([]*fms.InvoicePaymentSummaryItem, 0)
- wg.Add(1)
- go func() {
- defer wg.Done()
- // 获取汇总数据
- summaryCond := cond
- summaryCond += ` AND a.register_id IN ?`
- summaryPars := pars
- summaryPars = append(summaryPars, queryRegisterIds)
- summaryData, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
- if e != nil {
- resp.FailMsg("获取失败", "获取商品到款汇总列表失败, Err: "+e.Error(), c)
- return
- }
- summaryList = summaryData
- //summaryIds := make([]int, 0)
- paymentIds := make([]int, 0)
- for i := range summaryList {
- //summaryIds = append(summaryIds, summaryList[i].SummaryId)
- if summaryList[i].PaymentId > 0 {
- paymentIds = append(paymentIds, summaryList[i].PaymentId)
- }
- }
- // 合同套餐
- /*contractServiceCond := `contract_register_id IN ?`
- contractServicePars := make([]interface{}, 0)
- contractServicePars = append(contractServicePars, queryRegisterIds)
- contractServiceOB := new(fms.ContractService)
- contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
- if e != nil {
- listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
- return
- }
- contractServiceMap := make(map[int][]*fms.ContractService, 0)
- servicesNameMap := make(map[int][]string, 0)
- for i := range contractServiceList {
- if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
- contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
- }
- contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
- servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
- }
- */
- servicesNameMap, serviceFormatMap, e := fmsService.GetContractServiceNameFormat(queryRegisterIds)
- if e != nil {
- listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
- return
- }
- // 到款套餐分配
- serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
- if len(paymentIds) > 0 {
- serviceAmountCond := `contract_payment_id IN ?`
- serviceAmountPars := make([]interface{}, 0)
- serviceAmountPars = append(serviceAmountPars, paymentIds)
- serviceAmountOB := new(fms.ContractPaymentServiceAmount)
- serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
- if e != nil {
- listErr = fmt.Errorf("获取到款套餐分配列表失败, Err: %s", e.Error())
- return
- }
- for i := range serviceAmountList {
- if serviceAmountMap[serviceAmountList[i].ContractPaymentId] == nil {
- serviceAmountMap[serviceAmountList[i].ContractPaymentId] = make([]*fms.ContractPaymentServiceAmount, 0)
- }
- serviceAmountMap[serviceAmountList[i].ContractPaymentId] = append(serviceAmountMap[serviceAmountList[i].ContractPaymentId], serviceAmountList[i])
- }
- }
- // 重组汇总数据
- summaryMap := make(map[int][]*fms.InvoicePaymentCensusInfo)
- amountMap := make(map[string]*fms.ContractPaymentServiceAmount)
- for i := range summaryList {
- v := new(fms.InvoicePaymentCensusInfo)
- v.InvoiceId = summaryList[i].InvoiceId
- v.InvoiceDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].InvoiceDate)
- v.InvoiceAmount = summaryList[i].InvoiceAmount
- v.SellerId = summaryList[i].SellerId
- v.SellerName = summaryList[i].SellerName
- v.SellerGroupId = summaryList[i].SellerGroupId
- v.SellerGroupName = summaryList[i].SellerGroupName
- v.SellerType = summaryList[i].ServiceProductId
- v.PaymentId = summaryList[i].PaymentId
- v.PaymentDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].PaymentDate)
- v.PaymentAmount = summaryList[i].PaymentAmount
- v.PayType = summaryList[i].PayType
- // 套餐到款分配
- svaList := make([]*fms.ContractPaymentServiceAmountItem, 0)
- amountList := serviceAmountMap[summaryList[i].PaymentId]
- if amountList != nil {
- for i := range amountList {
- k := fmt.Sprintf("%d-%d", amountList[i].ContractPaymentId, amountList[i].ServiceTemplateId)
- amountMap[k] = amountList[i]
- }
- }
- // 合同对应的所有套餐
- svList := serviceFormatMap[summaryList[i].RegisterId]
- if svList != nil {
- for ii := range svList {
- vv := new(fms.ContractPaymentServiceAmountItem)
- vv.ServiceTemplateId = svList[ii].ServiceTemplateId
- vv.ServiceTemplateName = svList[ii].FormatTitle
- vv.ServiceTemplatePid = svList[ii].ServiceTemplatePid
- k2 := fmt.Sprintf("%d-%d", summaryList[i].PaymentId, svList[ii].ServiceTemplateId)
- a := amountMap[k2]
- if a != nil {
- vv.ContractPaymentServiceAmountId = a.ContractPaymentServiceAmountId
- vv.ContractPaymentId = a.ContractPaymentId
- vv.Amount = a.Amount
- }
- svaList = append(svaList, vv)
- }
- }
- v.ServiceAmountList = svaList
- summaryMap[summaryList[i].SummaryId] = append(summaryMap[summaryList[i].SummaryId], v)
- }
- // 货币列表
- currencyOB := new(fms.CurrencyUnit)
- currencyCond := `enable = 1`
- currencyPars := make([]interface{}, 0)
- currencyList, e := currencyOB.List(currencyCond, currencyPars)
- if e != nil {
- resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
- return
- }
- unitMap := make(map[string]string)
- for i := range currencyList {
- unitMap[currencyList[i].Code] = currencyList[i].UnitName
- }
- // 响应列表
- for i := range registerList {
- v := new(fms.InvoicePaymentCensusItem)
- v.SummaryId = registerList[i].SummaryId
- v.ContractRegisterId = registerList[i].ContractRegisterId
- v.CompanyName = registerList[i].CompanyName
- v.NewCompany = registerList[i].NewCompany
- v.StartDate = utils.TimeTransferString(utils.FormatDate, registerList[i].StartDate)
- v.EndDate = utils.TimeTransferString(utils.FormatDate, registerList[i].EndDate)
- v.ServicesName = servicesNameMap[registerList[i].ContractRegisterId]
- v.InvoicePaymentList = summaryMap[registerList[i].SummaryId]
- v.ContractType = registerList[i].ContractType
- v.ContractCode = registerList[i].ContractCode
- v.ContractAmount = registerList[i].ContractAmount
- v.UnitName = unitMap[registerList[i].CurrencyUnit]
- v.ProductIds = registerList[i].ProductIds
- v.NotInvoiceTotal = registerList[i].ContractAmount - registerList[i].InvoicedAmount
- respList = append(respList, v)
- }
- }()
- // 开票到款金额合计(换算后)
- var invoiceTotal, paymentTotal, contractAmount float64
- wg.Add(1)
- go func() {
- defer wg.Done()
- if len(summaryIds) == 0 {
- return
- }
- amountTotalCond := `a.id IN ?`
- amountTotalPars := make([]interface{}, 0)
- amountTotalPars = append(amountTotalPars, summaryIds)
- invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
- if e != nil {
- totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
- return
- }
- invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
- paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
- if e != nil {
- totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
- return
- }
- paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
- }()
- // 分币种金额统计
- invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
- paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
- contractAmountCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
- wg.Add(1)
- go func() {
- defer wg.Done()
- currencyOB := new(fms.CurrencyUnit)
- currencyCond := `enable = 1`
- currencyPars := make([]interface{}, 0)
- currencyList, e := currencyOB.List(currencyCond, currencyPars)
- if e != nil {
- totalGroupErr = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
- return
- }
- unitMap := make(map[string]string)
- for i := range currencyList {
- unitMap[currencyList[i].Code] = currencyList[i].UnitName
- invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
- Name: currencyList[i].Name,
- UnitName: currencyList[i].UnitName,
- Code: currencyList[i].Code,
- FlagImg: currencyList[i].FlagImg,
- })
- paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
- Name: currencyList[i].Name,
- UnitName: currencyList[i].UnitName,
- Code: currencyList[i].Code,
- FlagImg: currencyList[i].FlagImg,
- })
- contractAmountCurrencyTotals = append(contractAmountCurrencyTotals, &fms.InvoiceListCurrencyTotal{
- Name: currencyList[i].Name,
- UnitName: currencyList[i].UnitName,
- Code: currencyList[i].Code,
- FlagImg: currencyList[i].FlagImg,
- })
- }
- if len(summaryIds) == 0 {
- return
- }
- totalGroupCond := `a.id IN ?`
- totalGroupPars := make([]interface{}, 0)
- totalGroupPars = append(totalGroupPars, summaryIds)
- invoiceSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 1)
- if e != nil {
- totalGroupErr = fmt.Errorf("获取汇总货币合计开票金额失败, Err: %s", e.Error())
- return
- }
- paymentSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 2)
- if e != nil {
- totalGroupErr = fmt.Errorf("获取汇总货币合计到款金额失败, Err: %s", e.Error())
- return
- }
- invoiceSumMap := make(map[string]float64)
- paymentSumMap := make(map[string]float64)
- contractSumMap := make(map[string]float64)
- for i := range invoiceSumGroup {
- invoiceSumMap[invoiceSumGroup[i].CurrencyUnit] = invoiceSumGroup[i].OriginAmountTotal
- continue
- }
- for i := range paymentSumGroup {
- paymentSumMap[paymentSumGroup[i].CurrencyUnit] = paymentSumGroup[i].OriginAmountTotal
- continue
- }
- for i := range invoiceCurrencyTotals {
- a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
- invoiceCurrencyTotals[i].Amount = a
- }
- for i := range paymentCurrencyTotals {
- a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
- paymentCurrencyTotals[i].Amount = a
- }
- contractRegisters, err := fms.GetContractInfoByRegisterIds(queryRegisterIds)
- if err != nil {
- resp.FailMsg("查询合同信息失败", "查询合同信息失败, Err: "+err.Error(), c)
- return
- }
- for _, v := range contractRegisters {
- if v.RMBRate == 0.0{
- v.RMBRate = 1
- }
- amount,_ := strconv.ParseFloat(fmt.Sprintf("%.2f",v.ContractAmount / v.RMBRate), 64)
- contractAmount += amount
- contractSumMap[v.CurrencyUnit] = v.ContractAmount
- }
- for _, v := range contractAmountCurrencyTotals {
- v.Amount = contractSumMap[v.Code] - invoiceSumMap[v.Code]
- }
- }()
- wg.Wait()
- if listErr != nil {
- resp.FailMsg("获取失败", listErr.Error(), c)
- return
- }
- if totalErr != nil {
- resp.FailMsg("获取失败", totalErr.Error(), c)
- return
- }
- if totalGroupErr != nil {
- resp.FailMsg("获取失败", totalGroupErr.Error(), c)
- return
- }
- results.DataList = respList
- results.InvoiceTotal = invoiceTotal
- results.PaymentTotal = paymentTotal
- results.NotInvoiceTotal = contractAmount-invoiceTotal
- results.InvoiceCurrencyTotal = invoiceCurrencyTotals
- results.PaymentCurrencyTotal = paymentCurrencyTotals
- results.NotInvoiceCurrencyTotal = contractAmountCurrencyTotals
- }
- // 是否导出
- if req.IsExport == 1 {
- ExportNotInvoiceCensusList(c, results)
- return
- }
- page.SetTotal(total)
- baseData := new(base.BaseData)
- baseData.SetPage(page)
- baseData.SetList(results)
- resp.OkData("获取成功", baseData, c)
- }
- // List
- // @Title 开票未到款统计列表
- // @Description 开票未到款统计列表
- // @Param Keyword query string false "关键词"
- // @Param SellGroupId query int false "销售组别ID"
- // @Param ServiceType query int false "套餐类型"
- // @Param StartDate query string false "合同开始日期"
- // @Param EndDate query string false "合同结束日期"
- // @Param TimeType query int false "时间类型: 1-开票时间; 2-到款时间"
- // @Param HasInvoice query string false "是否已开票: 0-否; 1-是; 空-全部"
- // @Param HasPayment query string false "是否已到款: 0-否; 1-是; 空-全部"
- // @Param IsExport query int false "是否导出: 0-否; 1-是"
- // @Param ListParam query int false "套餐类型: 0-全部; 1-ficc; 2-权益"
- // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:'invoice_time':开票日 、 'payment_date':到款日"
- // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
- // @Success 200 {object} fms.ContractRegisterItem
- // @router /census/invoice_payment/not_payment/list [get]
- func (ct *InvoicePaymentController) NotPaymentList(c *gin.Context) {
- var req fms.InvoicePaymentCensusListReq
- if e := c.BindQuery(&req); e != nil {
- err, ok := e.(validator.ValidationErrors)
- if !ok {
- resp.FailData("参数解析失败", "Err:"+e.Error(), c)
- return
- }
- resp.FailData("参数解析失败", err.Translate(global.Trans), c)
- return
- }
- cond := `1 = 1`
- pars := make([]interface{}, 0)
- // 客户姓名/销售
- if req.Keyword != "" {
- kw := "%" + req.Keyword + "%"
- cond += ` AND (b.company_name LIKE ? OR b.contract_code LIKE ?)`
- pars = append(pars, kw, kw)
- }
- if req.SellerIds != "" {
- sellerIds := strings.Split(req.SellerIds, ",")
- cond += ` AND (c.seller_id in ?)`
- pars = append(pars, sellerIds)
- }
- // 套餐筛选
- if req.ServiceTypes != "" {
- serviceTypes := strings.Split(req.ServiceTypes, ",")
- tempRegisterIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
- if e != nil {
- resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
- return
- }
- if len(tempRegisterIds) > 0 {
- cond += ` AND a.register_id IN ?`
- pars = append(pars, tempRegisterIds)
- } else {
- cond += ` AND 1 = 2`
- }
- }
- if req.ListParam == 1{
- cond += ` AND a.service_product_id = 1 `
- }else if req.ListParam == 2 {
- cond += ` AND a.service_product_id = 2 `
- }
- // 开票到款日期
- //if req.TimeType != 0 {
- // if req.TimeType == 1 && req.StartDate != "" && req.EndDate != "" {
- // st := fmt.Sprint(req.StartDate, " 00:00:00")
- // ed := fmt.Sprint(req.EndDate, " 23:59:59")
- // cond += ` AND (c.invoice_time BETWEEN ? AND ?) `
- // pars = append(pars, st, ed)
- // }else if req.TimeType == 2 && req.StartDate != "" && req.EndDate != "" {
- // st := fmt.Sprint(req.StartDate, " 00:00:00")
- // ed := fmt.Sprint(req.EndDate, " 23:59:59")
- // cond += ` AND (d.invoice_time BETWEEN ? AND ?) `
- // pars = append(pars, st, ed)
- // }else if req.TimeType == 3 && req.StartDate != "" && req.EndDate != "" {
- // st := fmt.Sprint(req.StartDate, " 00:00:00")
- // ed := fmt.Sprint(req.EndDate, " 23:59:59")
- // cond += ` AND (c.invoice_time BETWEEN ? AND ?) AND (d.invoice_time BETWEEN ? AND ?) `
- // pars = append(pars, st, ed, st, ed)
- // }
- //} else if req.StartDate != "" && req.EndDate != "" {
- // st := fmt.Sprint(req.StartDate, " 00:00:00")
- // ed := fmt.Sprint(req.EndDate, " 23:59:59")
- // cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
- // pars = append(pars, st, ed, st, ed)
- //}
- //if req.HasInvoice == "1" {
- // cond += ` AND a.invoice_id > 0 `
- //}else if req.HasInvoice == "0" {
- // cond += ` AND a.invoice_id = 0 `
- //}
- //
- //if req.HasPayment == "1" {
- // cond += ` AND a.payment_id > 0 `
- //}else if req.HasPayment == "0" {
- // cond += ` AND a.payment_id = 0 `
- //}
- page := new(base.Page)
- page.SetPageSize(req.PageSize)
- page.SetCurrent(req.Current)
- //排序
- if req.SortType == "" {
- req.SortType = "desc"
- }
- if req.SortType != "desc" && req.SortType != "asc" {
- resp.Fail("排序类型不正确", c)
- return
- }
- if req.SortType == "asc" {
- page.AddOrderItem(base.OrderItem{Column: "e.invoice_time", Asc: true})
- }else{
- page.AddOrderItem(base.OrderItem{Column: "e.invoice_time", Asc: false})
- }
- if req.IsExport == 1 {
- page.SetPageSize(10000)
- page.SetCurrent(1)
- }
- registerList, total, e := fms.GetNoPaymentCensusPageList(page, cond, pars)
- if e != nil {
- resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
- return
- }
- queryRegisterIds := make([]int, 0)
- for i := range registerList {
- queryRegisterIds = append(queryRegisterIds, registerList[i].ContractRegisterId)
- }
- //fmt.Println("queryRegisterIds:",queryRegisterIds)
- results := new(fms.NotInvoicePaymentCensusResp)
- if len(queryRegisterIds) > 0 {
- // 获取汇总数据IDs, 用于查询合计数据
- summaryIdsCond := cond
- summaryIdsPars := pars
- summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
- if e != nil {
- resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
- return
- }
- //fmt.Println("summaryIds:",summaryIds)
- var listErr, totalErr, totalGroupErr error
- wg := sync.WaitGroup{}
- // 响应列表
- respList := make([]*fms.InvoicePaymentCensusItem, 0)
- summaryList := make([]*fms.InvoicePaymentSummaryItem, 0)
- wg.Add(1)
- go func() {
- defer wg.Done()
- // 获取汇总数据
- summaryCond := cond
- summaryCond += ` AND a.register_id IN ?`
- summaryPars := pars
- summaryPars = append(summaryPars, queryRegisterIds)
- summaryData, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
- if e != nil {
- resp.FailMsg("获取失败", "获取商品到款汇总列表失败, Err: "+e.Error(), c)
- return
- }
- summaryList = summaryData
- //summaryIds := make([]int, 0)
- paymentIds := make([]int, 0)
- for i := range summaryList {
- //summaryIds = append(summaryIds, summaryList[i].SummaryId)
- if summaryList[i].PaymentId > 0 {
- paymentIds = append(paymentIds, summaryList[i].PaymentId)
- }
- }
- // 合同套餐
- /*contractServiceCond := `contract_register_id IN ?`
- contractServicePars := make([]interface{}, 0)
- contractServicePars = append(contractServicePars, queryRegisterIds)
- contractServiceOB := new(fms.ContractService)
- contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
- if e != nil {
- listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
- return
- }
- contractServiceMap := make(map[int][]*fms.ContractService, 0)
- servicesNameMap := make(map[int][]string, 0)
- for i := range contractServiceList {
- if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
- contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
- }
- contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
- servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
- }
- */
- servicesNameMap, serviceFormatMap, e := fmsService.GetContractServiceNameFormat(queryRegisterIds)
- if e != nil {
- listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
- return
- }
- // 到款套餐分配
- serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
- if len(paymentIds) > 0 {
- serviceAmountCond := `contract_payment_id IN ?`
- serviceAmountPars := make([]interface{}, 0)
- serviceAmountPars = append(serviceAmountPars, paymentIds)
- serviceAmountOB := new(fms.ContractPaymentServiceAmount)
- serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
- if e != nil {
- listErr = fmt.Errorf("获取到款套餐分配列表失败, Err: %s", e.Error())
- return
- }
- for i := range serviceAmountList {
- if serviceAmountMap[serviceAmountList[i].ContractPaymentId] == nil {
- serviceAmountMap[serviceAmountList[i].ContractPaymentId] = make([]*fms.ContractPaymentServiceAmount, 0)
- }
- serviceAmountMap[serviceAmountList[i].ContractPaymentId] = append(serviceAmountMap[serviceAmountList[i].ContractPaymentId], serviceAmountList[i])
- }
- }
- // 重组汇总数据
- summaryMap := make(map[int][]*fms.InvoicePaymentCensusInfo)
- amountMap := make(map[string]*fms.ContractPaymentServiceAmount)
- for i := range summaryList {
- v := new(fms.InvoicePaymentCensusInfo)
- v.InvoiceId = summaryList[i].InvoiceId
- v.InvoiceDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].InvoiceDate)
- v.InvoiceAmount = summaryList[i].InvoiceAmount
- v.SellerId = summaryList[i].SellerId
- v.SellerName = summaryList[i].SellerName
- v.SellerGroupId = summaryList[i].SellerGroupId
- v.SellerGroupName = summaryList[i].SellerGroupName
- v.SellerType = summaryList[i].ServiceProductId
- v.PaymentId = summaryList[i].PaymentId
- v.PaymentDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].PaymentDate)
- v.PaymentAmount = summaryList[i].PaymentAmount
- v.PayType = summaryList[i].PayType
- // 套餐到款分配
- svaList := make([]*fms.ContractPaymentServiceAmountItem, 0)
- amountList := serviceAmountMap[summaryList[i].PaymentId]
- if amountList != nil {
- for i := range amountList {
- k := fmt.Sprintf("%d-%d", amountList[i].ContractPaymentId, amountList[i].ServiceTemplateId)
- amountMap[k] = amountList[i]
- }
- }
- // 合同对应的所有套餐
- svList := serviceFormatMap[summaryList[i].RegisterId]
- if svList != nil {
- for ii := range svList {
- vv := new(fms.ContractPaymentServiceAmountItem)
- vv.ServiceTemplateId = svList[ii].ServiceTemplateId
- vv.ServiceTemplateName = svList[ii].FormatTitle
- vv.ServiceTemplatePid = svList[ii].ServiceTemplatePid
- k2 := fmt.Sprintf("%d-%d", summaryList[i].PaymentId, svList[ii].ServiceTemplateId)
- a := amountMap[k2]
- if a != nil {
- vv.ContractPaymentServiceAmountId = a.ContractPaymentServiceAmountId
- vv.ContractPaymentId = a.ContractPaymentId
- vv.Amount = a.Amount
- }
- svaList = append(svaList, vv)
- }
- }
- v.ServiceAmountList = svaList
- summaryMap[summaryList[i].SummaryId] = append(summaryMap[summaryList[i].SummaryId], v)
- }
- // 货币列表
- currencyOB := new(fms.CurrencyUnit)
- currencyCond := `enable = 1`
- currencyPars := make([]interface{}, 0)
- currencyList, e := currencyOB.List(currencyCond, currencyPars)
- if e != nil {
- resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
- return
- }
- unitMap := make(map[string]string)
- for i := range currencyList {
- unitMap[currencyList[i].Code] = currencyList[i].UnitName
- }
- // 响应列表
- for i := range registerList {
- v := new(fms.InvoicePaymentCensusItem)
- v.SummaryId = registerList[i].SummaryId
- v.ContractRegisterId = registerList[i].ContractRegisterId
- v.CompanyName = registerList[i].CompanyName
- v.NewCompany = registerList[i].NewCompany
- v.StartDate = utils.TimeTransferString(utils.FormatDate, registerList[i].StartDate)
- v.EndDate = utils.TimeTransferString(utils.FormatDate, registerList[i].EndDate)
- v.ServicesName = servicesNameMap[registerList[i].ContractRegisterId]
- v.InvoicePaymentList = summaryMap[registerList[i].SummaryId]
- v.ContractType = registerList[i].ContractType
- v.ContractCode = registerList[i].ContractCode
- v.ContractAmount = registerList[i].ContractAmount
- v.UnitName = unitMap[registerList[i].CurrencyUnit]
- v.ProductIds = registerList[i].ProductIds
- respList = append(respList, v)
- }
- }()
- // 开票到款金额合计(换算后)
- // 开票到款金额合计(换算后)
- var invoiceTotal, paymentTotal, contractAmount float64
- wg.Add(1)
- go func() {
- defer wg.Done()
- if len(summaryIds) == 0 {
- return
- }
- amountTotalCond := `a.id IN ?`
- amountTotalPars := make([]interface{}, 0)
- amountTotalPars = append(amountTotalPars, summaryIds)
- invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
- if e != nil {
- totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
- return
- }
- invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
- paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
- if e != nil {
- totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
- return
- }
- paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
- }()
- // 分币种金额统计
- invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
- paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
- contractAmountCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
- wg.Add(1)
- go func() {
- defer wg.Done()
- currencyOB := new(fms.CurrencyUnit)
- currencyCond := `enable = 1`
- currencyPars := make([]interface{}, 0)
- currencyList, e := currencyOB.List(currencyCond, currencyPars)
- if e != nil {
- totalGroupErr = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
- return
- }
- unitMap := make(map[string]string)
- for i := range currencyList {
- unitMap[currencyList[i].Code] = currencyList[i].UnitName
- invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
- Name: currencyList[i].Name,
- UnitName: currencyList[i].UnitName,
- Code: currencyList[i].Code,
- FlagImg: currencyList[i].FlagImg,
- })
- paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
- Name: currencyList[i].Name,
- UnitName: currencyList[i].UnitName,
- Code: currencyList[i].Code,
- FlagImg: currencyList[i].FlagImg,
- })
- contractAmountCurrencyTotals = append(contractAmountCurrencyTotals, &fms.InvoiceListCurrencyTotal{
- Name: currencyList[i].Name,
- UnitName: currencyList[i].UnitName,
- Code: currencyList[i].Code,
- FlagImg: currencyList[i].FlagImg,
- })
- }
- if len(summaryIds) == 0 {
- return
- }
- totalGroupCond := `a.id IN ?`
- totalGroupPars := make([]interface{}, 0)
- totalGroupPars = append(totalGroupPars, summaryIds)
- invoiceSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 1)
- if e != nil {
- totalGroupErr = fmt.Errorf("获取汇总货币合计开票金额失败, Err: %s", e.Error())
- return
- }
- paymentSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 2)
- if e != nil {
- totalGroupErr = fmt.Errorf("获取汇总货币合计到款金额失败, Err: %s", e.Error())
- return
- }
- invoiceSumMap := make(map[string]float64)
- paymentSumMap := make(map[string]float64)
- contractSumMap := make(map[string]float64)
- for i := range invoiceSumGroup {
- invoiceSumMap[invoiceSumGroup[i].CurrencyUnit] = invoiceSumGroup[i].OriginAmountTotal
- continue
- }
- for i := range paymentSumGroup {
- paymentSumMap[paymentSumGroup[i].CurrencyUnit] = paymentSumGroup[i].OriginAmountTotal
- continue
- }
- for i := range invoiceCurrencyTotals {
- a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
- invoiceCurrencyTotals[i].Amount = a
- }
- for i := range paymentCurrencyTotals {
- a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
- paymentCurrencyTotals[i].Amount = a
- }
- contractRegisters, err := fms.GetContractInfoByRegisterIds(queryRegisterIds)
- if err != nil {
- resp.FailMsg("查询合同信息失败", "查询合同信息失败, Err: "+err.Error(), c)
- return
- }
- for _, v := range contractRegisters {
- if v.RMBRate == 0.0 {
- v.RMBRate = 1
- }
- amount, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.ContractAmount/v.RMBRate), 64)
- contractAmount += amount
- contractSumMap[v.CurrencyUnit] = v.ContractAmount
- }
- for _, v := range contractAmountCurrencyTotals {
- v.Amount = contractSumMap[v.Code] - invoiceSumMap[v.Code]
- }
- }()
- wg.Wait()
- if listErr != nil {
- resp.FailMsg("获取失败", listErr.Error(), c)
- return
- }
- if totalErr != nil {
- resp.FailMsg("获取失败", totalErr.Error(), c)
- return
- }
- if totalGroupErr != nil {
- resp.FailMsg("获取失败", totalGroupErr.Error(), c)
- return
- }
- results.DataList = respList
- results.InvoiceTotal = invoiceTotal
- results.PaymentTotal = paymentTotal
- results.NotInvoiceTotal = contractAmount - invoiceTotal
- results.InvoiceCurrencyTotal = invoiceCurrencyTotals
- results.PaymentCurrencyTotal = paymentCurrencyTotals
- results.NotInvoiceCurrencyTotal = contractAmountCurrencyTotals
- }
- // 是否导出
- if req.IsExport == 1 {
- ExportNotPaymentCensusList(c, results)
- return
- }
- page.SetTotal(total)
- baseData := new(base.BaseData)
- baseData.SetPage(page)
- baseData.SetList(results)
- resp.OkData("获取成功", baseData, c)
- }
- // ExportNotInvoiceCensusList 导出未开票统计列表
- func ExportNotInvoiceCensusList(c *gin.Context, results *fms.NotInvoicePaymentCensusResp) {
- list := results.DataList
- if len(list) == 0 {
- resp.Fail("列表数据为空", c)
- return
- }
- // 生成Excel文件
- xlsxFile := xlsx.NewFile()
- style := xlsx.NewStyle()
- alignment := xlsx.Alignment{
- Horizontal: "center",
- Vertical: "center",
- WrapText: true,
- }
- style.Alignment = alignment
- style.ApplyAlignment = true
- sheet, err := xlsxFile.AddSheet("未开票统计")
- if err != nil {
- resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
- return
- }
- _ = sheet.SetColWidth(1, 1, 30)
- _ = sheet.SetColWidth(3, 3, 30)
- // 前三行-开票金额合计
- rowA := sheet.AddRow()
- cellAA := rowA.AddCell()
- cellAA.SetString(fmt.Sprintf("未开票合计金额(换算后):%.2f(元)", results.NotInvoiceTotal))
- rowBData := "未开票金额:"
- for _, v := range results.NotInvoiceCurrencyTotal {
- rowBData += fmt.Sprintf("%s%.2f(%s) ", v.Name, v.Amount, v.UnitName)
- }
- rowB := sheet.AddRow()
- rowB.AddCell().SetString(rowBData)
- sheet.AddRow()
- // 表头, 套餐动态获取
- rowTitle := []string{"序号", "客户名称", "是否新客户", "合同有效期", "开票日", "开票金额", "到款日", "到款金额", "付款方式", "销售",
- "销售组别", "销售类型"}
- serviceTempCond := ``
- serviceTempPars := make([]interface{}, 0)
- serviceTempOB := new(fms.ContractServiceTemplate)
- serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
- if e != nil {
- resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
- return
- }
- serviceTempListMap := make(map[int]*fms.ContractServiceTemplate)
- var serviceTempShow [] *fms.ContractServiceTemplate
- for i := range serviceTempList {
- serviceTempListMap[serviceTempList[i].ServiceTemplateId] = serviceTempList[i]
- }
- for i := range serviceTempList {
- if serviceTempList[i].ProductId == 1 {
- rowTitle = append(rowTitle, serviceTempList[i].Title)
- serviceTempShow = append(serviceTempShow, serviceTempList[i])
- }else if serviceTempList[i].ProductId == 2{
- if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "权益大套餐" || (serviceTempList[i].Title == "策略" && serviceTempListMap[serviceTempList[i].Pid].Pid==0){
- }else if serviceTempList[i].Title == "医药" || serviceTempList[i].Title == "消费" || serviceTempList[i].Title == "科技" || serviceTempList[i].Title == "智造" {
- }else {
- serviceTempShow = append(serviceTempShow, serviceTempList[i])
- }
- if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "45万" || serviceTempList[i].Title == "70万" {
- continue
- }
- if serviceTempList[i].Pid > 0 && serviceTempListMap[serviceTempList[i].Pid].Pid > 0 {
- continue
- }
- rowTitle = append(rowTitle, serviceTempList[i].Title)
- }
- }
- hi := 0
- insertHi := 0
- titleRow := sheet.AddRow()
- for i := range rowTitle {
- v := titleRow.AddCell()
- v.SetString(rowTitle[i])
- v.SetStyle(style)
- if rowTitle[i] != "权益大套餐" && rowTitle[i] != "医药" && rowTitle[i] != "消费" && rowTitle[i] != "科技" && rowTitle[i] != "智造" {
- v.VMerge = 1
- hi ++
- }else{
- if insertHi == 0 {
- insertHi = hi
- }
- v.HMerge = 1
- titleRow.AddCell().SetString("")
- }
- }
- // 新增一行放主观和客观
- specialRow := sheet.AddRow()
- for i:=0; i< hi; i++ {
- if i == insertHi {
- v1 := specialRow.AddCell()
- v1.SetString("45万")
- v1.SetStyle(style)
- v1 = specialRow.AddCell()
- v1.SetString("70万")
- v1.SetStyle(style)
- for j:=0; j<8;j++ {
- if j%2 ==0 {
- v1 = specialRow.AddCell()
- v1.SetString("主观")
- v1.SetStyle(style)
- }else{
- v1 = specialRow.AddCell()
- v1.SetString("客观")
- v1.SetStyle(style)
- }
- }
- }else{
- v := specialRow.AddCell()
- v.SetString("")
- }
- }
- newCompanyMap := map[int]string{0: "否", 1: "是"}
- sellerTypeMap := map[int]string{1:"FICC销售", 2:"权益销售"}
- for k, v := range list {
- dataRow := sheet.AddRow()
- // 前四个单元格根据每行开票到款条数向下合并
- l := len(v.InvoicePaymentList)
- mergeRowNum := l - 1
- // 序号
- sortNum := k + 1
- colA := dataRow.AddCell()
- colA.VMerge = mergeRowNum
- colA.SetString(fmt.Sprint(sortNum))
- // 客户名称
- colB := dataRow.AddCell()
- colB.VMerge = mergeRowNum
- colB.SetString(v.CompanyName)
- // 是否新客户
- colC := dataRow.AddCell()
- colC.VMerge = mergeRowNum
- colC.SetString(newCompanyMap[v.NewCompany])
- // 合同有效期
- colD := dataRow.AddCell()
- colD.VMerge = mergeRowNum
- colD.SetString(fmt.Sprint(v.StartDate, "至", v.EndDate))
- // 开票到款信息
- for k2, v2 := range v.InvoicePaymentList {
- rowData := []string{
- v2.InvoiceDate, // 开票日
- fmt.Sprint(v2.InvoiceAmount), // 开票金额
- v2.PaymentDate, // 到款日
- fmt.Sprint(v2.PaymentAmount), // 到款金额
- fms.ContractPaymentPayTypeNameMap[v2.PayType], // 付款方式
- v2.SellerName, // 销售
- v2.SellerGroupName, // 组别
- sellerTypeMap[v2.SellerType], // 销售类型
- }
- // 套餐金额信息
- serviceTempShowAmount := make(map[int]string)
- for i := range serviceTempShow {
- for s2 := range v2.ServiceAmountList {
- item := v2.ServiceAmountList[s2]
- if item.ServiceTemplateId == serviceTempShow[i].ServiceTemplateId {
- serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount)
- break
- }else if serviceTempShow[i].Pid == item.ServiceTemplateId {
- serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount/2)
- break
- }
- }
- }
- for i := range serviceTempShow {
- sa := ""
- if am, ok := serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId]; ok {
- sa = am
- }
- rowData = append(rowData, sa)
- }
- // 首行开票到款
- if k2 == 0 {
- for i := range rowData {
- dataRow.AddCell().SetString(rowData[i])
- }
- continue
- }
- // 其他行开票到款, 加四列空的单元格用于合并
- dataRowExtra := sheet.AddRow()
- for i := 0; i < 4; i++ {
- dataRowExtra.AddCell()
- }
- for i := range rowData {
- dataRowExtra.AddCell().SetString(rowData[i])
- }
- }
- }
- // 输出文件
- var buffer bytes.Buffer
- _ = xlsxFile.Write(&buffer)
- content := bytes.NewReader(buffer.Bytes())
- randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
- fileName := "商品到款统计_" + randStr + ".xlsx"
- c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
- c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
- http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
- }
- // ExportNotPaymentCensusList 导出开票未到款统计列表
- func ExportNotPaymentCensusList(c *gin.Context, results *fms.NotInvoicePaymentCensusResp) {
- list := results.DataList
- if len(list) == 0 {
- resp.Fail("列表数据为空", c)
- return
- }
- // 生成Excel文件
- xlsxFile := xlsx.NewFile()
- style := xlsx.NewStyle()
- alignment := xlsx.Alignment{
- Horizontal: "center",
- Vertical: "center",
- WrapText: true,
- }
- style.Alignment = alignment
- style.ApplyAlignment = true
- sheet, err := xlsxFile.AddSheet("开票未到款统计")
- if err != nil {
- resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
- return
- }
- _ = sheet.SetColWidth(1, 1, 30)
- _ = sheet.SetColWidth(3, 3, 30)
- // 前三行-开票金额合计
- rowA := sheet.AddRow()
- cellAA := rowA.AddCell()
- cellAA.SetString(fmt.Sprintf("开票未到款合计金额(换算后):%.2f(元)", results.InvoiceTotal))
- rowBData := "已开票金额:"
- for _, v := range results.InvoiceCurrencyTotal {
- rowBData += fmt.Sprintf("%s%.2f(%s) ", v.Name, v.Amount, v.UnitName)
- }
- rowB := sheet.AddRow()
- rowB.AddCell().SetString(rowBData)
- sheet.AddRow()
- // 表头, 套餐动态获取
- rowTitle := []string{"序号", "客户名称", "是否新客户", "合同有效期", "开票日", "开票金额", "到款日", "到款金额", "付款方式", "销售",
- "销售组别", "销售类型"}
- serviceTempCond := ``
- serviceTempPars := make([]interface{}, 0)
- serviceTempOB := new(fms.ContractServiceTemplate)
- serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
- if e != nil {
- resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
- return
- }
- serviceTempListMap := make(map[int]*fms.ContractServiceTemplate)
- var serviceTempShow [] *fms.ContractServiceTemplate
- for i := range serviceTempList {
- serviceTempListMap[serviceTempList[i].ServiceTemplateId] = serviceTempList[i]
- }
- for i := range serviceTempList {
- if serviceTempList[i].ProductId == 1 {
- rowTitle = append(rowTitle, serviceTempList[i].Title)
- serviceTempShow = append(serviceTempShow, serviceTempList[i])
- }else if serviceTempList[i].ProductId == 2{
- if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "权益大套餐" || (serviceTempList[i].Title == "策略" && serviceTempListMap[serviceTempList[i].Pid].Pid==0){
- }else if serviceTempList[i].Title == "医药" || serviceTempList[i].Title == "消费" || serviceTempList[i].Title == "科技" || serviceTempList[i].Title == "智造" {
- }else {
- serviceTempShow = append(serviceTempShow, serviceTempList[i])
- }
- if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "45万" || serviceTempList[i].Title == "70万" {
- continue
- }
- if serviceTempList[i].Pid > 0 && serviceTempListMap[serviceTempList[i].Pid].Pid > 0 {
- continue
- }
- rowTitle = append(rowTitle, serviceTempList[i].Title)
- }
- }
- hi := 0
- insertHi := 0
- titleRow := sheet.AddRow()
- for i := range rowTitle {
- v := titleRow.AddCell()
- v.SetString(rowTitle[i])
- v.SetStyle(style)
- if rowTitle[i] != "权益大套餐" && rowTitle[i] != "医药" && rowTitle[i] != "消费" && rowTitle[i] != "科技" && rowTitle[i] != "智造" {
- v.VMerge = 1
- hi ++
- }else{
- if insertHi == 0 {
- insertHi = hi
- }
- v.HMerge = 1
- titleRow.AddCell().SetString("")
- }
- }
- // 新增一行放主观和客观
- specialRow := sheet.AddRow()
- for i:=0; i< hi; i++ {
- if i == insertHi {
- v1 := specialRow.AddCell()
- v1.SetString("45万")
- v1.SetStyle(style)
- v1 = specialRow.AddCell()
- v1.SetString("70万")
- v1.SetStyle(style)
- for j:=0; j<8;j++ {
- if j%2 ==0 {
- v1 = specialRow.AddCell()
- v1.SetString("主观")
- v1.SetStyle(style)
- }else{
- v1 = specialRow.AddCell()
- v1.SetString("客观")
- v1.SetStyle(style)
- }
- }
- }else{
- v := specialRow.AddCell()
- v.SetString("")
- }
- }
- newCompanyMap := map[int]string{0: "否", 1: "是"}
- sellerTypeMap := map[int]string{1:"FICC销售", 2:"权益销售"}
- for k, v := range list {
- dataRow := sheet.AddRow()
- // 前四个单元格根据每行开票到款条数向下合并
- l := len(v.InvoicePaymentList)
- mergeRowNum := l - 1
- // 序号
- sortNum := k + 1
- colA := dataRow.AddCell()
- colA.VMerge = mergeRowNum
- colA.SetString(fmt.Sprint(sortNum))
- // 客户名称
- colB := dataRow.AddCell()
- colB.VMerge = mergeRowNum
- colB.SetString(v.CompanyName)
- // 是否新客户
- colC := dataRow.AddCell()
- colC.VMerge = mergeRowNum
- colC.SetString(newCompanyMap[v.NewCompany])
- // 合同有效期
- colD := dataRow.AddCell()
- colD.VMerge = mergeRowNum
- colD.SetString(fmt.Sprint(v.StartDate, "至", v.EndDate))
- // 开票到款信息
- for k2, v2 := range v.InvoicePaymentList {
- rowData := []string{
- v2.InvoiceDate, // 开票日
- fmt.Sprint(v2.InvoiceAmount), // 开票金额
- v2.PaymentDate, // 到款日
- fmt.Sprint(v2.PaymentAmount), // 到款金额
- fms.ContractPaymentPayTypeNameMap[v2.PayType], // 付款方式
- v2.SellerName, // 销售
- v2.SellerGroupName, // 组别
- sellerTypeMap[v2.SellerType], // 销售类型
- }
- // 套餐金额信息
- serviceTempShowAmount := make(map[int]string)
- for i := range serviceTempShow {
- for s2 := range v2.ServiceAmountList {
- item := v2.ServiceAmountList[s2]
- if item.ServiceTemplateId == serviceTempShow[i].ServiceTemplateId {
- serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount)
- break
- }else if serviceTempShow[i].Pid == item.ServiceTemplateId {
- serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount/2)
- break
- }
- }
- }
- for i := range serviceTempShow {
- sa := ""
- if am, ok := serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId]; ok {
- sa = am
- }
- rowData = append(rowData, sa)
- }
- // 首行开票到款
- if k2 == 0 {
- for i := range rowData {
- dataRow.AddCell().SetString(rowData[i])
- }
- continue
- }
- // 其他行开票到款, 加四列空的单元格用于合并
- dataRowExtra := sheet.AddRow()
- for i := 0; i < 4; i++ {
- dataRowExtra.AddCell()
- }
- for i := range rowData {
- dataRowExtra.AddCell().SetString(rowData[i])
- }
- }
- }
- // 输出文件
- var buffer bytes.Buffer
- _ = xlsxFile.Write(&buffer)
- content := bytes.NewReader(buffer.Bytes())
- randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
- fileName := "商品到款统计_" + randStr + ".xlsx"
- c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
- c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
- http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
- }
|