123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986 |
- package contract
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/go-playground/validator/v10"
- "github.com/shopspring/decimal"
- "github.com/tealeg/xlsx"
- "hongze/fms_api/controller/resp"
- "hongze/fms_api/global"
- "hongze/fms_api/models/base"
- "hongze/fms_api/models/crm"
- "hongze/fms_api/models/fms"
- "hongze/fms_api/models/system"
- "hongze/fms_api/services/alarm_msg"
- crmService "hongze/fms_api/services/crm"
- fmsService "hongze/fms_api/services/fms"
- "hongze/fms_api/utils"
- "net/http"
- "os"
- "path"
- "strconv"
- "strings"
- "time"
- )
- // RegisterController 合同登记
- type RegisterController struct{}
- // List
- // @Title 合同登记列表
- // @Description 合同登记列表
- // @Param Keyword query string false "关键词"
- // @Param StartDate query string false "合同开始日期"
- // @Param EndDate query string false "合同结束日期"
- // @Param ServiceType query int false "套餐类型"
- // @Param ContractType query int false "合同类型"
- // @Param RegisterStatus query int false "登记状态"
- // @Success 200 {object} fms.ContractRegisterItem
- // @router /contract/register/list [get]
- func (rg *RegisterController) List(c *gin.Context) {
- var req fms.ContractRegisterListReq
- 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 (company_name LIKE ? OR contract_code LIKE ? OR seller_name LIKE ? OR actual_company_name LIKE ?)`
- pars = append(pars, kw, kw, kw, kw)
- }
- if req.StartDate != "" && req.EndDate != "" {
- st := fmt.Sprint(req.StartDate, " 00:00:00")
- ed := fmt.Sprint(req.EndDate, " 23:59:59")
- cond += ` AND (create_time BETWEEN ? AND ?)`
- pars = append(pars, st, ed)
- }
- if req.ContractType != 0 {
- cond += ` AND contract_type = ?`
- pars = append(pars, req.ContractType)
- }
- if req.RegisterStatus != 0 {
- cond += ` AND register_status = ?`
- pars = append(pars, req.RegisterStatus)
- }
- // 套餐筛选
- 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 contract_register_id IN ?`
- pars = append(pars, registerIds)
- } else {
- cond += ` AND 1 = 2`
- }
- }
- page := new(base.Page)
- page.SetPageSize(req.PageSize)
- page.SetCurrent(req.Current)
- page.AddOrderItem(base.OrderItem{Column: "create_time", Asc: false})
- total, list, e := fms.GetContractRegisterItemPageList(page, cond, pars)
- if e != nil {
- resp.FailMsg("获取失败", "获取合同登记列表失败, Err: "+e.Error(), c)
- return
- }
- registerIds := make([]int, 0)
- for i := range list {
- registerIds = append(registerIds, list[i].ContractRegisterId)
- }
- serviceMap := make(map[int]string, 0)
- invoiceMap := make(map[int][]*fms.ContractInvoiceItem, 0)
- paymentMap := make(map[int][]*fms.ContractInvoiceItem, 0)
- if len(registerIds) > 0 {
- // 获取服务套餐
- servicesNameList, e := fms.GetContractRegisterServicesNameByRegisterIds(registerIds)
- if e != nil {
- resp.FailMsg("获取失败", "获取套餐拼接字符串失败, Err: "+e.Error(), c)
- return
- }
- for i := range servicesNameList {
- serviceMap[servicesNameList[i].ContractRegisterId] = servicesNameList[i].ServicesName
- }
- // 获取开票/到款列表
- invoiceCond := `contract_register_id IN ?`
- invoicePars := make([]interface{}, 0)
- invoicePars = append(invoicePars, registerIds)
- invoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
- if e != nil {
- resp.FailMsg("获取失败", "获取开票/到款列表失败, Err: "+e.Error(), c)
- return
- }
- for i := range invoiceList {
- if invoiceMap[invoiceList[i].ContractRegisterId] == nil {
- invoiceMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoiceItem, 0)
- }
- if paymentMap[invoiceList[i].ContractRegisterId] == nil {
- paymentMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoiceItem, 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])
- }
- }
- }
- respList := make([]*fms.ContractRegisterList, 0)
- for i := range list {
- v := new(fms.ContractRegisterList)
- v.ContractRegisterItem = list[i]
- v.ServicesName = serviceMap[list[i].ContractRegisterId]
- v.InvoiceList = invoiceMap[list[i].ContractRegisterId]
- v.PaymentList = paymentMap[list[i].ContractRegisterId]
- respList = append(respList, v)
- }
- page.SetTotal(total)
- baseData := new(base.BaseData)
- baseData.SetPage(page)
- baseData.SetList(respList)
- resp.OkData("获取成功", baseData, c)
- }
- // Add
- // @Title 新增合同登记
- // @Description 新增合同登记
- // @Param request body fms.ContractRegisterAddReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /contract/register/add [post]
- func (rg *RegisterController) Add(c *gin.Context) {
- req := new(fms.ContractRegisterAddReq)
- err := c.ShouldBind(&req)
- if err != nil {
- errs, ok := err.(validator.ValidationErrors)
- if !ok {
- resp.FailData("参数解析失败", "Err:"+err.Error(), c)
- return
- }
- resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
- return
- }
- claims, _ := c.Get("adminInfo")
- adminInfo := claims.(*system.SysAdmin)
- // 日期校验
- startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
- if e != nil {
- resp.FailMsg("合同开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
- return
- }
- endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
- if e != nil {
- resp.FailMsg("合同结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
- return
- }
- signDate, _ := time.ParseInLocation(utils.FormatDate, "0000-00-00", time.Local)
- if req.SignDate != "" {
- signDateTime, e := time.ParseInLocation(utils.FormatDate, req.SignDate, time.Local)
- if e != nil {
- resp.FailMsg("合同签订日期格式有误", "合同签订日期格式有误, Err: "+e.Error(), c)
- return
- }
- signDate = signDateTime
- }
- // 是否存在相同合同编号的登记
- ob := new(fms.ContractRegister)
- existCond := `contract_code = ?`
- existPars := make([]interface{}, 0)
- existPars = append(existPars, req.ContractCode)
- exist, e := ob.FetchByCondition(existCond, existPars)
- if e != nil && e != utils.ErrNoRow {
- resp.FailMsg("操作失败", "获取相同登记号失败, Err: "+e.Error(), c)
- return
- }
- if exist != nil && exist.ContractRegisterId > 0 {
- resp.Fail("合同编号已存在", c)
- return
- }
- nowTime := time.Now().Local()
- ob.ContractCode = req.ContractCode
- ob.RelateContractCode = req.RelateContractCode
- ob.CrmContractId = req.CrmContractId
- ob.ContractSource = req.ContractSource
- ob.CompanyName = req.CompanyName
- ob.ActualCompanyName = req.ActualCompanyName
- ob.ProductId = req.ProductId
- ob.SellerId = req.SellerId
- ob.SellerName = req.SellerName
- ob.ContractType = req.ContractType
- ob.ContractAmount = req.ContractAmount
- ob.StartDate = startDate
- ob.EndDate = endDate
- ob.SignDate = signDate
- ob.AgreedPayTime = req.AgreedPayTime
- ob.ContractStatus = req.ContractStatus
- ob.RegisterStatus = fms.ContractRegisterStatusIng
- ob.Remark = req.Remark
- ob.ServiceRemark = req.ServiceRemark
- ob.HasPayment = req.HasPayment
- ob.NewCompany = req.NewCompany
- ob.Set()
- // 存在代付的直接完成登记, 且不允许进行开票/到款登记
- if req.HasPayment == 1 {
- ob.RegisterStatus = fms.ContractRegisterStatusComplete
- }
- // 套餐信息
- serviceList, e := fmsService.HandleContractServiceAndDetail(req.ProductId, req.Services, true)
- if e != nil {
- resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
- return
- }
- // 新增合同及套餐
- if e = fms.CreateContractRegisterAndServices(ob, serviceList); e != nil {
- resp.FailMsg("操作失败", "新增合同及套餐失败, Err: "+e.Error(), c)
- return
- }
- // 操作日志
- go func() {
- opData := ""
- opDataByte, e := json.Marshal(req)
- if e != nil {
- return
- }
- opData = string(opDataByte)
- logItem := new(fms.ContractRegisterLog)
- logItem.ContractRegisterId = ob.ContractRegisterId
- logItem.AdminId = int(adminInfo.AdminId)
- logItem.AdminName = adminInfo.RealName
- logItem.OpData = opData
- logItem.OpType = fms.ContractRegisterOpTypeSave
- logItem.CreateTime = nowTime
- logItem.Remark = req.Remark
- if e = logItem.Create(); e != nil {
- return
- }
- }()
- resp.Ok("操作成功", c)
- }
- // Edit
- // @Title 编辑合同登记
- // @Description 编辑合同登记
- // @Param request body fms.ContractRegisterEditReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /contract/register/edit [post]
- func (rg *RegisterController) Edit(c *gin.Context) {
- req := new(fms.ContractRegisterEditReq)
- err := c.ShouldBind(&req)
- if err != nil {
- errs, ok := err.(validator.ValidationErrors)
- if !ok {
- resp.FailData("参数解析失败", "Err:"+err.Error(), c)
- return
- }
- resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
- return
- }
- claims, _ := c.Get("adminInfo")
- adminInfo := claims.(*system.SysAdmin)
- // 日期校验
- startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
- if e != nil {
- resp.FailMsg("合同开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
- return
- }
- endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
- if e != nil {
- resp.FailMsg("合同结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
- return
- }
- signDate, _ := time.ParseInLocation(utils.FormatDate, "0000-00-00", time.Local)
- if req.SignDate != "" {
- signDateTime, e := time.ParseInLocation(utils.FormatDate, req.SignDate, time.Local)
- if e != nil {
- resp.FailMsg("合同签订日期格式有误", "合同签订日期格式有误, Err: "+e.Error(), c)
- return
- }
- signDate = signDateTime
- }
- ob := new(fms.ContractRegister)
- item, e := ob.Fetch(req.ContractRegisterId)
- if e != nil {
- if e == utils.ErrNoRow {
- resp.Fail("登记记录不存在或已被删除", c)
- return
- }
- resp.FailMsg("操作失败", "获取合同登记信息失败, Err:"+e.Error(), c)
- return
- }
- // 是否存在相同合同编号的登记
- existCond := `contract_code = ?`
- existPars := make([]interface{}, 0)
- existPars = append(existPars, req.ContractCode)
- exist, e := ob.FetchByCondition(existCond, existPars)
- if e != nil && e != utils.ErrNoRow {
- resp.FailMsg("操作失败", "获取相同登记号失败, Err: "+e.Error(), c)
- return
- }
- if exist != nil && exist.ContractRegisterId > 0 && exist.ContractRegisterId != item.ContractRegisterId {
- resp.Fail("合同编号已存在", c)
- return
- }
- originHasPayment := item.HasPayment
- updateCols := []string{
- "ContractCode", "RelateContractCode", "CrmContractId", "ContractSource", "CompanyName", "ActualCompanyName",
- "SellerId", "SellerName", "ContractType", "ContractAmount", "StartDate", "EndDate", "SignDate", "AgreedPayTime",
- "ContractStatus", "RegisterStatus", "Remark", "ServiceRemark", "HasPayment", "NewCompany", "ModifyTime",
- }
- nowTime := time.Now().Local()
- item.ContractCode = req.ContractCode
- item.RelateContractCode = req.RelateContractCode
- item.CrmContractId = req.CrmContractId
- item.ContractSource = req.ContractSource
- item.CompanyName = req.CompanyName
- item.ActualCompanyName = req.ActualCompanyName
- item.SellerId = req.SellerId
- item.SellerName = req.SellerName
- item.ContractType = req.ContractType
- item.ContractAmount = req.ContractAmount
- item.StartDate = startDate
- item.EndDate = endDate
- item.SignDate = signDate
- item.AgreedPayTime = req.AgreedPayTime
- item.ContractStatus = req.ContractStatus
- item.RegisterStatus = fms.ContractRegisterStatusIng
- item.Remark = req.Remark
- item.ServiceRemark = req.ServiceRemark
- item.HasPayment = req.HasPayment
- item.NewCompany = req.NewCompany
- item.ModifyTime = nowTime
- // 存在代付的直接完成登记, 且不允许进行开票/到款登记
- if req.HasPayment == 1 {
- item.RegisterStatus = fms.ContractRegisterStatusComplete
- updateCols = append(updateCols, "RegisterStatus")
- }
- // 套餐信息
- serviceList, e := fmsService.HandleContractServiceAndDetail(req.ProductId, req.Services, true)
- if e != nil {
- resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
- return
- }
- // 更新合同及套餐
- if e = fms.UpdateContractRegisterAndServices(item, updateCols, serviceList); e != nil {
- resp.FailMsg("操作失败", "更新合同及套餐失败, Err: "+e.Error(), c)
- return
- }
- // 若从无代付修改为有代付, 则删除无代付期间新增的所有开票/到款登记(此情况并不经常出现, 但是可能会存在这种操作)
- if originHasPayment == 0 && req.HasPayment == 1 {
- go func() {
- if e = fms.DeleteContractInvoicesByRegisterId(item.ContractRegisterId); e != nil {
- alarm_msg.SendAlarmMsg("无代付修改为有代付, 删除开票到款记录失败, ErrMsg: "+err.Error(), 3)
- }
- }()
- }
- // 校验金额-是否修改状态
- go fmsService.CheckContractRegisterAmount(item.ContractRegisterId)
- // 操作日志
- go func() {
- opData := ""
- opDataByte, e := json.Marshal(req)
- if e != nil {
- return
- }
- opData = string(opDataByte)
- logItem := new(fms.ContractRegisterLog)
- logItem.ContractRegisterId = item.ContractRegisterId
- logItem.AdminId = int(adminInfo.AdminId)
- logItem.AdminName = adminInfo.RealName
- logItem.OpData = opData
- logItem.OpType = fms.ContractRegisterOpTypeEdit
- logItem.CreateTime = nowTime
- logItem.Remark = req.Remark
- if e = logItem.Create(); e != nil {
- return
- }
- }()
- resp.Ok("操作成功", c)
- }
- // Del
- // @Title 删除合同登记
- // @Description 删除合同登记
- // @Param request body fms.ContractRegisterDelReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /contract/register/del [post]
- func (rg *RegisterController) Del(c *gin.Context) {
- req := new(fms.ContractRegisterDelReq)
- err := c.ShouldBind(&req)
- if err != nil {
- errs, ok := err.(validator.ValidationErrors)
- if !ok {
- resp.FailData("参数解析失败", "Err:"+err.Error(), c)
- return
- }
- resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
- return
- }
- claims, _ := c.Get("adminInfo")
- adminInfo := claims.(*system.SysAdmin)
- ob := new(fms.ContractRegister)
- item, e := ob.Fetch(req.ContractRegisterId)
- if e != nil {
- if e == utils.ErrNoRow {
- resp.Fail("合同登记不存在或已被删除", c)
- return
- }
- resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
- return
- }
- nowTime := time.Now().Local()
- item.IsDeleted = 1
- item.ModifyTime = nowTime
- updateCols := []string{"IsDeleted", "ModifyTime"}
- if e = item.Update(updateCols); e != nil {
- resp.FailMsg("操作失败", "更新合同登记失败, Err:"+e.Error(), c)
- return
- }
- // 删除对应的开票到款登记
- go func() {
- if e = fms.DeleteContractInvoicesByRegisterId(item.ContractRegisterId); e != nil {
- alarm_msg.SendAlarmMsg("删除合同登记后, 删除开票到款记录失败, ErrMsg: "+err.Error(), 3)
- }
- }()
- // 操作日志
- go func() {
- opData := ""
- opDataByte, e := json.Marshal(req)
- if e != nil {
- return
- }
- opData = string(opDataByte)
- logItem := new(fms.ContractRegisterLog)
- logItem.ContractRegisterId = req.ContractRegisterId
- logItem.AdminId = int(adminInfo.AdminId)
- logItem.AdminName = adminInfo.RealName
- logItem.OpData = opData
- logItem.OpType = fms.ContractRegisterOpTypeDel
- logItem.CreateTime = nowTime
- if e = logItem.Create(); e != nil {
- return
- }
- }()
- resp.Ok("操作成功", c)
- }
- // Detail
- // @Title 合同登记详情
- // @Description 合同登记详情
- // @Param ContractRegisterId query int false "合同登记ID"
- // @Success 200 {object} fms.ContractRegisterDetail
- // @router /contract/register/detail [get]
- func (rg *RegisterController) Detail(c *gin.Context) {
- var req fms.ContractRegisterDetailReq
- 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
- }
- result := new(fms.ContractRegisterDetail)
- // 合同登记信息
- item, e := fms.GetContractRegisterItemById(req.ContractRegisterId)
- if e != nil {
- resp.FailData("获取失败", "获取合同登记详情失败, Err:"+e.Error(), c)
- return
- }
- result.ContractRegisterItem = item
- // 套餐信息
- serviceList, e := fmsService.GetContractServiceAndDetail(req.ContractRegisterId)
- if e != nil {
- resp.FailData("获取失败", "获取合同套餐信息失败, Err: "+e.Error(), c)
- return
- }
- result.ServiceList = serviceList
- // 开票/到款信息
- invoiceCond := `contract_register_id = ?`
- invoicePars := make([]interface{}, 0)
- invoicePars = append(invoicePars, req.ContractRegisterId)
- invoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
- if e != nil {
- resp.FailData("获取失败", "获取合同开票/到款信息失败, Err: "+e.Error(), c)
- return
- }
- result.InvoiceList = make([]*fms.ContractInvoiceItem, 0)
- result.PaymentList = make([]*fms.ContractInvoiceItem, 0)
- for i := range invoiceList {
- if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
- result.InvoiceList = append(result.InvoiceList, invoiceList[i])
- continue
- }
- if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
- result.PaymentList = append(result.PaymentList, invoiceList[i])
- }
- }
- // 合同登记进度
- logCond := `contract_register_id = ?`
- logPars := make([]interface{}, 0)
- logPars = append(logPars, req.ContractRegisterId)
- logList, e := fms.GetContractRegisterLogItemList(logCond, logPars)
- if e != nil {
- resp.FailData("获取失败", "获取合同登记进度失败, Err: "+e.Error(), c)
- return
- }
- result.Logs = logList
- resp.OkData("获取成功", result, c)
- }
- // UpdateStatus
- // @Title 修改合同状态
- // @Description 修改合同状态
- // @Param request body fms.ContractRegisterUpdateStatusReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /contract/register/update_status [post]
- func (rg *RegisterController) UpdateStatus(c *gin.Context) {
- req := new(fms.ContractRegisterUpdateStatusReq)
- err := c.ShouldBind(&req)
- if err != nil {
- errs, ok := err.(validator.ValidationErrors)
- if !ok {
- resp.FailData("参数解析失败", "Err:"+err.Error(), c)
- return
- }
- resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
- return
- }
- claims, _ := c.Get("adminInfo")
- adminInfo := claims.(*system.SysAdmin)
- ob := new(fms.ContractRegister)
- item, e := ob.Fetch(req.ContractRegisterId)
- if e != nil {
- if e == utils.ErrNoRow {
- resp.Fail("合同登记不存在或已被删除", c)
- return
- }
- resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
- return
- }
- nowTime := time.Now().Local()
- item.ContractStatus = req.ContractStatus
- item.ModifyTime = nowTime
- updateCols := []string{"ContractStatus", "ModifyTime"}
- if e = item.Update(updateCols); e != nil {
- resp.FailMsg("操作失败", "更新合同登记失败, Err:"+e.Error(), c)
- return
- }
- // 校验金额-是否修改状态
- go fmsService.CheckContractRegisterAmount(req.ContractRegisterId)
- // 操作日志
- go func() {
- opData := ""
- opDataByte, e := json.Marshal(req)
- if e != nil {
- return
- }
- opData = string(opDataByte)
- logItem := new(fms.ContractRegisterLog)
- logItem.ContractRegisterId = req.ContractRegisterId
- logItem.AdminId = int(adminInfo.AdminId)
- logItem.AdminName = adminInfo.RealName
- logItem.OpData = opData
- logItem.OpType = fms.ContractRegisterOpTypeStatus
- logItem.CreateTime = nowTime
- if e = logItem.Create(); e != nil {
- return
- }
- }()
- resp.Ok("操作成功", c)
- }
- // Invoice
- // @Title 开票/到款登记
- // @Description 开票/到款登记
- // @Param request body fms.ContractInvoiceSaveReq true "type json string"
- // @Success 200 string "操作成功"
- // @router /contract/register/invoice [post]
- func (rg *RegisterController) Invoice(c *gin.Context) {
- req := new(fms.ContractInvoiceSaveReq)
- err := c.ShouldBind(&req)
- if err != nil {
- errs, ok := err.(validator.ValidationErrors)
- if !ok {
- resp.FailData("参数解析失败", "Err:"+err.Error(), c)
- return
- }
- resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
- return
- }
- claims, _ := c.Get("adminInfo")
- adminInfo := claims.(*system.SysAdmin)
- registerOB := new(fms.ContractRegister)
- item, e := registerOB.Fetch(req.ContractRegisterId)
- if e != nil {
- if e == utils.ErrNoRow {
- resp.Fail("合同登记不存在或已被删除", c)
- return
- }
- resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
- return
- }
- // 合同存在代付不允许开票/到款登记
- if item.HasPayment == 1 {
- resp.Fail("合同存在代付不允许添加开票/到款登记", c)
- return
- }
- // 合同有效时长
- dayDiff := item.EndDate.Sub(item.StartDate).Hours() / 24
- noChangeInvoiceIds := make([]int, 0)
- newInvoice := make([]*fms.ContractInvoice, 0)
- if len(req.AmountList) > 0 {
- // 获取销售分组信息
- sellerList, e := crmService.GetSellerDepartmentListWithGroupAndTeam()
- if e != nil {
- resp.FailData("获取销售失败", "Err:"+e.Error(), c)
- return
- }
- sellerMap := make(map[int]*crm.SellerAdminWithGroupTeam)
- for i := range sellerList {
- sellerMap[sellerList[i].SellerId] = sellerList[i]
- }
- for i := range req.AmountList {
- if req.AmountList[i].Amount <= 0 {
- resp.Fail("登记金额有误", c)
- return
- }
- if req.AmountList[i].InvoiceDate == "" {
- resp.Fail("请选择日期", c)
- return
- }
- // 开票登记销售必填
- if req.InvoiceType == fms.ContractInvoiceTypeMake && req.AmountList[i].SellerId <= 0 {
- resp.Fail("请选择销售", c)
- return
- }
- t, e := time.ParseInLocation(utils.FormatDate, req.AmountList[i].InvoiceDate, time.Local)
- if e != nil {
- resp.FailData("日期格式有误", "Err:"+e.Error(), c)
- return
- }
- if req.AmountList[i].InvoiceId > 0 {
- noChangeInvoiceIds = append(noChangeInvoiceIds, req.AmountList[i].InvoiceId)
- }
- if req.AmountList[i].InvoiceId == 0 {
- v := &fms.ContractInvoice{
- ContractRegisterId: req.ContractRegisterId,
- ContractCode: item.ContractCode,
- Amount: req.AmountList[i].Amount,
- InvoiceType: req.InvoiceType,
- InvoiceDate: t,
- AdminId: int(adminInfo.AdminId),
- AdminName: adminInfo.RealName,
- Remark: req.AmountList[i].Remark,
- }
- v.Set()
- // 开票登记-销售信息
- if req.InvoiceType == fms.ContractInvoiceTypeMake {
- sellerItem := sellerMap[req.AmountList[i].SellerId]
- if sellerItem == nil {
- resp.Fail("销售信息异常", c)
- return
- }
- v.SellerId = sellerItem.SellerId
- v.SellerName = sellerItem.SellerName
- v.SellerGroupId = sellerItem.GroupId
- v.SellerGroupName = sellerItem.GroupName
- v.SellerTeamId = sellerItem.TeamId
- v.SellerTeamName = sellerItem.TeamName
- }
- // 到款登记-付款方式
- if req.InvoiceType == fms.ContractInvoiceTypePay {
- v.PayType = fmsService.CalculateContractPaymentType(req.AmountList[i].Amount, item.ContractAmount, dayDiff)
- }
- newInvoice = append(newInvoice, v)
- }
- }
- }
- // 获取原有的登记信息
- invoiceCond := `contract_register_id = ? AND invoice_type = ?`
- invoicePars := make([]interface{}, 0)
- invoicePars = append(invoicePars, req.ContractRegisterId, req.InvoiceType)
- originInvoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
- if e != nil {
- resp.FailMsg("获取失败", "获取开票/到款列表失败, Err: "+e.Error(), c)
- return
- }
- // 比对原有和现有的登记信息
- logList := make([]*fms.ContractRegisterLog, 0)
- opData := ""
- opDataByte, e := json.Marshal(req)
- if e != nil {
- return
- }
- opData = string(opDataByte)
- opType := fms.ContractRegisterOpTypeInvoice
- if req.InvoiceType == fms.ContractInvoiceTypePay {
- opType = fms.ContractRegisterOpTypePayment
- }
- nowTime := time.Now().Local()
- // 需要删除的记录
- deleteInvoiceIds := make([]int, 0)
- for i := range originInvoiceList {
- if !utils.InArrayByInt(noChangeInvoiceIds, originInvoiceList[i].ContractInvoiceId) {
- deleteInvoiceIds = append(deleteInvoiceIds, originInvoiceList[i].ContractInvoiceId)
- logList = append(logList, &fms.ContractRegisterLog{
- ContractRegisterId: req.ContractRegisterId,
- AdminId: int(adminInfo.AdminId),
- AdminName: adminInfo.RealName,
- OpData: opData,
- OpType: opType,
- CreateTime: nowTime,
- AmountRemark: fmt.Sprint("删除", fms.ContractInvoiceKeyNameMap[opType], "金额", originInvoiceList[i].Amount, "元"),
- })
- }
- }
- // 新增的记录
- if len(newInvoice) > 0 {
- newAmount := decimal.NewFromFloat(0).Round(2)
- for i := range newInvoice {
- a := decimal.NewFromFloat(newInvoice[i].Amount).Round(2)
- newAmount = newAmount.Add(a)
- }
- ia, _ := newAmount.Round(2).Float64()
- logList = append(logList, &fms.ContractRegisterLog{
- ContractRegisterId: req.ContractRegisterId,
- AdminId: int(adminInfo.AdminId),
- AdminName: adminInfo.RealName,
- OpData: opData,
- OpType: opType,
- CreateTime: nowTime,
- AmountRemark: fmt.Sprint("新增", fms.ContractInvoiceKeyNameMap[opType], "金额", ia, "元"),
- })
- }
- // 删除并新增登记
- ob := new(fms.ContractInvoice)
- if e := ob.DeleteAndCreateNewInvoice(req.ContractRegisterId, req.InvoiceType, deleteInvoiceIds, newInvoice); e != nil {
- resp.FailData("日期格式有误", "Err:"+e.Error(), c)
- return
- }
- // 校验金额-是否修改状态
- go fmsService.CheckContractRegisterAmount(req.ContractRegisterId)
- // 操作日志
- go func() {
- logOB := new(fms.ContractRegisterLog)
- if e := logOB.AddInBatches(logList); e != nil {
- return
- }
- }()
- resp.Ok("操作成功", c)
- }
- // Export
- // @Title 合同登记-导出
- // @Description 合同登记-导出
- // @Param Keyword query string false "关键词"
- // @Param StartDate query string false "合同开始日期"
- // @Param EndDate query string false "合同结束日期"
- // @Param ServiceType query int false "套餐类型"
- // @Param ContractType query int false "合同类型"
- // @Param RegisterStatus query int false "登记状态"
- // @Success 200 string "操作成功"
- // @router /contract/register/export [get]
- func (rg *RegisterController) Export(c *gin.Context) {
- var req fms.ContractRegisterListReq
- 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 (company_name LIKE ? OR contract_code LIKE ? OR seller_name LIKE ? OR actual_company_name LIKE ?)`
- pars = append(pars, kw, kw, kw, kw)
- }
- if req.StartDate != "" && req.EndDate != "" {
- st := fmt.Sprint(req.StartDate, " 00:00:00")
- ed := fmt.Sprint(req.EndDate, " 23:59:59")
- cond += ` AND (create_time BETWEEN ? AND ?)`
- pars = append(pars, st, ed)
- }
- if req.ContractType != 0 {
- cond += ` AND contract_type = ?`
- pars = append(pars, req.ContractType)
- }
- if req.RegisterStatus != 0 {
- cond += ` AND register_status = ?`
- pars = append(pars, req.RegisterStatus)
- }
- 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 contract_register_id IN ?`
- pars = append(pars, registerIds)
- } else {
- cond += ` AND 1 = 2`
- }
- }
- // 获取列表数据
- cr := new(fms.ContractRegister)
- list, e := cr.List(cond, pars)
- if e != nil {
- resp.FailData("获取合同列表失败", "Err:"+e.Error(), c)
- return
- }
- if len(list) == 0 {
- resp.Fail("无有效数据可导出", c)
- return
- }
- registerIds := make([]int, 0)
- for i := range list {
- registerIds = append(registerIds, list[i].ContractRegisterId)
- }
- // 获取小套餐品种
- cpCond := `product_id = ? AND permission_name <> ?`
- cpPars := make([]interface{}, 0)
- cpPars = append(cpPars, crm.CompanyProductFicc, crm.ChartPermissionStrategyName)
- cp := new(crm.ChartPermission)
- permissionList, e := cp.List(cpCond, cpPars)
- if e != nil {
- resp.FailData("获取小套餐品种失败", "Err:"+e.Error(), c)
- return
- }
- permissionLen := len(permissionList)
- permissionNameIdMap := make(map[string]int)
- for i := range permissionList {
- permissionNameIdMap[permissionList[i].PermissionName] = permissionList[i].ChartPermissionId
- }
- // 获取套餐服务
- serviceList, e := fms.GetContractServiceTemplateMapByProductId(crm.CompanyProductFicc)
- if e != nil {
- resp.FailData("获取套餐服务失败", "Err:"+e.Error(), c)
- return
- }
- // 除大小套餐外的其他套餐(下面的导出需要以此动态处理)
- otherService := make([]*fms.ContractServiceTemplateItem, 0)
- for i := range serviceList {
- if serviceList[i].Title != "FICC大套餐" && serviceList[i].Title != "FICC小套餐" {
- otherService = append(otherService, serviceList[i])
- }
- }
- otherServiceLen := len(otherService)
- // 套餐/开票/到款列表
- serviceMap := make(map[int][]*fms.ContractService)
- serviceChartPermissionsMap := make(map[int][]int)
- invoiceMap := make(map[int][]*fms.ContractInvoice)
- paymentMap := make(map[int][]*fms.ContractInvoice)
- maxInvoice := 0
- maxPayment := 0
- if len(registerIds) > 0 {
- // 获取套餐信息
- csCond := `contract_register_id IN ?`
- csPars := make([]interface{}, 0)
- csPars = append(csPars, registerIds)
- cs := new(fms.ContractService)
- serviceList, e := cs.List(csCond, csPars)
- if e != nil {
- resp.FailData("获取合同套餐列表失败", "Err:"+e.Error(), c)
- return
- }
- for i := range serviceList {
- cid := serviceList[i].ContractRegisterId
- if serviceMap[cid] == nil {
- serviceMap[cid] = make([]*fms.ContractService, 0)
- }
- serviceMap[cid] = append(serviceMap[cid], serviceList[i])
- // 小套餐权限
- if serviceChartPermissionsMap[cid] == nil {
- serviceChartPermissionsMap[cid] = make([]int, 0)
- }
- if serviceList[i].ChartPermissionIds != "" {
- ids := utils.JoinStr2IntArr(serviceList[i].ChartPermissionIds, ",")
- serviceChartPermissionsMap[cid] = append(serviceChartPermissionsMap[cid], ids...)
- }
- }
- // 获取开票/到款详情, 并取最大的开票/到款数(用于动态扩展第二列表头)
- ci := new(fms.ContractInvoice)
- invoiceList, e := ci.List(csCond, csPars, "")
- if e != nil {
- resp.FailData("获取开票/到款列表失败", "Err:"+e.Error(), c)
- return
- }
- for k := range invoiceList {
- cid := invoiceList[k].ContractRegisterId
- if invoiceMap[cid] == nil {
- invoiceMap[cid] = make([]*fms.ContractInvoice, 0)
- }
- if paymentMap[cid] == nil {
- paymentMap[cid] = make([]*fms.ContractInvoice, 0)
- }
- if invoiceList[k].InvoiceType == fms.ContractInvoiceTypeMake {
- invoiceMap[cid] = append(invoiceMap[cid], invoiceList[k])
- continue
- }
- if invoiceList[k].InvoiceType == fms.ContractInvoiceTypePay {
- paymentMap[cid] = append(paymentMap[cid], invoiceList[k])
- continue
- }
- }
- // 取最大开票/到款数
- for j := range invoiceMap {
- if len(invoiceMap[j]) > maxInvoice {
- maxInvoice = len(invoiceMap[j])
- }
- }
- for p := range paymentMap {
- if len(paymentMap[p]) > maxPayment {
- maxPayment = len(paymentMap[p])
- }
- }
- }
- // 生成Excel文件
- xlsxFile := xlsx.NewFile()
- style := xlsx.NewStyle()
- alignment := xlsx.Alignment{
- Horizontal: "center",
- Vertical: "center",
- WrapText: true,
- }
- style.Alignment = alignment
- style.ApplyAlignment = true
- sheetName := "财务列表"
- sheet, err := xlsxFile.AddSheet(sheetName)
- if err != nil {
- resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
- return
- }
- // 1行表头
- titleRow := sheet.AddRow()
- titleRow.SetHeight(40)
- // 1行1列-右合并两格
- cell1 := titleRow.AddCell()
- cell1.HMerge = 3
- cell1.SetString("FICC客户签约表 2022")
- cell1.SetStyle(style)
- // 右增两列空白格用于第一列合并, 否则后续单元格合并会有问题
- titleRow.AddCell().SetString("")
- titleRow.AddCell().SetString("")
- titleRow.AddCell().SetString("")
- // 1行2列
- cell2 := titleRow.AddCell()
- cell2.SetString("FICC大套餐")
- cell2.SetStyle(style)
- // 1行3列-右合并小套餐数
- if permissionLen >= 1 {
- cell3 := titleRow.AddCell()
- cell3.HMerge = permissionLen - 1
- cell3.SetString("FICC小套餐")
- cell3.SetStyle(style)
- // 同上右增单元格小套餐数-1的空白单元格用于合并
- for i := 0; i < permissionLen-1; i++ {
- titleRow.AddCell().SetString("")
- }
- }
- for i := range otherService {
- cellOther := titleRow.AddCell()
- cellOther.SetString(otherService[i].Title)
- cellOther.SetStyle(style)
- }
- // 第二行表头
- titleRow2 := sheet.AddRow()
- titleRow2.SetHeight(60)
- row2Title := make([]string, 0)
- row2Title = append(row2Title, "客户名称", "新客户\n0-是\n1-否", "合同类型\n续约-0\n新增-1\n代付-2\n补充协议-3", "销售", "FICC大套餐")
- for i := range permissionList {
- row2Title = append(row2Title, permissionList[i].PermissionName)
- }
- // 其他套餐
- for i := range otherService {
- row2Title = append(row2Title, otherService[i].Title)
- }
- row2Title = append(row2Title, "套餐备注", "开始时间", "到期时间", "2022年合同金额", "约定付款时间", "签订日", "合同状态",
- "合同编号", "合规备注")
- // 设置表头
- for i := range row2Title {
- v := titleRow2.AddCell()
- v.SetString(row2Title[i])
- v.SetStyle(style)
- }
- // 第二行表头-开票/收款(动态添加)
- invoiceTitle := []string{"开票日", "开票金额", "销售", "备注"}
- for i := 0; i < maxInvoice; i++ {
- n := i + 1
- for ii := range invoiceTitle {
- c := titleRow2.AddCell()
- t := fmt.Sprintf("%s%d", invoiceTitle[ii], n)
- c.SetString(t)
- c.SetStyle(style)
- row2Title = append(row2Title, t)
- }
- }
- paymentTitle := []string{"到款日", "到款金额", "备注"}
- for i := 0; i < maxPayment; i++ {
- n := i + 1
- for ii := range paymentTitle {
- c := titleRow2.AddCell()
- t := fmt.Sprintf("%s%d", paymentTitle[ii], n)
- c.SetString(t)
- c.SetStyle(style)
- row2Title = append(row2Title, t)
- }
- }
- // 此处取第二行标题NameKeyMap, 后面的动态匹配
- row2NameKeyMap := make(map[string]int)
- for i := range row2Title {
- row2NameKeyMap[row2Title[i]] = i
- }
- newCompanyMap := map[int]string{0: "1", 1: "0"}
- contractTMap := map[int]int{
- fms.ContractTypeNew: 1,
- fms.ContractTypeRenew: 0,
- fms.ContractTypeAgentPay: 2, // 代付合同
- fms.ContractTypePlus: 3, // 补充协议
- }
- for _, v := range list {
- k := -1 // 套餐匹配用
- dataRow := sheet.AddRow()
- dataRow.SetHeight(20)
- k += 4
- dataRow.AddCell().SetString(v.CompanyName)
- dataRow.AddCell().SetString(newCompanyMap[v.NewCompany])
- dataRow.AddCell().SetString(fmt.Sprint(contractTMap[v.ContractType]))
- dataRow.AddCell().SetString(v.SellerName)
- // 大套餐
- k += 1
- col4Name := row2Title[k]
- svList := serviceMap[v.ContractRegisterId]
- col4 := ""
- if svList != nil && len(svList) > 0 {
- for isv := range svList {
- if svList[isv].Title == col4Name {
- col4 = "是"
- break
- }
- }
- }
- dataRow.AddCell().SetString(col4)
- // 小套餐
- serviceChartPermissionIds := serviceChartPermissionsMap[v.ContractRegisterId]
- for i := 0; i < permissionLen; i++ {
- k += 1
- colName := row2Title[k]
- chartPermissionId := permissionNameIdMap[colName]
- if utils.InArray(chartPermissionId, serviceChartPermissionIds) {
- dataRow.AddCell().SetString("是")
- } else {
- dataRow.AddCell().SetString("")
- }
- }
- // 除大小套餐外的其他套餐(处理方式其实跟上面的大套餐一样, 只是中间隔了小套餐按照顺序要这么处理=_=!)
- for i := 0; i < otherServiceLen; i++ {
- k += 1
- otherColName := row2Title[k]
- otherCol := ""
- if svList != nil && len(svList) > 0 {
- for isv := range svList {
- if svList[isv].Title == otherColName {
- otherCol = "是"
- break
- }
- }
- }
- dataRow.AddCell().SetString(otherCol)
- }
- // 其他信息
- dataRow.AddCell().SetString(v.ServiceRemark) // 套餐备注
- dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.StartDate)) // 开始时间
- dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.EndDate)) // 到期时间
- dataRow.AddCell().SetString(fmt.Sprint("¥", v.ContractAmount)) // 2022年合同金额
- dataRow.AddCell().SetString(v.AgreedPayTime) // 约定付款时间
- dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.SignDate)) // 签订日
- dataRow.AddCell().SetString(fms.ContractStatusKeyNameMap[v.ContractStatus]) // 合同状态
- dataRow.AddCell().SetString(v.ContractCode) // 合同编号
- dataRow.AddCell().SetString(v.Remark) // 合规备注
- // 开票/到款信息
- ivList := invoiceMap[v.ContractRegisterId]
- ivListLen := len(ivList)
- if ivList != nil && len(ivList) > 0 {
- for ia := 0; ia < maxInvoice; ia++ {
- if ia < ivListLen {
- dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", ivList[ia].InvoiceDate)) // 开票日
- dataRow.AddCell().SetString(fmt.Sprint(ivList[ia].Amount)) // 开票金额
- dataRow.AddCell().SetString(ivList[ia].SellerName) // 销售名称
- dataRow.AddCell().SetString(ivList[ia].Remark) // 开票备注
- } else {
- // 这里要把不够的填充为空
- dataRow.AddCell().SetString("")
- dataRow.AddCell().SetString("")
- dataRow.AddCell().SetString("")
- dataRow.AddCell().SetString("")
- }
- }
- }
- pyList := paymentMap[v.ContractRegisterId]
- pyListLen := len(pyList)
- if pyList != nil && pyListLen > 0 {
- for ib := 0; ib < maxPayment; ib++ {
- if ib < pyListLen {
- dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", pyList[ib].InvoiceDate)) // 收款日
- dataRow.AddCell().SetString(fmt.Sprint(pyList[ib].Amount)) // 收款金额
- dataRow.AddCell().SetString(pyList[ib].Remark) // 收款备注
- } else {
- // 已经是最后的几列了其实可以不用填充空, 万一后面要加列此处还是填充上吧
- dataRow.AddCell().SetString("")
- dataRow.AddCell().SetString("")
- dataRow.AddCell().SetString("")
- }
- }
- }
- }
- // 输出文件
- var buffer bytes.Buffer
- _ = xlsxFile.Write(&buffer)
- content := bytes.NewReader(buffer.Bytes())
- randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
- fileName := sheetName + 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)
- }
- // InvoiceList
- // @Title 开票/到款列表
- // @Description 开票/到款列表
- // @Param InvoiceType query int false "类型: 1-开票登记; 2-到款登记"
- // @Param ContractCode query string false "合同编号"
- // @Param StartDate query string false "开始日期"
- // @Param EndDate query string false "结束日期"
- // @Param MinAmount query float64 false "开票金额区间-最小值"
- // @Param MaxAmount query float64 false "开票金额区间-最大值"
- // @Success 200 {object} fms.ContractInvoiceItem
- // @router /contract/register/invoice_list [get]
- func (rg *RegisterController) InvoiceList(c *gin.Context) {
- var req fms.ContractInvoiceListReq
- 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 := `invoice_type = ?`
- pars := make([]interface{}, 0)
- pars = append(pars, req.InvoiceType)
- // 合同编号
- if req.ContractCode != "" {
- kw := fmt.Sprint("%", req.ContractCode, "%")
- cond += ` AND contract_code LIKE ?`
- pars = append(pars, kw)
- }
- if req.StartDate != "" && req.EndDate != "" {
- st := fmt.Sprint(req.StartDate, " 00:00:00")
- ed := fmt.Sprint(req.EndDate, " 23:59:59")
- cond += ` AND (invoice_time BETWEEN ? AND ?)`
- pars = append(pars, st, ed)
- }
- if req.MinAmount > 0 {
- cond += ` AND amount >= ?`
- pars = append(pars, req.MinAmount)
- }
- if req.MaxAmount > 0 {
- cond += ` AND amount <= ?`
- pars = append(pars, req.MaxAmount)
- }
- page := new(base.Page)
- page.SetPageSize(req.PageSize)
- page.SetCurrent(req.Current)
- page.AddOrderItem(base.OrderItem{Column: "invoice_time", Asc: false})
- total, list, e := fms.GetContractInvoiceItemPageList(page, cond, pars)
- if e != nil {
- resp.FailMsg("获取失败", "获取合同开票/到款列表失败, Err: "+e.Error(), c)
- return
- }
- page.SetTotal(total)
- baseData := new(base.BaseData)
- baseData.SetPage(page)
- baseData.SetList(list)
- resp.OkData("获取成功", baseData, c)
- }
- // InvoiceExport
- // @Title 开票/到款列表-导出
- // @Description 合同登记-导出
- // @Param InvoiceType query int false "类型: 1-开票登记; 2-到款登记"
- // @Param ContractCode query string false "合同编号"
- // @Param StartDate query string false "开始日期"
- // @Param EndDate query string false "结束日期"
- // @Param MinAmount query float64 false "开票金额区间-最小值"
- // @Param MaxAmount query float64 false "开票金额区间-最大值"
- // @Success 200 string "操作成功"
- // @router /contract/register/invoice_export [get]
- func (rg *RegisterController) InvoiceExport(c *gin.Context) {
- var req fms.ContractInvoiceListReq
- 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 := `invoice_type = ?`
- pars := make([]interface{}, 0)
- pars = append(pars, req.InvoiceType)
- // 合同编号
- if req.ContractCode != "" {
- kw := fmt.Sprint("%", req.ContractCode, "%")
- cond += ` AND contract_code LIKE ?`
- pars = append(pars, kw)
- }
- if req.StartDate != "" && req.EndDate != "" {
- st := fmt.Sprint(req.StartDate, " 00:00:00")
- ed := fmt.Sprint(req.EndDate, " 23:59:59")
- cond += ` AND (invoice_time BETWEEN ? AND ?)`
- pars = append(pars, st, ed)
- }
- if req.MinAmount > 0 {
- cond += ` AND amount >= ?`
- pars = append(pars, req.MinAmount)
- }
- if req.MaxAmount > 0 {
- cond += ` AND amount <= ?`
- pars = append(pars, req.MaxAmount)
- }
- // 获取列表数据
- cr := new(fms.ContractInvoice)
- orderRule := `invoice_time DESC`
- list, e := cr.List(cond, pars, orderRule)
- if e != nil {
- resp.FailData("获取开票/到款列表失败", "Err:"+e.Error(), c)
- return
- }
- 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
- }
- // 表头
- titleRow := sheet.AddRow()
- titleRow.SetHeight(40)
- cell1 := titleRow.AddCell()
- cell1.SetString("合同编号")
- cell1.SetStyle(style)
- cell2 := titleRow.AddCell()
- cell2.SetString("开票金额")
- cell2.SetStyle(style)
- cell3 := titleRow.AddCell()
- cell3.SetString("开票日期")
- cell3.SetStyle(style)
- for _, v := range list {
- dataRow := sheet.AddRow()
- dataRow.SetHeight(20)
- dataRow.AddCell().SetString(v.ContractCode)
- dataRow.AddCell().SetString(fmt.Sprint(v.Amount))
- dataRow.AddCell().SetString(utils.TimeTransferString("2006-01-02", v.InvoiceDate))
- }
- // 输出文件
- 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)
- }
- // Import
- // @Title 合同登记-导入
- // @Description 合同登记-导入
- // @Success 200 string "操作成功"
- // @router /contract/register/import [post]
- func (rg *RegisterController) Import(c *gin.Context) {
- h, err := c.FormFile("File")
- if err != nil {
- resp.FailData("获取文件失败", "Err:"+err.Error(), c)
- return
- }
- ext := path.Ext(h.Filename)
- if ext != ".xlsx" && ext != ".xls" {
- resp.Fail("请上传Excel文件", c)
- return
- }
- uploadDir := "static/xls"
- err = os.MkdirAll(uploadDir, 766)
- if err != nil {
- resp.FailData("存储目录创建失败", "Err:"+err.Error(), c)
- return
- }
- uploadPath := uploadDir + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + h.Filename
- err = c.SaveUploadedFile(h, uploadPath)
- if err != nil {
- resp.FailData("保存本地文件失败", "Err:"+err.Error(), c)
- return
- }
- defer func() {
- _ = os.Remove(uploadPath)
- }()
- xlFile, err := xlsx.OpenFile(uploadPath)
- if err != nil {
- resp.FailData("打开文件失败", "Err:"+err.Error(), c)
- return
- }
- // 获取所有已登记,根据合同编号去重
- contractCodeArr := make([]string, 0)
- registerOB := new(fms.ContractRegister)
- registerCond := ``
- registerPars := make([]interface{}, 0)
- registerList, e := registerOB.List(registerCond, registerPars)
- if e != nil {
- resp.FailData("获取合同登记列表失败", "Err:"+e.Error(), c)
- return
- }
- for i := range registerList {
- contractCodeArr = append(contractCodeArr, registerList[i].ContractCode)
- }
- // 获取所有销售Map
- sellerList, e := crmService.GetSellerDepartmentListWithGroupAndTeam()
- if e != nil {
- resp.FailData("获取销售列表失败", "Err:"+e.Error(), c)
- return
- }
- sellerMap := make(map[string]*crm.SellerAdminWithGroupTeam)
- for i := range sellerList {
- sellerMap[sellerList[i].SellerName] = sellerList[i]
- }
- // 获取品种权限Map
- chartPermissionNameIdMap := make(map[string]int)
- cpCond := `product_id = ?`
- cpPars := make([]interface{}, 0)
- cpPars = append(cpPars, crm.CompanyProductFicc)
- cp := new(crm.ChartPermission)
- permissionList, e := cp.List(cpCond, cpPars)
- if e != nil {
- resp.FailData("获取权限列表失败", "Err:"+e.Error(), c)
- return
- }
- for i := range permissionList {
- chartPermissionNameIdMap[permissionList[i].PermissionName] = permissionList[i].ChartPermissionId
- }
- // 获取所有套餐模板
- 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
- }
- serviceTempNameMap := make(map[string]*fms.ContractServiceTemplate)
- for i := range serviceTempList {
- serviceTempNameMap[serviceTempList[i].Title] = serviceTempList[i]
- }
- titleMap := make(map[int]string)
- newIds := make([]int, 0)
- newCompanyArr := []string{"0", "1"}
- newCompanyMap := map[string]int{"0": 1, "1": 0}
- contractTypeArr := []string{"0", "1", "2", "3"}
- contractTypeMap := map[string]int{
- "0": fms.ContractTypeRenew,
- "1": fms.ContractTypeNew,
- "2": fms.ContractTypeAgentPay,
- "3": fms.ContractTypePlus,
- }
- // 更新登记状态
- defer func() {
- if len(newIds) > 0 {
- go func() {
- for i := range newIds {
- fmsService.CheckContractRegisterAmount(newIds[i])
- }
- }()
- }
- }()
- invoiceMax := 3
- checkDate := time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local)
- for _, sheet := range xlFile.Sheets {
- // 遍历行读取
- maxRow := sheet.MaxRow
- for i := 0; i < maxRow; i++ {
- // 第二行开始读取表头
- if i == 1 {
- row := sheet.Row(i)
- cells := row.Cells
- for k, cell := range cells {
- text := cell.String()
- titleMap[k] = text
- // 只检验前面部分表头, 后面很多动态列
- if k == 0 {
- if text != "客户名称" {
- resp.Fail("【客户名称】列名称有误, 请参考模板导入", c)
- return
- }
- }
- if k == 1 {
- if !strings.Contains(text, "新客户") {
- resp.Fail("【新客户】列名称有误, 请参考模板导入", c)
- return
- }
- }
- if k == 2 {
- if !strings.Contains(text, "合同类型") {
- resp.Fail("【合同类型】列名称有误, 请参考模板导入", c)
- return
- }
- }
- if k == 3 {
- if text != "销售" {
- resp.Fail("【销售】列名称有误, 请参考模板导入", c)
- return
- }
- }
- if k == 4 {
- if text != "FICC大套餐" {
- resp.Fail("【FICC大套餐】列名称有误, 请参考模板导入", c)
- return
- }
- }
- }
- }
- // 数据行
- if i >= 2 {
- row := sheet.Row(i)
- cells := row.Cells
- // 登记信息
- rowRegister := new(fms.ContractRegister)
- // 套餐
- rowServices := make([]*fms.ContractService, 0)
- // 开票/到款
- rowInvoices := make([]*fms.ContractInvoice, 0)
- for ir := 0; ir < invoiceMax; ir++ {
- rowInvoices = append(rowInvoices, new(fms.ContractInvoice))
- }
- rowPayments := make([]*fms.ContractInvoice, 0)
- for ir := 0; ir < invoiceMax; ir++ {
- rowPayments = append(rowPayments, new(fms.ContractInvoice))
- }
- // 小套餐权限
- rowChartPermissionIdArr := make([]string, 0)
- isSkip := false
- for k, cell := range cells {
- v := utils.TrimStr(cell.String())
- // 客户名称
- if k == 0 {
- if v == "" {
- resp.Fail(fmt.Sprintf("第%d行客户名称不可为空, 请按模板导入", i+1), c)
- return
- }
- rowRegister.CompanyName = v
- continue
- }
- // 是否新客户
- if k == 1 {
- if !utils.InArrayByStr(newCompanyArr, v) {
- resp.Fail(fmt.Sprintf("第%d行新客户有误, 请按模板导入", i+1), c)
- return
- }
- rowRegister.NewCompany = newCompanyMap[v]
- continue
- }
- // 合同类型
- if k == 2 {
- if !utils.InArrayByStr(contractTypeArr, v) {
- resp.Fail(fmt.Sprintf("第%d行合同类型有误, 请按模板导入", i+1), c)
- return
- }
- rowRegister.ContractType = contractTypeMap[v]
- if rowRegister.ContractType == 0 {
- resp.Fail(fmt.Sprintf("第%d行合同类型匹配有误, 请按模板导入", i+1), c)
- return
- }
- continue
- }
- // 销售
- if k == 3 {
- if v == "" {
- resp.Fail(fmt.Sprintf("第%d行销售名称不可为空, 请按模板导入", i+1), c)
- return
- }
- sellerItem := sellerMap[v]
- if sellerItem == nil {
- resp.Fail(fmt.Sprintf("第%d行销售名称与系统销售不匹配, 请核对名称后导入", i+1), c)
- return
- }
- rowRegister.SellerId = sellerItem.SellerId
- rowRegister.SellerName = sellerItem.SellerName
- continue
- }
- // FICC大套餐
- if k == 4 {
- if v == "是" {
- tempItem := serviceTempNameMap[titleMap[k]]
- if tempItem == nil {
- resp.Fail(fmt.Sprintf("第%d行套餐名称不匹配, 请按模板导入", i+1), c)
- return
- }
- cs := &fms.ContractService{
- ProductId: crm.CompanyProductFicc,
- ServiceTemplateId: tempItem.ServiceTemplateId,
- Title: tempItem.Title,
- Value: tempItem.Value,
- TableValue: tempItem.TableValue,
- ChartPermissionId: tempItem.ChartPermissionId,
- }
- cs.Set()
- rowServices = append(rowServices, cs)
- }
- continue
- }
- // FICC小套餐
- if k >= 5 && k <= 25 {
- // 小套餐权限
- if v == "是" {
- rowChartPermissionIdArr = append(rowChartPermissionIdArr, strconv.Itoa(chartPermissionNameIdMap[titleMap[k]]))
- }
- continue
- }
- // 其他类型套餐
- if k >= 26 && k <= 32 {
- if v == "是" {
- tempItem := serviceTempNameMap[titleMap[k]]
- if tempItem == nil {
- resp.Fail(fmt.Sprintf("第%d行套餐名称不匹配, 请按模板导入", i+1), c)
- return
- }
- cs := &fms.ContractService{
- ProductId: crm.CompanyProductFicc,
- ServiceTemplateId: tempItem.ServiceTemplateId,
- Title: tempItem.Title,
- Value: tempItem.Value,
- TableValue: tempItem.TableValue,
- ChartPermissionId: tempItem.ChartPermissionId,
- }
- cs.Set()
- rowServices = append(rowServices, cs)
- }
- continue
- }
- // 套餐备注
- if k == 33 {
- rowRegister.ServiceRemark = v
- continue
- }
- // 开始时间/到期时间
- if k == 34 {
- // 转换失败可能是因为格式为Excel日期格式, 读取出来会是一串数字, 将其转换成日期字符串再处理
- va := cell.Value
- if va == "" {
- resp.Fail(fmt.Sprintf("第%d行开始时间不可为空, 请按模板导入", i+1), c)
- return
- }
- startDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
- if e != nil {
- d := utils.ConvertToFormatDay(va, "2006/01/02")
- startDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
- if e != nil {
- resp.Fail(fmt.Sprintf("第%d行开始时间格式转换有误, 请按模板导入", i+1), c)
- return
- }
- }
- // 转换后的日期小于1900-01-01表示当前生成的日期是有问题的
- if startDate.Before(checkDate) {
- resp.Fail(fmt.Sprintf("第%d行开始时间格式有误, 请按模板导入", i+1), c)
- return
- }
- rowRegister.StartDate = startDate
- continue
- }
- if k == 35 {
- va := cell.Value
- if va == "" {
- resp.Fail(fmt.Sprintf("第%d行到期时间不可为空, 请按模板导入", i+1), c)
- return
- }
- endDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
- if e != nil {
- d := utils.ConvertToFormatDay(va, "2006/01/02")
- endDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
- if e != nil {
- resp.Fail(fmt.Sprintf("第%d行到期时间格式转换有误, 请按模板导入", i+1), c)
- return
- }
- }
- if endDate.Before(checkDate) {
- resp.Fail(fmt.Sprintf("第%d行到期时间格式有误, 请按模板导入", i+1), c)
- return
- }
- rowRegister.EndDate = endDate
- continue
- }
- // 合同金额
- if k == 36 {
- amountStr := v
- amount, e := strconv.ParseFloat(amountStr, 64)
- if e != nil {
- resp.Fail(fmt.Sprintf("第%d行合同金额有误, 请按模板导入", i+1), c)
- return
- }
- rowRegister.ContractAmount = amount
- continue
- }
- // 约定付款日期
- if k == 37 {
- rowRegister.AgreedPayTime = v
- continue
- }
- // 签订日
- if k == 38 {
- va := cell.Value
- if va == "" {
- continue
- }
- signDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
- if e != nil {
- d := utils.ConvertToFormatDay(va, "2006/01/02")
- signDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
- if e != nil {
- resp.Fail(fmt.Sprintf("第%d行签订日格式转换有误, 请按模板导入", i+1), c)
- return
- }
- }
- if signDate.Before(checkDate) {
- resp.Fail(fmt.Sprintf("第%d行签订日格式有误, 请按模板导入", i+1), c)
- return
- }
- rowRegister.SignDate = signDate
- continue
- }
- // 合同状态
- if k == 39 {
- rowRegister.ContractStatus = fms.ContractStatusNameKeyMap[v]
- if rowRegister.ContractStatus == 0 {
- resp.Fail(fmt.Sprintf("第%d行合同状态不匹配, 请按模板导入", i+1), c)
- return
- }
- continue
- }
- // 合同编号
- if k == 40 {
- rowContractCode := v
- if rowContractCode == "" {
- resp.Fail(fmt.Sprintf("第%d行合同编号不可为空, 请按模板导入", i+1), c)
- return
- }
- if utils.InArrayByStr(contractCodeArr, rowContractCode) {
- // 此合同已登记, 跳过本行的读取
- isSkip = true
- break
- }
- rowRegister.ContractCode = rowContractCode
- continue
- }
- // 合规备注
- if k == 41 {
- rowRegister.Remark = v
- continue
- }
- // 开票列表
- k2 := 41
- for ir := 0; ir < invoiceMax; ir++ {
- n := ir + 1
- // 开票日
- k2 += 1
- if k == k2 {
- if v != "" {
- va := cell.Value
- invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
- if e != nil {
- d := utils.ConvertToFormatDay(va, "2006/01/02")
- invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
- if e != nil {
- resp.Fail(fmt.Sprintf("第%d行开票时间%d格式转换有误, 请按模板导入", i+1, n), c)
- return
- }
- }
- if invoiceDate.Before(checkDate) {
- resp.Fail(fmt.Sprintf("第%d行开票时间%d格式有误, 请按模板导入", i+1, n), c)
- return
- }
- rowInvoices[ir].InvoiceDate = invoiceDate
- rowInvoices[ir].ContractCode = rowRegister.ContractCode
- rowInvoices[ir].InvoiceType = fms.ContractInvoiceTypeMake
- }
- continue
- }
- // 开票金额
- k2 += 1
- if k == k2 {
- if v != "" {
- amountStr := v
- amount, e := strconv.ParseFloat(amountStr, 64)
- if e != nil {
- resp.Fail(fmt.Sprintf("第%d行开票金额%d有误, 请按模板导入", i+1, n), c)
- return
- }
- rowInvoices[ir].Amount = amount
- }
- continue
- }
- // 开票销售
- k2 += 1
- if k == k2 {
- if v != "" {
- sellerItem := sellerMap[v]
- if sellerItem == nil {
- resp.Fail(fmt.Sprintf("第%d行开票销售名称与系统销售不匹配, 请核对名称后导入", i+1), c)
- return
- }
- rowInvoices[ir].SellerId = sellerItem.SellerId
- rowInvoices[ir].SellerName = sellerItem.SellerName
- rowInvoices[ir].SellerGroupId = sellerItem.GroupId
- rowInvoices[ir].SellerGroupName = sellerItem.GroupName
- rowInvoices[ir].SellerTeamId = sellerItem.TeamId
- rowInvoices[ir].SellerTeamName = sellerItem.TeamName
- }
- continue
- }
- // 备注
- k2 += 1
- if k == k2 {
- if v != "" {
- rowInvoices[ir].Remark = v
- }
- continue
- }
- }
- // 到款列表
- for ir := 0; ir < invoiceMax; ir++ {
- n := ir + 1
- // 到款日
- k2 += 1
- if k == k2 {
- if v != "" {
- va := cell.Value
- invoiceDate, e := time.ParseInLocation("2006/01/02", va, time.Local)
- if e != nil {
- d := utils.ConvertToFormatDay(va, "2006/01/02")
- invoiceDate, e = time.ParseInLocation("2006/01/02", d, time.Local)
- if e != nil {
- resp.Fail(fmt.Sprintf("第%d行到款时间%d格式转换有误, 请按模板导入", i+1, n), c)
- return
- }
- }
- if invoiceDate.Before(checkDate) {
- resp.Fail(fmt.Sprintf("第%d行到款时间%d格式有误, 请按模板导入", i+1, n), c)
- return
- }
- rowPayments[ir].InvoiceDate = invoiceDate
- rowPayments[ir].ContractCode = rowRegister.ContractCode
- rowPayments[ir].InvoiceType = fms.ContractInvoiceTypePay
- }
- continue
- }
- // 到款金额
- k2 += 1
- if k == k2 {
- if v != "" {
- amountStr := v
- amount, e := strconv.ParseFloat(amountStr, 64)
- if e != nil {
- resp.Fail(fmt.Sprintf("第%d行到款金额%d有误, 请按模板导入", i+1, n), c)
- return
- }
- rowPayments[ir].Amount = amount
- }
- continue
- }
- // 备注
- k2 += 1
- if k == k2 {
- if v != "" {
- rowPayments[ir].Remark = v
- }
- continue
- }
- }
- }
- if isSkip {
- continue
- }
- // 小套餐
- if len(rowChartPermissionIdArr) > 0 {
- // 说明有小套餐
- tempItem := serviceTempNameMap["FICC小套餐"]
- if tempItem == nil {
- resp.Fail(fmt.Sprintf("第%d行小套餐名称不匹配, 请按模板导入", i+1), c)
- return
- }
- rowChartPermissionIds := strings.Join(rowChartPermissionIdArr, ",")
- cs := &fms.ContractService{
- ProductId: crm.CompanyProductFicc,
- ServiceTemplateId: tempItem.ServiceTemplateId,
- Title: tempItem.Title,
- Value: tempItem.Value,
- TableValue: tempItem.TableValue,
- ChartPermissionId: tempItem.ChartPermissionId,
- ChartPermissionIds: rowChartPermissionIds,
- }
- cs.Set()
- rowServices = append(rowServices, cs)
- }
- // 如果导入的最后一条合同编号为空并且合同编号之后的字段均为空
- // excel这个包读行的时候不会再往后面没数据的地方读取, 所以此处需要重新判断一次
- if rowRegister.ContractCode == "" {
- resp.Fail(fmt.Sprintf("第%d行合同编号为空", i+1), c)
- return
- }
- rowRegister.RegisterStatus = fms.ContractRegisterStatusIng
- lastInvoices := make([]*fms.ContractInvoice, 0)
- // 过滤信息不完整的开票到款
- for l := range rowInvoices {
- if rowInvoices[l].ContractCode != "" && rowInvoices[l].SellerId > 0 {
- lastInvoices = append(lastInvoices, rowInvoices[l])
- }
- }
- for l := range rowPayments {
- if rowPayments[l].ContractCode != "" {
- lastInvoices = append(lastInvoices, rowPayments[l])
- }
- }
- // 新增登记、套餐、开票到款信息
- newId, e := fms.CreateImportContractRegister(rowRegister, rowServices, lastInvoices)
- if e != nil {
- resp.FailData(fmt.Sprintf("第%d行导入失败", i+1), "新增导入登记失败, Err: "+e.Error(), c)
- return
- }
- newIds = append(newIds, newId)
- }
- }
- }
- resp.Ok("操作成功", c)
- }
|