pre_payment.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. ob := new(fms.ContractPrePayment)
  132. ob.CompanyName = req.CompanyName
  133. ob.SellerId = req.SellerId
  134. ob.SellerName = req.SellerName
  135. ob.CurrencyUnit = req.CurrencyUnit
  136. ob.StartDate = startDate
  137. ob.EndDate = endDate
  138. ob.AdminId = int(adminInfo.AdminId)
  139. ob.AdminName = adminInfo.AdminName
  140. ob.Amount = req.Amount / rate
  141. ob.OriginAmount = req.Amount
  142. ob.CurrencyUnit = req.CurrencyUnit
  143. ob.Remark = req.Remark
  144. ob.NewCompany = req.NewCompany
  145. ob.Set()
  146. // 新增合同及套餐
  147. if e = ob.Create(); e != nil {
  148. resp.FailMsg("操作失败", "新增合同及套餐失败, Err: "+e.Error(), c)
  149. return
  150. }
  151. //// 开票到款汇总
  152. //nowTime := time.Now().Local()
  153. //v := new(fms.InvoicePaymentSummary)
  154. //v.CreateTime = nowTime
  155. //v.ModifyTime = nowTime
  156. //v.PrePayid = ob.PrePayId
  157. //
  158. //if e = v.Create(); e != nil {
  159. // err = fmt.Errorf("新增汇总数据失败, Err: %s", e.Error())
  160. //}
  161. resp.Ok("操作成功", c)
  162. }
  163. // Edit
  164. // @Title 编辑到款预登记
  165. // @Description 编辑到款预登记
  166. // @Param request body fms.ContractRegisterEditReq true "type json string"
  167. // @Success 200 string "操作成功"
  168. // @router /contract/pre_pay/edit [post]
  169. func (rg *PrePaymentController) Edit(c *gin.Context) {
  170. req := new(fms.PrepayEditReq)
  171. err := c.ShouldBind(&req)
  172. if err != nil {
  173. errs, ok := err.(validator.ValidationErrors)
  174. if !ok {
  175. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  176. return
  177. }
  178. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  179. return
  180. }
  181. claims, _ := c.Get("adminInfo")
  182. adminInfo := claims.(*system.SysAdmin)
  183. // 日期校验
  184. startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  185. if e != nil {
  186. resp.FailMsg("约定开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
  187. return
  188. }
  189. endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  190. if e != nil {
  191. resp.FailMsg("约定结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
  192. return
  193. }
  194. // 货币及汇率
  195. rateList, e := fmsService.GetTodayCurrencyRateList()
  196. if e != nil {
  197. resp.FailMsg("操作失败", "获取今日货币汇率失败, Err: "+e.Error(), c)
  198. return
  199. }
  200. var rate float64
  201. for i := range rateList {
  202. if req.CurrencyUnit == rateList[i].Code {
  203. rate = rateList[i].RMBRate
  204. break
  205. }
  206. }
  207. if rate <= 0 {
  208. resp.FailMsg("操作失败", "货币汇率信息有误", c)
  209. return
  210. }
  211. ob := new(fms.ContractPrePayment)
  212. item, e := ob.Fetch(req.PrePayId)
  213. if e != nil {
  214. if e == utils.ErrNoRow {
  215. resp.Fail("预到款登记记录不存在或已被删除", c)
  216. return
  217. }
  218. resp.FailMsg("操作失败", "获取预到款登记信息失败, Err:"+e.Error(), c)
  219. return
  220. }
  221. item.CompanyName = req.CompanyName
  222. item.SellerId = req.SellerId
  223. item.SellerName = req.SellerName
  224. item.CurrencyUnit = req.CurrencyUnit
  225. item.StartDate = startDate
  226. item.EndDate = endDate
  227. item.AdminId = int(adminInfo.AdminId)
  228. item.AdminName = adminInfo.AdminName
  229. item.Amount = req.Amount / rate
  230. item.OriginAmount = req.Amount
  231. item.CurrencyUnit = req.CurrencyUnit
  232. item.Remark = req.Remark
  233. item.NewCompany = req.NewCompany
  234. item.ModifyTime = time.Now().Local()
  235. updateCols := []string{
  236. "ContractCode", "CompanyName", "SellerId", "SellerName", "Amount", "StartDate", "EndDate",
  237. "Remark", "ModifyTime","NewCompany", "OriginAmount", "CurrencyUnit",
  238. }
  239. if e = item.Update(updateCols); e != nil {
  240. resp.FailMsg("操作失败", "更新到预款登记失败, Err:"+e.Error(), c)
  241. return
  242. }
  243. resp.Ok("操作成功", c)
  244. }
  245. // Del
  246. // @Title 删除到款预登记
  247. // @Description 删除到款预登记
  248. // @Param request body fms.ContractRegisterDelReq true "type json string"
  249. // @Success 200 string "操作成功"
  250. // @router /contract/pre_pay/del [post]
  251. func (rg *PrePaymentController) Del(c *gin.Context) {
  252. req := new(fms.PrepayDelReq)
  253. err := c.ShouldBind(&req)
  254. if err != nil {
  255. errs, ok := err.(validator.ValidationErrors)
  256. if !ok {
  257. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  258. return
  259. }
  260. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  261. return
  262. }
  263. ob := new(fms.ContractPrePayment)
  264. item, e := ob.Fetch(req.PrePayId)
  265. if e != nil {
  266. if e == utils.ErrNoRow {
  267. resp.Fail("合同登记不存在或已被删除", c)
  268. return
  269. }
  270. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  271. return
  272. }
  273. e = item.Delete()
  274. if e != nil {
  275. resp.FailMsg("删除记录失败", "Err:"+e.Error(), c)
  276. return
  277. }
  278. // 操作日志
  279. //go func() {
  280. // opData := ""
  281. // opDataByte, e := json.Marshal(req)
  282. // if e != nil {
  283. // return
  284. // }
  285. // opData = string(opDataByte)
  286. //
  287. // logItem := new(fms.ContractRegisterLog)
  288. // logItem.ContractRegisterId = req.ContractRegisterId
  289. // logItem.AdminId = int(adminInfo.AdminId)
  290. // logItem.AdminName = adminInfo.RealName
  291. // logItem.OpData = opData
  292. // logItem.OpType = fms.ContractRegisterOpTypeDel
  293. // logItem.CreateTime = nowTime
  294. // if e = logItem.Create(); e != nil {
  295. // return
  296. // }
  297. //}()
  298. resp.Ok("操作成功", c)
  299. }