register.go 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832
  1. package contract
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "github.com/go-playground/validator/v10"
  8. "github.com/shopspring/decimal"
  9. "github.com/tealeg/xlsx"
  10. "hongze/fms_api/controller/resp"
  11. "hongze/fms_api/global"
  12. "hongze/fms_api/models/base"
  13. "hongze/fms_api/models/crm"
  14. "hongze/fms_api/models/fms"
  15. "hongze/fms_api/models/system"
  16. fmsService "hongze/fms_api/services/fms"
  17. "hongze/fms_api/utils"
  18. "net/http"
  19. "os"
  20. "path"
  21. "strconv"
  22. "strings"
  23. "time"
  24. )
  25. // RegisterController 合同登记
  26. type RegisterController struct{}
  27. // List
  28. // @Title 合同登记列表
  29. // @Description 合同登记列表
  30. // @Param Keyword query string false "关键词"
  31. // @Param StartDate query string false "合同开始日期"
  32. // @Param EndDate query string false "合同结束日期"
  33. // @Param ServiceType query int false "套餐类型"
  34. // @Param ContractType query int false "合同类型"
  35. // @Param RegisterStatus query int false "登记状态"
  36. // @Success 200 {object} fms.ContractRegisterItem
  37. // @router /contract/register/list [get]
  38. func (rg *RegisterController) List(c *gin.Context) {
  39. var req fms.ContractRegisterListReq
  40. if e := c.BindQuery(&req); e != nil {
  41. err, ok := e.(validator.ValidationErrors)
  42. if !ok {
  43. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  44. return
  45. }
  46. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  47. return
  48. }
  49. cond := `1 = 1`
  50. pars := make([]interface{}, 0)
  51. // 合同编号/客户姓名/销售/实际使用方
  52. if req.Keyword != "" {
  53. kw := "%" + req.Keyword + "%"
  54. cond += ` AND (company_name LIKE ? OR contract_code LIKE ? OR seller_name LIKE ? OR actual_company_name LIKE ?)`
  55. pars = append(pars, kw, kw, kw, kw)
  56. }
  57. if req.StartDate != "" && req.EndDate != "" {
  58. st := fmt.Sprint(req.StartDate, " 00:00:00")
  59. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  60. cond += ` AND (create_time BETWEEN ? AND ?)`
  61. pars = append(pars, st, ed)
  62. }
  63. if req.ContractType != 0 {
  64. cond += ` AND contract_type = ?`
  65. pars = append(pars, req.ContractType)
  66. }
  67. if req.RegisterStatus != 0 {
  68. cond += ` AND register_status = ?`
  69. pars = append(pars, req.RegisterStatus)
  70. }
  71. // 套餐筛选
  72. if req.ServiceType != 0 {
  73. registerIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
  74. if e != nil {
  75. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  76. return
  77. }
  78. if len(registerIds) > 0 {
  79. cond += ` AND contract_register_id IN ?`
  80. pars = append(pars, registerIds)
  81. } else {
  82. cond += ` AND 1 = 2`
  83. }
  84. }
  85. page := new(base.Page)
  86. page.SetPageSize(req.PageSize)
  87. page.SetCurrent(req.Current)
  88. page.AddOrderItem(base.OrderItem{Column: "create_time", Asc: false})
  89. total, list, e := fms.GetContractRegisterItemPageList(page, cond, pars)
  90. if e != nil {
  91. resp.FailMsg("获取失败", "获取合同登记列表失败, Err: "+e.Error(), c)
  92. return
  93. }
  94. registerIds := make([]int, 0)
  95. for i := range list {
  96. registerIds = append(registerIds, list[i].ContractRegisterId)
  97. }
  98. serviceMap := make(map[int]string, 0)
  99. invoiceMap := make(map[int][]*fms.ContractInvoiceItem, 0)
  100. paymentMap := make(map[int][]*fms.ContractInvoiceItem, 0)
  101. if len(registerIds) > 0 {
  102. // 获取服务套餐
  103. servicesNameList, e := fms.GetContractRegisterServicesNameByRegisterIds(registerIds)
  104. if e != nil {
  105. resp.FailMsg("获取失败", "获取套餐拼接字符串失败, Err: "+e.Error(), c)
  106. return
  107. }
  108. for i := range servicesNameList {
  109. serviceMap[servicesNameList[i].ContractRegisterId] = servicesNameList[i].ServicesName
  110. }
  111. // 获取开票/到款列表
  112. invoiceCond := `contract_register_id IN ?`
  113. invoicePars := make([]interface{}, 0)
  114. invoicePars = append(invoicePars, registerIds)
  115. invoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  116. if e != nil {
  117. resp.FailMsg("获取失败", "获取开票/到款列表失败, Err: "+e.Error(), c)
  118. return
  119. }
  120. for i := range invoiceList {
  121. if invoiceMap[invoiceList[i].ContractRegisterId] == nil {
  122. invoiceMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoiceItem, 0)
  123. }
  124. if paymentMap[invoiceList[i].ContractRegisterId] == nil {
  125. paymentMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoiceItem, 0)
  126. }
  127. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  128. invoiceMap[invoiceList[i].ContractRegisterId] = append(invoiceMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  129. }
  130. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  131. paymentMap[invoiceList[i].ContractRegisterId] = append(paymentMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  132. }
  133. }
  134. }
  135. respList := make([]*fms.ContractRegisterList, 0)
  136. for i := range list {
  137. v := new(fms.ContractRegisterList)
  138. v.ContractRegisterItem = list[i]
  139. v.ServicesName = serviceMap[list[i].ContractRegisterId]
  140. v.InvoiceList = invoiceMap[list[i].ContractRegisterId]
  141. v.PaymentList = paymentMap[list[i].ContractRegisterId]
  142. respList = append(respList, v)
  143. }
  144. page.SetTotal(total)
  145. baseData := new(base.BaseData)
  146. baseData.SetPage(page)
  147. baseData.SetList(respList)
  148. resp.OkData("获取成功", baseData, c)
  149. }
  150. // Add
  151. // @Title 新增合同登记
  152. // @Description 新增合同登记
  153. // @Param request body fms.ContractRegisterAddReq true "type json string"
  154. // @Success 200 string "操作成功"
  155. // @router /contract/register/add [post]
  156. func (rg *RegisterController) Add(c *gin.Context) {
  157. req := new(fms.ContractRegisterAddReq)
  158. err := c.ShouldBind(&req)
  159. if err != nil {
  160. errs, ok := err.(validator.ValidationErrors)
  161. if !ok {
  162. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  163. return
  164. }
  165. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  166. return
  167. }
  168. claims, _ := c.Get("adminInfo")
  169. adminInfo := claims.(*system.SysAdmin)
  170. // 日期校验
  171. startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  172. if e != nil {
  173. resp.FailMsg("合同开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
  174. return
  175. }
  176. endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  177. if e != nil {
  178. resp.FailMsg("合同结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
  179. return
  180. }
  181. signDate, _ := time.ParseInLocation(utils.FormatDate, "0000-00-00", time.Local)
  182. if req.SignDate != "" {
  183. signDateTime, e := time.ParseInLocation(utils.FormatDate, req.SignDate, time.Local)
  184. if e != nil {
  185. resp.FailMsg("合同签订日期格式有误", "合同签订日期格式有误, Err: "+e.Error(), c)
  186. return
  187. }
  188. signDate = signDateTime
  189. }
  190. // 是否存在相同合同编号的登记
  191. ob := new(fms.ContractRegister)
  192. existCond := `contract_code = ?`
  193. existPars := make([]interface{}, 0)
  194. existPars = append(existPars, req.ContractCode)
  195. exist, e := ob.FetchByCondition(existCond, existPars)
  196. if e != nil && e != utils.ErrNoRow {
  197. resp.FailMsg("操作失败", "获取相同登记号失败, Err: "+e.Error(), c)
  198. return
  199. }
  200. if exist != nil && exist.ContractRegisterId > 0 {
  201. resp.Fail("合同编号已存在", c)
  202. return
  203. }
  204. nowTime := time.Now().Local()
  205. ob.ContractCode = req.ContractCode
  206. ob.RelateContractCode = req.RelateContractCode
  207. ob.CrmContractId = req.CrmContractId
  208. ob.ContractSource = req.ContractSource
  209. ob.CompanyName = req.CompanyName
  210. ob.ActualCompanyName = req.ActualCompanyName
  211. ob.ProductId = req.ProductId
  212. ob.SellerId = req.SellerId
  213. ob.SellerName = req.SellerName
  214. ob.ContractType = req.ContractType
  215. ob.ContractAmount = req.ContractAmount
  216. ob.StartDate = startDate
  217. ob.EndDate = endDate
  218. ob.SignDate = signDate
  219. ob.AgreedPayTime = req.AgreedPayTime
  220. ob.ContractStatus = req.ContractStatus
  221. ob.RegisterStatus = fms.ContractRegisterStatusIng
  222. ob.Remark = req.Remark
  223. ob.ServiceRemark = req.ServiceRemark
  224. ob.HasPayment = req.HasPayment
  225. ob.Set()
  226. // 存在代付的直接完成登记, 且不允许进行开票/到款登记
  227. if req.HasPayment == 1 {
  228. ob.RegisterStatus = fms.ContractRegisterStatusComplete
  229. }
  230. // 套餐信息
  231. serviceList, e := fmsService.HandleContractServiceAndDetail(req.ProductId, req.Services, true)
  232. if e != nil {
  233. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  234. return
  235. }
  236. // 新增合同及套餐
  237. if e = fms.CreateContractRegisterAndServices(ob, serviceList); e != nil {
  238. resp.FailMsg("操作失败", "新增合同及套餐失败, Err: "+e.Error(), c)
  239. return
  240. }
  241. // 操作日志
  242. go func() {
  243. opData := ""
  244. opDataByte, e := json.Marshal(req)
  245. if e != nil {
  246. return
  247. }
  248. opData = string(opDataByte)
  249. logItem := new(fms.ContractRegisterLog)
  250. logItem.ContractRegisterId = ob.ContractRegisterId
  251. logItem.AdminId = int(adminInfo.AdminId)
  252. logItem.AdminName = adminInfo.RealName
  253. logItem.OpData = opData
  254. logItem.OpType = fms.ContractRegisterOpTypeSave
  255. logItem.CreateTime = nowTime
  256. logItem.Remark = req.Remark
  257. if e = logItem.Create(); e != nil {
  258. return
  259. }
  260. }()
  261. resp.Ok("操作成功", c)
  262. }
  263. // Edit
  264. // @Title 编辑合同登记
  265. // @Description 编辑合同登记
  266. // @Param request body fms.ContractRegisterEditReq true "type json string"
  267. // @Success 200 string "操作成功"
  268. // @router /contract/register/edit [post]
  269. func (rg *RegisterController) Edit(c *gin.Context) {
  270. req := new(fms.ContractRegisterEditReq)
  271. err := c.ShouldBind(&req)
  272. if err != nil {
  273. errs, ok := err.(validator.ValidationErrors)
  274. if !ok {
  275. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  276. return
  277. }
  278. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  279. return
  280. }
  281. claims, _ := c.Get("adminInfo")
  282. adminInfo := claims.(*system.SysAdmin)
  283. // 日期校验
  284. startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  285. if e != nil {
  286. resp.FailMsg("合同开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
  287. return
  288. }
  289. endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  290. if e != nil {
  291. resp.FailMsg("合同结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
  292. return
  293. }
  294. signDate, _ := time.ParseInLocation(utils.FormatDate, "0000-00-00", time.Local)
  295. if req.SignDate != "" {
  296. signDateTime, e := time.ParseInLocation(utils.FormatDate, req.SignDate, time.Local)
  297. if e != nil {
  298. resp.FailMsg("合同签订日期格式有误", "合同签订日期格式有误, Err: "+e.Error(), c)
  299. return
  300. }
  301. signDate = signDateTime
  302. }
  303. ob := new(fms.ContractRegister)
  304. item, e := ob.Fetch(req.ContractRegisterId)
  305. if e != nil {
  306. if e == utils.ErrNoRow {
  307. resp.Fail("登记记录不存在或已被删除", c)
  308. return
  309. }
  310. resp.FailMsg("操作失败", "获取合同登记信息失败, Err:"+e.Error(), c)
  311. return
  312. }
  313. // 是否存在相同合同编号的登记
  314. existCond := `contract_code = ?`
  315. existPars := make([]interface{}, 0)
  316. existPars = append(existPars, req.ContractCode)
  317. exist, e := ob.FetchByCondition(existCond, existPars)
  318. if e != nil && e != utils.ErrNoRow {
  319. resp.FailMsg("操作失败", "获取相同登记号失败, Err: "+e.Error(), c)
  320. return
  321. }
  322. if exist != nil && exist.ContractRegisterId > 0 && exist.ContractRegisterId != item.ContractRegisterId {
  323. resp.Fail("合同编号已存在", c)
  324. return
  325. }
  326. updateCols := []string{
  327. "ContractCode", "RelateContractCode", "CrmContractId", "ContractSource", "CompanyName", "ActualCompanyName",
  328. "SellerId", "SellerName", "ContractType", "ContractAmount", "StartDate", "EndDate", "SignDate", "AgreedPayTime",
  329. "ContractStatus", "RegisterStatus", "Remark", "ServiceRemark", "HasPayment", "ModifyTime",
  330. }
  331. nowTime := time.Now().Local()
  332. item.ContractCode = req.ContractCode
  333. item.RelateContractCode = req.RelateContractCode
  334. item.CrmContractId = req.CrmContractId
  335. item.ContractSource = req.ContractSource
  336. item.CompanyName = req.CompanyName
  337. item.ActualCompanyName = req.ActualCompanyName
  338. item.SellerId = req.SellerId
  339. item.SellerName = req.SellerName
  340. item.ContractType = req.ContractType
  341. item.ContractAmount = req.ContractAmount
  342. item.StartDate = startDate
  343. item.EndDate = endDate
  344. item.SignDate = signDate
  345. item.AgreedPayTime = req.AgreedPayTime
  346. item.ContractStatus = req.ContractStatus
  347. item.RegisterStatus = fms.ContractRegisterStatusIng
  348. item.Remark = req.Remark
  349. item.ServiceRemark = req.ServiceRemark
  350. item.HasPayment = req.HasPayment
  351. item.ModifyTime = nowTime
  352. // 存在代付的直接完成登记, 且不允许进行开票/到款登记
  353. if req.HasPayment == 1 {
  354. item.RegisterStatus = fms.ContractRegisterStatusComplete
  355. updateCols = append(updateCols, "RegisterStatus")
  356. }
  357. // 套餐信息
  358. serviceList, e := fmsService.HandleContractServiceAndDetail(req.ProductId, req.Services, true)
  359. if e != nil {
  360. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  361. return
  362. }
  363. // 更新合同及套餐
  364. if e = fms.UpdateContractRegisterAndServices(item, updateCols, serviceList); e != nil {
  365. resp.FailMsg("操作失败", "更新合同及套餐失败, Err: "+e.Error(), c)
  366. return
  367. }
  368. // 校验金额-是否修改状态
  369. go fmsService.CheckContractRegisterAmount(item.ContractRegisterId)
  370. // 操作日志
  371. go func() {
  372. opData := ""
  373. opDataByte, e := json.Marshal(req)
  374. if e != nil {
  375. return
  376. }
  377. opData = string(opDataByte)
  378. logItem := new(fms.ContractRegisterLog)
  379. logItem.ContractRegisterId = item.ContractRegisterId
  380. logItem.AdminId = int(adminInfo.AdminId)
  381. logItem.AdminName = adminInfo.RealName
  382. logItem.OpData = opData
  383. logItem.OpType = fms.ContractRegisterOpTypeEdit
  384. logItem.CreateTime = nowTime
  385. logItem.Remark = req.Remark
  386. if e = logItem.Create(); e != nil {
  387. return
  388. }
  389. }()
  390. resp.Ok("操作成功", c)
  391. }
  392. // Del
  393. // @Title 删除合同登记
  394. // @Description 删除合同登记
  395. // @Param request body fms.ContractRegisterDelReq true "type json string"
  396. // @Success 200 string "操作成功"
  397. // @router /contract/register/del [post]
  398. func (rg *RegisterController) Del(c *gin.Context) {
  399. req := new(fms.ContractRegisterDelReq)
  400. err := c.ShouldBind(&req)
  401. if err != nil {
  402. errs, ok := err.(validator.ValidationErrors)
  403. if !ok {
  404. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  405. return
  406. }
  407. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  408. return
  409. }
  410. claims, _ := c.Get("adminInfo")
  411. adminInfo := claims.(*system.SysAdmin)
  412. ob := new(fms.ContractRegister)
  413. item, e := ob.Fetch(req.ContractRegisterId)
  414. if e != nil {
  415. if e == utils.ErrNoRow {
  416. resp.Fail("合同登记不存在或已被删除", c)
  417. return
  418. }
  419. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  420. return
  421. }
  422. nowTime := time.Now().Local()
  423. item.IsDeleted = 1
  424. item.ModifyTime = nowTime
  425. updateCols := []string{"IsDeleted", "ModifyTime"}
  426. if e = item.Update(updateCols); e != nil {
  427. resp.FailMsg("操作失败", "更新合同登记失败, Err:"+e.Error(), c)
  428. return
  429. }
  430. // 操作日志
  431. go func() {
  432. opData := ""
  433. opDataByte, e := json.Marshal(req)
  434. if e != nil {
  435. return
  436. }
  437. opData = string(opDataByte)
  438. logItem := new(fms.ContractRegisterLog)
  439. logItem.ContractRegisterId = req.ContractRegisterId
  440. logItem.AdminId = int(adminInfo.AdminId)
  441. logItem.AdminName = adminInfo.RealName
  442. logItem.OpData = opData
  443. logItem.OpType = fms.ContractRegisterOpTypeDel
  444. logItem.CreateTime = nowTime
  445. if e = logItem.Create(); e != nil {
  446. return
  447. }
  448. }()
  449. resp.Ok("操作成功", c)
  450. }
  451. // Detail
  452. // @Title 合同登记详情
  453. // @Description 合同登记详情
  454. // @Param ContractRegisterId query int false "合同登记ID"
  455. // @Success 200 {object} fms.ContractRegisterDetail
  456. // @router /contract/register/detail [get]
  457. func (rg *RegisterController) Detail(c *gin.Context) {
  458. var req fms.ContractRegisterDetailReq
  459. if e := c.BindQuery(&req); e != nil {
  460. err, ok := e.(validator.ValidationErrors)
  461. if !ok {
  462. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  463. return
  464. }
  465. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  466. return
  467. }
  468. result := new(fms.ContractRegisterDetail)
  469. // 合同登记信息
  470. item, e := fms.GetContractRegisterItemById(req.ContractRegisterId)
  471. if e != nil {
  472. resp.FailData("获取失败", "获取合同登记详情失败, Err:"+e.Error(), c)
  473. return
  474. }
  475. result.ContractRegisterItem = item
  476. // 套餐信息
  477. serviceList, e := fmsService.GetContractServiceAndDetail(req.ContractRegisterId)
  478. if e != nil {
  479. resp.FailData("获取失败", "获取合同套餐信息失败, Err: "+e.Error(), c)
  480. return
  481. }
  482. result.ServiceList = serviceList
  483. // 开票/到款信息
  484. invoiceCond := `contract_register_id = ?`
  485. invoicePars := make([]interface{}, 0)
  486. invoicePars = append(invoicePars, req.ContractRegisterId)
  487. invoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  488. if e != nil {
  489. resp.FailData("获取失败", "获取合同开票/到款信息失败, Err: "+e.Error(), c)
  490. return
  491. }
  492. result.InvoiceList = make([]*fms.ContractInvoiceItem, 0)
  493. result.PaymentList = make([]*fms.ContractInvoiceItem, 0)
  494. for i := range invoiceList {
  495. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  496. result.InvoiceList = append(result.InvoiceList, invoiceList[i])
  497. continue
  498. }
  499. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  500. result.PaymentList = append(result.PaymentList, invoiceList[i])
  501. }
  502. }
  503. // 合同登记进度
  504. logCond := `contract_register_id = ?`
  505. logPars := make([]interface{}, 0)
  506. logPars = append(logPars, req.ContractRegisterId)
  507. logList, e := fms.GetContractRegisterLogItemList(logCond, logPars)
  508. if e != nil {
  509. resp.FailData("获取失败", "获取合同登记进度失败, Err: "+e.Error(), c)
  510. return
  511. }
  512. result.Logs = logList
  513. resp.OkData("获取成功", result, c)
  514. }
  515. // UpdateStatus
  516. // @Title 修改合同状态
  517. // @Description 修改合同状态
  518. // @Param request body fms.ContractRegisterUpdateStatusReq true "type json string"
  519. // @Success 200 string "操作成功"
  520. // @router /contract/register/update_status [post]
  521. func (rg *RegisterController) UpdateStatus(c *gin.Context) {
  522. req := new(fms.ContractRegisterUpdateStatusReq)
  523. err := c.ShouldBind(&req)
  524. if err != nil {
  525. errs, ok := err.(validator.ValidationErrors)
  526. if !ok {
  527. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  528. return
  529. }
  530. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  531. return
  532. }
  533. claims, _ := c.Get("adminInfo")
  534. adminInfo := claims.(*system.SysAdmin)
  535. ob := new(fms.ContractRegister)
  536. item, e := ob.Fetch(req.ContractRegisterId)
  537. if e != nil {
  538. if e == utils.ErrNoRow {
  539. resp.Fail("合同登记不存在或已被删除", c)
  540. return
  541. }
  542. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  543. return
  544. }
  545. nowTime := time.Now().Local()
  546. item.ContractStatus = req.ContractStatus
  547. item.ModifyTime = nowTime
  548. updateCols := []string{"ContractStatus", "ModifyTime"}
  549. if e = item.Update(updateCols); e != nil {
  550. resp.FailMsg("操作失败", "更新合同登记失败, Err:"+e.Error(), c)
  551. return
  552. }
  553. // 校验金额-是否修改状态
  554. go fmsService.CheckContractRegisterAmount(req.ContractRegisterId)
  555. // 操作日志
  556. go func() {
  557. opData := ""
  558. opDataByte, e := json.Marshal(req)
  559. if e != nil {
  560. return
  561. }
  562. opData = string(opDataByte)
  563. logItem := new(fms.ContractRegisterLog)
  564. logItem.ContractRegisterId = req.ContractRegisterId
  565. logItem.AdminId = int(adminInfo.AdminId)
  566. logItem.AdminName = adminInfo.RealName
  567. logItem.OpData = opData
  568. logItem.OpType = fms.ContractRegisterOpTypeStatus
  569. logItem.CreateTime = nowTime
  570. if e = logItem.Create(); e != nil {
  571. return
  572. }
  573. }()
  574. resp.Ok("操作成功", c)
  575. }
  576. // Invoice
  577. // @Title 开票/到款登记
  578. // @Description 开票/到款登记
  579. // @Param request body fms.ContractInvoiceSaveReq true "type json string"
  580. // @Success 200 string "操作成功"
  581. // @router /contract/register/invoice [post]
  582. func (rg *RegisterController) Invoice(c *gin.Context) {
  583. req := new(fms.ContractInvoiceSaveReq)
  584. err := c.ShouldBind(&req)
  585. if err != nil {
  586. errs, ok := err.(validator.ValidationErrors)
  587. if !ok {
  588. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  589. return
  590. }
  591. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  592. return
  593. }
  594. claims, _ := c.Get("adminInfo")
  595. adminInfo := claims.(*system.SysAdmin)
  596. registerOB := new(fms.ContractRegister)
  597. item, e := registerOB.Fetch(req.ContractRegisterId)
  598. if e != nil {
  599. if e == utils.ErrNoRow {
  600. resp.Fail("合同登记不存在或已被删除", c)
  601. return
  602. }
  603. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  604. return
  605. }
  606. // 合同存在代付不允许开票/到款登记
  607. if item.HasPayment == 1 {
  608. resp.Fail("合同存在代付不允许添加开票/到款登记", c)
  609. return
  610. }
  611. noChangeInvoiceIds := make([]int, 0)
  612. newInvoice := make([]*fms.ContractInvoice, 0)
  613. if len(req.AmountList) > 0 {
  614. for i := range req.AmountList {
  615. if req.AmountList[i].Amount <= 0 {
  616. resp.Fail("登记金额有误", c)
  617. return
  618. }
  619. if req.AmountList[i].InvoiceDate == "" {
  620. resp.Fail("请选择日期", c)
  621. return
  622. }
  623. t, e := time.ParseInLocation(utils.FormatDate, req.AmountList[i].InvoiceDate, time.Local)
  624. if e != nil {
  625. resp.FailData("日期格式有误", "Err:"+e.Error(), c)
  626. return
  627. }
  628. if req.AmountList[i].InvoiceId > 0 {
  629. noChangeInvoiceIds = append(noChangeInvoiceIds, req.AmountList[i].InvoiceId)
  630. } else {
  631. v := &fms.ContractInvoice{
  632. ContractRegisterId: req.ContractRegisterId,
  633. ContractCode: item.ContractCode,
  634. Amount: req.AmountList[i].Amount,
  635. InvoiceType: req.InvoiceType,
  636. InvoiceDate: t,
  637. AdminId: int(adminInfo.AdminId),
  638. AdminName: adminInfo.RealName,
  639. Remark: req.AmountList[i].Remark,
  640. }
  641. v.Set()
  642. newInvoice = append(newInvoice, v)
  643. }
  644. }
  645. }
  646. // 获取原有的登记信息
  647. invoiceCond := `contract_register_id = ? AND invoice_type = ?`
  648. invoicePars := make([]interface{}, 0)
  649. invoicePars = append(invoicePars, req.ContractRegisterId, req.InvoiceType)
  650. originInvoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  651. if e != nil {
  652. resp.FailMsg("获取失败", "获取开票/到款列表失败, Err: "+e.Error(), c)
  653. return
  654. }
  655. // 比对原有和现有的登记信息
  656. logList := make([]*fms.ContractRegisterLog, 0)
  657. opData := ""
  658. opDataByte, e := json.Marshal(req)
  659. if e != nil {
  660. return
  661. }
  662. opData = string(opDataByte)
  663. opType := fms.ContractRegisterOpTypeInvoice
  664. if req.InvoiceType == fms.ContractInvoiceTypePay {
  665. opType = fms.ContractRegisterOpTypePayment
  666. }
  667. nowTime := time.Now().Local()
  668. // 需要删除的记录
  669. deleteInvoiceIds := make([]int, 0)
  670. for i := range originInvoiceList {
  671. if !utils.InArrayByInt(noChangeInvoiceIds, originInvoiceList[i].ContractInvoiceId) {
  672. deleteInvoiceIds = append(deleteInvoiceIds, originInvoiceList[i].ContractInvoiceId)
  673. logList = append(logList, &fms.ContractRegisterLog{
  674. ContractRegisterId: req.ContractRegisterId,
  675. AdminId: int(adminInfo.AdminId),
  676. AdminName: adminInfo.RealName,
  677. OpData: opData,
  678. OpType: opType,
  679. CreateTime: nowTime,
  680. AmountRemark: fmt.Sprint("删除", fms.ContractInvoiceKeyNameMap[opType], "金额", originInvoiceList[i].Amount, "元"),
  681. })
  682. }
  683. }
  684. // 新增的记录
  685. if len(newInvoice) > 0 {
  686. newAmount := decimal.NewFromFloat(0).Round(2)
  687. for i := range newInvoice {
  688. a := decimal.NewFromFloat(newInvoice[i].Amount).Round(2)
  689. newAmount = newAmount.Add(a)
  690. }
  691. ia, _ := newAmount.Round(2).Float64()
  692. logList = append(logList, &fms.ContractRegisterLog{
  693. ContractRegisterId: req.ContractRegisterId,
  694. AdminId: int(adminInfo.AdminId),
  695. AdminName: adminInfo.RealName,
  696. OpData: opData,
  697. OpType: opType,
  698. CreateTime: nowTime,
  699. AmountRemark: fmt.Sprint("新增", fms.ContractInvoiceKeyNameMap[opType], "金额", ia, "元"),
  700. })
  701. }
  702. // 删除并新增登记
  703. ob := new(fms.ContractInvoice)
  704. if e := ob.DeleteAndCreateNewInvoice(req.ContractRegisterId, req.InvoiceType, deleteInvoiceIds, newInvoice); e != nil {
  705. resp.FailData("日期格式有误", "Err:"+e.Error(), c)
  706. return
  707. }
  708. // 校验金额-是否修改状态
  709. go fmsService.CheckContractRegisterAmount(req.ContractRegisterId)
  710. // 操作日志
  711. go func() {
  712. logOB := new(fms.ContractRegisterLog)
  713. if e := logOB.AddInBatches(logList); e != nil {
  714. return
  715. }
  716. }()
  717. resp.Ok("操作成功", c)
  718. }
  719. // Export
  720. // @Title 合同登记-导出
  721. // @Description 合同登记-导出
  722. // @Param Keyword query string false "关键词"
  723. // @Param StartDate query string false "合同开始日期"
  724. // @Param EndDate query string false "合同结束日期"
  725. // @Param ServiceType query int false "套餐类型"
  726. // @Param ContractType query int false "合同类型"
  727. // @Param RegisterStatus query int false "登记状态"
  728. // @Success 200 string "操作成功"
  729. // @router /contract/register/export [get]
  730. func (rg *RegisterController) Export(c *gin.Context) {
  731. var req fms.ContractRegisterListReq
  732. if e := c.BindQuery(&req); e != nil {
  733. err, ok := e.(validator.ValidationErrors)
  734. if !ok {
  735. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  736. return
  737. }
  738. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  739. return
  740. }
  741. cond := `1 = 1`
  742. pars := make([]interface{}, 0)
  743. // 合同编号/客户姓名/销售/实际使用方
  744. if req.Keyword != "" {
  745. kw := "%" + req.Keyword + "%"
  746. cond += ` AND (company_name LIKE ? OR contract_code LIKE ? OR seller_name LIKE ? OR actual_company_name LIKE ?)`
  747. pars = append(pars, kw, kw, kw, kw)
  748. }
  749. if req.StartDate != "" && req.EndDate != "" {
  750. st := fmt.Sprint(req.StartDate, " 00:00:00")
  751. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  752. cond += ` AND (create_time BETWEEN ? AND ?)`
  753. pars = append(pars, st, ed)
  754. }
  755. if req.ContractType != 0 {
  756. cond += ` AND contract_type = ?`
  757. pars = append(pars, req.ContractType)
  758. }
  759. if req.RegisterStatus != 0 {
  760. cond += ` AND register_status = ?`
  761. pars = append(pars, req.RegisterStatus)
  762. }
  763. if req.ServiceType != 0 {
  764. registerIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
  765. if e != nil {
  766. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  767. return
  768. }
  769. if len(registerIds) > 0 {
  770. cond += ` AND contract_register_id IN ?`
  771. pars = append(pars, registerIds)
  772. } else {
  773. cond += ` AND 1 = 2`
  774. }
  775. }
  776. // 获取列表数据
  777. cr := new(fms.ContractRegister)
  778. list, e := cr.List(cond, pars)
  779. if e != nil {
  780. resp.FailData("获取合同列表失败", "Err:"+e.Error(), c)
  781. return
  782. }
  783. if len(list) == 0 {
  784. resp.Fail("无有效数据可导出", c)
  785. return
  786. }
  787. registerIds := make([]int, 0)
  788. for i := range list {
  789. registerIds = append(registerIds, list[i].ContractRegisterId)
  790. }
  791. // 获取小套餐品种
  792. cpCond := `product_id = ? AND permission_name <> ?`
  793. cpPars := make([]interface{}, 0)
  794. cpPars = append(cpPars, crm.CompanyProductFicc, crm.ChartPermissionStrategyName)
  795. cp := new(crm.ChartPermission)
  796. permissionList, e := cp.List(cpCond, cpPars)
  797. if e != nil {
  798. resp.FailData("获取小套餐品种失败", "Err:"+e.Error(), c)
  799. return
  800. }
  801. permissionLen := len(permissionList)
  802. permissionNameIdMap := make(map[string]int)
  803. for i := range permissionList {
  804. permissionNameIdMap[permissionList[i].PermissionName] = permissionList[i].ChartPermissionId
  805. }
  806. // 获取套餐服务
  807. //serviceList, e := fms.GetContractServiceTemplateMapByProductId(crm.CompanyProductFicc)
  808. //if e != nil {
  809. // resp.FailData("获取套餐服务失败", "Err:"+e.Error(), c)
  810. // return
  811. //}
  812. // 套餐/开票/到款列表
  813. serviceMap := make(map[int][]*fms.ContractService)
  814. serviceChartPermissionsMap := make(map[int][]int)
  815. invoiceMap := make(map[int][]*fms.ContractInvoice)
  816. paymentMap := make(map[int][]*fms.ContractInvoice)
  817. maxInvoice := 0
  818. maxPayment := 0
  819. if len(registerIds) > 0 {
  820. // 获取套餐信息
  821. csCond := `contract_register_id IN ?`
  822. csPars := make([]interface{}, 0)
  823. csPars = append(csPars, registerIds)
  824. cs := new(fms.ContractService)
  825. serviceList, e := cs.List(csCond, csPars)
  826. if e != nil {
  827. resp.FailData("获取合同套餐列表失败", "Err:"+e.Error(), c)
  828. return
  829. }
  830. for i := range serviceList {
  831. cid := serviceList[i].ContractRegisterId
  832. if serviceMap[cid] == nil {
  833. serviceMap[cid] = make([]*fms.ContractService, 0)
  834. }
  835. serviceMap[cid] = append(serviceMap[cid], serviceList[i])
  836. // 小套餐权限
  837. if serviceChartPermissionsMap[cid] == nil {
  838. serviceChartPermissionsMap[cid] = make([]int, 0)
  839. }
  840. if serviceList[i].ChartPermissionIds != "" {
  841. ids := utils.JoinStr2IntArr(serviceList[i].ChartPermissionIds, ",")
  842. serviceChartPermissionsMap[cid] = append(serviceChartPermissionsMap[cid], ids...)
  843. }
  844. }
  845. // 获取开票/到款详情, 并取最大的开票/到款数(用于动态扩展第二列表头)
  846. ci := new(fms.ContractInvoice)
  847. invoiceList, e := ci.List(csCond, csPars, "")
  848. if e != nil {
  849. resp.FailData("获取开票/到款列表失败", "Err:"+e.Error(), c)
  850. return
  851. }
  852. for k := range invoiceList {
  853. cid := invoiceList[k].ContractRegisterId
  854. if invoiceMap[cid] == nil {
  855. invoiceMap[cid] = make([]*fms.ContractInvoice, 0)
  856. }
  857. if paymentMap[cid] == nil {
  858. paymentMap[cid] = make([]*fms.ContractInvoice, 0)
  859. }
  860. if invoiceList[k].InvoiceType == fms.ContractInvoiceTypeMake {
  861. invoiceMap[cid] = append(invoiceMap[cid], invoiceList[k])
  862. continue
  863. }
  864. if invoiceList[k].InvoiceType == fms.ContractInvoiceTypePay {
  865. paymentMap[cid] = append(paymentMap[cid], invoiceList[k])
  866. continue
  867. }
  868. }
  869. // 取最大开票/到款数
  870. for j := range invoiceMap {
  871. if len(invoiceMap[j]) > maxInvoice {
  872. maxInvoice = len(invoiceMap[j])
  873. }
  874. }
  875. for p := range paymentMap {
  876. if len(paymentMap[p]) > maxPayment {
  877. maxPayment = len(paymentMap[p])
  878. }
  879. }
  880. }
  881. // 生成Excel文件
  882. //dir, err := os.Executable()
  883. //exPath := filepath.Dir(dir)
  884. //filePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  885. xlsxFile := xlsx.NewFile()
  886. //if err != nil {
  887. // resp.FailData("生成文件失败", "Err:"+err.Error(), c)
  888. // return
  889. //}
  890. style := xlsx.NewStyle()
  891. alignment := xlsx.Alignment{
  892. Horizontal: "center",
  893. Vertical: "center",
  894. WrapText: true,
  895. }
  896. style.Alignment = alignment
  897. style.ApplyAlignment = true
  898. sheet, err := xlsxFile.AddSheet("合同登记")
  899. if err != nil {
  900. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  901. return
  902. }
  903. //_ = sheet.SetColWidth(0, 0, 60)
  904. //_ = sheet.SetColWidth(4, 10, 60)
  905. // 1行表头
  906. titleRow := sheet.AddRow()
  907. titleRow.SetHeight(40)
  908. // 1行1列-右合并两格
  909. cell1 := titleRow.AddCell()
  910. cell1.HMerge = 2
  911. cell1.SetValue("FICC客户签约表 2022")
  912. cell1.SetStyle(style)
  913. // 右增两列空白格用于第一列合并, 否则后续单元格合并会有问题
  914. titleRow.AddCell().SetValue("")
  915. titleRow.AddCell().SetValue("")
  916. // 1行2列
  917. cell2 := titleRow.AddCell()
  918. cell2.SetValue("FICC大套餐")
  919. cell2.SetStyle(style)
  920. // 1行3列-右合并小套餐数
  921. if permissionLen >= 1 {
  922. cell3 := titleRow.AddCell()
  923. cell3.HMerge = permissionLen - 1
  924. cell3.SetValue("FICC小套餐")
  925. cell3.SetStyle(style)
  926. // 同上右增单元格小套餐数-1的空白单元格用于合并
  927. for i := 0; i < permissionLen-1; i++ {
  928. titleRow.AddCell().SetValue("")
  929. }
  930. }
  931. cell4 := titleRow.AddCell()
  932. cell4.SetValue("市场策略")
  933. cell4.SetStyle(style)
  934. cell5 := titleRow.AddCell()
  935. cell5.SetValue("财富管理")
  936. cell5.SetStyle(style)
  937. // 第二行表头
  938. titleRow2 := sheet.AddRow()
  939. titleRow2.SetHeight(60)
  940. row2Title := make([]string, 0)
  941. row2Title = append(row2Title, "客户名称", "续约-0\n新增-1", "销售", "FICC大套餐")
  942. for i := range permissionList {
  943. row2Title = append(row2Title, permissionList[i].PermissionName)
  944. }
  945. row2Title = append(row2Title, "市场策略", "财富管理", "套餐备注", "开始时间", "到期时间", "2022年合同金额", "约定付款时间", "签订日",
  946. "签订月", "合同状态", "合同编号", "备注")
  947. for i := range row2Title {
  948. v := titleRow2.AddCell()
  949. v.SetValue(row2Title[i])
  950. v.SetStyle(style)
  951. }
  952. // 第二行表头-开票/收款(动态添加)
  953. for i := 0; i < maxInvoice; i++ {
  954. n := i + 1
  955. c1 := titleRow2.AddCell()
  956. t1 := fmt.Sprintf("%s%d", "开票日", n)
  957. c1.SetValue(t1)
  958. c1.SetStyle(style)
  959. c2 := titleRow2.AddCell()
  960. t2 := fmt.Sprintf("%s%d", "开票金额", n)
  961. c2.SetValue(t2)
  962. c2.SetStyle(style)
  963. row2Title = append(row2Title, t1, t2)
  964. }
  965. for i := 0; i < maxPayment; i++ {
  966. n := i + 1
  967. c1 := titleRow2.AddCell()
  968. t1 := fmt.Sprintf("%s%d", "收款日", n)
  969. c1.SetValue(t1)
  970. c1.SetStyle(style)
  971. c2 := titleRow2.AddCell()
  972. t2 := fmt.Sprintf("%s%d", "收款金额", n)
  973. c2.SetValue(t2)
  974. c2.SetStyle(style)
  975. row2Title = append(row2Title, t1, t2)
  976. }
  977. // 此处取第二行标题NameKeyMap, 后面的动态匹配
  978. row2NameKeyMap := make(map[string]int)
  979. for i := range row2Title {
  980. row2NameKeyMap[row2Title[i]] = i
  981. }
  982. contractTMap := map[int]int{
  983. fms.ContractTypeNew: 1,
  984. fms.ContractTypeRenew: 0,
  985. fms.ContractTypeAgentPay: 2, // 代付合同
  986. fms.ContractTypePlus: 3, // 补充协议
  987. }
  988. for _, v := range list {
  989. k := -1
  990. dataRow := sheet.AddRow()
  991. dataRow.SetHeight(20)
  992. k += 3
  993. dataRow.AddCell().SetString(v.CompanyName)
  994. dataRow.AddCell().SetString(fmt.Sprint(contractTMap[v.ContractType]))
  995. dataRow.AddCell().SetString(v.SellerName)
  996. // 大套餐
  997. k += 1
  998. col4Name := row2Title[k]
  999. svList := serviceMap[v.ContractRegisterId]
  1000. col4 := ""
  1001. if svList != nil && len(svList) > 0 {
  1002. for isv := range svList {
  1003. if svList[isv].Title == col4Name {
  1004. col4 = "是"
  1005. break
  1006. }
  1007. }
  1008. }
  1009. dataRow.AddCell().SetString(col4)
  1010. // 小套餐
  1011. serviceChartPermissionIds := serviceChartPermissionsMap[v.ContractRegisterId]
  1012. for i := 0; i < permissionLen; i++ {
  1013. k += 1
  1014. colName := row2Title[k]
  1015. chartPermissionId := permissionNameIdMap[colName]
  1016. if utils.InArray(chartPermissionId, serviceChartPermissionIds) {
  1017. dataRow.AddCell().SetString("是")
  1018. } else {
  1019. dataRow.AddCell().SetString("")
  1020. }
  1021. }
  1022. // 财富管理/市场策略(处理方式其实跟上面的大套餐一样, 只是中间隔了小套餐按照顺序要这么处理=_=!)
  1023. k += 1
  1024. col5Name := row2Title[k]
  1025. col5 := ""
  1026. if svList != nil && len(svList) > 0 {
  1027. for isv := range svList {
  1028. if svList[isv].Title == col5Name {
  1029. col5 = "是"
  1030. break
  1031. }
  1032. }
  1033. }
  1034. dataRow.AddCell().SetString(col5)
  1035. k += 1
  1036. col6Name := row2Title[k]
  1037. col6 := ""
  1038. if svList != nil && len(svList) > 0 {
  1039. for isv := range svList {
  1040. if svList[isv].Title == col6Name {
  1041. col6 = "是"
  1042. break
  1043. }
  1044. }
  1045. }
  1046. dataRow.AddCell().SetString(col6)
  1047. // 其他信息
  1048. dataRow.AddCell().SetString(v.ServiceRemark) // 套餐备注
  1049. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.StartDate)) // 开始时间
  1050. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.EndDate)) // 到期时间
  1051. dataRow.AddCell().SetString(fmt.Sprint("¥", v.ContractAmount)) // 2022年合同金额
  1052. dataRow.AddCell().SetString(v.AgreedPayTime) // 约定付款时间
  1053. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.SignDate)) // 签订日
  1054. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01", v.SignDate)) // 签订月
  1055. dataRow.AddCell().SetString(fms.ContractStatusKeyNameMap[v.ContractStatus]) // 合同状态
  1056. dataRow.AddCell().SetString(v.ContractCode) // 合同编号
  1057. dataRow.AddCell().SetString(v.Remark) // 备注
  1058. // 开票/到款信息
  1059. ivList := invoiceMap[v.ContractRegisterId]
  1060. ivListLen := len(ivList)
  1061. if ivList != nil && len(ivList) > 0 {
  1062. for ia := 0; ia < maxInvoice; ia++ {
  1063. if ia < ivListLen {
  1064. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", ivList[ia].InvoiceDate)) // 开票日
  1065. dataRow.AddCell().SetString(fmt.Sprint(ivList[ia].Amount)) // 开票金额
  1066. } else {
  1067. // 这里要把不够的填充为空
  1068. dataRow.AddCell().SetString("")
  1069. dataRow.AddCell().SetString("")
  1070. }
  1071. }
  1072. }
  1073. pyList := paymentMap[v.ContractRegisterId]
  1074. pyListLen := len(pyList)
  1075. if pyList != nil && pyListLen > 0 {
  1076. for ib := 0; ib < maxInvoice; ib++ {
  1077. if ib < pyListLen {
  1078. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", pyList[ib].InvoiceDate)) // 收款日
  1079. dataRow.AddCell().SetString(fmt.Sprint(pyList[ib].Amount)) // 收款金额
  1080. } else {
  1081. // 已经是最后的几列了其实可以不用填充空, 万一后面要加列此处还是填充上吧
  1082. dataRow.AddCell().SetString("")
  1083. dataRow.AddCell().SetString("")
  1084. }
  1085. }
  1086. }
  1087. }
  1088. // 输出文件
  1089. var buffer bytes.Buffer
  1090. _ = xlsxFile.Write(&buffer)
  1091. content := bytes.NewReader(buffer.Bytes())
  1092. //err = xlsxFile.Save(filePath)
  1093. //if err != nil {
  1094. // resp.FailData("保存文件失败", "Err:"+err.Error(), c)
  1095. // return
  1096. //}
  1097. //defer func() {
  1098. // _ = os.Remove(filePath)
  1099. //}()
  1100. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1101. fileName := "财务列表_" + randStr + ".xlsx"
  1102. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  1103. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1104. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  1105. }
  1106. // InvoiceList
  1107. // @Title 开票/到款列表
  1108. // @Description 开票/到款列表
  1109. // @Param InvoiceType query int false "类型: 1-开票登记; 2-到款登记"
  1110. // @Param ContractCode query string false "合同编号"
  1111. // @Param StartDate query string false "开始日期"
  1112. // @Param EndDate query string false "结束日期"
  1113. // @Param MinAmount query float64 false "开票金额区间-最小值"
  1114. // @Param MaxAmount query float64 false "开票金额区间-最大值"
  1115. // @Success 200 {object} fms.ContractInvoiceItem
  1116. // @router /contract/register/invoice_list [get]
  1117. func (rg *RegisterController) InvoiceList(c *gin.Context) {
  1118. var req fms.ContractInvoiceListReq
  1119. if e := c.BindQuery(&req); e != nil {
  1120. err, ok := e.(validator.ValidationErrors)
  1121. if !ok {
  1122. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  1123. return
  1124. }
  1125. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1126. return
  1127. }
  1128. cond := `invoice_type = ?`
  1129. pars := make([]interface{}, 0)
  1130. pars = append(pars, req.InvoiceType)
  1131. // 合同编号
  1132. if req.ContractCode != "" {
  1133. kw := fmt.Sprint("%", req.ContractCode, "%")
  1134. cond += ` AND contract_code LIKE ?`
  1135. pars = append(pars, kw)
  1136. }
  1137. if req.StartDate != "" && req.EndDate != "" {
  1138. st := fmt.Sprint(req.StartDate, " 00:00:00")
  1139. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1140. cond += ` AND (invoice_time BETWEEN ? AND ?)`
  1141. pars = append(pars, st, ed)
  1142. }
  1143. if req.MinAmount > 0 {
  1144. cond += ` AND amount >= ?`
  1145. pars = append(pars, req.MinAmount)
  1146. }
  1147. if req.MaxAmount > 0 {
  1148. cond += ` AND amount <= ?`
  1149. pars = append(pars, req.MaxAmount)
  1150. }
  1151. page := new(base.Page)
  1152. page.SetPageSize(req.PageSize)
  1153. page.SetCurrent(req.Current)
  1154. page.AddOrderItem(base.OrderItem{Column: "invoice_time", Asc: false})
  1155. total, list, e := fms.GetContractInvoiceItemPageList(page, cond, pars)
  1156. if e != nil {
  1157. resp.FailMsg("获取失败", "获取合同开票/到款列表失败, Err: "+e.Error(), c)
  1158. return
  1159. }
  1160. page.SetTotal(total)
  1161. baseData := new(base.BaseData)
  1162. baseData.SetPage(page)
  1163. baseData.SetList(list)
  1164. resp.OkData("获取成功", baseData, c)
  1165. }
  1166. // InvoiceExport
  1167. // @Title 开票/到款列表-导出
  1168. // @Description 合同登记-导出
  1169. // @Param InvoiceType query int false "类型: 1-开票登记; 2-到款登记"
  1170. // @Param ContractCode query string false "合同编号"
  1171. // @Param StartDate query string false "开始日期"
  1172. // @Param EndDate query string false "结束日期"
  1173. // @Param MinAmount query float64 false "开票金额区间-最小值"
  1174. // @Param MaxAmount query float64 false "开票金额区间-最大值"
  1175. // @Success 200 string "操作成功"
  1176. // @router /contract/register/invoice_export [get]
  1177. func (rg *RegisterController) InvoiceExport(c *gin.Context) {
  1178. var req fms.ContractInvoiceListReq
  1179. if e := c.BindQuery(&req); e != nil {
  1180. err, ok := e.(validator.ValidationErrors)
  1181. if !ok {
  1182. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  1183. return
  1184. }
  1185. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1186. return
  1187. }
  1188. cond := `invoice_type = ?`
  1189. pars := make([]interface{}, 0)
  1190. pars = append(pars, req.InvoiceType)
  1191. // 合同编号
  1192. if req.ContractCode != "" {
  1193. kw := fmt.Sprint("%", req.ContractCode, "%")
  1194. cond += ` AND contract_code LIKE ?`
  1195. pars = append(pars, kw)
  1196. }
  1197. if req.StartDate != "" && req.EndDate != "" {
  1198. st := fmt.Sprint(req.StartDate, " 00:00:00")
  1199. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1200. cond += ` AND (invoice_time BETWEEN ? AND ?)`
  1201. pars = append(pars, st, ed)
  1202. }
  1203. if req.MinAmount > 0 {
  1204. cond += ` AND amount >= ?`
  1205. pars = append(pars, req.MinAmount)
  1206. }
  1207. if req.MaxAmount > 0 {
  1208. cond += ` AND amount <= ?`
  1209. pars = append(pars, req.MaxAmount)
  1210. }
  1211. // 获取列表数据
  1212. cr := new(fms.ContractInvoice)
  1213. orderRule := `invoice_time DESC`
  1214. list, e := cr.List(cond, pars, orderRule)
  1215. if e != nil {
  1216. resp.FailData("获取开票/到款列表失败", "Err:"+e.Error(), c)
  1217. return
  1218. }
  1219. if len(list) == 0 {
  1220. resp.Fail("无有效数据可导出", c)
  1221. return
  1222. }
  1223. // 生成Excel文件
  1224. xlsxFile := xlsx.NewFile()
  1225. style := xlsx.NewStyle()
  1226. alignment := xlsx.Alignment{
  1227. Horizontal: "center",
  1228. Vertical: "center",
  1229. WrapText: true,
  1230. }
  1231. style.Alignment = alignment
  1232. style.ApplyAlignment = true
  1233. sheet, err := xlsxFile.AddSheet("开票列表")
  1234. if err != nil {
  1235. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  1236. return
  1237. }
  1238. // 表头
  1239. titleRow := sheet.AddRow()
  1240. titleRow.SetHeight(40)
  1241. cell1 := titleRow.AddCell()
  1242. cell1.SetValue("合同编号")
  1243. cell1.SetStyle(style)
  1244. cell2 := titleRow.AddCell()
  1245. cell2.SetValue("开票金额")
  1246. cell2.SetStyle(style)
  1247. cell3 := titleRow.AddCell()
  1248. cell3.SetValue("开票日期")
  1249. cell3.SetStyle(style)
  1250. for _, v := range list {
  1251. dataRow := sheet.AddRow()
  1252. dataRow.SetHeight(20)
  1253. dataRow.AddCell().SetString(v.ContractCode)
  1254. dataRow.AddCell().SetString(fmt.Sprint(v.Amount))
  1255. dataRow.AddCell().SetString(utils.TimeTransferString("2006-01-02", v.InvoiceDate))
  1256. }
  1257. // 输出文件
  1258. var buffer bytes.Buffer
  1259. _ = xlsxFile.Write(&buffer)
  1260. content := bytes.NewReader(buffer.Bytes())
  1261. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1262. fileName := "开票列表_" + randStr + ".xlsx"
  1263. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  1264. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1265. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  1266. }
  1267. // Import
  1268. // @Title 合同登记-导入
  1269. // @Description 合同登记-导入
  1270. // @Success 200 string "操作成功"
  1271. // @router /contract/register/import [post]
  1272. func (rg *RegisterController) Import(c *gin.Context) {
  1273. h, err := c.FormFile("File")
  1274. if err != nil {
  1275. resp.FailData("获取文件失败", "Err:"+err.Error(), c)
  1276. return
  1277. }
  1278. ext := path.Ext(h.Filename)
  1279. if ext != ".xlsx" && ext != ".xls" {
  1280. resp.Fail("请上传Excel文件", c)
  1281. return
  1282. }
  1283. uploadDir := "static/xls"
  1284. err = os.MkdirAll(uploadDir, 766)
  1285. if err != nil {
  1286. resp.FailData("存储目录创建失败", "Err:"+err.Error(), c)
  1287. return
  1288. }
  1289. uploadPath := uploadDir + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + h.Filename
  1290. err = c.SaveUploadedFile(h, uploadPath)
  1291. if err != nil {
  1292. resp.FailData("保存本地文件失败", "Err:"+err.Error(), c)
  1293. return
  1294. }
  1295. defer func() {
  1296. _ = os.Remove(uploadPath)
  1297. }()
  1298. xlFile, err := xlsx.OpenFile(uploadPath)
  1299. if err != nil {
  1300. resp.FailData("打开文件失败", "Err:"+err.Error(), c)
  1301. return
  1302. }
  1303. // 获取所有已登记,根据合同编号去重
  1304. contractCodeArr := make([]string, 0)
  1305. registerOB := new(fms.ContractRegister)
  1306. registerCond := ``
  1307. registerPars := make([]interface{}, 0)
  1308. registerList, e := registerOB.List(registerCond, registerPars)
  1309. if e != nil {
  1310. resp.FailData("获取合同登记列表失败", "Err:"+e.Error(), c)
  1311. return
  1312. }
  1313. for i := range registerList {
  1314. contractCodeArr = append(contractCodeArr, registerList[i].ContractCode)
  1315. }
  1316. // 获取所有销售名称Map
  1317. sellerNameIdMap := make(map[string]int)
  1318. sellerCond := ``
  1319. sellerPars := make([]interface{}, 0)
  1320. sellerList, e := crm.GetCompanySellerByRoleCodes(sellerCond, sellerPars)
  1321. if e != nil {
  1322. resp.FailData("获取销售列表失败", "Err:"+e.Error(), c)
  1323. return
  1324. }
  1325. for i := range sellerList {
  1326. sellerNameIdMap[sellerList[i].RealName] = sellerList[i].AdminId
  1327. }
  1328. // 获取品种权限Map
  1329. chartPermissionNameIdMap := make(map[string]int)
  1330. cpCond := `product_id = ?`
  1331. cpPars := make([]interface{}, 0)
  1332. cpPars = append(cpPars, crm.CompanyProductFicc)
  1333. cp := new(crm.ChartPermission)
  1334. permissionList, e := cp.List(cpCond, cpPars)
  1335. if e != nil {
  1336. resp.FailData("获取权限列表失败", "Err:"+e.Error(), c)
  1337. return
  1338. }
  1339. for i := range permissionList {
  1340. chartPermissionNameIdMap[permissionList[i].PermissionName] = permissionList[i].ChartPermissionId
  1341. }
  1342. // 获取所有套餐模板
  1343. serviceTempCond := ``
  1344. serviceTempPars := make([]interface{}, 0)
  1345. serviceTempOB := new(fms.ContractServiceTemplate)
  1346. serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
  1347. if e != nil {
  1348. resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
  1349. return
  1350. }
  1351. serviceTempNameMap := make(map[string]*fms.ContractServiceTemplate)
  1352. for i := range serviceTempList {
  1353. serviceTempNameMap[serviceTempList[i].Title] = serviceTempList[i]
  1354. }
  1355. titleMap := make(map[int]string)
  1356. newIds := make([]int, 0)
  1357. contractTypeArr := []string{"0", "1", "2", "3"}
  1358. for _, sheet := range xlFile.Sheets {
  1359. // 遍历行读取
  1360. maxRow := sheet.MaxRow
  1361. for i := 0; i < maxRow; i++ {
  1362. // 第二行开始读取表头
  1363. if i == 1 {
  1364. row := sheet.Row(i)
  1365. cells := row.Cells
  1366. for k, cell := range cells {
  1367. text := cell.String()
  1368. titleMap[k] = text
  1369. // 只检验前面部分表头
  1370. if k == 0 {
  1371. if text != "客户名称" {
  1372. resp.Fail("匹配失败, 请按模板导入", c)
  1373. return
  1374. }
  1375. }
  1376. if k == 1 {
  1377. if !strings.Contains(text, "续约") {
  1378. resp.Fail("匹配失败, 请按模板导入", c)
  1379. return
  1380. }
  1381. }
  1382. if k == 2 {
  1383. if text != "销售" {
  1384. resp.Fail("匹配失败, 请按模板导入", c)
  1385. return
  1386. }
  1387. }
  1388. if k == 3 {
  1389. if text != "FICC大套餐" {
  1390. resp.Fail("匹配失败, 请按模板导入", c)
  1391. return
  1392. }
  1393. }
  1394. }
  1395. }
  1396. // 数据行
  1397. if i >= 2 {
  1398. row := sheet.Row(i)
  1399. cells := row.Cells
  1400. // 登记信息
  1401. rowRegister := new(fms.ContractRegister)
  1402. // 套餐
  1403. rowServices := make([]*fms.ContractService, 0)
  1404. // 开票/到款
  1405. rowInvoices := make([]*fms.ContractInvoice, 0)
  1406. rowChartPermissionIdArr := make([]string, 0)
  1407. rowInvoice1 := new(fms.ContractInvoice)
  1408. rowInvoice2 := new(fms.ContractInvoice)
  1409. rowInvoice3 := new(fms.ContractInvoice)
  1410. rowInvoice4 := new(fms.ContractInvoice)
  1411. rowInvoice5 := new(fms.ContractInvoice)
  1412. rowInvoice6 := new(fms.ContractInvoice)
  1413. for k, cell := range cells {
  1414. v := utils.TrimStr(cell.String())
  1415. // 客户名称
  1416. if k == 0 {
  1417. rowRegister.CompanyName = v
  1418. }
  1419. // 续约/新增
  1420. if k == 1 {
  1421. if !utils.InArrayByStr(contractTypeArr, v) {
  1422. resp.Fail("合同类型有误, 请按模板导入", c)
  1423. return
  1424. }
  1425. if v == "0" {
  1426. rowRegister.ContractType = fms.ContractTypeRenew
  1427. }
  1428. if v == "1" {
  1429. rowRegister.ContractType = fms.ContractTypeNew
  1430. }
  1431. if v == "2" {
  1432. rowRegister.ContractType = fms.ContractTypeAgentPay
  1433. }
  1434. if v == "3" {
  1435. rowRegister.ContractType = fms.ContractTypePlus
  1436. }
  1437. }
  1438. // 销售
  1439. if k == 2 {
  1440. rowRegister.SellerName = v
  1441. rowRegister.SellerId = sellerNameIdMap[rowRegister.SellerName]
  1442. }
  1443. // FICC大套餐
  1444. if k == 3 {
  1445. if v == "是" {
  1446. tempItem := serviceTempNameMap[titleMap[k]]
  1447. if tempItem == nil {
  1448. resp.Fail("套餐名称不匹配, 请按模板导入", c)
  1449. return
  1450. }
  1451. cs := &fms.ContractService{
  1452. ProductId: crm.CompanyProductFicc,
  1453. ServiceTemplateId: tempItem.ServiceTemplateId,
  1454. Title: tempItem.Title,
  1455. Value: tempItem.Value,
  1456. TableValue: tempItem.TableValue,
  1457. ChartPermissionId: tempItem.ChartPermissionId,
  1458. }
  1459. cs.Set()
  1460. rowServices = append(rowServices, cs)
  1461. }
  1462. }
  1463. // FICC小套餐
  1464. if k >= 4 && k <= 23 {
  1465. // 小套餐权限
  1466. if v == "是" {
  1467. rowChartPermissionIdArr = append(rowChartPermissionIdArr, strconv.Itoa(chartPermissionNameIdMap[titleMap[k]]))
  1468. }
  1469. }
  1470. // 市场策略/财富管理
  1471. if k == 24 || k == 25 {
  1472. if v == "是" {
  1473. tempItem := serviceTempNameMap[titleMap[k]]
  1474. if tempItem == nil {
  1475. resp.Fail("市场策略/财富管理套餐名称不匹配, 请按模板导入", c)
  1476. return
  1477. }
  1478. cs := &fms.ContractService{
  1479. ProductId: crm.CompanyProductFicc,
  1480. ServiceTemplateId: tempItem.ServiceTemplateId,
  1481. Title: tempItem.Title,
  1482. Value: tempItem.Value,
  1483. TableValue: tempItem.TableValue,
  1484. ChartPermissionId: tempItem.ChartPermissionId,
  1485. }
  1486. cs.Set()
  1487. rowServices = append(rowServices, cs)
  1488. }
  1489. }
  1490. // 套餐备注
  1491. if k == 26 {
  1492. rowRegister.ServiceRemark = v
  1493. }
  1494. // 开始时间/到期时间
  1495. if k == 27 {
  1496. startDate, e := time.ParseInLocation("2006/01/02", v, time.Local)
  1497. if e != nil {
  1498. resp.Fail("开始时间格式有误, 请按模板导入", c)
  1499. return
  1500. }
  1501. rowRegister.StartDate = startDate
  1502. }
  1503. if k == 28 {
  1504. endDate, e := time.ParseInLocation("2006/01/02", v, time.Local)
  1505. if e != nil {
  1506. resp.Fail("到期时间格式有误, 请按模板导入", c)
  1507. return
  1508. }
  1509. rowRegister.EndDate = endDate
  1510. }
  1511. // 合同金额
  1512. if k == 29 {
  1513. amountStr := v
  1514. amount, e := strconv.ParseFloat(amountStr, 64)
  1515. if e != nil {
  1516. resp.Fail("合同金额有误, 请按模板导入", c)
  1517. return
  1518. }
  1519. rowRegister.ContractAmount = amount
  1520. }
  1521. // 约定付款日期
  1522. if k == 30 {
  1523. rowRegister.AgreedPayTime = v
  1524. }
  1525. // 签订日
  1526. // k == 32为签订月,可忽略
  1527. if k == 31 {
  1528. signDate, e := time.ParseInLocation("2006/01/02", v, time.Local)
  1529. if e != nil {
  1530. resp.Fail("开始时间格式有误, 请按模板导入", c)
  1531. return
  1532. }
  1533. rowRegister.SignDate = signDate
  1534. }
  1535. // 合同状态
  1536. if k == 33 {
  1537. rowRegister.ContractStatus = fms.ContractStatusNameKeyMap[v]
  1538. }
  1539. // 合同编号
  1540. if k == 34 {
  1541. rowContractCode := v
  1542. if rowContractCode == "" {
  1543. resp.Fail("合同编号不可为空, 请按模板导入", c)
  1544. return
  1545. }
  1546. if utils.InArrayByStr(contractCodeArr, rowContractCode) {
  1547. // 此合同已登记, 跳过本行的读取
  1548. break
  1549. }
  1550. rowRegister.ContractCode = rowContractCode
  1551. }
  1552. // 备注
  1553. if k == 35 {
  1554. rowRegister.Remark = v
  1555. }
  1556. // 开票日/开票金额
  1557. if k == 36 {
  1558. if v != "" {
  1559. fmt.Println("36", v)
  1560. invoiceDate, e := time.ParseInLocation("2006/01/02", v, time.Local)
  1561. if e != nil {
  1562. resp.Fail("开票时间1格式有误, 请按模板导入", c)
  1563. return
  1564. }
  1565. rowInvoice1.InvoiceDate = invoiceDate
  1566. rowInvoice1.ContractCode = rowRegister.ContractCode
  1567. rowInvoice1.InvoiceType = fms.ContractInvoiceTypeMake
  1568. }
  1569. }
  1570. if k == 37 && v != "" {
  1571. amountStr := v
  1572. amount, e := strconv.ParseFloat(amountStr, 64)
  1573. if e != nil {
  1574. resp.Fail("开票金额1有误, 请按模板导入", c)
  1575. return
  1576. }
  1577. rowInvoice1.Amount = amount
  1578. rowInvoices = append(rowInvoices, rowInvoice1)
  1579. }
  1580. if k == 38 {
  1581. if v != "" {
  1582. invoiceDate, e := time.ParseInLocation("2006/01/02", v, time.Local)
  1583. if e != nil {
  1584. resp.Fail("开票时间2格式有误, 请按模板导入", c)
  1585. return
  1586. }
  1587. rowInvoice2.InvoiceDate = invoiceDate
  1588. rowInvoice2.ContractCode = rowRegister.ContractCode
  1589. rowInvoice2.InvoiceType = fms.ContractInvoiceTypeMake
  1590. }
  1591. }
  1592. if k == 39 && v != "" {
  1593. amountStr := v
  1594. amount, e := strconv.ParseFloat(amountStr, 64)
  1595. if e != nil {
  1596. resp.Fail("开票金额2有误, 请按模板导入", c)
  1597. return
  1598. }
  1599. rowInvoice2.Amount = amount
  1600. rowInvoices = append(rowInvoices, rowInvoice2)
  1601. }
  1602. if k == 40 {
  1603. if v != "" {
  1604. invoiceDate, e := time.ParseInLocation("2006/01/02", v, time.Local)
  1605. if e != nil {
  1606. resp.Fail("开票时间3格式有误, 请按模板导入", c)
  1607. return
  1608. }
  1609. rowInvoice3.InvoiceDate = invoiceDate
  1610. rowInvoice3.ContractCode = rowRegister.ContractCode
  1611. rowInvoice3.InvoiceType = fms.ContractInvoiceTypeMake
  1612. }
  1613. }
  1614. if k == 41 && v != "" {
  1615. amountStr := v
  1616. amount, e := strconv.ParseFloat(amountStr, 64)
  1617. if e != nil {
  1618. resp.Fail("开票金额3有误, 请按模板导入", c)
  1619. return
  1620. }
  1621. rowInvoice3.Amount = amount
  1622. rowInvoices = append(rowInvoices, rowInvoice3)
  1623. }
  1624. // 收款日/收款金额
  1625. if k == 42 {
  1626. if v != "" {
  1627. invoiceDate, e := time.ParseInLocation("2006/01/02", v, time.Local)
  1628. if e != nil {
  1629. resp.Fail("收款时间1格式有误, 请按模板导入", c)
  1630. return
  1631. }
  1632. rowInvoice4.InvoiceDate = invoiceDate
  1633. rowInvoice4.ContractCode = rowRegister.ContractCode
  1634. rowInvoice4.InvoiceType = fms.ContractInvoiceTypePay
  1635. }
  1636. }
  1637. if k == 43 && v != "" {
  1638. amountStr := v
  1639. amount, e := strconv.ParseFloat(amountStr, 64)
  1640. if e != nil {
  1641. resp.Fail("收款金额1有误, 请按模板导入", c)
  1642. return
  1643. }
  1644. rowInvoice4.Amount = amount
  1645. rowInvoices = append(rowInvoices, rowInvoice4)
  1646. }
  1647. if k == 44 {
  1648. if v != "" {
  1649. invoiceDate, e := time.ParseInLocation("2006/01/02", v, time.Local)
  1650. if e != nil {
  1651. resp.Fail("收款时间2格式有误, 请按模板导入", c)
  1652. return
  1653. }
  1654. rowInvoice5.InvoiceDate = invoiceDate
  1655. rowInvoice5.ContractCode = rowRegister.ContractCode
  1656. rowInvoice5.InvoiceType = fms.ContractInvoiceTypePay
  1657. }
  1658. }
  1659. if k == 45 && v != "" {
  1660. amountStr := v
  1661. amount, e := strconv.ParseFloat(amountStr, 64)
  1662. if e != nil {
  1663. resp.Fail("收款金额2有误, 请按模板导入", c)
  1664. return
  1665. }
  1666. rowInvoice5.Amount = amount
  1667. rowInvoices = append(rowInvoices, rowInvoice5)
  1668. }
  1669. if k == 46 {
  1670. if v != "" {
  1671. invoiceDate, e := time.ParseInLocation("2006/01/02", v, time.Local)
  1672. if e != nil {
  1673. resp.Fail("收款时间3格式有误, 请按模板导入", c)
  1674. return
  1675. }
  1676. rowInvoice6.InvoiceDate = invoiceDate
  1677. rowInvoice6.ContractCode = rowRegister.ContractCode
  1678. rowInvoice6.InvoiceType = fms.ContractInvoiceTypePay
  1679. }
  1680. }
  1681. if k == 47 && v != "" {
  1682. amountStr := v
  1683. amount, e := strconv.ParseFloat(amountStr, 64)
  1684. if e != nil {
  1685. resp.Fail("收款金额3有误, 请按模板导入", c)
  1686. return
  1687. }
  1688. rowInvoice6.Amount = amount
  1689. rowInvoices = append(rowInvoices, rowInvoice6)
  1690. }
  1691. }
  1692. // 小套餐
  1693. if len(rowChartPermissionIdArr) > 0 {
  1694. // 说明有小套餐
  1695. tempItem := serviceTempNameMap["FICC小套餐"]
  1696. if tempItem == nil {
  1697. resp.Fail("小套餐名称不匹配, 请按模板导入", c)
  1698. return
  1699. }
  1700. rowChartPermissionIds := strings.Join(rowChartPermissionIdArr, ",")
  1701. cs := &fms.ContractService{
  1702. ProductId: crm.CompanyProductFicc,
  1703. ServiceTemplateId: tempItem.ServiceTemplateId,
  1704. Title: tempItem.Title,
  1705. Value: tempItem.Value,
  1706. TableValue: tempItem.TableValue,
  1707. ChartPermissionId: tempItem.ChartPermissionId,
  1708. ChartPermissionIds: rowChartPermissionIds,
  1709. }
  1710. cs.Set()
  1711. rowServices = append(rowServices, cs)
  1712. }
  1713. // 新增登记、套餐、开票到款信息
  1714. newId, e := fms.CreateImportContractRegister(rowRegister, rowServices, rowInvoices)
  1715. if e != nil {
  1716. resp.FailData("导入失败", "新增导入登记失败, Err: "+e.Error(), c)
  1717. return
  1718. }
  1719. if rowRegister.ContractType != fms.ContractTypePlus {
  1720. newIds = append(newIds, newId)
  1721. }
  1722. }
  1723. }
  1724. }
  1725. // 更新登记状态
  1726. go func() {
  1727. for i := range newIds {
  1728. fmsService.CheckContractRegisterAmount(newIds[i])
  1729. }
  1730. }()
  1731. resp.Ok("操作成功", c)
  1732. }