pre_payment.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. package contract
  2. import (
  3. "fmt"
  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 PrePaymentController 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 SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
  24. // @Success 200 {object} fms.ContractRegisterItem
  25. // @router /contract/pre_pay/list [get]
  26. func (rg *PrePaymentController) List(c *gin.Context) {
  27. var req fms.PrePayListReq
  28. if e := c.BindQuery(&req); e != nil {
  29. err, ok := e.(validator.ValidationErrors)
  30. if !ok {
  31. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  32. return
  33. }
  34. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  35. return
  36. }
  37. cond := `1 = 1`
  38. pars := make([]interface{}, 0)
  39. // 客户姓名/销售
  40. if req.Keyword != "" {
  41. kw := "%" + req.Keyword + "%"
  42. cond += ` AND (company_name LIKE ? OR seller_name LIKE ? )`
  43. pars = append(pars, kw, kw)
  44. }
  45. if req.StartDate != "" && req.EndDate != "" {
  46. st := fmt.Sprint(req.StartDate, " 00:00:00")
  47. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  48. cond += ` AND (create_time BETWEEN ? AND ?)`
  49. pars = append(pars, st, ed)
  50. }
  51. // 货币列表
  52. currencyOB := new(fms.CurrencyUnit)
  53. currencyCond := `enable = 1`
  54. currencyPars := make([]interface{}, 0)
  55. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  56. if e != nil {
  57. resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
  58. return
  59. }
  60. unitMap := make(map[string]string)
  61. for i := range currencyList {
  62. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  63. }
  64. page := new(base.Page)
  65. page.SetPageSize(req.PageSize)
  66. page.SetCurrent(req.Current)
  67. sortTypeMap := map[int]bool{0: false, 1: true, 2: false}
  68. page.AddOrderItem(base.OrderItem{Column: "create_time", Asc: sortTypeMap[req.SortType]})
  69. total, list, e := fms.GetPrePayItemPageList(page, cond, pars)
  70. if e != nil {
  71. resp.FailMsg("获取失败", "获取预登记列表失败, Err: "+e.Error(), c)
  72. return
  73. }
  74. for i := range list {
  75. list[i].UnitName = unitMap[list[i].CurrencyUnit]
  76. }
  77. page.SetTotal(total)
  78. baseData := new(base.BaseData)
  79. baseData.SetPage(page)
  80. baseData.SetList(list)
  81. resp.OkData("获取成功", baseData, c)
  82. }
  83. // Add
  84. // @Title 新增到款预登记
  85. // @Description 新增到款预登记
  86. // @Param request body fms.PrepayAddReq true "type json string"
  87. // @Success 200 string "操作成功"
  88. // @router /contract/pre_pay/add [post]
  89. func (rg *PrePaymentController) Add(c *gin.Context) {
  90. req := new(fms.PrepayAddReq)
  91. err := c.ShouldBind(&req)
  92. if err != nil {
  93. errs, ok := err.(validator.ValidationErrors)
  94. if !ok {
  95. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  96. return
  97. }
  98. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  99. return
  100. }
  101. claims, _ := c.Get("adminInfo")
  102. adminInfo := claims.(*system.SysAdmin)
  103. // 日期校验
  104. startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  105. if e != nil {
  106. resp.FailMsg("约定开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
  107. return
  108. }
  109. endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  110. if e != nil {
  111. resp.FailMsg("约定结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
  112. return
  113. }
  114. // 货币及汇率
  115. rateList, e := fmsService.GetTodayCurrencyRateList()
  116. if e != nil {
  117. resp.FailMsg("操作失败", "获取今日货币汇率失败, Err: "+e.Error(), c)
  118. return
  119. }
  120. var rate float64
  121. for i := range rateList {
  122. if req.CurrencyUnit == rateList[i].Code {
  123. rate = rateList[i].RMBRate
  124. break
  125. }
  126. }
  127. if rate <= 0 {
  128. resp.FailMsg("操作失败", "货币汇率信息有误", c)
  129. return
  130. }
  131. payDate,err := time.Parse(utils.FormatDate,req.PaymentDate)
  132. if e != nil {
  133. resp.FailMsg("日期转换失败", "日期转换失败, Err: "+e.Error(), c)
  134. return
  135. }
  136. ob := new(fms.ContractPrePayment)
  137. ob.CompanyName = req.CompanyName
  138. ob.CurrencyUnit = req.CurrencyUnit
  139. ob.StartDate = startDate
  140. ob.EndDate = endDate
  141. ob.AdminId = int(adminInfo.AdminId)
  142. ob.AdminName = adminInfo.AdminName
  143. ob.Amount = req.Amount / rate
  144. ob.OriginAmount = req.Amount
  145. ob.CurrencyUnit = req.CurrencyUnit
  146. ob.Remark = req.Remark
  147. ob.PaymentDate = payDate
  148. ob.Set()
  149. // 新增合同及套餐
  150. if e = ob.Create(); e != nil {
  151. resp.FailMsg("操作失败", "新增合同及套餐失败, Err: "+e.Error(), c)
  152. return
  153. }
  154. //// 开票到款汇总
  155. //nowTime := time.Now().Local()
  156. //v := new(fms.InvoicePaymentSummary)
  157. //v.CreateTime = nowTime
  158. //v.ModifyTime = nowTime
  159. //v.PrePayid = ob.PrePayId
  160. //
  161. //if e = v.Create(); e != nil {
  162. // err = fmt.Errorf("新增汇总数据失败, Err: %s", e.Error())
  163. //}
  164. resp.Ok("操作成功", c)
  165. }
  166. // Edit
  167. // @Title 编辑到款预登记
  168. // @Description 编辑到款预登记
  169. // @Param request body fms.ContractRegisterEditReq true "type json string"
  170. // @Success 200 string "操作成功"
  171. // @router /contract/pre_pay/edit [post]
  172. func (rg *PrePaymentController) Edit(c *gin.Context) {
  173. req := new(fms.PrepayEditReq)
  174. err := c.ShouldBind(&req)
  175. if err != nil {
  176. errs, ok := err.(validator.ValidationErrors)
  177. if !ok {
  178. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  179. return
  180. }
  181. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  182. return
  183. }
  184. claims, _ := c.Get("adminInfo")
  185. adminInfo := claims.(*system.SysAdmin)
  186. // 日期校验
  187. startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  188. if e != nil {
  189. resp.FailMsg("约定开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
  190. return
  191. }
  192. endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  193. if e != nil {
  194. resp.FailMsg("约定结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
  195. return
  196. }
  197. // 货币及汇率
  198. rateList, e := fmsService.GetTodayCurrencyRateList()
  199. if e != nil {
  200. resp.FailMsg("操作失败", "获取今日货币汇率失败, Err: "+e.Error(), c)
  201. return
  202. }
  203. var rate float64
  204. for i := range rateList {
  205. if req.CurrencyUnit == rateList[i].Code {
  206. rate = rateList[i].RMBRate
  207. break
  208. }
  209. }
  210. if rate <= 0 {
  211. resp.FailMsg("操作失败", "货币汇率信息有误", c)
  212. return
  213. }
  214. ob := new(fms.ContractPrePayment)
  215. item, e := ob.Fetch(req.PrePayId)
  216. if e != nil {
  217. if e == utils.ErrNoRow {
  218. resp.Fail("预到款登记记录不存在或已被删除", c)
  219. return
  220. }
  221. resp.FailMsg("操作失败", "获取预到款登记信息失败, Err:"+e.Error(), c)
  222. return
  223. }
  224. payDate,err := time.Parse(utils.FormatDate,req.PaymentDate)
  225. if e != nil {
  226. resp.FailMsg("日期转换失败", "日期转换失败, Err: "+e.Error(), c)
  227. return
  228. }
  229. item.CompanyName = req.CompanyName
  230. item.CurrencyUnit = req.CurrencyUnit
  231. item.StartDate = startDate
  232. item.EndDate = endDate
  233. item.AdminId = int(adminInfo.AdminId)
  234. item.AdminName = adminInfo.AdminName
  235. item.Amount = req.Amount / rate
  236. item.OriginAmount = req.Amount
  237. item.CurrencyUnit = req.CurrencyUnit
  238. item.Remark = req.Remark
  239. item.PaymentDate = payDate
  240. item.ModifyTime = time.Now().Local()
  241. updateCols := []string{
  242. "ContractCode", "CompanyName", "SellerId", "SellerName", "Amount", "StartDate", "EndDate",
  243. "Remark", "ModifyTime","NewCompany", "OriginAmount", "CurrencyUnit", "PaymentDate",
  244. }
  245. if e = item.Update(updateCols); e != nil {
  246. resp.FailMsg("操作失败", "更新到预款登记失败, Err:"+e.Error(), c)
  247. return
  248. }
  249. resp.Ok("操作成功", c)
  250. }
  251. // Del
  252. // @Title 删除到款预登记
  253. // @Description 删除到款预登记
  254. // @Param request body fms.ContractRegisterDelReq true "type json string"
  255. // @Success 200 string "操作成功"
  256. // @router /contract/pre_pay/del [post]
  257. func (rg *PrePaymentController) Del(c *gin.Context) {
  258. req := new(fms.PrepayDelReq)
  259. err := c.ShouldBind(&req)
  260. if err != nil {
  261. errs, ok := err.(validator.ValidationErrors)
  262. if !ok {
  263. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  264. return
  265. }
  266. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  267. return
  268. }
  269. ob := new(fms.ContractPrePayment)
  270. item, e := ob.Fetch(req.PrePayId)
  271. if e != nil {
  272. if e == utils.ErrNoRow {
  273. resp.Fail("合同登记不存在或已被删除", c)
  274. return
  275. }
  276. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  277. return
  278. }
  279. e = item.Delete()
  280. if e != nil {
  281. resp.FailMsg("删除记录失败", "Err:"+e.Error(), c)
  282. return
  283. }
  284. // 操作日志
  285. //go func() {
  286. // opData := ""
  287. // opDataByte, e := json.Marshal(req)
  288. // if e != nil {
  289. // return
  290. // }
  291. // opData = string(opDataByte)
  292. //
  293. // logItem := new(fms.ContractRegisterLog)
  294. // logItem.ContractRegisterId = req.ContractRegisterId
  295. // logItem.AdminId = int(adminInfo.AdminId)
  296. // logItem.AdminName = adminInfo.RealName
  297. // logItem.OpData = opData
  298. // logItem.OpType = fms.ContractRegisterOpTypeDel
  299. // logItem.CreateTime = nowTime
  300. // if e = logItem.Create(); e != nil {
  301. // return
  302. // }
  303. //}()
  304. resp.Ok("操作成功", c)
  305. }