register.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. package contract
  2. import (
  3. "encoding/json"
  4. "github.com/gin-gonic/gin"
  5. "github.com/go-playground/validator/v10"
  6. "hongze/fms_api/controller/resp"
  7. "hongze/fms_api/global"
  8. "hongze/fms_api/models/base"
  9. "hongze/fms_api/models/fms"
  10. "hongze/fms_api/models/system"
  11. fmsService "hongze/fms_api/services/fms"
  12. "hongze/fms_api/utils"
  13. "time"
  14. )
  15. // RegisterController 合同登记
  16. type RegisterController struct{}
  17. // List
  18. // @Title 合同登记列表
  19. // @Description 合同登记列表
  20. // @Param Keyword query string false "关键词"
  21. // @Param StartDate query string false "合同开始日期"
  22. // @Param EndDate query string false "合同结束日期"
  23. // @Param ServiceType query int false "套餐类型"
  24. // @Param ContractType query int false "合同类型"
  25. // @Param RegisterStatus query int false "登记状态"
  26. // @Success 200 {object} fms.ContractRegisterItem
  27. // @router /contract/register/list [get]
  28. func (rg *RegisterController) List(c *gin.Context) {
  29. var req fms.ContractRegisterListReq
  30. if e := c.BindQuery(&req); e != nil {
  31. err, ok := e.(validator.ValidationErrors)
  32. if !ok {
  33. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  34. return
  35. }
  36. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  37. return
  38. }
  39. cond := `1 = 1`
  40. pars := make([]interface{}, 0)
  41. if req.Keyword != "" {
  42. kw := "%" + req.Keyword + "%"
  43. cond += ` AND (company_name LIKE ? OR contract_code LIKE ? OR seller_name LIKE ?)`
  44. pars = append(pars, kw, kw, kw)
  45. }
  46. if req.StartDate != "" && req.EndDate != "" {
  47. cond += ` AND (start_time >= ? AND end_time <= ?)`
  48. pars = append(pars, req.StartDate, req.EndDate)
  49. }
  50. if req.ContractType != 0 {
  51. cond += ` AND contract_type = ?`
  52. pars = append(pars, req.ContractType)
  53. }
  54. if req.RegisterStatus != 0 {
  55. cond += ` AND register_status = ?`
  56. pars = append(pars, req.RegisterStatus)
  57. }
  58. // 套餐筛选
  59. if req.ServiceType != 0 {
  60. registerIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
  61. if e != nil {
  62. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  63. return
  64. }
  65. if len(registerIds) > 0 {
  66. cond += ` AND contract_register_id IN ?`
  67. pars = append(pars, registerIds)
  68. } else {
  69. cond += ` AND 1 = 2`
  70. }
  71. }
  72. page := new(base.Page)
  73. page.SetPageSize(req.PageSize)
  74. page.SetCurrent(req.Current)
  75. page.AddOrderItem(base.OrderItem{Column: "create_time", Asc: false})
  76. total, list, e := fms.GetContractRegisterItemPageList(page, cond, pars)
  77. if e != nil {
  78. resp.FailMsg("获取失败", "获取合同登记列表失败, Err: "+e.Error(), c)
  79. return
  80. }
  81. registerIds := make([]int, 0)
  82. for i := range list {
  83. registerIds = append(registerIds, list[i].ContractRegisterId)
  84. }
  85. serviceMap := make(map[int]string, 0)
  86. invoiceMap := make(map[int][]*fms.ContractInvoiceItem, 0)
  87. paymentMap := make(map[int][]*fms.ContractInvoiceItem, 0)
  88. if len(registerIds) > 0 {
  89. // 获取服务套餐
  90. servicesNameList, e := fms.GetContractRegisterServicesNameByRegisterIds(registerIds)
  91. if e != nil {
  92. resp.FailMsg("获取失败", "获取套餐拼接字符串失败, Err: "+e.Error(), c)
  93. return
  94. }
  95. for i := range servicesNameList {
  96. serviceMap[servicesNameList[i].ContractRegisterId] = servicesNameList[i].ServicesName
  97. }
  98. // 获取开票/到款列表
  99. invoiceCond := `contract_register_id IN ?`
  100. invoicePars := make([]interface{}, 0)
  101. invoicePars = append(invoicePars, registerIds)
  102. invoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  103. if e != nil {
  104. resp.FailMsg("获取失败", "获取开票/到款列表失败, Err: "+e.Error(), c)
  105. return
  106. }
  107. for i := range invoiceList {
  108. if invoiceMap[invoiceList[i].ContractRegisterId] == nil {
  109. invoiceMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoiceItem, 0)
  110. }
  111. if paymentMap[invoiceList[i].ContractRegisterId] == nil {
  112. paymentMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoiceItem, 0)
  113. }
  114. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  115. invoiceMap[invoiceList[i].ContractRegisterId] = append(invoiceMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  116. }
  117. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  118. paymentMap[invoiceList[i].ContractRegisterId] = append(paymentMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  119. }
  120. }
  121. }
  122. respList := make([]*fms.ContractRegisterList, 0)
  123. for i := range list {
  124. v := new(fms.ContractRegisterList)
  125. v.ContractRegisterItem = *list[i]
  126. v.ServicesName = serviceMap[list[i].ContractRegisterId]
  127. v.InvoiceList = invoiceMap[list[i].ContractRegisterId]
  128. v.PaymentList = paymentMap[list[i].ContractRegisterId]
  129. respList = append(respList, v)
  130. }
  131. page.SetTotal(total)
  132. baseData := new(base.BaseData)
  133. baseData.SetPage(page)
  134. baseData.SetList(respList)
  135. resp.OkData("获取成功", baseData, c)
  136. }
  137. // Add
  138. // @Title 新增合同登记
  139. // @Description 新增合同登记
  140. // @Param request body fms.ContractRegisterAddReq true "type json string"
  141. // @Success 200 string "操作成功"
  142. // @router /contract/register/add [post]
  143. func (rg *RegisterController) Add(c *gin.Context) {
  144. req := new(fms.ContractRegisterAddReq)
  145. err := c.ShouldBind(&req)
  146. if err != nil {
  147. errs, ok := err.(validator.ValidationErrors)
  148. if !ok {
  149. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  150. return
  151. }
  152. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  153. return
  154. }
  155. claims, _ := c.Get("adminInfo")
  156. adminInfo := claims.(*system.SysAdmin)
  157. // 日期校验
  158. startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  159. if e != nil {
  160. resp.FailMsg("合同开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
  161. return
  162. }
  163. endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  164. if e != nil {
  165. resp.FailMsg("合同结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
  166. return
  167. }
  168. signDate, e := time.ParseInLocation(utils.FormatDate, req.SignDate, time.Local)
  169. if e != nil {
  170. resp.FailMsg("合同签订日期格式有误", "合同签订日期格式有误, Err: "+e.Error(), c)
  171. return
  172. }
  173. // 是否存在相同合同编号的登记
  174. ob := new(fms.ContractRegister)
  175. existCond := `contract_code = ?`
  176. existPars := make([]interface{}, 0)
  177. existPars = append(existPars, req.ContractCode)
  178. exist, e := ob.FetchByCondition(existCond, existPars)
  179. if e != nil && e != utils.ErrNoRow {
  180. resp.FailMsg("操作失败", "获取相同登记号失败, Err: "+e.Error(), c)
  181. return
  182. }
  183. if exist != nil && exist.ContractRegisterId > 0 {
  184. resp.Fail("合同编号已存在", c)
  185. return
  186. }
  187. nowTime := time.Now().Local()
  188. ob.ContractCode = req.ContractCode
  189. ob.CrmContractId = req.CrmContractId
  190. ob.ContractSource = req.ContractSource
  191. ob.CompanyName = req.CompanyName
  192. ob.ProductId = req.ProductId
  193. ob.SellerId = req.SellerId
  194. ob.SellerName = req.SellerName
  195. ob.ContractType = req.ContractType
  196. ob.ContractAmount = req.ContractAmount
  197. ob.StartDate = startDate
  198. ob.EndDate = endDate
  199. ob.SignDate = signDate
  200. ob.AgreedPayTime = req.AgreedPayTime
  201. ob.ContractStatus = req.ContractStatus
  202. ob.RegisterStatus = fms.ContractRegisterStatusIng
  203. ob.Remark = req.Remark
  204. ob.Set()
  205. // 套餐信息
  206. serviceList, e := fmsService.HandleContractServiceAndDetail(req.ProductId, req.Services)
  207. if e != nil {
  208. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  209. return
  210. }
  211. // 新增合同及套餐
  212. if e = fms.CreateContractRegisterAndServices(ob, serviceList); e != nil {
  213. resp.FailMsg("操作失败", "新增合同及套餐失败, Err: "+e.Error(), c)
  214. return
  215. }
  216. // 操作日志
  217. go func() {
  218. opData := ""
  219. opDataByte, e := json.Marshal(req)
  220. if e != nil {
  221. return
  222. }
  223. opData = string(opDataByte)
  224. logItem := new(fms.ContractRegisterLog)
  225. logItem.ContractRegisterId = ob.ContractRegisterId
  226. logItem.AdminId = int(adminInfo.AdminId)
  227. logItem.AdminName = adminInfo.AdminName
  228. logItem.OpData = opData
  229. logItem.OpType = fms.ContractRegisterOpTypeSave
  230. logItem.CreateTime = nowTime
  231. if e = logItem.Create(); e != nil {
  232. return
  233. }
  234. }()
  235. resp.Ok("操作成功", c)
  236. }
  237. // Edit
  238. // @Title 编辑合同登记
  239. // @Description 编辑合同登记
  240. // @Param request body fms.ContractRegisterEditReq true "type json string"
  241. // @Success 200 string "操作成功"
  242. // @router /contract/register/edit [post]
  243. func (rg *RegisterController) Edit(c *gin.Context) {
  244. req := new(fms.ContractRegisterEditReq)
  245. err := c.ShouldBind(&req)
  246. if err != nil {
  247. errs, ok := err.(validator.ValidationErrors)
  248. if !ok {
  249. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  250. return
  251. }
  252. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  253. return
  254. }
  255. claims, _ := c.Get("adminInfo")
  256. adminInfo := claims.(*system.SysAdmin)
  257. // 日期校验
  258. startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  259. if e != nil {
  260. resp.FailMsg("合同开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
  261. return
  262. }
  263. endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  264. if e != nil {
  265. resp.FailMsg("合同结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
  266. return
  267. }
  268. signDate, e := time.ParseInLocation(utils.FormatDate, req.SignDate, time.Local)
  269. if e != nil {
  270. resp.FailMsg("合同签订日期格式有误", "合同签订日期格式有误, Err: "+e.Error(), c)
  271. return
  272. }
  273. ob := new(fms.ContractRegister)
  274. item, e := ob.Fetch(req.ContractRegisterId)
  275. if e != nil {
  276. if e == utils.ErrNoRow {
  277. resp.Fail("登记记录不存在或已被删除", c)
  278. return
  279. }
  280. resp.FailMsg("操作失败", "获取合同登记信息失败, Err:"+e.Error(), c)
  281. return
  282. }
  283. // 是否存在相同合同编号的登记
  284. existCond := `contract_code = ?`
  285. existPars := make([]interface{}, 0)
  286. existPars = append(existPars, req.ContractCode)
  287. exist, e := ob.FetchByCondition(existCond, existPars)
  288. if e != nil && e != utils.ErrNoRow {
  289. resp.FailMsg("操作失败", "获取相同登记号失败, Err: "+e.Error(), c)
  290. return
  291. }
  292. if exist != nil && exist.ContractRegisterId > 0 && exist.ContractRegisterId != item.ContractRegisterId {
  293. resp.Fail("合同编号已存在", c)
  294. return
  295. }
  296. nowTime := time.Now().Local()
  297. item.ContractCode = req.ContractCode
  298. item.CrmContractId = req.CrmContractId
  299. item.ContractSource = req.ContractSource
  300. item.CompanyName = req.CompanyName
  301. item.SellerId = req.SellerId
  302. item.SellerName = req.SellerName
  303. item.ContractType = req.ContractType
  304. item.ContractAmount = req.ContractAmount
  305. item.StartDate = startDate
  306. item.EndDate = endDate
  307. item.SignDate = signDate
  308. item.AgreedPayTime = req.AgreedPayTime
  309. item.ContractStatus = req.ContractStatus
  310. item.RegisterStatus = fms.ContractRegisterStatusIng
  311. item.Remark = req.Remark
  312. item.ModifyTime = nowTime
  313. updateCols := []string{
  314. "ContractCode", "CrmContractId", "ContractSource", "CompanyName", "SellerId", "SellerName",
  315. "ContractType", "ContractAmount", "StartDate", "EndDate", "SignDate", "AgreedPayTime", "ContractStatus",
  316. "RegisterStatus", "Remark", "ModifyTime",
  317. }
  318. // 套餐信息
  319. serviceList, e := fmsService.HandleContractServiceAndDetail(req.ProductId, req.Services)
  320. if e != nil {
  321. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  322. return
  323. }
  324. // 更新合同及套餐
  325. if e = fms.UpdateContractRegisterAndServices(item, updateCols, serviceList); e != nil {
  326. resp.FailMsg("操作失败", "更新合同及套餐失败, Err: "+e.Error(), c)
  327. return
  328. }
  329. // 校验金额-是否修改状态
  330. go fmsService.CheckContractRegisterAmount(item.ContractRegisterId)
  331. // 操作日志
  332. go func() {
  333. opData := ""
  334. opDataByte, e := json.Marshal(req)
  335. if e != nil {
  336. return
  337. }
  338. opData = string(opDataByte)
  339. logItem := new(fms.ContractRegisterLog)
  340. logItem.ContractRegisterId = item.ContractRegisterId
  341. logItem.AdminId = int(adminInfo.AdminId)
  342. logItem.AdminName = adminInfo.AdminName
  343. logItem.OpData = opData
  344. logItem.OpType = fms.ContractRegisterOpTypeSave
  345. logItem.CreateTime = nowTime
  346. if e = logItem.Create(); e != nil {
  347. return
  348. }
  349. }()
  350. resp.Ok("操作成功", c)
  351. }
  352. // Del
  353. // @Title 删除合同登记
  354. // @Description 删除合同登记
  355. // @Param request body fms.ContractRegisterDelReq true "type json string"
  356. // @Success 200 string "操作成功"
  357. // @router /contract/register/del [post]
  358. func (rg *RegisterController) Del(c *gin.Context) {
  359. req := new(fms.ContractRegisterDelReq)
  360. err := c.ShouldBind(&req)
  361. if err != nil {
  362. errs, ok := err.(validator.ValidationErrors)
  363. if !ok {
  364. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  365. return
  366. }
  367. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  368. return
  369. }
  370. claims, _ := c.Get("adminInfo")
  371. adminInfo := claims.(*system.SysAdmin)
  372. ob := new(fms.ContractRegister)
  373. item, e := ob.Fetch(req.ContractRegisterId)
  374. if e != nil {
  375. if e == utils.ErrNoRow {
  376. resp.Fail("合同登记不存在或已被删除", c)
  377. return
  378. }
  379. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  380. return
  381. }
  382. nowTime := time.Now().Local()
  383. item.IsDeleted = 1
  384. item.ModifyTime = nowTime
  385. updateCols := []string{"IsDeleted", "ModifyTime"}
  386. if e = item.Update(updateCols); e != nil {
  387. resp.FailMsg("操作失败", "更新合同登记失败, Err:"+e.Error(), c)
  388. return
  389. }
  390. // 操作日志
  391. go func() {
  392. opData := ""
  393. opDataByte, e := json.Marshal(req)
  394. if e != nil {
  395. return
  396. }
  397. opData = string(opDataByte)
  398. logItem := new(fms.ContractRegisterLog)
  399. logItem.ContractRegisterId = req.ContractRegisterId
  400. logItem.AdminId = int(adminInfo.AdminId)
  401. logItem.AdminName = adminInfo.AdminName
  402. logItem.OpData = opData
  403. logItem.OpType = fms.ContractRegisterOpTypeDel
  404. logItem.CreateTime = nowTime
  405. if e = logItem.Create(); e != nil {
  406. return
  407. }
  408. }()
  409. resp.Ok("操作成功", c)
  410. }
  411. // Detail
  412. // @Title 合同登记详情
  413. // @Description 合同登记详情
  414. // @Param ContractRegisterId query int false "合同登记ID"
  415. // @Success 200 {object} fms.ContractRegisterDetail
  416. // @router /contract/register/detail [get]
  417. func (rg *RegisterController) Detail(c *gin.Context) {
  418. var req fms.ContractRegisterDetailReq
  419. if e := c.BindQuery(&req); e != nil {
  420. err, ok := e.(validator.ValidationErrors)
  421. if !ok {
  422. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  423. return
  424. }
  425. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  426. return
  427. }
  428. result := new(fms.ContractRegisterDetail)
  429. // 合同登记信息
  430. item, e := fms.GetContractRegisterItemById(req.ContractRegisterId)
  431. if e != nil {
  432. resp.FailData("获取失败", "获取合同登记详情失败, Err:"+e.Error(), c)
  433. return
  434. }
  435. result.ContractRegisterItem = *item
  436. // 套餐信息
  437. serviceList, e := fmsService.GetContractServiceAndDetail(req.ContractRegisterId)
  438. if e != nil {
  439. resp.FailData("获取失败", "获取合同套餐信息失败, Err: "+e.Error(), c)
  440. return
  441. }
  442. result.ServiceList = serviceList
  443. // 开票/到款信息
  444. invoiceCond := `contract_register_id = ?`
  445. invoicePars := make([]interface{}, 0)
  446. invoicePars = append(invoicePars, req.ContractRegisterId)
  447. invoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  448. if e != nil {
  449. resp.FailData("获取失败", "获取合同开票/到款信息失败, Err: "+e.Error(), c)
  450. return
  451. }
  452. result.InvoiceList = make([]*fms.ContractInvoiceItem, 0)
  453. result.PaymentList = make([]*fms.ContractInvoiceItem, 0)
  454. for i := range invoiceList {
  455. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  456. result.InvoiceList = append(result.InvoiceList, invoiceList[i])
  457. continue
  458. }
  459. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  460. result.InvoiceList = append(result.InvoiceList, invoiceList[i])
  461. }
  462. }
  463. // 合同登记进度
  464. logCond := `contract_register_id = ?`
  465. logPars := make([]interface{}, 0)
  466. logPars = append(logPars, req.ContractRegisterId)
  467. logList, e := fms.GetContractRegisterLogItemList(logCond, logPars)
  468. if e != nil {
  469. resp.FailData("获取失败", "获取合同登记进度失败, Err: "+e.Error(), c)
  470. return
  471. }
  472. result.Logs = logList
  473. resp.OkData("获取成功", result, c)
  474. }
  475. // UpdateStatus
  476. // @Title 修改合同状态
  477. // @Description 修改合同状态
  478. // @Param request body fms.ContractRegisterUpdateStatusReq true "type json string"
  479. // @Success 200 string "操作成功"
  480. // @router /contract/register/update_status [post]
  481. func (rg *RegisterController) UpdateStatus(c *gin.Context) {
  482. req := new(fms.ContractRegisterUpdateStatusReq)
  483. err := c.ShouldBind(&req)
  484. if err != nil {
  485. errs, ok := err.(validator.ValidationErrors)
  486. if !ok {
  487. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  488. return
  489. }
  490. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  491. return
  492. }
  493. claims, _ := c.Get("adminInfo")
  494. adminInfo := claims.(*system.SysAdmin)
  495. ob := new(fms.ContractRegister)
  496. item, e := ob.Fetch(req.ContractRegisterId)
  497. if e != nil {
  498. if e == utils.ErrNoRow {
  499. resp.Fail("合同登记不存在或已被删除", c)
  500. return
  501. }
  502. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  503. return
  504. }
  505. nowTime := time.Now().Local()
  506. item.ContractStatus = req.ContractStatus
  507. item.ModifyTime = nowTime
  508. updateCols := []string{"ContractStatus", "ModifyTime"}
  509. if e = item.Update(updateCols); e != nil {
  510. resp.FailMsg("操作失败", "更新合同登记失败, Err:"+e.Error(), c)
  511. return
  512. }
  513. // 操作日志
  514. go func() {
  515. opData := ""
  516. opDataByte, e := json.Marshal(req)
  517. if e != nil {
  518. return
  519. }
  520. opData = string(opDataByte)
  521. logItem := new(fms.ContractRegisterLog)
  522. logItem.ContractRegisterId = req.ContractRegisterId
  523. logItem.AdminId = int(adminInfo.AdminId)
  524. logItem.AdminName = adminInfo.AdminName
  525. logItem.OpData = opData
  526. logItem.OpType = fms.ContractRegisterOpTypeStatus
  527. logItem.CreateTime = nowTime
  528. if e = logItem.Create(); e != nil {
  529. return
  530. }
  531. }()
  532. resp.Ok("操作成功", c)
  533. }
  534. // Invoice
  535. // @Title 开票/到款登记
  536. // @Description 开票/到款登记
  537. // @Param request body fms.ContractInvoiceSaveReq true "type json string"
  538. // @Success 200 string "操作成功"
  539. // @router /contract/register/invoice [post]
  540. func (rg *RegisterController) Invoice(c *gin.Context) {
  541. req := new(fms.ContractInvoiceSaveReq)
  542. err := c.ShouldBind(&req)
  543. if err != nil {
  544. errs, ok := err.(validator.ValidationErrors)
  545. if !ok {
  546. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  547. return
  548. }
  549. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  550. return
  551. }
  552. claims, _ := c.Get("adminInfo")
  553. adminInfo := claims.(*system.SysAdmin)
  554. newInvoice := make([]*fms.ContractInvoice, 0)
  555. if len(req.AmountList) > 0 {
  556. for i := range req.AmountList {
  557. if req.AmountList[i].Amount <= 0 {
  558. resp.Fail("登记金额有误", c)
  559. return
  560. }
  561. if req.AmountList[i].InvoiceDate == "" {
  562. resp.Fail("请选择日期", c)
  563. return
  564. }
  565. t, e := time.ParseInLocation(utils.FormatDate, req.AmountList[i].InvoiceDate, time.Local)
  566. if e != nil {
  567. resp.FailData("日期格式有误", "Err:"+e.Error(), c)
  568. return
  569. }
  570. v := &fms.ContractInvoice{
  571. ContractRegisterId: req.ContractRegisterId,
  572. Amount: req.AmountList[i].Amount,
  573. InvoiceType: req.InvoiceType,
  574. InvoiceDate: t,
  575. AdminId: int(adminInfo.AdminId),
  576. AdminName: adminInfo.AdminName,
  577. }
  578. v.Set()
  579. newInvoice = append(newInvoice, v)
  580. }
  581. }
  582. // 删除并新增登记
  583. ob := new(fms.ContractInvoice)
  584. if e := ob.DeleteAndCreateNewInvoice(req.ContractRegisterId, req.InvoiceType, newInvoice); e != nil {
  585. resp.FailData("日期格式有误", "Err:"+e.Error(), c)
  586. return
  587. }
  588. // 校验金额-是否修改状态
  589. go fmsService.CheckContractRegisterAmount(req.ContractRegisterId)
  590. // 操作日志
  591. go func() {
  592. opData := ""
  593. opDataByte, e := json.Marshal(req)
  594. if e != nil {
  595. return
  596. }
  597. opData = string(opDataByte)
  598. opType := fms.ContractRegisterOpTypeInvoice
  599. if req.InvoiceType == fms.ContractInvoiceTypePay {
  600. opType = fms.ContractRegisterOpTypePayment
  601. }
  602. logItem := new(fms.ContractRegisterLog)
  603. logItem.ContractRegisterId = req.ContractRegisterId
  604. logItem.AdminId = int(adminInfo.AdminId)
  605. logItem.AdminName = adminInfo.AdminName
  606. logItem.OpData = opData
  607. logItem.OpType = opType
  608. logItem.CreateTime = time.Now().Local()
  609. if e = logItem.Create(); e != nil {
  610. return
  611. }
  612. }()
  613. resp.Ok("操作成功", c)
  614. }
  615. // 导出
  616. func (rg *RegisterController) Export(c *gin.Context) {
  617. }