pre_register.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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/crm"
  10. "hongze/fms_api/models/fms"
  11. "hongze/fms_api/models/system"
  12. crmService "hongze/fms_api/services/crm"
  13. fmsService "hongze/fms_api/services/fms"
  14. "hongze/fms_api/utils"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. // // RegisterController 合同登记
  20. type PreRegisterController struct{}
  21. // InvoiceList
  22. // @Title 开票/到款列表
  23. // @Description 开票/到款列表
  24. // @Param Keyword query string false "关键词"
  25. // @Success 200 {object} fms.ContractInvoiceItem
  26. // @router /contract/pre_register/list [get]
  27. func (rg *PreRegisterController) List(c *gin.Context) {
  28. var req fms.PreRegisterListReq
  29. if e := c.BindQuery(&req); e != nil {
  30. err, ok := e.(validator.ValidationErrors)
  31. if !ok {
  32. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  33. return
  34. }
  35. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  36. return
  37. }
  38. pageSize := req.PageSize
  39. pageIndex := req.Current
  40. if pageSize <= 0 {
  41. pageSize = utils.PageSize20
  42. }
  43. if pageIndex <= 0 {
  44. pageIndex = 1
  45. }
  46. cond := ` invoice_type IN (3, 4) `
  47. pars := make([]interface{}, 0)
  48. if req.Keyword != "" {
  49. kw := "%" + req.Keyword + "%"
  50. cond += ` AND company_name LIKE ? `
  51. pars = append(pars, kw)
  52. }
  53. // 货币列表
  54. currencyOB := new(fms.CurrencyUnit)
  55. currencyCond := `enable = 1`
  56. currencyPars := make([]interface{}, 0)
  57. currencyList, e := currencyOB.List(currencyCond, currencyPars)
  58. if e != nil {
  59. resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
  60. return
  61. }
  62. unitMap := make(map[string]string)
  63. for i := range currencyList {
  64. unitMap[currencyList[i].Code] = currencyList[i].UnitName
  65. }
  66. page := new(base.Page)
  67. page.SetPageSize(req.PageSize)
  68. page.SetCurrent(req.Current)
  69. sortTypeMap := map[int]bool{0: false, 1: true, 2: false}
  70. page.AddOrderItem(base.OrderItem{Column: "create_time", Asc: sortTypeMap[0]})
  71. total, list, e := fms.GetContractPreRegisterItemPageList(page, cond, pars)
  72. if e != nil {
  73. resp.FailMsg("获取失败", "获取预登记列表失败, Err: "+e.Error(), c)
  74. return
  75. }
  76. serviceMap := make(map[int]string, 0)
  77. registerIds := make([]int, 0)
  78. for i := range list {
  79. list[i].UnitName = unitMap[list[i].CurrencyUnit]
  80. registerIds = append(registerIds, list[i].ContractRegisterId)
  81. }
  82. if len(registerIds) > 0 {
  83. serviceMap, _, e = fmsService.GetContractServiceNameFormat(registerIds)
  84. if e != nil {
  85. resp.FailMsg("获取失败", e.Error(), c)
  86. return
  87. }
  88. }
  89. respList := make([]*fms.ContractPreRegisterList, 0)
  90. for _, v := range list {
  91. item := new(fms.ContractPreRegisterList)
  92. item.ContractPreRegisterItem = v
  93. item.ServicesName = serviceMap[v.ContractRegisterId]
  94. respList = append(respList, item)
  95. }
  96. page.SetTotal(total)
  97. baseData := new(base.BaseData)
  98. baseData.SetPage(page)
  99. baseData.SetList(respList)
  100. resp.OkData("获取成功", baseData, c)
  101. }
  102. // Add
  103. // @Title 新增预登记
  104. // @Description 新增预登记
  105. // @Param request body fms.PrepayAddReq true "type json string"
  106. // @Success 200 string "操作成功"
  107. // @router /contract/pre_register/add [post]
  108. func (rg *PreRegisterController) Add(c *gin.Context) {
  109. req := new(fms.PreRegisterAddReq)
  110. err := c.ShouldBind(&req)
  111. if err != nil {
  112. errs, ok := err.(validator.ValidationErrors)
  113. if !ok {
  114. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  115. return
  116. }
  117. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  118. return
  119. }
  120. claims, _ := c.Get("adminInfo")
  121. adminInfo := claims.(*system.SysAdmin)
  122. // 日期校验
  123. var startDate, endDate time.Time
  124. if req.StartDate != "" && req.EndDate != "" {
  125. startDate, err = time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  126. if err != nil {
  127. resp.FailMsg("约定开始日期格式有误", "合同开始日期格式有误, Err: "+err.Error(), c)
  128. return
  129. }
  130. endDate, err = time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  131. if err != nil {
  132. resp.FailMsg("约定结束日期格式有误", "合同结束日期格式有误, Err: "+err.Error(), c)
  133. return
  134. }
  135. }
  136. var currencyUnit string
  137. if len(req.List) > 0 {
  138. currencyUnit = req.List[0].CurrencyUnit
  139. }
  140. // 获取销售分组信息
  141. sellerList, e := crmService.GetSellerDepartmentListWithGroupAndTeam()
  142. if e != nil {
  143. resp.FailData("获取销售失败", "Err:"+e.Error(), c)
  144. return
  145. }
  146. sellerMap := make(map[int]*crm.SellerAdminWithGroupTeam)
  147. for i := range sellerList {
  148. sellerMap[sellerList[i].SellerId] = sellerList[i]
  149. }
  150. admin := new(crm.Admin)
  151. sellerIds := strings.Split(req.SellerIds, ",")
  152. if len(sellerIds) > 2 {
  153. resp.Fail("最多只能选择两个销售", c)
  154. return
  155. }
  156. var sellers []*crm.Admin
  157. var pars []interface{}
  158. cond := ``
  159. if len(sellerIds) > 0 {
  160. cond = ` admin_id in (?) `
  161. pars = append(pars, sellerIds)
  162. sellers, e = admin.List(cond, pars)
  163. if e != nil {
  164. resp.FailMsg("获取销售信息失败", "获取销售信息失败, Err: "+e.Error(), c)
  165. return
  166. }
  167. }
  168. //新增合同
  169. ob := new(fms.ContractRegister)
  170. //ob.ContractCode = req.ContractCode
  171. ob.CompanyName = req.CompanyName
  172. ob.ProductIds = req.ProductIds
  173. ob.StartDate = startDate
  174. ob.EndDate = endDate
  175. ob.HasInvoice = 1
  176. ob.Set()
  177. ob.CurrencyUnit = currencyUnit
  178. if req.RegisterType == fms.ContractInvoiceTypePreMake {
  179. //sellerItem := sellerMap[r.SellerId]
  180. //if sellerItem == nil {
  181. // resp.Fail("销售信息异常", c)
  182. // return
  183. //}
  184. for _, v := range sellers {
  185. if v.DepartmentId == crm.SellerDepartmentId {
  186. ob.SellerId = v.AdminId
  187. ob.SellerName = v.AdminName
  188. } else if v.DepartmentId == crm.RaiSellerDepartmentId {
  189. ob.RaiSellerId = v.AdminId
  190. ob.RaiSellerName = v.AdminName
  191. }
  192. }
  193. }
  194. // 套餐信息
  195. serviceAmountMap := make(map[int]float64)
  196. serviceList, e := fmsService.HandleContractServiceAndDetail(req.Services, true, serviceAmountMap)
  197. if e != nil {
  198. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  199. return
  200. }
  201. if e = fms.CreateContractRegisterAndServicesAndPayMent(ob, serviceList); e != nil {
  202. resp.FailMsg("操作失败", "新增合同及套餐失败, Err: "+e.Error(), c)
  203. return
  204. }
  205. for _, r := range req.List {
  206. // 货币及汇率
  207. rateList, e := fmsService.GetTodayCurrencyRateList()
  208. if e != nil {
  209. resp.FailMsg("操作失败", "获取今日货币汇率失败, Err: "+e.Error(), c)
  210. return
  211. }
  212. var rate float64
  213. for i := range rateList {
  214. if r.CurrencyUnit == rateList[i].Code {
  215. rate = rateList[i].RMBRate
  216. break
  217. }
  218. }
  219. if rate <= 0 {
  220. resp.FailMsg("操作失败", "货币汇率信息有误", c)
  221. return
  222. }
  223. registerDate, e := time.Parse(utils.FormatDate, r.RegisterDate)
  224. if e != nil {
  225. resp.FailMsg("日期转换失败", "日期转换失败, Err: "+e.Error(), c)
  226. return
  227. }
  228. v := &fms.ContractInvoice{
  229. ContractRegisterId: ob.ContractRegisterId,
  230. OriginAmount: r.Amount,
  231. CurrencyUnit: r.CurrencyUnit,
  232. InvoiceType: req.RegisterType,
  233. InvoiceDate: registerDate,
  234. AdminId: int(adminInfo.AdminId),
  235. AdminName: adminInfo.AdminName,
  236. Remark: r.Remark,
  237. ServiceProductId: r.ServiceProductId,
  238. IsPrePay: 1,
  239. StartDate: startDate,
  240. EndDate: endDate,
  241. TimeBase: base.TimeBase{},
  242. }
  243. v.Set()
  244. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.OriginAmount/rate), 64)
  245. v.Amount = a
  246. if req.RegisterType == fms.ContractInvoiceTypePreMake {
  247. sellerItem := sellerMap[r.SellerId]
  248. if sellerItem == nil {
  249. resp.Fail("销售信息异常", c)
  250. return
  251. }
  252. v.SellerId = sellerItem.SellerId
  253. v.SellerName = sellerItem.SellerName
  254. v.SellerGroupId = sellerItem.GroupId
  255. v.SellerGroupName = sellerItem.GroupName
  256. v.SellerTeamId = sellerItem.TeamId
  257. v.SellerTeamName = sellerItem.TeamName
  258. }
  259. // 新增开票到款记录
  260. if e = v.Create(); e != nil {
  261. resp.FailMsg("操作失败", "新增预登记失败, Err: "+e.Error(), c)
  262. return
  263. }
  264. //预登记列表
  265. pp := &fms.ContractPreRegister{
  266. ContractRegisterId: ob.ContractRegisterId,
  267. CompanyName: req.CompanyName,
  268. InvoiceType: req.RegisterType,
  269. AdminId: int(adminInfo.AdminId),
  270. AdminName: adminInfo.AdminName,
  271. ServiceProductId: r.ServiceProductId,
  272. IsPrePay: 1,
  273. StartDate: startDate,
  274. EndDate: endDate,
  275. TimeBase: base.TimeBase{},
  276. }
  277. pp.Set()
  278. pp.CurrencyUnit = r.CurrencyUnit
  279. if pp.InvoiceType == fms.ContractInvoiceTypePreMake {
  280. //开票走开票字段
  281. pp.InvoiceId = v.ContractInvoiceId
  282. pp.InvoiceDate = registerDate
  283. pp.OriginAmount = r.Amount
  284. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", pp.OriginAmount/rate), 64)
  285. pp.Amount = a
  286. pp.Remark = r.Remark
  287. sellerItem := sellerMap[r.SellerId]
  288. if sellerItem == nil {
  289. resp.Fail("销售信息异常", c)
  290. return
  291. }
  292. pp.SellerId = sellerItem.SellerId
  293. pp.SellerName = sellerItem.SellerName
  294. pp.SellerGroupId = sellerItem.GroupId
  295. pp.SellerGroupName = sellerItem.GroupName
  296. pp.SellerTeamId = sellerItem.TeamId
  297. pp.SellerTeamName = sellerItem.TeamName
  298. } else {
  299. //到款
  300. pp.ArriveId = v.ContractInvoiceId
  301. pp.ArriveDate = registerDate
  302. pp.ArriveOriginAmount = r.Amount
  303. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", pp.ArriveOriginAmount/rate), 64)
  304. pp.ArriveAmount = a
  305. pp.ArriveRemark = r.Remark
  306. }
  307. // 新增开票到款记录
  308. if e = pp.Create(); e != nil {
  309. resp.FailMsg("操作失败", "新增预登记失败, Err: "+e.Error(), c)
  310. return
  311. }
  312. v.PreRegisterId = pp.PreRegisterId
  313. if e = v.Update([]string{"PreRegisterId"}); e != nil {
  314. fmt.Println("更新开票登记失败, Err: " + e.Error())
  315. return
  316. }
  317. }
  318. // 开票到款汇总
  319. go fmsService.SummaryInvoicePaymentByContractRegisterId(ob.ContractRegisterId)
  320. resp.Ok("操作成功", c)
  321. }
  322. // Edit
  323. // @Title 编辑预登记
  324. // @Description 编辑预登记
  325. // @Param request body fms.ContractRegisterEditReq true "type json string"
  326. // @Success 200 string "操作成功"
  327. // @router /contract/pre_register/edit [post]
  328. func (rg *PreRegisterController) Edit(c *gin.Context) {
  329. req := new(fms.PreRegisterEditReq)
  330. err := c.ShouldBind(&req)
  331. if err != nil {
  332. errs, ok := err.(validator.ValidationErrors)
  333. if !ok {
  334. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  335. return
  336. }
  337. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  338. return
  339. }
  340. claims, _ := c.Get("adminInfo")
  341. adminInfo := claims.(*system.SysAdmin)
  342. // 日期校验
  343. var startDate, endDate time.Time
  344. if req.StartDate != "" && req.EndDate != "" {
  345. startDate, err = time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  346. if err != nil {
  347. resp.FailMsg("约定开始日期格式有误", "合同开始日期格式有误, Err: "+err.Error(), c)
  348. return
  349. }
  350. endDate, err = time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  351. if err != nil {
  352. resp.FailMsg("约定结束日期格式有误", "合同结束日期格式有误, Err: "+err.Error(), c)
  353. return
  354. }
  355. }
  356. var currencyUnit string
  357. if len(req.List) > 0 {
  358. currencyUnit = req.List[0].CurrencyUnit
  359. }
  360. // 获取销售分组信息
  361. sellerList, e := crmService.GetSellerDepartmentListWithGroupAndTeam()
  362. if e != nil {
  363. resp.FailData("获取销售失败", "Err:"+e.Error(), c)
  364. return
  365. }
  366. sellerMap := make(map[int]*crm.SellerAdminWithGroupTeam)
  367. for i := range sellerList {
  368. sellerMap[sellerList[i].SellerId] = sellerList[i]
  369. }
  370. admin := new(crm.Admin)
  371. sellerIds := strings.Split(req.SellerIds, ",")
  372. if len(sellerIds) > 2 {
  373. resp.Fail("最多只能选择两个销售", c)
  374. return
  375. }
  376. var sellers []*crm.Admin
  377. var pars []interface{}
  378. cond := ``
  379. if len(sellerIds) > 0 {
  380. cond = ` admin_id in (?) `
  381. pars = append(pars, sellerIds)
  382. sellers, e = admin.List(cond, pars)
  383. if e != nil {
  384. resp.FailMsg("获取销售信息失败", "获取销售信息失败, Err: "+e.Error(), c)
  385. return
  386. }
  387. }
  388. //修改合同
  389. ob := new(fms.ContractRegister)
  390. //ob.ContractCode = req.ContractCode
  391. ob.ContractRegisterId = req.ContractRegisterId
  392. ob.CompanyName = req.CompanyName
  393. ob.ProductIds = req.ProductIds
  394. ob.StartDate = startDate
  395. ob.EndDate = endDate
  396. ob.ModifyTime = time.Now().Local()
  397. ob.CurrencyUnit = currencyUnit
  398. if req.RegisterType == fms.ContractInvoiceTypePreMake {
  399. //sellerItem := sellerMap[r.SellerId]
  400. //if sellerItem == nil {
  401. // resp.Fail("销售信息异常", c)
  402. // return
  403. //}
  404. for _, v := range sellers {
  405. if v.DepartmentId == crm.SellerDepartmentId {
  406. ob.SellerId = v.AdminId
  407. ob.SellerName = v.AdminName
  408. } else if v.DepartmentId == crm.RaiSellerDepartmentId {
  409. ob.RaiSellerId = v.AdminId
  410. ob.RaiSellerName = v.AdminName
  411. }
  412. }
  413. }
  414. // 套餐信息
  415. serviceAmountMap := make(map[int]float64)
  416. serviceList, e := fmsService.HandleContractServiceAndDetail(req.Services, true, serviceAmountMap)
  417. if e != nil {
  418. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  419. return
  420. }
  421. updateCols := []string{
  422. "ProductIds", "CompanyName", "SellerId", "SellerName", "StartDate", "EndDate",
  423. "RaiSellerId", "RaiSellerName", "ModifyTime", "CurrencyUnit",
  424. }
  425. invoiceList := make([]*fms.ContractInvoice, 0)
  426. for _, r := range req.List {
  427. // 货币及汇率
  428. rateList, e := fmsService.GetTodayCurrencyRateList()
  429. if e != nil {
  430. resp.FailMsg("操作失败", "获取今日货币汇率失败, Err: "+e.Error(), c)
  431. return
  432. }
  433. var rate float64
  434. for i := range rateList {
  435. if r.CurrencyUnit == rateList[i].Code {
  436. rate = rateList[i].RMBRate
  437. break
  438. }
  439. }
  440. if rate <= 0 {
  441. resp.FailMsg("操作失败", "货币汇率信息有误", c)
  442. return
  443. }
  444. registerDate, e := time.Parse(utils.FormatDate, r.RegisterDate)
  445. if e != nil {
  446. resp.FailMsg("日期转换失败", "日期转换失败, Err: "+e.Error(), c)
  447. return
  448. }
  449. v := &fms.ContractInvoice{
  450. ContractInvoiceId: r.InvoiceId,
  451. ContractRegisterId: ob.ContractRegisterId,
  452. OriginAmount: r.Amount,
  453. CurrencyUnit: r.CurrencyUnit,
  454. InvoiceType: req.RegisterType,
  455. InvoiceDate: registerDate,
  456. AdminId: int(adminInfo.AdminId),
  457. AdminName: adminInfo.AdminName,
  458. Remark: r.Remark,
  459. ServiceProductId: r.ServiceProductId,
  460. IsPrePay: 1,
  461. StartDate: startDate,
  462. EndDate: endDate,
  463. }
  464. v.TimeBase.ModifyTime = time.Now().Local()
  465. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.OriginAmount/rate), 64)
  466. v.Amount = a
  467. if req.RegisterType == fms.ContractInvoiceTypePreMake {
  468. sellerItem := sellerMap[r.SellerId]
  469. if sellerItem == nil {
  470. resp.Fail("销售信息异常", c)
  471. return
  472. }
  473. v.SellerId = sellerItem.SellerId
  474. v.SellerName = sellerItem.SellerName
  475. v.SellerGroupId = sellerItem.GroupId
  476. v.SellerGroupName = sellerItem.GroupName
  477. v.SellerTeamId = sellerItem.TeamId
  478. v.SellerTeamName = sellerItem.TeamName
  479. }
  480. //预登记列表
  481. pp := &fms.ContractPreRegister{
  482. PreRegisterId: r.PreRegisterId,
  483. ContractRegisterId: ob.ContractRegisterId,
  484. CompanyName: req.CompanyName,
  485. InvoiceType: req.RegisterType,
  486. AdminId: int(adminInfo.AdminId),
  487. AdminName: adminInfo.AdminName,
  488. ServiceProductId: r.ServiceProductId,
  489. IsPrePay: 1,
  490. StartDate: startDate,
  491. EndDate: endDate,
  492. TimeBase: base.TimeBase{},
  493. }
  494. pp.Set()
  495. pp.CurrencyUnit = r.CurrencyUnit
  496. ppUpdateCols := make([]string, 0)
  497. if pp.InvoiceType == fms.ContractInvoiceTypePreMake {
  498. //开票走开票字段
  499. pp.Remark = r.Remark
  500. pp.InvoiceDate = registerDate
  501. pp.OriginAmount = r.Amount
  502. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", pp.OriginAmount/rate), 64)
  503. pp.Amount = a
  504. sellerItem := sellerMap[r.SellerId]
  505. if sellerItem == nil {
  506. resp.Fail("销售信息异常", c)
  507. return
  508. }
  509. pp.InvoiceId = v.ContractInvoiceId
  510. pp.SellerId = sellerItem.SellerId
  511. pp.SellerName = sellerItem.SellerName
  512. pp.SellerGroupId = sellerItem.GroupId
  513. pp.SellerGroupName = sellerItem.GroupName
  514. pp.SellerTeamId = sellerItem.TeamId
  515. pp.SellerTeamName = sellerItem.TeamName
  516. ppUpdateCols = append(ppUpdateCols, "InvoiceDate", "Amount", "OriginAmount", "SellerId",
  517. "SellerName", "SellerGroupId", "SellerGroupName", "SellerTeamId", "SellerTeamName", "InvoiceId")
  518. } else {
  519. //到款
  520. pp.ArriveRemark = r.Remark
  521. pp.ArriveDate = registerDate
  522. pp.ArriveId = v.ContractInvoiceId
  523. pp.ArriveOriginAmount = r.Amount
  524. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", pp.ArriveOriginAmount/rate), 64)
  525. pp.ArriveAmount = a
  526. ppUpdateCols = append(ppUpdateCols, "CurrencyUnit", "ArriveDate", "ArriveAmount", "ArriveOriginAmount", "ArriveId")
  527. }
  528. if pp.PreRegisterId == 0 {
  529. if e = pp.Create(); e != nil {
  530. resp.FailMsg("操作失败", "新增预登记失败, Err: "+e.Error(), c)
  531. return
  532. }
  533. v.PreRegisterId = pp.PreRegisterId
  534. } else {
  535. ppUpdateCols = append(ppUpdateCols,
  536. "CurrencyUnit", "CompanyName", "Remark", "ArriveRemark", "InvoiceTime", "ArriveTime",
  537. "ServiceProductId", "CurrencyUnit", "StartDate", "EndDate", "ModifyTime")
  538. if e = pp.Update(ppUpdateCols); e != nil {
  539. resp.FailMsg("操作失败", "更新预登记记录失败, Err: "+e.Error(), c)
  540. return
  541. }
  542. }
  543. invoiceList = append(invoiceList, v)
  544. }
  545. invoiceUpdateCols := []string{
  546. "CurrencyUnit", "OriginAmount", "Amount", "InvoiceDate", "AdminId", "AdminName", "Remark",
  547. "ServiceProductId", "StartDate", "EndDate", "ModifyTime",
  548. }
  549. // 更新合同登记、套餐、开票到款
  550. if e = fms.UpdateContractPreRegister(ob, updateCols, serviceList, invoiceList, invoiceUpdateCols, req.DelInvoiceIds, req.DelPreRegisterIds); e != nil {
  551. resp.FailMsg("操作失败", "更新合同及套餐失败, Err: "+e.Error(), c)
  552. return
  553. }
  554. // 开票到款汇总
  555. go fmsService.SummaryInvoicePaymentByContractRegisterId(ob.ContractRegisterId)
  556. resp.Ok("操作成功", c)
  557. }
  558. // Del
  559. // @Title 删除到款预登记
  560. // @Description 删除到款预登记
  561. // @Param request body fms.ContractRegisterDelReq true "type json string"
  562. // @Success 200 string "操作成功"
  563. // @router /contract/pre_register/del [post]
  564. func (rg *PreRegisterController) Del(c *gin.Context) {
  565. req := new(fms.PreRegisterDelReq)
  566. err := c.ShouldBind(&req)
  567. if err != nil {
  568. errs, ok := err.(validator.ValidationErrors)
  569. if !ok {
  570. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  571. return
  572. }
  573. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  574. return
  575. }
  576. if req.InvoiceId != 0 {
  577. ob := new(fms.ContractInvoice)
  578. item, e := ob.Fetch(req.InvoiceId)
  579. if e != nil {
  580. if e == utils.ErrNoRow {
  581. resp.Fail("合同登记不存在或已被删除", c)
  582. return
  583. }
  584. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  585. return
  586. }
  587. e = item.Delete()
  588. if e != nil {
  589. resp.FailMsg("删除记录失败", "Err:"+e.Error(), c)
  590. return
  591. }
  592. }
  593. if req.ArriveId != 0 {
  594. ob := new(fms.ContractInvoice)
  595. item, e := ob.Fetch(req.ArriveId)
  596. if e != nil {
  597. if e == utils.ErrNoRow {
  598. resp.Fail("合同登记不存在或已被删除", c)
  599. return
  600. }
  601. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  602. return
  603. }
  604. e = item.Delete()
  605. if e != nil {
  606. resp.FailMsg("删除记录失败", "Err:"+e.Error(), c)
  607. return
  608. }
  609. }
  610. ob := new(fms.ContractPreRegister)
  611. item, e := ob.Fetch(req.PreRegisterId)
  612. if e != nil {
  613. if e == utils.ErrNoRow {
  614. resp.Fail("预登记记录不存在或已被删除", c)
  615. return
  616. }
  617. resp.FailMsg("获取预登记失败", "Err:"+e.Error(), c)
  618. return
  619. }
  620. e = item.Delete()
  621. if e != nil {
  622. resp.FailMsg("删除预登记记录失败", "Err:"+e.Error(), c)
  623. return
  624. }
  625. e = fms.DeleteInvoicePaymentSummaryByInvoiceIdAndPaymentId(req.InvoiceId, req.ArriveId, req.ContractRegisterId)
  626. if e != nil {
  627. resp.FailMsg("删除统计记录失败", "Err:"+e.Error(), c)
  628. return
  629. }
  630. //todo 合同看情况要不要删
  631. resp.Ok("操作成功", c)
  632. }
  633. // Detail
  634. // @Title 预登记记录详情
  635. // @Description 预登记记录详情
  636. // @Param request body fms.PreRegisterDetailReq true "type json string"
  637. // @Success 200 string "操作成功"
  638. // @router /contract/pre_register/detail [post]
  639. func (rg *PreRegisterController) Detail(c *gin.Context) {
  640. req := new(fms.PreRegisterDetailReq)
  641. err := c.ShouldBind(&req)
  642. if err != nil {
  643. errs, ok := err.(validator.ValidationErrors)
  644. if !ok {
  645. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  646. return
  647. }
  648. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  649. return
  650. }
  651. ob := new(fms.ContractRegister)
  652. item, e := ob.Fetch(req.ContractRegisterId)
  653. if e != nil {
  654. if e == utils.ErrNoRow {
  655. resp.Fail("合同登记不存在或已被删除", c)
  656. return
  657. }
  658. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  659. return
  660. }
  661. nItme := fms.ContractRegisterItem{
  662. ContractRegisterId: item.ContractRegisterId,
  663. ContractCode: item.ContractCode,
  664. RelateContractCode: item.RelateContractCode,
  665. CrmContractId: item.CrmContractId,
  666. ContractSource: item.ContractSource,
  667. CompanyName: item.CompanyName,
  668. ActualCompanyName: item.ActualCompanyName,
  669. ProductIds: item.ProductIds,
  670. SellerId: item.SellerId,
  671. SellerName: item.SellerName,
  672. RaiSellerId: item.RaiSellerId,
  673. RaiSellerName: item.RaiSellerName,
  674. ContractType: item.ContractType,
  675. ContractAmount: item.ContractAmount,
  676. InvoicedAmount: item.InvoicedAmount,
  677. PaymentAmount: item.PaymentAmount,
  678. CurrencyUnit: item.CurrencyUnit,
  679. RMBRate: item.RMBRate,
  680. StartDate: item.StartDate.Format(utils.FormatDate),
  681. EndDate: item.EndDate.Format(utils.FormatDate),
  682. SignDate: item.SignDate.Format(utils.FormatDate),
  683. AgreedPayTime: item.AgreedPayTime,
  684. ContractStatus: item.ContractStatus,
  685. RegisterStatus: item.RegisterStatus,
  686. Remark: item.Remark,
  687. ServiceRemark: item.ServiceRemark,
  688. HasPayment: item.HasPayment,
  689. NewCompany: item.NewCompany,
  690. CreateTime: item.CreateTime.Format(utils.FormatDateTime),
  691. }
  692. if nItme.StartDate == "0001-01-01" {
  693. nItme.StartDate = ""
  694. }
  695. if nItme.EndDate == "0001-01-01" {
  696. nItme.EndDate = ""
  697. }
  698. cond := ``
  699. pars := make([]interface{}, 0)
  700. if req.InvoiceId == 0 && req.ArriveId == 0 {
  701. cond = `contract_register_id = ? AND invoice_type = ? `
  702. pars = append(pars, req.ContractRegisterId, req.InvoiceType)
  703. } else {
  704. cond = `contract_register_id = ? AND invoice_type = ? AND contract_invoice_id =? `
  705. pars = append(pars, req.ContractRegisterId, req.InvoiceType)
  706. }
  707. if req.InvoiceType == 4 {
  708. pars = append(pars, req.ArriveId)
  709. } else {
  710. pars = append(pars, req.InvoiceId)
  711. }
  712. list, e := fms.GetContractInvoiceDetailItemList(cond, pars)
  713. if e != nil {
  714. err = fmt.Errorf("获取开票到款列表失败, Err: %s", e.Error())
  715. return
  716. }
  717. //获取ficc小套餐品种
  718. sysConf := new(system.SysConfig)
  719. confCond := `config_code = ?`
  720. confPars := make([]interface{}, 0)
  721. confPars = append(confPars, system.ConfigKeyCrmPermissionFiccClassify)
  722. confItem, e := sysConf.FetchByCondition(confCond, confPars)
  723. if e != nil {
  724. resp.FailData("获取失败", "Err:"+e.Error(), c)
  725. return
  726. }
  727. if confItem.ConfigValue == "" {
  728. resp.FailData("获取失败", "FICC品种分类配置为空", c)
  729. return
  730. }
  731. classifyArr := strings.Split(confItem.ConfigValue, ",")
  732. if len(classifyArr) == 0 {
  733. resp.FailData("获取失败", "FICC品种分类配置为空", c)
  734. return
  735. }
  736. // 获取FICC权限
  737. ficcCond := `enabled = 1 AND permission_type = 0 AND product_id = ? AND classify_name IN ?`
  738. ficcPars := make([]interface{}, 0)
  739. ficcPars = append(ficcPars, 1, classifyArr)
  740. items, e := crm.GetPermissionSetItemsByCondition(ficcCond, ficcPars)
  741. if e != nil {
  742. resp.FailData("获取失败", "获取FICC权限信息失败, Err: "+e.Error(), c)
  743. return
  744. }
  745. ficcItemMap := make(map[string][]*crm.PermissionSetItem, 0)
  746. for i := range items {
  747. if ficcItemMap[items[i].ClassifyName] == nil {
  748. ficcItemMap[items[i].ClassifyName] = make([]*crm.PermissionSetItem, 0)
  749. }
  750. ficcItemMap[items[i].ClassifyName] = append(ficcItemMap[items[i].ClassifyName], items[i])
  751. }
  752. sTempIdMap := make(map[string]int, 0)
  753. for i := range classifyArr {
  754. if classifyArr[i] == "市场策略" {
  755. continue
  756. }
  757. checkList := make([]int, 0)
  758. if classifyArr[i] == "宏观经济" {
  759. checkList = append(checkList, 1)
  760. }
  761. pItem := fms.ContractServiceTemplateItem{
  762. ServiceTemplateId: 90000 + i*100,
  763. }
  764. ficcItems := ficcItemMap[classifyArr[i]]
  765. for _, ficcItem := range ficcItems {
  766. sTempIdMap[strconv.Itoa(ficcItem.ChartPermissionId)] = pItem.ServiceTemplateId + ficcItem.ChartPermissionId
  767. }
  768. }
  769. services, e := fms.GetContractServiceAndDetailList(req.ContractRegisterId)
  770. if e != nil {
  771. err = fmt.Errorf(fmt.Sprint("查找合同服务异常", e))
  772. return
  773. }
  774. serviceTemplateIdSlice := make([]string, 0)
  775. for _, service := range services {
  776. if service.Title == "FICC小套餐" {
  777. serviceTemplateIdSlice = strings.Split(service.ChartPermissionIds, ",")
  778. }
  779. }
  780. serviceTemplateIds := make([]int, 0)
  781. for _, v := range serviceTemplateIdSlice {
  782. serviceTemplateIds = append(serviceTemplateIds, sTempIdMap[v])
  783. }
  784. respItem := fms.ContractPreRegisterDetail{
  785. ContractRegisterItem: &nItme,
  786. List: list,
  787. Services: services,
  788. ServiceTemplateIds: serviceTemplateIds,
  789. }
  790. resp.OkData("获取成功", respItem, c)
  791. }
  792. // Save
  793. // @Title 预登记保存
  794. // @Description 预登记保存
  795. // @Param request body fms.PrepayAddReq true "type json string"
  796. // @Success 200 string "操作成功"
  797. // @router /contract/pre_register/save [post]
  798. func (rg *PreRegisterController) Save(c *gin.Context) {
  799. req := new(fms.PreRegisterSaveReq)
  800. err := c.ShouldBind(&req)
  801. if err != nil {
  802. errs, ok := err.(validator.ValidationErrors)
  803. if !ok {
  804. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  805. return
  806. }
  807. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  808. return
  809. }
  810. claims, _ := c.Get("adminInfo")
  811. adminInfo := claims.(*system.SysAdmin)
  812. // 日期校验
  813. var startDate, endDate time.Time
  814. if req.StartDate != "" && req.EndDate != "" {
  815. startDate, err = time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  816. if err != nil {
  817. resp.FailMsg("约定开始日期格式有误", "合同开始日期格式有误, Err: "+err.Error(), c)
  818. return
  819. }
  820. endDate, err = time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  821. if err != nil {
  822. resp.FailMsg("约定结束日期格式有误", "合同结束日期格式有误, Err: "+err.Error(), c)
  823. return
  824. }
  825. }
  826. // 获取销售分组信息
  827. sellerList, e := crmService.GetSellerDepartmentListWithGroupAndTeam()
  828. if e != nil {
  829. resp.FailData("获取销售失败", "Err:"+e.Error(), c)
  830. return
  831. }
  832. sellerMap := make(map[int]*crm.SellerAdminWithGroupTeam)
  833. for i := range sellerList {
  834. sellerMap[sellerList[i].SellerId] = sellerList[i]
  835. }
  836. admin := new(crm.Admin)
  837. sellerIds := strings.Split(req.SellerIds, ",")
  838. if len(sellerIds) > 2 {
  839. resp.Fail("最多只能选择两个销售", c)
  840. return
  841. }
  842. var sellers []*crm.Admin
  843. var pars []interface{}
  844. cond := ``
  845. if len(sellerIds) > 0 {
  846. cond = ` admin_id in (?) `
  847. pars = append(pars, sellerIds)
  848. sellers, e = admin.List(cond, pars)
  849. if e != nil {
  850. resp.FailMsg("获取销售信息失败", "获取销售信息失败, Err: "+e.Error(), c)
  851. return
  852. }
  853. }
  854. updateCols := []string{"CompanyName", "ProductIds", "StartDate", "EndDate", "ModifyTime"}
  855. //更新合同
  856. ob := new(fms.ContractRegister)
  857. //ob.ContractCode = req.ContractCode
  858. ob.ContractRegisterId = req.ContractRegisterId
  859. ob.CompanyName = req.CompanyName
  860. ob.ProductIds = req.ProductIds
  861. ob.StartDate = startDate
  862. ob.EndDate = endDate
  863. ob.ModifyTime = time.Now()
  864. ob.CurrencyUnit = req.List[0].CurrencyUnit
  865. if req.RegisterType == fms.ContractInvoiceTypePreMake {
  866. //sellerItem := sellerMap[r.SellerId]
  867. //if sellerItem == nil {
  868. // resp.Fail("销售信息异常", c)
  869. // return
  870. //}
  871. for _, v := range sellers {
  872. if v.DepartmentId == crm.SellerDepartmentId {
  873. ob.SellerId = v.AdminId
  874. ob.SellerName = v.AdminName
  875. updateCols = append(updateCols, "SellerId", "SellerName")
  876. } else if v.DepartmentId == crm.RaiSellerDepartmentId {
  877. ob.RaiSellerId = v.AdminId
  878. ob.RaiSellerName = v.AdminName
  879. updateCols = append(updateCols, "RaiSellerId", "RaiSellerName")
  880. }
  881. }
  882. }
  883. // 套餐信息
  884. serviceAmountMap := make(map[int]float64)
  885. serviceList, e := fmsService.HandleContractServiceAndDetail(req.Services, true, serviceAmountMap)
  886. if e != nil {
  887. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  888. return
  889. }
  890. invoiceUpdateCols := make([]string, 0)
  891. invoiceHandleType := 0
  892. invoiceList := make([]*fms.ContractInvoice, 0)
  893. ppList := make([]*fms.ContractPreRegister, 0)
  894. for _, r := range req.List {
  895. // 货币及汇率
  896. rateList, e := fmsService.GetTodayCurrencyRateList()
  897. if e != nil {
  898. resp.FailMsg("操作失败", "获取今日货币汇率失败, Err: "+e.Error(), c)
  899. return
  900. }
  901. var rate float64
  902. for i := range rateList {
  903. if r.CurrencyUnit == rateList[i].Code {
  904. rate = rateList[i].RMBRate
  905. break
  906. }
  907. }
  908. if rate <= 0 {
  909. resp.FailMsg("操作失败", "货币汇率信息有误", c)
  910. return
  911. }
  912. registerDate, e := time.Parse(utils.FormatDate, r.RegisterDate)
  913. if e != nil {
  914. resp.FailMsg("日期转换失败", "日期转换失败, Err: "+e.Error(), c)
  915. return
  916. }
  917. v := &fms.ContractInvoice{
  918. ContractInvoiceId: r.InvoiceId,
  919. ContractRegisterId: ob.ContractRegisterId,
  920. OriginAmount: r.Amount,
  921. CurrencyUnit: r.CurrencyUnit,
  922. InvoiceType: req.RegisterType,
  923. InvoiceDate: registerDate,
  924. AdminId: int(adminInfo.AdminId),
  925. AdminName: adminInfo.AdminName,
  926. Remark: r.Remark,
  927. ServiceProductId: r.ServiceProductId,
  928. IsPrePay: 1,
  929. StartDate: startDate,
  930. EndDate: endDate,
  931. TimeBase: base.TimeBase{},
  932. }
  933. v.TimeBase.ModifyTime = time.Now()
  934. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.OriginAmount/rate), 64)
  935. v.Amount = a
  936. if req.RegisterType == fms.ContractInvoiceTypePreMake {
  937. sellerItem := sellerMap[r.SellerId]
  938. if sellerItem == nil {
  939. resp.Fail("销售信息异常", c)
  940. return
  941. }
  942. v.SellerId = sellerItem.SellerId
  943. v.SellerName = sellerItem.SellerName
  944. v.SellerGroupId = sellerItem.GroupId
  945. v.SellerGroupName = sellerItem.GroupName
  946. v.SellerTeamId = sellerItem.TeamId
  947. v.SellerTeamName = sellerItem.TeamName
  948. invoiceUpdateCols = append(invoiceUpdateCols, "SellerId", "SellerName", "SellerGroupId",
  949. "SellerGroupName", "SellerTeamId", "SellerTeamName")
  950. }
  951. // 新增开票到款记录
  952. if v.ContractInvoiceId != 0 {
  953. //更新
  954. invoiceHandleType = 1
  955. invoiceUpdateCols = append(invoiceUpdateCols, "OriginAmount", "CurrencyUnit",
  956. "InvoiceDate", "Remark", "ServiceProductId", "StartDate", "EndDate", "ModifyTime")
  957. } else {
  958. //新增
  959. invoiceHandleType = 2
  960. }
  961. invoiceList = append(invoiceList, v)
  962. //预登记列表
  963. ppUpdateCols := make([]string, 0)
  964. pp := &fms.ContractPreRegister{
  965. PreRegisterId: r.PreRegisterId,
  966. ContractRegisterId: ob.ContractRegisterId,
  967. CompanyName: req.CompanyName,
  968. InvoiceType: req.RegisterType,
  969. InvoiceDate: registerDate,
  970. AdminId: int(adminInfo.AdminId),
  971. AdminName: adminInfo.AdminName,
  972. ServiceProductId: r.ServiceProductId,
  973. IsPrePay: 1,
  974. StartDate: startDate,
  975. EndDate: endDate,
  976. TimeBase: base.TimeBase{},
  977. }
  978. pp.Set()
  979. pp.CurrencyUnit = r.CurrencyUnit
  980. if pp.InvoiceType == fms.ContractInvoiceTypePreMake {
  981. //开票走开票字段
  982. pp.Remark = r.Remark
  983. pp.InvoiceDate = registerDate
  984. pp.OriginAmount = r.Amount
  985. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", pp.OriginAmount/rate), 64)
  986. pp.Amount = a
  987. sellerItem := sellerMap[r.SellerId]
  988. if sellerItem == nil {
  989. resp.Fail("销售信息异常", c)
  990. return
  991. }
  992. pp.SellerId = sellerItem.SellerId
  993. pp.SellerName = sellerItem.SellerName
  994. pp.SellerGroupId = sellerItem.GroupId
  995. pp.SellerGroupName = sellerItem.GroupName
  996. pp.SellerTeamId = sellerItem.TeamId
  997. pp.SellerTeamName = sellerItem.TeamName
  998. ppUpdateCols = append(ppUpdateCols, "InvoiceDate", "Amount", "OriginAmount", "SellerId",
  999. "SellerName", "SellerGroupId", "SellerGroupName", "SellerTeamId", "SellerTeamName", "Remark")
  1000. } else {
  1001. //到款
  1002. pp.ArriveRemark = r.Remark
  1003. pp.ArriveDate = registerDate
  1004. pp.ArriveOriginAmount = r.Amount
  1005. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", pp.ArriveOriginAmount/rate), 64)
  1006. pp.ArriveAmount = a
  1007. ppUpdateCols = append(ppUpdateCols, "ArriveRemark", "CurrencyUnit", "ArriveDate", "ArriveAmount", "ArriveOriginAmount")
  1008. }
  1009. ppUpdateCols = append(ppUpdateCols,
  1010. "CompanyName", "AdminId", "AdminName", "ServiceProductId",
  1011. "CurrencyUnit", "StartDate", "EndDate", "ModifyTime")
  1012. if e = pp.Update(ppUpdateCols); e != nil {
  1013. resp.FailMsg("操作失败", "更新预登记记录失败, Err: "+e.Error(), c)
  1014. return
  1015. }
  1016. ppList = append(ppList, pp)
  1017. }
  1018. if e = fms.UpdateContractRegisterPre(ob, updateCols, serviceList, invoiceList, invoiceUpdateCols, invoiceHandleType, ppList); e != nil {
  1019. resp.FailMsg("操作失败", "新增合同及套餐失败, Err: "+e.Error(), c)
  1020. return
  1021. }
  1022. // 开票到款汇总
  1023. go fmsService.SummaryInvoicePaymentByContractRegisterId(ob.ContractRegisterId)
  1024. resp.Ok("操作成功", c)
  1025. }