register.go 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  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. "time"
  20. )
  21. // RegisterController 合同登记
  22. type RegisterController struct{}
  23. // List
  24. // @Title 合同登记列表
  25. // @Description 合同登记列表
  26. // @Param Keyword query string false "关键词"
  27. // @Param StartDate query string false "合同开始日期"
  28. // @Param EndDate query string false "合同结束日期"
  29. // @Param ServiceType query int false "套餐类型"
  30. // @Param ContractType query int false "合同类型"
  31. // @Param RegisterStatus query int false "登记状态"
  32. // @Success 200 {object} fms.ContractRegisterItem
  33. // @router /contract/register/list [get]
  34. func (rg *RegisterController) List(c *gin.Context) {
  35. var req fms.ContractRegisterListReq
  36. if e := c.BindQuery(&req); e != nil {
  37. err, ok := e.(validator.ValidationErrors)
  38. if !ok {
  39. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  40. return
  41. }
  42. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  43. return
  44. }
  45. cond := `1 = 1`
  46. pars := make([]interface{}, 0)
  47. // 合同编号/客户姓名/销售/代付方
  48. if req.Keyword != "" {
  49. kw := "%" + req.Keyword + "%"
  50. cond += ` AND (company_name LIKE ? OR contract_code LIKE ? OR seller_name LIKE ? OR pay_company_name LIKE ?)`
  51. pars = append(pars, kw, kw, kw)
  52. }
  53. if req.StartDate != "" && req.EndDate != "" {
  54. st := fmt.Sprint(req.StartDate, " 00:00:00")
  55. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  56. cond += ` AND (create_time BETWEEN ? AND ?)`
  57. pars = append(pars, st, ed)
  58. }
  59. if req.ContractType != 0 {
  60. cond += ` AND contract_type = ?`
  61. pars = append(pars, req.ContractType)
  62. }
  63. if req.RegisterStatus != 0 {
  64. cond += ` AND register_status = ?`
  65. pars = append(pars, req.RegisterStatus)
  66. }
  67. // 套餐筛选
  68. if req.ServiceType != 0 {
  69. registerIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
  70. if e != nil {
  71. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  72. return
  73. }
  74. if len(registerIds) > 0 {
  75. cond += ` AND contract_register_id IN ?`
  76. pars = append(pars, registerIds)
  77. } else {
  78. cond += ` AND 1 = 2`
  79. }
  80. }
  81. page := new(base.Page)
  82. page.SetPageSize(req.PageSize)
  83. page.SetCurrent(req.Current)
  84. page.AddOrderItem(base.OrderItem{Column: "create_time", Asc: false})
  85. total, list, e := fms.GetContractRegisterItemPageList(page, cond, pars)
  86. if e != nil {
  87. resp.FailMsg("获取失败", "获取合同登记列表失败, Err: "+e.Error(), c)
  88. return
  89. }
  90. registerIds := make([]int, 0)
  91. for i := range list {
  92. registerIds = append(registerIds, list[i].ContractRegisterId)
  93. }
  94. serviceMap := make(map[int]string, 0)
  95. invoiceMap := make(map[int][]*fms.ContractInvoiceItem, 0)
  96. paymentMap := make(map[int][]*fms.ContractInvoiceItem, 0)
  97. if len(registerIds) > 0 {
  98. // 获取服务套餐
  99. servicesNameList, e := fms.GetContractRegisterServicesNameByRegisterIds(registerIds)
  100. if e != nil {
  101. resp.FailMsg("获取失败", "获取套餐拼接字符串失败, Err: "+e.Error(), c)
  102. return
  103. }
  104. for i := range servicesNameList {
  105. serviceMap[servicesNameList[i].ContractRegisterId] = servicesNameList[i].ServicesName
  106. }
  107. // 获取开票/到款列表
  108. invoiceCond := `contract_register_id IN ?`
  109. invoicePars := make([]interface{}, 0)
  110. invoicePars = append(invoicePars, registerIds)
  111. invoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  112. if e != nil {
  113. resp.FailMsg("获取失败", "获取开票/到款列表失败, Err: "+e.Error(), c)
  114. return
  115. }
  116. for i := range invoiceList {
  117. if invoiceMap[invoiceList[i].ContractRegisterId] == nil {
  118. invoiceMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoiceItem, 0)
  119. }
  120. if paymentMap[invoiceList[i].ContractRegisterId] == nil {
  121. paymentMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoiceItem, 0)
  122. }
  123. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  124. invoiceMap[invoiceList[i].ContractRegisterId] = append(invoiceMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  125. }
  126. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  127. paymentMap[invoiceList[i].ContractRegisterId] = append(paymentMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  128. }
  129. }
  130. }
  131. respList := make([]*fms.ContractRegisterList, 0)
  132. for i := range list {
  133. v := new(fms.ContractRegisterList)
  134. v.ContractRegisterItem = list[i]
  135. v.ServicesName = serviceMap[list[i].ContractRegisterId]
  136. v.InvoiceList = invoiceMap[list[i].ContractRegisterId]
  137. v.PaymentList = paymentMap[list[i].ContractRegisterId]
  138. respList = append(respList, v)
  139. }
  140. page.SetTotal(total)
  141. baseData := new(base.BaseData)
  142. baseData.SetPage(page)
  143. baseData.SetList(respList)
  144. resp.OkData("获取成功", baseData, c)
  145. }
  146. // Add
  147. // @Title 新增合同登记
  148. // @Description 新增合同登记
  149. // @Param request body fms.ContractRegisterAddReq true "type json string"
  150. // @Success 200 string "操作成功"
  151. // @router /contract/register/add [post]
  152. func (rg *RegisterController) Add(c *gin.Context) {
  153. req := new(fms.ContractRegisterAddReq)
  154. err := c.ShouldBind(&req)
  155. if err != nil {
  156. errs, ok := err.(validator.ValidationErrors)
  157. if !ok {
  158. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  159. return
  160. }
  161. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  162. return
  163. }
  164. claims, _ := c.Get("adminInfo")
  165. adminInfo := claims.(*system.SysAdmin)
  166. // 日期校验
  167. startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  168. if e != nil {
  169. resp.FailMsg("合同开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
  170. return
  171. }
  172. endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  173. if e != nil {
  174. resp.FailMsg("合同结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
  175. return
  176. }
  177. signDate, e := time.ParseInLocation(utils.FormatDate, req.SignDate, time.Local)
  178. if e != nil {
  179. resp.FailMsg("合同签订日期格式有误", "合同签订日期格式有误, Err: "+e.Error(), c)
  180. return
  181. }
  182. // 是否存在相同合同编号的登记
  183. ob := new(fms.ContractRegister)
  184. existCond := `contract_code = ?`
  185. existPars := make([]interface{}, 0)
  186. existPars = append(existPars, req.ContractCode)
  187. exist, e := ob.FetchByCondition(existCond, existPars)
  188. if e != nil && e != utils.ErrNoRow {
  189. resp.FailMsg("操作失败", "获取相同登记号失败, Err: "+e.Error(), c)
  190. return
  191. }
  192. if exist != nil && exist.ContractRegisterId > 0 {
  193. resp.Fail("合同编号已存在", c)
  194. return
  195. }
  196. nowTime := time.Now().Local()
  197. ob.ContractCode = req.ContractCode
  198. ob.PayContractCode = req.PayContractCode
  199. ob.CrmContractId = req.CrmContractId
  200. ob.ContractSource = req.ContractSource
  201. ob.CompanyName = req.CompanyName
  202. ob.PayCompanyName = req.PayCompanyName
  203. ob.ProductId = req.ProductId
  204. ob.SellerId = req.SellerId
  205. ob.SellerName = req.SellerName
  206. ob.ContractType = req.ContractType
  207. ob.ContractAmount = req.ContractAmount
  208. ob.StartDate = startDate
  209. ob.EndDate = endDate
  210. ob.SignDate = signDate
  211. ob.AgreedPayTime = req.AgreedPayTime
  212. ob.ContractStatus = req.ContractStatus
  213. ob.RegisterStatus = fms.ContractRegisterStatusIng
  214. ob.Remark = req.Remark
  215. ob.Set()
  216. // 补充协议直接完成
  217. if req.ContractType == fms.ContractTypePlus {
  218. ob.RegisterStatus = fms.ContractRegisterStatusComplete
  219. }
  220. // 套餐信息
  221. serviceList, e := fmsService.HandleContractServiceAndDetail(req.ProductId, req.Services, true)
  222. if e != nil {
  223. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  224. return
  225. }
  226. // 新增合同及套餐
  227. if e = fms.CreateContractRegisterAndServices(ob, serviceList); e != nil {
  228. resp.FailMsg("操作失败", "新增合同及套餐失败, Err: "+e.Error(), c)
  229. return
  230. }
  231. // 操作日志
  232. go func() {
  233. opData := ""
  234. opDataByte, e := json.Marshal(req)
  235. if e != nil {
  236. return
  237. }
  238. opData = string(opDataByte)
  239. logItem := new(fms.ContractRegisterLog)
  240. logItem.ContractRegisterId = ob.ContractRegisterId
  241. logItem.AdminId = int(adminInfo.AdminId)
  242. logItem.AdminName = adminInfo.RealName
  243. logItem.OpData = opData
  244. logItem.OpType = fms.ContractRegisterOpTypeSave
  245. logItem.CreateTime = nowTime
  246. if e = logItem.Create(); e != nil {
  247. return
  248. }
  249. }()
  250. resp.Ok("操作成功", c)
  251. }
  252. // Edit
  253. // @Title 编辑合同登记
  254. // @Description 编辑合同登记
  255. // @Param request body fms.ContractRegisterEditReq true "type json string"
  256. // @Success 200 string "操作成功"
  257. // @router /contract/register/edit [post]
  258. func (rg *RegisterController) Edit(c *gin.Context) {
  259. req := new(fms.ContractRegisterEditReq)
  260. err := c.ShouldBind(&req)
  261. if err != nil {
  262. errs, ok := err.(validator.ValidationErrors)
  263. if !ok {
  264. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  265. return
  266. }
  267. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  268. return
  269. }
  270. claims, _ := c.Get("adminInfo")
  271. adminInfo := claims.(*system.SysAdmin)
  272. // 日期校验
  273. startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  274. if e != nil {
  275. resp.FailMsg("合同开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
  276. return
  277. }
  278. endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  279. if e != nil {
  280. resp.FailMsg("合同结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
  281. return
  282. }
  283. signDate, e := time.ParseInLocation(utils.FormatDate, req.SignDate, time.Local)
  284. if e != nil {
  285. resp.FailMsg("合同签订日期格式有误", "合同签订日期格式有误, Err: "+e.Error(), c)
  286. return
  287. }
  288. ob := new(fms.ContractRegister)
  289. item, e := ob.Fetch(req.ContractRegisterId)
  290. if e != nil {
  291. if e == utils.ErrNoRow {
  292. resp.Fail("登记记录不存在或已被删除", c)
  293. return
  294. }
  295. resp.FailMsg("操作失败", "获取合同登记信息失败, Err:"+e.Error(), c)
  296. return
  297. }
  298. // 是否存在相同合同编号的登记
  299. existCond := `contract_code = ?`
  300. existPars := make([]interface{}, 0)
  301. existPars = append(existPars, req.ContractCode)
  302. exist, e := ob.FetchByCondition(existCond, existPars)
  303. if e != nil && e != utils.ErrNoRow {
  304. resp.FailMsg("操作失败", "获取相同登记号失败, Err: "+e.Error(), c)
  305. return
  306. }
  307. if exist != nil && exist.ContractRegisterId > 0 && exist.ContractRegisterId != item.ContractRegisterId {
  308. resp.Fail("合同编号已存在", c)
  309. return
  310. }
  311. nowTime := time.Now().Local()
  312. item.ContractCode = req.ContractCode
  313. item.PayContractCode = req.PayContractCode
  314. item.CrmContractId = req.CrmContractId
  315. item.ContractSource = req.ContractSource
  316. item.CompanyName = req.CompanyName
  317. item.PayCompanyName = req.PayCompanyName
  318. item.SellerId = req.SellerId
  319. item.SellerName = req.SellerName
  320. item.ContractType = req.ContractType
  321. item.ContractAmount = req.ContractAmount
  322. item.StartDate = startDate
  323. item.EndDate = endDate
  324. item.SignDate = signDate
  325. item.AgreedPayTime = req.AgreedPayTime
  326. item.ContractStatus = req.ContractStatus
  327. item.RegisterStatus = fms.ContractRegisterStatusIng
  328. // 补充协议直接完成
  329. if req.ContractType == fms.ContractTypePlus {
  330. item.RegisterStatus = fms.ContractRegisterStatusComplete
  331. }
  332. item.Remark = req.Remark
  333. item.ModifyTime = nowTime
  334. updateCols := []string{
  335. "ContractCode", "CrmContractId", "ContractSource", "CompanyName", "SellerId", "SellerName",
  336. "ContractType", "ContractAmount", "StartDate", "EndDate", "SignDate", "AgreedPayTime", "ContractStatus",
  337. "RegisterStatus", "Remark", "ModifyTime",
  338. }
  339. // 套餐信息
  340. serviceList, e := fmsService.HandleContractServiceAndDetail(req.ProductId, req.Services, true)
  341. if e != nil {
  342. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  343. return
  344. }
  345. // 更新合同及套餐
  346. if e = fms.UpdateContractRegisterAndServices(item, updateCols, serviceList); e != nil {
  347. resp.FailMsg("操作失败", "更新合同及套餐失败, Err: "+e.Error(), c)
  348. return
  349. }
  350. // 非补充协议校验金额-是否修改状态
  351. if item.ContractType != fms.ContractTypePlus {
  352. go fmsService.CheckContractRegisterAmount(item.ContractRegisterId)
  353. }
  354. // 操作日志
  355. go func() {
  356. opData := ""
  357. opDataByte, e := json.Marshal(req)
  358. if e != nil {
  359. return
  360. }
  361. opData = string(opDataByte)
  362. logItem := new(fms.ContractRegisterLog)
  363. logItem.ContractRegisterId = item.ContractRegisterId
  364. logItem.AdminId = int(adminInfo.AdminId)
  365. logItem.AdminName = adminInfo.RealName
  366. logItem.OpData = opData
  367. logItem.OpType = fms.ContractRegisterOpTypeEdit
  368. logItem.CreateTime = nowTime
  369. if e = logItem.Create(); e != nil {
  370. return
  371. }
  372. }()
  373. resp.Ok("操作成功", c)
  374. }
  375. // Del
  376. // @Title 删除合同登记
  377. // @Description 删除合同登记
  378. // @Param request body fms.ContractRegisterDelReq true "type json string"
  379. // @Success 200 string "操作成功"
  380. // @router /contract/register/del [post]
  381. func (rg *RegisterController) Del(c *gin.Context) {
  382. req := new(fms.ContractRegisterDelReq)
  383. err := c.ShouldBind(&req)
  384. if err != nil {
  385. errs, ok := err.(validator.ValidationErrors)
  386. if !ok {
  387. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  388. return
  389. }
  390. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  391. return
  392. }
  393. claims, _ := c.Get("adminInfo")
  394. adminInfo := claims.(*system.SysAdmin)
  395. ob := new(fms.ContractRegister)
  396. item, e := ob.Fetch(req.ContractRegisterId)
  397. if e != nil {
  398. if e == utils.ErrNoRow {
  399. resp.Fail("合同登记不存在或已被删除", c)
  400. return
  401. }
  402. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  403. return
  404. }
  405. nowTime := time.Now().Local()
  406. item.IsDeleted = 1
  407. item.ModifyTime = nowTime
  408. updateCols := []string{"IsDeleted", "ModifyTime"}
  409. if e = item.Update(updateCols); e != nil {
  410. resp.FailMsg("操作失败", "更新合同登记失败, Err:"+e.Error(), c)
  411. return
  412. }
  413. // 操作日志
  414. go func() {
  415. opData := ""
  416. opDataByte, e := json.Marshal(req)
  417. if e != nil {
  418. return
  419. }
  420. opData = string(opDataByte)
  421. logItem := new(fms.ContractRegisterLog)
  422. logItem.ContractRegisterId = req.ContractRegisterId
  423. logItem.AdminId = int(adminInfo.AdminId)
  424. logItem.AdminName = adminInfo.RealName
  425. logItem.OpData = opData
  426. logItem.OpType = fms.ContractRegisterOpTypeDel
  427. logItem.CreateTime = nowTime
  428. if e = logItem.Create(); e != nil {
  429. return
  430. }
  431. }()
  432. resp.Ok("操作成功", c)
  433. }
  434. // Detail
  435. // @Title 合同登记详情
  436. // @Description 合同登记详情
  437. // @Param ContractRegisterId query int false "合同登记ID"
  438. // @Success 200 {object} fms.ContractRegisterDetail
  439. // @router /contract/register/detail [get]
  440. func (rg *RegisterController) Detail(c *gin.Context) {
  441. var req fms.ContractRegisterDetailReq
  442. if e := c.BindQuery(&req); e != nil {
  443. err, ok := e.(validator.ValidationErrors)
  444. if !ok {
  445. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  446. return
  447. }
  448. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  449. return
  450. }
  451. result := new(fms.ContractRegisterDetail)
  452. // 合同登记信息
  453. item, e := fms.GetContractRegisterItemById(req.ContractRegisterId)
  454. if e != nil {
  455. resp.FailData("获取失败", "获取合同登记详情失败, Err:"+e.Error(), c)
  456. return
  457. }
  458. result.ContractRegisterItem = item
  459. // 套餐信息
  460. serviceList, e := fmsService.GetContractServiceAndDetail(req.ContractRegisterId)
  461. if e != nil {
  462. resp.FailData("获取失败", "获取合同套餐信息失败, Err: "+e.Error(), c)
  463. return
  464. }
  465. result.ServiceList = serviceList
  466. // 开票/到款信息
  467. invoiceCond := `contract_register_id = ?`
  468. invoicePars := make([]interface{}, 0)
  469. invoicePars = append(invoicePars, req.ContractRegisterId)
  470. invoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  471. if e != nil {
  472. resp.FailData("获取失败", "获取合同开票/到款信息失败, Err: "+e.Error(), c)
  473. return
  474. }
  475. result.InvoiceList = make([]*fms.ContractInvoiceItem, 0)
  476. result.PaymentList = make([]*fms.ContractInvoiceItem, 0)
  477. for i := range invoiceList {
  478. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  479. result.InvoiceList = append(result.InvoiceList, invoiceList[i])
  480. continue
  481. }
  482. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  483. result.PaymentList = append(result.PaymentList, invoiceList[i])
  484. }
  485. }
  486. // 合同登记进度
  487. logCond := `contract_register_id = ?`
  488. logPars := make([]interface{}, 0)
  489. logPars = append(logPars, req.ContractRegisterId)
  490. logList, e := fms.GetContractRegisterLogItemList(logCond, logPars)
  491. if e != nil {
  492. resp.FailData("获取失败", "获取合同登记进度失败, Err: "+e.Error(), c)
  493. return
  494. }
  495. result.Logs = logList
  496. resp.OkData("获取成功", result, c)
  497. }
  498. // UpdateStatus
  499. // @Title 修改合同状态
  500. // @Description 修改合同状态
  501. // @Param request body fms.ContractRegisterUpdateStatusReq true "type json string"
  502. // @Success 200 string "操作成功"
  503. // @router /contract/register/update_status [post]
  504. func (rg *RegisterController) UpdateStatus(c *gin.Context) {
  505. req := new(fms.ContractRegisterUpdateStatusReq)
  506. err := c.ShouldBind(&req)
  507. if err != nil {
  508. errs, ok := err.(validator.ValidationErrors)
  509. if !ok {
  510. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  511. return
  512. }
  513. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  514. return
  515. }
  516. claims, _ := c.Get("adminInfo")
  517. adminInfo := claims.(*system.SysAdmin)
  518. ob := new(fms.ContractRegister)
  519. item, e := ob.Fetch(req.ContractRegisterId)
  520. if e != nil {
  521. if e == utils.ErrNoRow {
  522. resp.Fail("合同登记不存在或已被删除", c)
  523. return
  524. }
  525. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  526. return
  527. }
  528. nowTime := time.Now().Local()
  529. item.ContractStatus = req.ContractStatus
  530. item.ModifyTime = nowTime
  531. updateCols := []string{"ContractStatus", "ModifyTime"}
  532. if e = item.Update(updateCols); e != nil {
  533. resp.FailMsg("操作失败", "更新合同登记失败, Err:"+e.Error(), c)
  534. return
  535. }
  536. // 非补充协议校验金额-是否修改状态
  537. if item.ContractType != fms.ContractTypePlus {
  538. go fmsService.CheckContractRegisterAmount(req.ContractRegisterId)
  539. }
  540. // 操作日志
  541. go func() {
  542. opData := ""
  543. opDataByte, e := json.Marshal(req)
  544. if e != nil {
  545. return
  546. }
  547. opData = string(opDataByte)
  548. logItem := new(fms.ContractRegisterLog)
  549. logItem.ContractRegisterId = req.ContractRegisterId
  550. logItem.AdminId = int(adminInfo.AdminId)
  551. logItem.AdminName = adminInfo.RealName
  552. logItem.OpData = opData
  553. logItem.OpType = fms.ContractRegisterOpTypeStatus
  554. logItem.CreateTime = nowTime
  555. if e = logItem.Create(); e != nil {
  556. return
  557. }
  558. }()
  559. resp.Ok("操作成功", c)
  560. }
  561. // Invoice
  562. // @Title 开票/到款登记
  563. // @Description 开票/到款登记
  564. // @Param request body fms.ContractInvoiceSaveReq true "type json string"
  565. // @Success 200 string "操作成功"
  566. // @router /contract/register/invoice [post]
  567. func (rg *RegisterController) Invoice(c *gin.Context) {
  568. req := new(fms.ContractInvoiceSaveReq)
  569. err := c.ShouldBind(&req)
  570. if err != nil {
  571. errs, ok := err.(validator.ValidationErrors)
  572. if !ok {
  573. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  574. return
  575. }
  576. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  577. return
  578. }
  579. claims, _ := c.Get("adminInfo")
  580. adminInfo := claims.(*system.SysAdmin)
  581. registerOB := new(fms.ContractRegister)
  582. item, e := registerOB.Fetch(req.ContractRegisterId)
  583. if e != nil {
  584. if e == utils.ErrNoRow {
  585. resp.Fail("合同登记不存在或已被删除", c)
  586. return
  587. }
  588. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  589. return
  590. }
  591. if item.ContractType == fms.ContractTypePlus {
  592. resp.Fail("补充协议不允许添加开票/到款登记", c)
  593. return
  594. }
  595. noChangeInvoiceIds := make([]int, 0)
  596. newInvoice := make([]*fms.ContractInvoice, 0)
  597. if len(req.AmountList) > 0 {
  598. for i := range req.AmountList {
  599. if req.AmountList[i].Amount <= 0 {
  600. resp.Fail("登记金额有误", c)
  601. return
  602. }
  603. if req.AmountList[i].InvoiceDate == "" {
  604. resp.Fail("请选择日期", c)
  605. return
  606. }
  607. t, e := time.ParseInLocation(utils.FormatDate, req.AmountList[i].InvoiceDate, time.Local)
  608. if e != nil {
  609. resp.FailData("日期格式有误", "Err:"+e.Error(), c)
  610. return
  611. }
  612. if req.AmountList[i].InvoiceId > 0 {
  613. noChangeInvoiceIds = append(noChangeInvoiceIds, req.AmountList[i].InvoiceId)
  614. } else {
  615. v := &fms.ContractInvoice{
  616. ContractRegisterId: req.ContractRegisterId,
  617. ContractCode: item.ContractCode,
  618. Amount: req.AmountList[i].Amount,
  619. InvoiceType: req.InvoiceType,
  620. InvoiceDate: t,
  621. AdminId: int(adminInfo.AdminId),
  622. AdminName: adminInfo.RealName,
  623. }
  624. v.Set()
  625. newInvoice = append(newInvoice, v)
  626. }
  627. }
  628. }
  629. // 获取原有的登记信息
  630. invoiceCond := `contract_register_id = ? AND invoice_type = ?`
  631. invoicePars := make([]interface{}, 0)
  632. invoicePars = append(invoicePars, req.ContractRegisterId, req.InvoiceType)
  633. originInvoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  634. if e != nil {
  635. resp.FailMsg("获取失败", "获取开票/到款列表失败, Err: "+e.Error(), c)
  636. return
  637. }
  638. // 比对原有和现有的登记信息
  639. logList := make([]*fms.ContractRegisterLog, 0)
  640. opData := ""
  641. opDataByte, e := json.Marshal(req)
  642. if e != nil {
  643. return
  644. }
  645. opData = string(opDataByte)
  646. opType := fms.ContractRegisterOpTypeInvoice
  647. if req.InvoiceType == fms.ContractInvoiceTypePay {
  648. opType = fms.ContractRegisterOpTypePayment
  649. }
  650. nowTime := time.Now().Local()
  651. // 需要删除的记录
  652. deleteInvoiceIds := make([]int, 0)
  653. for i := range originInvoiceList {
  654. if !utils.InArrayByInt(noChangeInvoiceIds, originInvoiceList[i].ContractInvoiceId) {
  655. deleteInvoiceIds = append(deleteInvoiceIds, originInvoiceList[i].ContractInvoiceId)
  656. logList = append(logList, &fms.ContractRegisterLog{
  657. ContractRegisterId: req.ContractRegisterId,
  658. AdminId: int(adminInfo.AdminId),
  659. AdminName: adminInfo.RealName,
  660. OpData: opData,
  661. OpType: opType,
  662. CreateTime: nowTime,
  663. Remark: fmt.Sprint(adminInfo.RealName, " 删除", fms.ContractInvoiceKeyNameMap[opType], "金额", originInvoiceList[i].Amount, "元"),
  664. })
  665. }
  666. }
  667. // 新增的记录
  668. if len(newInvoice) > 0 {
  669. newAmount := decimal.NewFromFloat(0).Round(2)
  670. for i := range newInvoice {
  671. a := decimal.NewFromFloat(newInvoice[i].Amount).Round(2)
  672. newAmount = newAmount.Add(a)
  673. }
  674. ia, _ := newAmount.Round(2).Float64()
  675. logList = append(logList, &fms.ContractRegisterLog{
  676. ContractRegisterId: req.ContractRegisterId,
  677. AdminId: int(adminInfo.AdminId),
  678. AdminName: adminInfo.RealName,
  679. OpData: opData,
  680. OpType: opType,
  681. CreateTime: nowTime,
  682. Remark: fmt.Sprint(adminInfo.RealName, " 新增", fms.ContractInvoiceKeyNameMap[opType], "金额", ia, "元"),
  683. })
  684. }
  685. // 删除并新增登记
  686. ob := new(fms.ContractInvoice)
  687. if e := ob.DeleteAndCreateNewInvoice(req.ContractRegisterId, req.InvoiceType, deleteInvoiceIds, newInvoice); e != nil {
  688. resp.FailData("日期格式有误", "Err:"+e.Error(), c)
  689. return
  690. }
  691. // 校验金额-是否修改状态
  692. go fmsService.CheckContractRegisterAmount(req.ContractRegisterId)
  693. // 操作日志
  694. go func() {
  695. logOB := new(fms.ContractRegisterLog)
  696. if e := logOB.AddInBatches(logList); e != nil {
  697. return
  698. }
  699. }()
  700. resp.Ok("操作成功", c)
  701. }
  702. // Export
  703. // @Title 合同登记-导出
  704. // @Description 合同登记-导出
  705. // @Param Keyword query string false "关键词"
  706. // @Param StartDate query string false "合同开始日期"
  707. // @Param EndDate query string false "合同结束日期"
  708. // @Param ServiceType query int false "套餐类型"
  709. // @Param ContractType query int false "合同类型"
  710. // @Param RegisterStatus query int false "登记状态"
  711. // @Success 200 string "操作成功"
  712. // @router /contract/register/export [get]
  713. func (rg *RegisterController) Export(c *gin.Context) {
  714. var req fms.ContractRegisterListReq
  715. if e := c.BindQuery(&req); e != nil {
  716. err, ok := e.(validator.ValidationErrors)
  717. if !ok {
  718. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  719. return
  720. }
  721. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  722. return
  723. }
  724. cond := `1 = 1`
  725. pars := make([]interface{}, 0)
  726. if req.Keyword != "" {
  727. kw := "%" + req.Keyword + "%"
  728. cond += ` AND (company_name LIKE ? OR contract_code LIKE ? OR seller_name LIKE ?)`
  729. pars = append(pars, kw, kw, kw)
  730. }
  731. if req.StartDate != "" && req.EndDate != "" {
  732. st := fmt.Sprint(req.StartDate, " 00:00:00")
  733. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  734. cond += ` AND (create_time BETWEEN ? AND ?)`
  735. pars = append(pars, st, ed)
  736. }
  737. if req.ContractType != 0 {
  738. cond += ` AND contract_type = ?`
  739. pars = append(pars, req.ContractType)
  740. }
  741. if req.RegisterStatus != 0 {
  742. cond += ` AND register_status = ?`
  743. pars = append(pars, req.RegisterStatus)
  744. }
  745. if req.ServiceType != 0 {
  746. registerIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
  747. if e != nil {
  748. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  749. return
  750. }
  751. if len(registerIds) > 0 {
  752. cond += ` AND contract_register_id IN ?`
  753. pars = append(pars, registerIds)
  754. } else {
  755. cond += ` AND 1 = 2`
  756. }
  757. }
  758. // 获取列表数据
  759. cr := new(fms.ContractRegister)
  760. list, e := cr.List(cond, pars)
  761. if e != nil {
  762. resp.FailData("获取合同列表失败", "Err:"+e.Error(), c)
  763. return
  764. }
  765. if len(list) == 0 {
  766. resp.Fail("无有效数据可导出", c)
  767. return
  768. }
  769. registerIds := make([]int, 0)
  770. for i := range list {
  771. registerIds = append(registerIds, list[i].ContractRegisterId)
  772. }
  773. // 获取小套餐品种
  774. cpCond := `product_id = ? AND permission_name <> ?`
  775. cpPars := make([]interface{}, 0)
  776. cpPars = append(cpPars, crm.CompanyProductFicc, crm.ChartPermissionStrategyName)
  777. cp := new(crm.ChartPermission)
  778. permissionList, e := cp.List(cpCond, cpPars)
  779. if e != nil {
  780. resp.FailData("获取小套餐品种失败", "Err:"+e.Error(), c)
  781. return
  782. }
  783. permissionLen := len(permissionList)
  784. permissionNameIdMap := make(map[string]int)
  785. for i := range permissionList {
  786. permissionNameIdMap[permissionList[i].PermissionName] = permissionList[i].ChartPermissionId
  787. }
  788. // 获取套餐服务
  789. //serviceList, e := fms.GetContractServiceTemplateMapByProductId(crm.CompanyProductFicc)
  790. //if e != nil {
  791. // resp.FailData("获取套餐服务失败", "Err:"+e.Error(), c)
  792. // return
  793. //}
  794. // 套餐/开票/到款列表
  795. serviceMap := make(map[int][]*fms.ContractService)
  796. serviceChartPermissionsMap := make(map[int][]int)
  797. invoiceMap := make(map[int][]*fms.ContractInvoice)
  798. paymentMap := make(map[int][]*fms.ContractInvoice)
  799. maxInvoice := 0
  800. maxPayment := 0
  801. if len(registerIds) > 0 {
  802. // 获取套餐信息
  803. csCond := `contract_register_id IN ?`
  804. csPars := make([]interface{}, 0)
  805. csPars = append(csPars, registerIds)
  806. cs := new(fms.ContractService)
  807. serviceList, e := cs.List(csCond, csPars)
  808. if e != nil {
  809. resp.FailData("获取合同套餐列表失败", "Err:"+e.Error(), c)
  810. return
  811. }
  812. for i := range serviceList {
  813. cid := serviceList[i].ContractRegisterId
  814. if serviceMap[cid] == nil {
  815. serviceMap[cid] = make([]*fms.ContractService, 0)
  816. }
  817. serviceMap[cid] = append(serviceMap[cid], serviceList[i])
  818. // 小套餐权限
  819. if serviceChartPermissionsMap[cid] == nil {
  820. serviceChartPermissionsMap[cid] = make([]int, 0)
  821. }
  822. if serviceList[i].ChartPermissionIds != "" {
  823. ids := utils.JoinStr2IntArr(serviceList[i].ChartPermissionIds, ",")
  824. serviceChartPermissionsMap[cid] = append(serviceChartPermissionsMap[cid], ids...)
  825. }
  826. }
  827. // 获取开票/到款详情, 并取最大的开票/到款数(用于动态扩展第二列表头)
  828. ci := new(fms.ContractInvoice)
  829. invoiceList, e := ci.List(csCond, csPars, "")
  830. if e != nil {
  831. resp.FailData("获取开票/到款列表失败", "Err:"+e.Error(), c)
  832. return
  833. }
  834. for k := range invoiceList {
  835. cid := invoiceList[k].ContractRegisterId
  836. if invoiceMap[cid] == nil {
  837. invoiceMap[cid] = make([]*fms.ContractInvoice, 0)
  838. }
  839. if paymentMap[cid] == nil {
  840. paymentMap[cid] = make([]*fms.ContractInvoice, 0)
  841. }
  842. if invoiceList[k].InvoiceType == fms.ContractInvoiceTypeMake {
  843. invoiceMap[cid] = append(invoiceMap[cid], invoiceList[k])
  844. continue
  845. }
  846. if invoiceList[k].InvoiceType == fms.ContractInvoiceTypePay {
  847. paymentMap[cid] = append(paymentMap[cid], invoiceList[k])
  848. continue
  849. }
  850. }
  851. // 取最大开票/到款数
  852. for j := range invoiceMap {
  853. if len(invoiceMap[j]) > maxInvoice {
  854. maxInvoice = len(invoiceMap[j])
  855. }
  856. }
  857. for p := range paymentMap {
  858. if len(paymentMap[p]) > maxPayment {
  859. maxPayment = len(paymentMap[p])
  860. }
  861. }
  862. }
  863. // 生成Excel文件
  864. //dir, err := os.Executable()
  865. //exPath := filepath.Dir(dir)
  866. //filePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  867. xlsxFile := xlsx.NewFile()
  868. //if err != nil {
  869. // resp.FailData("生成文件失败", "Err:"+err.Error(), c)
  870. // return
  871. //}
  872. style := xlsx.NewStyle()
  873. alignment := xlsx.Alignment{
  874. Horizontal: "center",
  875. Vertical: "center",
  876. WrapText: true,
  877. }
  878. style.Alignment = alignment
  879. style.ApplyAlignment = true
  880. sheet, err := xlsxFile.AddSheet("合同登记")
  881. if err != nil {
  882. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  883. return
  884. }
  885. //_ = sheet.SetColWidth(0, 0, 60)
  886. //_ = sheet.SetColWidth(4, 10, 60)
  887. // 1行表头
  888. titleRow := sheet.AddRow()
  889. titleRow.SetHeight(40)
  890. // 1行1列-右合并两格
  891. cell1 := titleRow.AddCell()
  892. cell1.HMerge = 2
  893. cell1.SetValue("FICC客户签约表 2022")
  894. cell1.SetStyle(style)
  895. // 右增两列空白格用于第一列合并, 否则后续单元格合并会有问题
  896. titleRow.AddCell().SetValue("")
  897. titleRow.AddCell().SetValue("")
  898. // 1行2列
  899. cell2 := titleRow.AddCell()
  900. cell2.SetValue("FICC大套餐")
  901. cell2.SetStyle(style)
  902. // 1行3列-右合并小套餐数
  903. if permissionLen >= 1 {
  904. cell3 := titleRow.AddCell()
  905. cell3.HMerge = permissionLen - 1
  906. cell3.SetValue("FICC小套餐")
  907. cell3.SetStyle(style)
  908. // 同上右增单元格小套餐数-1的空白单元格用于合并
  909. for i := 0; i < permissionLen-1; i++ {
  910. titleRow.AddCell().SetValue("")
  911. }
  912. }
  913. cell4 := titleRow.AddCell()
  914. cell4.SetValue("市场策略")
  915. cell4.SetStyle(style)
  916. cell5 := titleRow.AddCell()
  917. cell5.SetValue("财富管理")
  918. cell5.SetStyle(style)
  919. // 第二行表头
  920. titleRow2 := sheet.AddRow()
  921. titleRow2.SetHeight(60)
  922. row2Title := make([]string, 0)
  923. row2Title = append(row2Title, "客户名称", "续约-0\n新增-1", "销售", "FICC大套餐")
  924. for i := range permissionList {
  925. row2Title = append(row2Title, permissionList[i].PermissionName)
  926. }
  927. row2Title = append(row2Title, "市场策略", "财富管理", "开始时间", "到期时间", "2022年合同金额", "约定付款时间", "签订日",
  928. "签订月", "合同状态", "合同编号", "备注")
  929. for i := range row2Title {
  930. v := titleRow2.AddCell()
  931. v.SetValue(row2Title[i])
  932. v.SetStyle(style)
  933. }
  934. // 第二行表头-开票/收款(动态添加)
  935. for i := 0; i < maxInvoice; i++ {
  936. n := i + 1
  937. c1 := titleRow2.AddCell()
  938. t1 := fmt.Sprintf("%s%d", "开票日", n)
  939. c1.SetValue(t1)
  940. c1.SetStyle(style)
  941. c2 := titleRow2.AddCell()
  942. t2 := fmt.Sprintf("%s%d", "开票金额", n)
  943. c2.SetValue(t2)
  944. c2.SetStyle(style)
  945. row2Title = append(row2Title, t1, t2)
  946. }
  947. for i := 0; i < maxPayment; i++ {
  948. n := i + 1
  949. c1 := titleRow2.AddCell()
  950. t1 := fmt.Sprintf("%s%d", "收款日", n)
  951. c1.SetValue(t1)
  952. c1.SetStyle(style)
  953. c2 := titleRow2.AddCell()
  954. t2 := fmt.Sprintf("%s%d", "收款金额", n)
  955. c2.SetValue(t2)
  956. c2.SetStyle(style)
  957. row2Title = append(row2Title, t1, t2)
  958. }
  959. // 此处取第二行标题NameKeyMap, 后面的动态匹配
  960. row2NameKeyMap := make(map[string]int)
  961. for i := range row2Title {
  962. row2NameKeyMap[row2Title[i]] = i
  963. }
  964. // TODO:代付和补充协议
  965. contractTMap := map[int]int{
  966. fms.ContractTypeNew: 1,
  967. fms.ContractTypeRenew: 0,
  968. }
  969. for _, v := range list {
  970. k := -1
  971. dataRow := sheet.AddRow()
  972. dataRow.SetHeight(20)
  973. k += 3
  974. dataRow.AddCell().SetString(v.CompanyName)
  975. dataRow.AddCell().SetString(fmt.Sprint(contractTMap[v.ContractType]))
  976. dataRow.AddCell().SetString(v.SellerName)
  977. // 大套餐
  978. k += 1
  979. col4Name := row2Title[k]
  980. svList := serviceMap[v.ContractRegisterId]
  981. col4 := ""
  982. if svList != nil && len(svList) > 0 {
  983. for isv := range svList {
  984. if svList[isv].Title == col4Name {
  985. col4 = "是"
  986. break
  987. }
  988. }
  989. }
  990. dataRow.AddCell().SetString(col4)
  991. // 小套餐
  992. serviceChartPermissionIds := serviceChartPermissionsMap[v.ContractRegisterId]
  993. for i := 0; i < permissionLen; i++ {
  994. k += 1
  995. colName := row2Title[k]
  996. chartPermissionId := permissionNameIdMap[colName]
  997. if utils.InArray(chartPermissionId, serviceChartPermissionIds) {
  998. dataRow.AddCell().SetString("是")
  999. } else {
  1000. dataRow.AddCell().SetString("")
  1001. }
  1002. }
  1003. // 财富管理/市场策略(处理方式其实跟上面的大套餐一样, 只是中间隔了小套餐按照顺序要这么处理=_=!)
  1004. k += 1
  1005. col5Name := row2Title[k]
  1006. col5 := ""
  1007. if svList != nil && len(svList) > 0 {
  1008. for isv := range svList {
  1009. if svList[isv].Title == col5Name {
  1010. col5 = "是"
  1011. break
  1012. }
  1013. }
  1014. }
  1015. dataRow.AddCell().SetString(col5)
  1016. k += 1
  1017. col6Name := row2Title[k]
  1018. col6 := ""
  1019. if svList != nil && len(svList) > 0 {
  1020. for isv := range svList {
  1021. if svList[isv].Title == col6Name {
  1022. col6 = "是"
  1023. break
  1024. }
  1025. }
  1026. }
  1027. dataRow.AddCell().SetString(col6)
  1028. // 其他信息
  1029. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.StartDate)) // 开始时间
  1030. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.EndDate)) // 到期时间
  1031. dataRow.AddCell().SetString(fmt.Sprint("¥", v.ContractAmount)) // 2022年合同金额
  1032. dataRow.AddCell().SetString(v.AgreedPayTime) // 约定付款时间
  1033. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.SignDate)) // 签订日
  1034. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01", v.SignDate)) // 签订月
  1035. dataRow.AddCell().SetString(fms.ContractStatusKeyNameMap[v.ContractStatus]) // 合同状态
  1036. dataRow.AddCell().SetString(v.ContractCode) // 合同编号
  1037. dataRow.AddCell().SetString(v.Remark) // 备注
  1038. // 开票/到款信息
  1039. ivList := invoiceMap[v.ContractRegisterId]
  1040. ivListLen := len(ivList)
  1041. if ivList != nil && len(ivList) > 0 {
  1042. for ia := 0; ia < maxInvoice; ia++ {
  1043. if ia < ivListLen {
  1044. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", ivList[ia].InvoiceDate)) // 开票日
  1045. dataRow.AddCell().SetString(fmt.Sprint(ivList[ia].Amount)) // 开票金额
  1046. } else {
  1047. // 这里要把不够的填充为空
  1048. dataRow.AddCell().SetString("")
  1049. dataRow.AddCell().SetString("")
  1050. }
  1051. }
  1052. }
  1053. pyList := paymentMap[v.ContractRegisterId]
  1054. pyListLen := len(pyList)
  1055. if pyList != nil && pyListLen > 0 {
  1056. for ib := 0; ib < maxInvoice; ib++ {
  1057. if ib < pyListLen {
  1058. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", pyList[ib].InvoiceDate)) // 收款日
  1059. dataRow.AddCell().SetString(fmt.Sprint(pyList[ib].Amount)) // 收款金额
  1060. } else {
  1061. // 已经是最后的几列了其实可以不用填充空, 万一后面要加列此处还是填充上吧
  1062. dataRow.AddCell().SetString("")
  1063. dataRow.AddCell().SetString("")
  1064. }
  1065. }
  1066. }
  1067. }
  1068. // 输出文件
  1069. var buffer bytes.Buffer
  1070. _ = xlsxFile.Write(&buffer)
  1071. content := bytes.NewReader(buffer.Bytes())
  1072. //err = xlsxFile.Save(filePath)
  1073. //if err != nil {
  1074. // resp.FailData("保存文件失败", "Err:"+err.Error(), c)
  1075. // return
  1076. //}
  1077. //defer func() {
  1078. // _ = os.Remove(filePath)
  1079. //}()
  1080. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1081. fileName := "财务列表_" + randStr + ".xlsx"
  1082. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  1083. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1084. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  1085. }
  1086. // InvoiceList
  1087. // @Title 开票/到款列表
  1088. // @Description 开票/到款列表
  1089. // @Param InvoiceType query int false "类型: 1-开票登记; 2-到款登记"
  1090. // @Param ContractCode query string false "合同编号"
  1091. // @Param StartDate query string false "开始日期"
  1092. // @Param EndDate query string false "结束日期"
  1093. // @Param MinAmount query float64 false "开票金额区间-最小值"
  1094. // @Param MaxAmount query float64 false "开票金额区间-最大值"
  1095. // @Success 200 {object} fms.ContractInvoiceItem
  1096. // @router /contract/register/invoice_list [get]
  1097. func (rg *RegisterController) InvoiceList(c *gin.Context) {
  1098. var req fms.ContractInvoiceListReq
  1099. if e := c.BindQuery(&req); e != nil {
  1100. err, ok := e.(validator.ValidationErrors)
  1101. if !ok {
  1102. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  1103. return
  1104. }
  1105. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1106. return
  1107. }
  1108. cond := `invoice_type = ?`
  1109. pars := make([]interface{}, 0)
  1110. pars = append(pars, req.InvoiceType)
  1111. // 合同编号
  1112. if req.ContractCode != "" {
  1113. cond += ` AND contract_code = ?`
  1114. pars = append(pars, req.ContractCode)
  1115. }
  1116. if req.StartDate != "" && req.EndDate != "" {
  1117. st := fmt.Sprint(req.StartDate, " 00:00:00")
  1118. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1119. cond += ` AND (create_time BETWEEN ? AND ?)`
  1120. pars = append(pars, st, ed)
  1121. }
  1122. if req.MinAmount > 0 && req.MaxAmount > 0 {
  1123. cond += ` AND (amount BETWEEN ? AND ?)`
  1124. pars = append(pars, req.MinAmount, req.MaxAmount)
  1125. }
  1126. page := new(base.Page)
  1127. page.SetPageSize(req.PageSize)
  1128. page.SetCurrent(req.Current)
  1129. page.AddOrderItem(base.OrderItem{Column: "invoice_time", Asc: false})
  1130. total, list, e := fms.GetContractInvoiceItemPageList(page, cond, pars)
  1131. if e != nil {
  1132. resp.FailMsg("获取失败", "获取合同开票/到款列表失败, Err: "+e.Error(), c)
  1133. return
  1134. }
  1135. page.SetTotal(total)
  1136. baseData := new(base.BaseData)
  1137. baseData.SetPage(page)
  1138. baseData.SetList(list)
  1139. resp.OkData("获取成功", baseData, c)
  1140. }
  1141. // InvoiceExport
  1142. // @Title 开票/到款列表-导出
  1143. // @Description 合同登记-导出
  1144. // @Param InvoiceType query int false "类型: 1-开票登记; 2-到款登记"
  1145. // @Param ContractCode query string false "合同编号"
  1146. // @Param StartDate query string false "开始日期"
  1147. // @Param EndDate query string false "结束日期"
  1148. // @Param MinAmount query float64 false "开票金额区间-最小值"
  1149. // @Param MaxAmount query float64 false "开票金额区间-最大值"
  1150. // @Success 200 string "操作成功"
  1151. // @router /contract/register/invoice_export [get]
  1152. func (rg *RegisterController) InvoiceExport(c *gin.Context) {
  1153. var req fms.ContractInvoiceListReq
  1154. if e := c.BindQuery(&req); e != nil {
  1155. err, ok := e.(validator.ValidationErrors)
  1156. if !ok {
  1157. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  1158. return
  1159. }
  1160. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1161. return
  1162. }
  1163. cond := `invoice_type = ?`
  1164. pars := make([]interface{}, 0)
  1165. pars = append(pars, req.InvoiceType)
  1166. // 合同编号
  1167. if req.ContractCode != "" {
  1168. cond += ` AND contract_code = ?`
  1169. pars = append(pars, req.ContractCode)
  1170. }
  1171. if req.StartDate != "" && req.EndDate != "" {
  1172. st := fmt.Sprint(req.StartDate, " 00:00:00")
  1173. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1174. cond += ` AND (create_time BETWEEN ? AND ?)`
  1175. pars = append(pars, st, ed)
  1176. }
  1177. if req.MinAmount > 0 && req.MaxAmount > 0 {
  1178. cond += ` AND (amount BETWEEN ? AND ?)`
  1179. pars = append(pars, req.MinAmount, req.MaxAmount)
  1180. }
  1181. // 获取列表数据
  1182. cr := new(fms.ContractInvoice)
  1183. orderRule := `invoice_time DESC`
  1184. list, e := cr.List(cond, pars, orderRule)
  1185. if e != nil {
  1186. resp.FailData("获取开票/到款列表失败", "Err:"+e.Error(), c)
  1187. return
  1188. }
  1189. if len(list) == 0 {
  1190. resp.Fail("无有效数据可导出", c)
  1191. return
  1192. }
  1193. // 生成Excel文件
  1194. xlsxFile := xlsx.NewFile()
  1195. style := xlsx.NewStyle()
  1196. alignment := xlsx.Alignment{
  1197. Horizontal: "center",
  1198. Vertical: "center",
  1199. WrapText: true,
  1200. }
  1201. style.Alignment = alignment
  1202. style.ApplyAlignment = true
  1203. sheet, err := xlsxFile.AddSheet("开票列表")
  1204. if err != nil {
  1205. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  1206. return
  1207. }
  1208. // 表头
  1209. titleRow := sheet.AddRow()
  1210. titleRow.SetHeight(40)
  1211. cell1 := titleRow.AddCell()
  1212. cell1.SetValue("合同编号")
  1213. cell1.SetStyle(style)
  1214. cell2 := titleRow.AddCell()
  1215. cell2.SetValue("开票金额")
  1216. cell2.SetStyle(style)
  1217. cell3 := titleRow.AddCell()
  1218. cell3.SetValue("开票日期")
  1219. cell3.SetStyle(style)
  1220. for _, v := range list {
  1221. dataRow := sheet.AddRow()
  1222. dataRow.SetHeight(20)
  1223. dataRow.AddCell().SetString(v.ContractCode)
  1224. dataRow.AddCell().SetString(fmt.Sprint(v.Amount))
  1225. dataRow.AddCell().SetString(utils.TimeTransferString("2006-01-02", v.InvoiceDate))
  1226. }
  1227. // 输出文件
  1228. var buffer bytes.Buffer
  1229. _ = xlsxFile.Write(&buffer)
  1230. content := bytes.NewReader(buffer.Bytes())
  1231. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1232. fileName := "开票列表_" + randStr + ".xlsx"
  1233. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  1234. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1235. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  1236. }