register.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. list[i].ServicesName = serviceMap[list[i].ContractRegisterId]
  126. v.ContractRegisterItem = *list[i]
  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.SellerId = req.SellerId
  193. ob.SellerName = req.SellerName
  194. ob.ContractType = req.ContractType
  195. ob.ContractAmount = req.ContractAmount
  196. ob.StartDate = startDate
  197. ob.EndDate = endDate
  198. ob.SignDate = signDate
  199. ob.AgreedPayTime = req.AgreedPayTime
  200. ob.ContractStatus = req.ContractStatus
  201. ob.RegisterStatus = fms.ContractRegisterStatusIng
  202. ob.Remark = req.Remark
  203. ob.Set()
  204. // 套餐信息
  205. serviceList, e := fmsService.HandleContractServiceAndDetail(req.ProductId, req.Services)
  206. if e != nil {
  207. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  208. return
  209. }
  210. // 新增合同及套餐
  211. if e = fms.CreateContractRegisterAndServices(ob, serviceList); e != nil {
  212. resp.FailMsg("操作失败", "新增合同及套餐失败, Err: "+e.Error(), c)
  213. return
  214. }
  215. // 操作日志
  216. go func() {
  217. opData := ""
  218. opDataByte, e := json.Marshal(req)
  219. if e != nil {
  220. return
  221. }
  222. opData = string(opDataByte)
  223. logItem := new(fms.ContractRegisterLog)
  224. logItem.ContractRegisterId = ob.ContractRegisterId
  225. logItem.AdminId = int(adminInfo.AdminId)
  226. logItem.AdminName = adminInfo.AdminName
  227. logItem.OpData = opData
  228. logItem.OpType = fms.ContractRegisterOpTypeSave
  229. logItem.CreateTime = nowTime
  230. if e = logItem.Create(); e != nil {
  231. return
  232. }
  233. }()
  234. resp.Ok("操作成功", c)
  235. }
  236. // Edit
  237. // @Title 编辑合同登记
  238. // @Description 编辑合同登记
  239. // @Param request body fms.ContractRegisterEditReq true "type json string"
  240. // @Success 200 string "操作成功"
  241. // @router /contract/register/edit [post]
  242. func (rg *RegisterController) Edit(c *gin.Context) {
  243. req := new(fms.ContractRegisterEditReq)
  244. err := c.ShouldBind(&req)
  245. if err != nil {
  246. errs, ok := err.(validator.ValidationErrors)
  247. if !ok {
  248. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  249. return
  250. }
  251. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  252. return
  253. }
  254. claims, _ := c.Get("adminInfo")
  255. adminInfo := claims.(*system.SysAdmin)
  256. // 日期校验
  257. startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  258. if e != nil {
  259. resp.FailMsg("合同开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
  260. return
  261. }
  262. endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  263. if e != nil {
  264. resp.FailMsg("合同结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
  265. return
  266. }
  267. signDate, e := time.ParseInLocation(utils.FormatDate, req.SignDate, time.Local)
  268. if e != nil {
  269. resp.FailMsg("合同签订日期格式有误", "合同签订日期格式有误, Err: "+e.Error(), c)
  270. return
  271. }
  272. ob := new(fms.ContractRegister)
  273. item, e := ob.Fetch(req.ContractRegisterId)
  274. if e != nil {
  275. if e == utils.ErrNoRow {
  276. resp.Fail("登记记录不存在或已被删除", c)
  277. return
  278. }
  279. resp.FailMsg("操作失败", "获取合同登记信息失败, Err:"+e.Error(), c)
  280. return
  281. }
  282. // 是否存在相同合同编号的登记
  283. existCond := `contract_code = ?`
  284. existPars := make([]interface{}, 0)
  285. existPars = append(existPars, req.ContractCode)
  286. exist, e := ob.FetchByCondition(existCond, existPars)
  287. if e != nil && e != utils.ErrNoRow {
  288. resp.FailMsg("操作失败", "获取相同登记号失败, Err: "+e.Error(), c)
  289. return
  290. }
  291. if exist != nil && exist.ContractRegisterId > 0 && exist.ContractRegisterId != item.ContractRegisterId {
  292. resp.Fail("合同编号已存在", c)
  293. return
  294. }
  295. nowTime := time.Now().Local()
  296. item.ContractCode = req.ContractCode
  297. item.CrmContractId = req.CrmContractId
  298. item.ContractSource = req.ContractSource
  299. item.CompanyName = req.CompanyName
  300. item.SellerId = req.SellerId
  301. item.SellerName = req.SellerName
  302. item.ContractType = req.ContractType
  303. item.ContractAmount = req.ContractAmount
  304. item.StartDate = startDate
  305. item.EndDate = endDate
  306. item.SignDate = signDate
  307. item.AgreedPayTime = req.AgreedPayTime
  308. item.ContractStatus = req.ContractStatus
  309. item.RegisterStatus = fms.ContractRegisterStatusIng
  310. item.Remark = req.Remark
  311. item.ModifyTime = nowTime
  312. updateCols := []string{
  313. "ContractCode", "CrmContractId", "ContractSource", "CompanyName", "SellerId", "SellerName",
  314. "ContractType", "ContractAmount", "StartDate", "EndDate", "SignDate", "AgreedPayTime", "ContractStatus",
  315. "RegisterStatus", "Remark", "ModifyTime",
  316. }
  317. // 套餐信息
  318. serviceList, e := fmsService.HandleContractServiceAndDetail(req.ProductId, req.Services)
  319. if e != nil {
  320. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  321. return
  322. }
  323. // 更新合同及套餐
  324. if e = fms.UpdateContractRegisterAndServices(ob, updateCols, serviceList); e != nil {
  325. resp.FailMsg("操作失败", "更新合同及套餐失败, Err: "+e.Error(), c)
  326. return
  327. }
  328. // 校验金额-是否修改状态
  329. go fmsService.CheckContractRegisterAmount(item.ContractRegisterId)
  330. // 操作日志
  331. go func() {
  332. opData := ""
  333. opDataByte, e := json.Marshal(req)
  334. if e != nil {
  335. return
  336. }
  337. opData = string(opDataByte)
  338. logItem := new(fms.ContractRegisterLog)
  339. logItem.ContractRegisterId = item.ContractRegisterId
  340. logItem.AdminId = int(adminInfo.AdminId)
  341. logItem.AdminName = adminInfo.AdminName
  342. logItem.OpData = opData
  343. logItem.OpType = fms.ContractRegisterOpTypeSave
  344. logItem.CreateTime = nowTime
  345. if e = logItem.Create(); e != nil {
  346. return
  347. }
  348. }()
  349. resp.Ok("操作成功", c)
  350. }
  351. // Del
  352. // @Title 删除合同登记
  353. // @Description 删除合同登记
  354. // @Param request body fms.ContractRegisterDelReq true "type json string"
  355. // @Success 200 string "操作成功"
  356. // @router /contract/register/del [post]
  357. func (rg *RegisterController) Del(c *gin.Context) {
  358. req := new(fms.ContractRegisterDelReq)
  359. err := c.ShouldBind(&req)
  360. if err != nil {
  361. errs, ok := err.(validator.ValidationErrors)
  362. if !ok {
  363. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  364. return
  365. }
  366. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  367. return
  368. }
  369. claims, _ := c.Get("adminInfo")
  370. adminInfo := claims.(*system.SysAdmin)
  371. ob := new(fms.ContractRegister)
  372. item, e := ob.Fetch(req.ContractRegisterId)
  373. if e != nil {
  374. if e == utils.ErrNoRow {
  375. resp.Fail("合同登记不存在或已被删除", c)
  376. return
  377. }
  378. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  379. return
  380. }
  381. nowTime := time.Now().Local()
  382. item.IsDeleted = 1
  383. item.ModifyTime = nowTime
  384. updateCols := []string{"IsDeleted", "ModifyTime"}
  385. if e = item.Update(updateCols); e != nil {
  386. resp.FailMsg("操作失败", "更新合同登记失败, Err:"+e.Error(), c)
  387. return
  388. }
  389. // 操作日志
  390. go func() {
  391. opData := ""
  392. opDataByte, e := json.Marshal(req)
  393. if e != nil {
  394. return
  395. }
  396. opData = string(opDataByte)
  397. logItem := new(fms.ContractRegisterLog)
  398. logItem.ContractRegisterId = req.ContractRegisterId
  399. logItem.AdminId = int(adminInfo.AdminId)
  400. logItem.AdminName = adminInfo.AdminName
  401. logItem.OpData = opData
  402. logItem.OpType = fms.ContractRegisterOpTypeDel
  403. logItem.CreateTime = nowTime
  404. if e = logItem.Create(); e != nil {
  405. return
  406. }
  407. }()
  408. resp.Ok("操作成功", c)
  409. }
  410. // Detail
  411. // @Title 合同登记详情
  412. // @Description 合同登记详情
  413. // @Param ContractRegisterId query int false "合同登记ID"
  414. // @Success 200 {object} fms.ContractRegisterDetail
  415. // @router /contract/register/detail [get]
  416. func (rg *RegisterController) Detail(c *gin.Context) {
  417. var req fms.ContractRegisterDetailReq
  418. if e := c.BindQuery(&req); e != nil {
  419. err, ok := e.(validator.ValidationErrors)
  420. if !ok {
  421. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  422. return
  423. }
  424. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  425. return
  426. }
  427. result := new(fms.ContractRegisterDetail)
  428. // 合同登记信息
  429. item, e := fms.GetContractRegisterItemById(req.ContractRegisterId)
  430. if e != nil {
  431. resp.FailData("获取失败", "获取合同登记详情失败, Err:"+e.Error(), c)
  432. return
  433. }
  434. result.ContractRegisterItem = *item
  435. // 套餐信息
  436. serviceList, e := fmsService.GetContractServiceAndDetail(req.ContractRegisterId)
  437. if e != nil {
  438. resp.FailData("获取失败", "获取合同套餐信息失败, Err: "+e.Error(), c)
  439. return
  440. }
  441. result.ServiceList = serviceList
  442. // 开票/到款信息
  443. invoiceCond := `contract_register_id = ?`
  444. invoicePars := make([]interface{}, 0)
  445. invoicePars = append(invoicePars, req.ContractRegisterId)
  446. invoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  447. if e != nil {
  448. resp.FailData("获取失败", "获取合同开票/到款信息失败, Err: "+e.Error(), c)
  449. return
  450. }
  451. result.InvoiceList = make([]*fms.ContractInvoiceItem, 0)
  452. result.PaymentList = make([]*fms.ContractInvoiceItem, 0)
  453. for i := range invoiceList {
  454. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  455. result.InvoiceList = append(result.InvoiceList, invoiceList[i])
  456. continue
  457. }
  458. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  459. result.InvoiceList = append(result.InvoiceList, invoiceList[i])
  460. }
  461. }
  462. // 合同登记进度
  463. logCond := `contract_register_id = ?`
  464. logPars := make([]interface{}, 0)
  465. logPars = append(logPars, req.ContractRegisterId)
  466. logList, e := fms.GetContractRegisterLogItemList(logCond, logPars)
  467. if e != nil {
  468. resp.FailData("获取失败", "获取合同登记进度失败, Err: "+e.Error(), c)
  469. return
  470. }
  471. result.Logs = logList
  472. resp.OkData("获取成功", result, c)
  473. }
  474. // UpdateStatus
  475. // @Title 修改合同状态
  476. // @Description 修改合同状态
  477. // @Param request body fms.ContractRegisterUpdateStatusReq true "type json string"
  478. // @Success 200 string "操作成功"
  479. // @router /contract/register/update_status [post]
  480. func (rg *RegisterController) UpdateStatus(c *gin.Context) {
  481. req := new(fms.ContractRegisterUpdateStatusReq)
  482. err := c.ShouldBind(&req)
  483. if err != nil {
  484. errs, ok := err.(validator.ValidationErrors)
  485. if !ok {
  486. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  487. return
  488. }
  489. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  490. return
  491. }
  492. claims, _ := c.Get("adminInfo")
  493. adminInfo := claims.(*system.SysAdmin)
  494. ob := new(fms.ContractRegister)
  495. item, e := ob.Fetch(req.ContractRegisterId)
  496. if e != nil {
  497. if e == utils.ErrNoRow {
  498. resp.Fail("合同登记不存在或已被删除", c)
  499. return
  500. }
  501. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  502. return
  503. }
  504. nowTime := time.Now().Local()
  505. item.ContractStatus = req.ContractStatus
  506. item.ModifyTime = nowTime
  507. updateCols := []string{"ContractStatus", "ModifyTime"}
  508. if e = item.Update(updateCols); e != nil {
  509. resp.FailMsg("操作失败", "更新合同登记失败, Err:"+e.Error(), c)
  510. return
  511. }
  512. // 操作日志
  513. go func() {
  514. opData := ""
  515. opDataByte, e := json.Marshal(req)
  516. if e != nil {
  517. return
  518. }
  519. opData = string(opDataByte)
  520. logItem := new(fms.ContractRegisterLog)
  521. logItem.ContractRegisterId = req.ContractRegisterId
  522. logItem.AdminId = int(adminInfo.AdminId)
  523. logItem.AdminName = adminInfo.AdminName
  524. logItem.OpData = opData
  525. logItem.OpType = fms.ContractRegisterOpTypeStatus
  526. logItem.CreateTime = nowTime
  527. if e = logItem.Create(); e != nil {
  528. return
  529. }
  530. }()
  531. resp.Ok("操作成功", c)
  532. }
  533. // Invoice
  534. // @Title 开票/到款登记
  535. // @Description 开票/到款登记
  536. // @Param request body fms.ContractInvoiceSaveReq true "type json string"
  537. // @Success 200 string "操作成功"
  538. // @router /contract/register/invoice [post]
  539. func (rg *RegisterController) Invoice(c *gin.Context) {
  540. req := new(fms.ContractInvoiceSaveReq)
  541. err := c.ShouldBind(&req)
  542. if err != nil {
  543. errs, ok := err.(validator.ValidationErrors)
  544. if !ok {
  545. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  546. return
  547. }
  548. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  549. return
  550. }
  551. claims, _ := c.Get("adminInfo")
  552. adminInfo := claims.(*system.SysAdmin)
  553. newInvoice := make([]*fms.ContractInvoice, 0)
  554. if len(req.AmountList) > 0 {
  555. for i := range req.AmountList {
  556. if req.AmountList[i].Amount <= 0 {
  557. resp.Fail("登记金额有误", c)
  558. return
  559. }
  560. if req.AmountList[i].InvoiceDate == "" {
  561. resp.Fail("请选择日期", c)
  562. return
  563. }
  564. t, e := time.ParseInLocation(utils.FormatDate, req.AmountList[i].InvoiceDate, time.Local)
  565. if e != nil {
  566. resp.FailData("日期格式有误", "Err:"+e.Error(), c)
  567. return
  568. }
  569. v := &fms.ContractInvoice{
  570. ContractRegisterId: req.ContractRegisterId,
  571. Amount: req.AmountList[i].Amount,
  572. InvoiceType: req.InvoiceType,
  573. InvoiceDate: t,
  574. AdminId: int(adminInfo.AdminId),
  575. AdminName: adminInfo.AdminName,
  576. }
  577. v.Set()
  578. newInvoice = append(newInvoice, v)
  579. }
  580. }
  581. // 删除并新增登记
  582. ob := new(fms.ContractInvoice)
  583. if e := ob.DeleteAndCreateNewInvoice(req.ContractRegisterId, req.InvoiceType, newInvoice); e != nil {
  584. resp.FailData("日期格式有误", "Err:"+e.Error(), c)
  585. return
  586. }
  587. // 校验金额-是否修改状态
  588. go fmsService.CheckContractRegisterAmount(req.ContractRegisterId)
  589. // 操作日志
  590. go func() {
  591. opData := ""
  592. opDataByte, e := json.Marshal(req)
  593. if e != nil {
  594. return
  595. }
  596. opData = string(opDataByte)
  597. opType := fms.ContractRegisterOpTypeInvoice
  598. if req.InvoiceType == fms.ContractInvoiceTypePay {
  599. opType = fms.ContractRegisterOpTypePayment
  600. }
  601. logItem := new(fms.ContractRegisterLog)
  602. logItem.ContractRegisterId = req.ContractRegisterId
  603. logItem.AdminId = int(adminInfo.AdminId)
  604. logItem.AdminName = adminInfo.AdminName
  605. logItem.OpData = opData
  606. logItem.OpType = opType
  607. logItem.CreateTime = time.Now().Local()
  608. if e = logItem.Create(); e != nil {
  609. return
  610. }
  611. }()
  612. resp.Ok("操作成功", c)
  613. }
  614. // 导出
  615. func (rg *RegisterController) Export(c *gin.Context) {
  616. }