pre_register.go 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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 = ob.Create(); e != nil {
  202. resp.FailMsg("操作失败", "新增合同失败, Err: "+e.Error(), c)
  203. return
  204. }
  205. if ob.ContractRegisterId == 0 {
  206. resp.FailMsg("操作失败", "新增合同失败,合同ID为0", c)
  207. return
  208. }
  209. if e = fms.CreateContractRegisterAndServicesAndPayMent(ob, serviceList); e != nil {
  210. resp.FailMsg("操作失败", "新增合同及套餐失败, Err: "+e.Error(), c)
  211. return
  212. }
  213. for _, r := range req.List {
  214. // 货币及汇率
  215. rateList, e := fmsService.GetTodayCurrencyRateList()
  216. if e != nil {
  217. resp.FailMsg("操作失败", "获取今日货币汇率失败, Err: "+e.Error(), c)
  218. return
  219. }
  220. var rate float64
  221. for i := range rateList {
  222. if r.CurrencyUnit == rateList[i].Code {
  223. rate = rateList[i].RMBRate
  224. break
  225. }
  226. }
  227. if rate <= 0 {
  228. resp.FailMsg("操作失败", "货币汇率信息有误", c)
  229. return
  230. }
  231. registerDate, e := time.Parse(utils.FormatDate, r.RegisterDate)
  232. if e != nil {
  233. resp.FailMsg("日期转换失败", "日期转换失败, Err: "+e.Error(), c)
  234. return
  235. }
  236. v := &fms.ContractInvoice{
  237. ContractRegisterId: ob.ContractRegisterId,
  238. OriginAmount: r.Amount,
  239. CurrencyUnit: r.CurrencyUnit,
  240. InvoiceType: req.RegisterType,
  241. InvoiceDate: registerDate,
  242. AdminId: int(adminInfo.AdminId),
  243. AdminName: adminInfo.AdminName,
  244. Remark: r.Remark,
  245. ServiceProductId: r.ServiceProductId,
  246. IsPrePay: 1,
  247. StartDate: startDate,
  248. EndDate: endDate,
  249. TimeBase: base.TimeBase{},
  250. }
  251. v.Set()
  252. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.OriginAmount/rate), 64)
  253. v.Amount = a
  254. sellerItem := sellerMap[r.SellerId]
  255. if sellerItem == nil {
  256. resp.Fail("销售信息异常", c)
  257. return
  258. }
  259. v.SellerId = sellerItem.SellerId
  260. v.SellerName = sellerItem.SellerName
  261. v.SellerGroupId = sellerItem.GroupId
  262. v.SellerGroupName = sellerItem.GroupName
  263. v.SellerTeamId = sellerItem.TeamId
  264. v.SellerTeamName = sellerItem.TeamName
  265. // 新增开票到款记录
  266. if e = v.Create(); e != nil {
  267. resp.FailMsg("操作失败", "新增预登记失败, Err: "+e.Error(), c)
  268. return
  269. }
  270. //预登记列表
  271. pp := &fms.ContractPreRegister{
  272. ContractRegisterId: ob.ContractRegisterId,
  273. CompanyName: req.CompanyName,
  274. InvoiceType: req.RegisterType,
  275. AdminId: int(adminInfo.AdminId),
  276. AdminName: adminInfo.AdminName,
  277. ServiceProductId: r.ServiceProductId,
  278. IsPrePay: 1,
  279. StartDate: startDate,
  280. EndDate: endDate,
  281. TimeBase: base.TimeBase{},
  282. }
  283. pp.Set()
  284. pp.CurrencyUnit = r.CurrencyUnit
  285. if pp.InvoiceType == fms.ContractInvoiceTypePreMake {
  286. //开票走开票字段
  287. pp.InvoiceId = v.ContractInvoiceId
  288. pp.InvoiceDate = registerDate
  289. pp.OriginAmount = r.Amount
  290. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", pp.OriginAmount/rate), 64)
  291. pp.Amount = a
  292. pp.Remark = r.Remark
  293. sellerItem := sellerMap[r.SellerId]
  294. if sellerItem == nil {
  295. resp.Fail("销售信息异常", c)
  296. return
  297. }
  298. pp.SellerId = sellerItem.SellerId
  299. pp.SellerName = sellerItem.SellerName
  300. pp.SellerGroupId = sellerItem.GroupId
  301. pp.SellerGroupName = sellerItem.GroupName
  302. pp.SellerTeamId = sellerItem.TeamId
  303. pp.SellerTeamName = sellerItem.TeamName
  304. pp.PaymentSellerId = sellerItem.SellerId
  305. pp.PaymentSellerName = sellerItem.SellerName
  306. pp.PaymentSellerGroupId = sellerItem.GroupId
  307. pp.PaymentSellerGroupName = sellerItem.GroupName
  308. pp.PaymentSellerTeamId = sellerItem.TeamId
  309. pp.PaymentSellerTeamName = sellerItem.TeamName
  310. } else {
  311. //到款
  312. pp.ArriveId = v.ContractInvoiceId
  313. pp.ArriveDate = registerDate
  314. pp.ArriveOriginAmount = r.Amount
  315. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", pp.ArriveOriginAmount/rate), 64)
  316. pp.ArriveAmount = a
  317. pp.ArriveRemark = r.Remark
  318. sellerItem := sellerMap[r.SellerId]
  319. if sellerItem == nil {
  320. resp.Fail("销售信息异常", c)
  321. return
  322. }
  323. pp.PaymentSellerId = sellerItem.SellerId
  324. pp.PaymentSellerName = sellerItem.SellerName
  325. pp.PaymentSellerGroupId = sellerItem.GroupId
  326. pp.PaymentSellerGroupName = sellerItem.GroupName
  327. pp.PaymentSellerTeamId = sellerItem.TeamId
  328. pp.PaymentSellerTeamName = sellerItem.TeamName
  329. }
  330. // 新增开票到款记录
  331. if pp.ContractRegisterId == 0 {
  332. resp.FailMsg("操作失败", "新增预登记失败,合同ID为0", c)
  333. return
  334. }
  335. if e = pp.Create(); e != nil {
  336. resp.FailMsg("操作失败", "新增预登记失败, Err: "+e.Error(), c)
  337. return
  338. }
  339. v.PreRegisterId = pp.PreRegisterId
  340. if e = v.Update([]string{"PreRegisterId"}); e != nil {
  341. fmt.Println("更新开票登记失败, Err: " + e.Error())
  342. return
  343. }
  344. }
  345. // 开票到款汇总
  346. go fmsService.SummaryInvoicePaymentByContractRegisterId(ob.ContractRegisterId)
  347. resp.Ok("操作成功", c)
  348. }
  349. // Edit
  350. // @Title 编辑预登记
  351. // @Description 编辑预登记
  352. // @Param request body fms.ContractRegisterEditReq true "type json string"
  353. // @Success 200 string "操作成功"
  354. // @router /contract/pre_register/edit [post]
  355. func (rg *PreRegisterController) Edit(c *gin.Context) {
  356. req := new(fms.PreRegisterEditReq)
  357. err := c.ShouldBind(&req)
  358. if err != nil {
  359. errs, ok := err.(validator.ValidationErrors)
  360. if !ok {
  361. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  362. return
  363. }
  364. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  365. return
  366. }
  367. claims, _ := c.Get("adminInfo")
  368. adminInfo := claims.(*system.SysAdmin)
  369. // 日期校验
  370. var startDate, endDate time.Time
  371. if req.StartDate != "" && req.EndDate != "" {
  372. startDate, err = time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  373. if err != nil {
  374. resp.FailMsg("约定开始日期格式有误", "合同开始日期格式有误, Err: "+err.Error(), c)
  375. return
  376. }
  377. endDate, err = time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  378. if err != nil {
  379. resp.FailMsg("约定结束日期格式有误", "合同结束日期格式有误, Err: "+err.Error(), c)
  380. return
  381. }
  382. }
  383. var currencyUnit string
  384. if len(req.List) > 0 {
  385. currencyUnit = req.List[0].CurrencyUnit
  386. }
  387. // 获取销售分组信息
  388. sellerList, e := crmService.GetSellerDepartmentListWithGroupAndTeam()
  389. if e != nil {
  390. resp.FailData("获取销售失败", "Err:"+e.Error(), c)
  391. return
  392. }
  393. sellerMap := make(map[int]*crm.SellerAdminWithGroupTeam)
  394. for i := range sellerList {
  395. sellerMap[sellerList[i].SellerId] = sellerList[i]
  396. }
  397. admin := new(crm.Admin)
  398. sellerIds := strings.Split(req.SellerIds, ",")
  399. if len(sellerIds) > 2 {
  400. resp.Fail("最多只能选择两个销售", c)
  401. return
  402. }
  403. var sellers []*crm.Admin
  404. var pars []interface{}
  405. cond := ``
  406. if len(sellerIds) > 0 {
  407. cond = ` admin_id in (?) `
  408. pars = append(pars, sellerIds)
  409. sellers, e = admin.List(cond, pars)
  410. if e != nil {
  411. resp.FailMsg("获取销售信息失败", "获取销售信息失败, Err: "+e.Error(), c)
  412. return
  413. }
  414. }
  415. //修改合同
  416. ob := new(fms.ContractRegister)
  417. //ob.ContractCode = req.ContractCode
  418. ob.ContractRegisterId = req.ContractRegisterId
  419. ob.CompanyName = req.CompanyName
  420. ob.ProductIds = req.ProductIds
  421. ob.StartDate = startDate
  422. ob.EndDate = endDate
  423. ob.ModifyTime = time.Now().Local()
  424. ob.CurrencyUnit = currencyUnit
  425. if req.RegisterType == fms.ContractInvoiceTypePreMake {
  426. //sellerItem := sellerMap[r.SellerId]
  427. //if sellerItem == nil {
  428. // resp.Fail("销售信息异常", c)
  429. // return
  430. //}
  431. for _, v := range sellers {
  432. if v.DepartmentId == crm.SellerDepartmentId {
  433. ob.SellerId = v.AdminId
  434. ob.SellerName = v.AdminName
  435. } else if v.DepartmentId == crm.RaiSellerDepartmentId {
  436. ob.RaiSellerId = v.AdminId
  437. ob.RaiSellerName = v.AdminName
  438. }
  439. }
  440. }
  441. // 套餐信息
  442. serviceAmountMap := make(map[int]float64)
  443. serviceList, e := fmsService.HandleContractServiceAndDetail(req.Services, true, serviceAmountMap)
  444. if e != nil {
  445. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  446. return
  447. }
  448. updateCols := []string{
  449. "ProductIds", "CompanyName", "SellerId", "SellerName", "StartDate", "EndDate",
  450. "RaiSellerId", "RaiSellerName", "ModifyTime", "CurrencyUnit",
  451. }
  452. invoiceList := make([]*fms.ContractInvoice, 0)
  453. ppList := make([]*fms.ContractPreRegister, 0)
  454. for _, r := range req.List {
  455. // 货币及汇率
  456. rateList, e := fmsService.GetTodayCurrencyRateList()
  457. if e != nil {
  458. resp.FailMsg("操作失败", "获取今日货币汇率失败, Err: "+e.Error(), c)
  459. return
  460. }
  461. var rate float64
  462. for i := range rateList {
  463. if r.CurrencyUnit == rateList[i].Code {
  464. rate = rateList[i].RMBRate
  465. break
  466. }
  467. }
  468. if rate <= 0 {
  469. resp.FailMsg("操作失败", "货币汇率信息有误", c)
  470. return
  471. }
  472. registerDate, e := time.Parse(utils.FormatDate, r.RegisterDate)
  473. if e != nil {
  474. resp.FailMsg("日期转换失败", "日期转换失败, Err: "+e.Error(), c)
  475. return
  476. }
  477. v := &fms.ContractInvoice{
  478. ContractInvoiceId: r.InvoiceId,
  479. ContractRegisterId: ob.ContractRegisterId,
  480. OriginAmount: r.Amount,
  481. CurrencyUnit: r.CurrencyUnit,
  482. InvoiceType: req.RegisterType,
  483. InvoiceDate: registerDate,
  484. AdminId: int(adminInfo.AdminId),
  485. AdminName: adminInfo.AdminName,
  486. Remark: r.Remark,
  487. ServiceProductId: r.ServiceProductId,
  488. IsPrePay: 1,
  489. StartDate: startDate,
  490. EndDate: endDate,
  491. }
  492. v.TimeBase.ModifyTime = time.Now().Local()
  493. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.OriginAmount/rate), 64)
  494. v.Amount = a
  495. sellerItem := sellerMap[r.SellerId]
  496. if sellerItem == nil {
  497. resp.Fail("销售信息异常", c)
  498. return
  499. }
  500. v.SellerId = sellerItem.SellerId
  501. v.SellerName = sellerItem.SellerName
  502. v.SellerGroupId = sellerItem.GroupId
  503. v.SellerGroupName = sellerItem.GroupName
  504. v.SellerTeamId = sellerItem.TeamId
  505. v.SellerTeamName = sellerItem.TeamName
  506. //预登记列表
  507. pp := &fms.ContractPreRegister{
  508. PreRegisterId: r.PreRegisterId,
  509. ContractRegisterId: ob.ContractRegisterId,
  510. CompanyName: req.CompanyName,
  511. InvoiceType: req.RegisterType,
  512. AdminId: int(adminInfo.AdminId),
  513. AdminName: adminInfo.AdminName,
  514. ServiceProductId: r.ServiceProductId,
  515. IsPrePay: 1,
  516. StartDate: startDate,
  517. EndDate: endDate,
  518. TimeBase: base.TimeBase{},
  519. }
  520. pp.Set()
  521. pp.CurrencyUnit = r.CurrencyUnit
  522. ppUpdateCols := make([]string, 0)
  523. if pp.InvoiceType == fms.ContractInvoiceTypePreMake {
  524. //开票走开票字段
  525. pp.Remark = r.Remark
  526. pp.InvoiceDate = registerDate
  527. pp.OriginAmount = r.Amount
  528. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", pp.OriginAmount/rate), 64)
  529. pp.Amount = a
  530. sellerItem := sellerMap[r.SellerId]
  531. if sellerItem == nil {
  532. resp.Fail("销售信息异常", c)
  533. return
  534. }
  535. pp.InvoiceId = v.ContractInvoiceId
  536. pp.SellerId = sellerItem.SellerId
  537. pp.SellerName = sellerItem.SellerName
  538. pp.SellerGroupId = sellerItem.GroupId
  539. pp.SellerGroupName = sellerItem.GroupName
  540. pp.SellerTeamId = sellerItem.TeamId
  541. pp.SellerTeamName = sellerItem.TeamName
  542. pp.PaymentSellerId = sellerItem.SellerId
  543. pp.PaymentSellerName = sellerItem.SellerName
  544. pp.PaymentSellerGroupId = sellerItem.GroupId
  545. pp.PaymentSellerGroupName = sellerItem.GroupName
  546. pp.PaymentSellerTeamId = sellerItem.TeamId
  547. pp.PaymentSellerTeamName = sellerItem.TeamName
  548. ppUpdateCols = append(ppUpdateCols, "InvoiceDate", "Amount", "OriginAmount", "SellerId",
  549. "SellerName", "SellerGroupId", "SellerGroupName", "SellerTeamId", "SellerTeamName", "InvoiceId",
  550. "PaymentSellerId", "PaymentSellerName", "PaymentSellerGroupId", "PaymentSellerGroupName", "PaymentSellerTeamId", "PaymentSellerTeamName")
  551. } else {
  552. //到款
  553. pp.ArriveRemark = r.Remark
  554. pp.ArriveDate = registerDate
  555. pp.ArriveId = v.ContractInvoiceId
  556. pp.ArriveOriginAmount = r.Amount
  557. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", pp.ArriveOriginAmount/rate), 64)
  558. pp.ArriveAmount = a
  559. sellerItem := sellerMap[r.SellerId]
  560. if sellerItem == nil {
  561. resp.Fail("销售信息异常", c)
  562. return
  563. }
  564. pp.PaymentSellerId = sellerItem.SellerId
  565. pp.PaymentSellerName = sellerItem.SellerName
  566. pp.PaymentSellerGroupId = sellerItem.GroupId
  567. pp.PaymentSellerGroupName = sellerItem.GroupName
  568. pp.PaymentSellerTeamId = sellerItem.TeamId
  569. pp.PaymentSellerTeamName = sellerItem.TeamName
  570. ppUpdateCols = append(ppUpdateCols, "CurrencyUnit", "ArriveDate", "ArriveAmount", "ArriveOriginAmount", "ArriveId",
  571. "PaymentSellerId", "PaymentSellerName", "PaymentSellerGroupId", "PaymentSellerGroupName", "PaymentSellerTeamId", "PaymentSellerTeamName")
  572. }
  573. if pp.PreRegisterId == 0 {
  574. if e = pp.Create(); e != nil {
  575. resp.FailMsg("操作失败", "新增预登记失败, Err: "+e.Error(), c)
  576. return
  577. }
  578. v.PreRegisterId = pp.PreRegisterId
  579. } else {
  580. ppUpdateCols = append(ppUpdateCols,
  581. "CurrencyUnit", "CompanyName", "Remark", "ArriveRemark", "InvoiceTime", "ArriveTime",
  582. "ServiceProductId", "CurrencyUnit", "StartDate", "EndDate", "ModifyTime")
  583. if e = pp.Update(ppUpdateCols); e != nil {
  584. resp.FailMsg("操作失败", "更新预登记记录失败, Err: "+e.Error(), c)
  585. return
  586. }
  587. }
  588. invoiceList = append(invoiceList, v)
  589. ppList = append(ppList, pp)
  590. }
  591. invoiceUpdateCols := []string{
  592. "CurrencyUnit", "OriginAmount", "Amount", "InvoiceDate", "AdminId", "AdminName", "Remark",
  593. "ServiceProductId", "StartDate", "EndDate", "ModifyTime","SellerId", "SellerName", "SellerGroupId","SellerGroupName","SellerTeamId","SellerTeamName",
  594. }
  595. // 更新合同登记、套餐、开票到款
  596. if e = fms.UpdateContractPreRegister(ob, updateCols, serviceList, invoiceList, invoiceUpdateCols, req.DelInvoiceIds, req.DelPreRegisterIds, ppList); e != nil {
  597. resp.FailMsg("操作失败", "更新合同及套餐失败, Err: "+e.Error(), c)
  598. return
  599. }
  600. // 开票到款汇总
  601. go fmsService.SummaryInvoicePaymentByContractRegisterId(ob.ContractRegisterId)
  602. resp.Ok("操作成功", c)
  603. }
  604. // Del
  605. // @Title 删除到款预登记
  606. // @Description 删除到款预登记
  607. // @Param request body fms.ContractRegisterDelReq true "type json string"
  608. // @Success 200 string "操作成功"
  609. // @router /contract/pre_register/del [post]
  610. func (rg *PreRegisterController) Del(c *gin.Context) {
  611. req := new(fms.PreRegisterDelReq)
  612. err := c.ShouldBind(&req)
  613. if err != nil {
  614. errs, ok := err.(validator.ValidationErrors)
  615. if !ok {
  616. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  617. return
  618. }
  619. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  620. return
  621. }
  622. if req.InvoiceId != 0 {
  623. ob := new(fms.ContractInvoice)
  624. item, e := ob.Fetch(req.InvoiceId)
  625. if e != nil {
  626. if e == utils.ErrNoRow {
  627. resp.Fail("合同登记不存在或已被删除", c)
  628. return
  629. }
  630. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  631. return
  632. }
  633. e = item.Delete()
  634. if e != nil {
  635. resp.FailMsg("删除记录失败", "Err:"+e.Error(), c)
  636. return
  637. }
  638. }
  639. if req.ArriveId != 0 {
  640. ob := new(fms.ContractInvoice)
  641. item, e := ob.Fetch(req.ArriveId)
  642. if e != nil {
  643. if e == utils.ErrNoRow {
  644. resp.Fail("合同登记不存在或已被删除", c)
  645. return
  646. }
  647. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  648. return
  649. }
  650. e = item.Delete()
  651. if e != nil {
  652. resp.FailMsg("删除记录失败", "Err:"+e.Error(), c)
  653. return
  654. }
  655. }
  656. ob := new(fms.ContractPreRegister)
  657. item, e := ob.Fetch(req.PreRegisterId)
  658. if e != nil {
  659. if e == utils.ErrNoRow {
  660. resp.Fail("预登记记录不存在或已被删除", c)
  661. return
  662. }
  663. resp.FailMsg("获取预登记失败", "Err:"+e.Error(), c)
  664. return
  665. }
  666. e = item.Delete()
  667. if e != nil {
  668. resp.FailMsg("删除预登记记录失败", "Err:"+e.Error(), c)
  669. return
  670. }
  671. e = fms.DeleteInvoicePaymentSummaryByInvoiceIdAndPaymentId(req.InvoiceId, req.ArriveId, req.ContractRegisterId)
  672. if e != nil {
  673. resp.FailMsg("删除统计记录失败", "Err:"+e.Error(), c)
  674. return
  675. }
  676. //todo 合同看情况要不要删
  677. resp.Ok("操作成功", c)
  678. }
  679. // Detail
  680. // @Title 预登记记录详情
  681. // @Description 预登记记录详情
  682. // @Param request body fms.PreRegisterDetailReq true "type json string"
  683. // @Success 200 string "操作成功"
  684. // @router /contract/pre_register/detail [post]
  685. func (rg *PreRegisterController) Detail(c *gin.Context) {
  686. req := new(fms.PreRegisterDetailReq)
  687. err := c.ShouldBind(&req)
  688. if err != nil {
  689. errs, ok := err.(validator.ValidationErrors)
  690. if !ok {
  691. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  692. return
  693. }
  694. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  695. return
  696. }
  697. ob := new(fms.ContractRegister)
  698. item, e := ob.Fetch(req.ContractRegisterId)
  699. if e != nil {
  700. if e == utils.ErrNoRow {
  701. resp.Fail("合同登记不存在或已被删除", c)
  702. return
  703. }
  704. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  705. return
  706. }
  707. nItme := fms.ContractRegisterItem{
  708. ContractRegisterId: item.ContractRegisterId,
  709. ContractCode: item.ContractCode,
  710. RelateContractCode: item.RelateContractCode,
  711. CrmContractId: item.CrmContractId,
  712. ContractSource: item.ContractSource,
  713. CompanyName: item.CompanyName,
  714. ActualCompanyName: item.ActualCompanyName,
  715. ProductIds: item.ProductIds,
  716. SellerId: item.SellerId,
  717. SellerName: item.SellerName,
  718. RaiSellerId: item.RaiSellerId,
  719. RaiSellerName: item.RaiSellerName,
  720. ContractType: item.ContractType,
  721. ContractAmount: item.ContractAmount,
  722. InvoicedAmount: item.InvoicedAmount,
  723. PaymentAmount: item.PaymentAmount,
  724. CurrencyUnit: item.CurrencyUnit,
  725. RMBRate: item.RMBRate,
  726. StartDate: item.StartDate.Format(utils.FormatDate),
  727. EndDate: item.EndDate.Format(utils.FormatDate),
  728. SignDate: item.SignDate.Format(utils.FormatDate),
  729. AgreedPayTime: item.AgreedPayTime,
  730. ContractStatus: item.ContractStatus,
  731. RegisterStatus: item.RegisterStatus,
  732. Remark: item.Remark,
  733. ServiceRemark: item.ServiceRemark,
  734. HasPayment: item.HasPayment,
  735. NewCompany: item.NewCompany,
  736. CreateTime: item.CreateTime.Format(utils.FormatDateTime),
  737. }
  738. if nItme.StartDate == "0001-01-01" {
  739. nItme.StartDate = ""
  740. }
  741. if nItme.EndDate == "0001-01-01" {
  742. nItme.EndDate = ""
  743. }
  744. cond := ``
  745. pars := make([]interface{}, 0)
  746. if req.InvoiceId == 0 && req.ArriveId == 0 {
  747. cond = `contract_register_id = ? AND invoice_type = ? `
  748. pars = append(pars, req.ContractRegisterId, req.InvoiceType)
  749. } else {
  750. cond = `contract_register_id = ? AND invoice_type = ? AND contract_invoice_id =? `
  751. pars = append(pars, req.ContractRegisterId, req.InvoiceType)
  752. }
  753. if req.InvoiceType == 4 {
  754. pars = append(pars, req.ArriveId)
  755. } else {
  756. pars = append(pars, req.InvoiceId)
  757. }
  758. list, e := fms.GetContractInvoiceDetailItemList(cond, pars)
  759. if e != nil {
  760. err = fmt.Errorf("获取开票到款列表失败, Err: %s", e.Error())
  761. return
  762. }
  763. dupList, e := fms.GetDuplicateContractInvoiceDetailItemList(nItme.CompanyName, nItme.StartDate, nItme.EndDate)
  764. if e != nil {
  765. err = fmt.Errorf("获取开票到款列表失败, Err: %s", e.Error())
  766. return
  767. }
  768. for _, v := range dupList {
  769. if v.ContractRegisterId != req.ContractRegisterId{
  770. list = append(list, v)
  771. }
  772. }
  773. //获取ficc小套餐品种
  774. sysConf := new(system.SysConfig)
  775. confCond := `config_code = ?`
  776. confPars := make([]interface{}, 0)
  777. confPars = append(confPars, system.ConfigKeyCrmPermissionFiccClassify)
  778. confItem, e := sysConf.FetchByCondition(confCond, confPars)
  779. if e != nil {
  780. resp.FailData("获取失败", "Err:"+e.Error(), c)
  781. return
  782. }
  783. if confItem.ConfigValue == "" {
  784. resp.FailData("获取失败", "FICC品种分类配置为空", c)
  785. return
  786. }
  787. classifyArr := strings.Split(confItem.ConfigValue, ",")
  788. if len(classifyArr) == 0 {
  789. resp.FailData("获取失败", "FICC品种分类配置为空", c)
  790. return
  791. }
  792. // 获取FICC权限
  793. ficcCond := `enabled = 1 AND permission_type = 0 AND product_id = ? AND classify_name IN ?`
  794. ficcPars := make([]interface{}, 0)
  795. ficcPars = append(ficcPars, 1, classifyArr)
  796. items, e := crm.GetPermissionSetItemsByCondition(ficcCond, ficcPars)
  797. if e != nil {
  798. resp.FailData("获取失败", "获取FICC权限信息失败, Err: "+e.Error(), c)
  799. return
  800. }
  801. ficcItemMap := make(map[string][]*crm.PermissionSetItem, 0)
  802. for i := range items {
  803. if ficcItemMap[items[i].ClassifyName] == nil {
  804. ficcItemMap[items[i].ClassifyName] = make([]*crm.PermissionSetItem, 0)
  805. }
  806. ficcItemMap[items[i].ClassifyName] = append(ficcItemMap[items[i].ClassifyName], items[i])
  807. }
  808. sTempIdMap := make(map[string]int, 0)
  809. for i := range classifyArr {
  810. if classifyArr[i] == "市场策略" {
  811. continue
  812. }
  813. checkList := make([]int, 0)
  814. if classifyArr[i] == "宏观经济" {
  815. checkList = append(checkList, 1)
  816. }
  817. pItem := fms.ContractServiceTemplateItem{
  818. ServiceTemplateId: 90000 + i*100,
  819. }
  820. ficcItems := ficcItemMap[classifyArr[i]]
  821. for _, ficcItem := range ficcItems {
  822. sTempIdMap[strconv.Itoa(ficcItem.ChartPermissionId)] = pItem.ServiceTemplateId + ficcItem.ChartPermissionId
  823. }
  824. }
  825. services, e := fms.GetContractServiceAndDetailList(req.ContractRegisterId)
  826. if e != nil {
  827. err = fmt.Errorf(fmt.Sprint("查找合同服务异常", e))
  828. return
  829. }
  830. serviceTemplateIdSlice := make([]string, 0)
  831. for _, service := range services {
  832. if service.Title == "FICC小套餐" {
  833. serviceTemplateIdSlice = strings.Split(service.ChartPermissionIds, ",")
  834. }
  835. }
  836. serviceTemplateIds := make([]int, 0)
  837. for _, v := range serviceTemplateIdSlice {
  838. serviceTemplateIds = append(serviceTemplateIds, sTempIdMap[v])
  839. }
  840. respItem := fms.ContractPreRegisterDetail{
  841. ContractRegisterItem: &nItme,
  842. List: list,
  843. Services: services,
  844. ServiceTemplateIds: serviceTemplateIds,
  845. }
  846. resp.OkData("获取成功", respItem, c)
  847. }
  848. // Save
  849. // @Title 预登记保存
  850. // @Description 预登记保存
  851. // @Param request body fms.PrepayAddReq true "type json string"
  852. // @Success 200 string "操作成功"
  853. // @router /contract/pre_register/save [post]
  854. func (rg *PreRegisterController) Save(c *gin.Context) {
  855. req := new(fms.PreRegisterSaveReq)
  856. err := c.ShouldBind(&req)
  857. if err != nil {
  858. errs, ok := err.(validator.ValidationErrors)
  859. if !ok {
  860. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  861. return
  862. }
  863. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  864. return
  865. }
  866. claims, _ := c.Get("adminInfo")
  867. adminInfo := claims.(*system.SysAdmin)
  868. // 日期校验
  869. var startDate, endDate time.Time
  870. if req.StartDate != "" && req.EndDate != "" {
  871. startDate, err = time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  872. if err != nil {
  873. resp.FailMsg("约定开始日期格式有误", "合同开始日期格式有误, Err: "+err.Error(), c)
  874. return
  875. }
  876. endDate, err = time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  877. if err != nil {
  878. resp.FailMsg("约定结束日期格式有误", "合同结束日期格式有误, Err: "+err.Error(), c)
  879. return
  880. }
  881. }
  882. // 获取销售分组信息
  883. sellerList, e := crmService.GetSellerDepartmentListWithGroupAndTeam()
  884. if e != nil {
  885. resp.FailData("获取销售失败", "Err:"+e.Error(), c)
  886. return
  887. }
  888. sellerMap := make(map[int]*crm.SellerAdminWithGroupTeam)
  889. for i := range sellerList {
  890. sellerMap[sellerList[i].SellerId] = sellerList[i]
  891. }
  892. admin := new(crm.Admin)
  893. sellerIds := strings.Split(req.SellerIds, ",")
  894. if len(sellerIds) > 2 {
  895. resp.Fail("最多只能选择两个销售", c)
  896. return
  897. }
  898. var sellers []*crm.Admin
  899. var pars []interface{}
  900. cond := ``
  901. if len(sellerIds) > 0 {
  902. cond = ` admin_id in (?) `
  903. pars = append(pars, sellerIds)
  904. sellers, e = admin.List(cond, pars)
  905. if e != nil {
  906. resp.FailMsg("获取销售信息失败", "获取销售信息失败, Err: "+e.Error(), c)
  907. return
  908. }
  909. }
  910. updateCols := []string{"CompanyName", "ProductIds", "StartDate", "EndDate", "ModifyTime"}
  911. //更新合同
  912. ob := new(fms.ContractRegister)
  913. //ob.ContractCode = req.ContractCode
  914. ob.ContractRegisterId = req.ContractRegisterId
  915. ob.CompanyName = req.CompanyName
  916. ob.ProductIds = req.ProductIds
  917. ob.StartDate = startDate
  918. ob.EndDate = endDate
  919. ob.ModifyTime = time.Now()
  920. ob.CurrencyUnit = req.List[0].CurrencyUnit
  921. if req.RegisterType == fms.ContractInvoiceTypePreMake {
  922. //sellerItem := sellerMap[r.SellerId]
  923. //if sellerItem == nil {
  924. // resp.Fail("销售信息异常", c)
  925. // return
  926. //}
  927. for _, v := range sellers {
  928. if v.DepartmentId == crm.SellerDepartmentId {
  929. ob.SellerId = v.AdminId
  930. ob.SellerName = v.AdminName
  931. updateCols = append(updateCols, "SellerId", "SellerName")
  932. } else if v.DepartmentId == crm.RaiSellerDepartmentId {
  933. ob.RaiSellerId = v.AdminId
  934. ob.RaiSellerName = v.AdminName
  935. updateCols = append(updateCols, "RaiSellerId", "RaiSellerName")
  936. }
  937. }
  938. }
  939. // 套餐信息
  940. serviceAmountMap := make(map[int]float64)
  941. serviceList, e := fmsService.HandleContractServiceAndDetail(req.Services, true, serviceAmountMap)
  942. if e != nil {
  943. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  944. return
  945. }
  946. invoiceUpdateCols := make([]string, 0)
  947. invoiceHandleType := 0
  948. invoiceList := make([]*fms.ContractInvoice, 0)
  949. ppList := make([]*fms.ContractPreRegister, 0)
  950. for _, r := range req.List {
  951. // 货币及汇率
  952. rateList, e := fmsService.GetTodayCurrencyRateList()
  953. if e != nil {
  954. resp.FailMsg("操作失败", "获取今日货币汇率失败, Err: "+e.Error(), c)
  955. return
  956. }
  957. var rate float64
  958. for i := range rateList {
  959. if r.CurrencyUnit == rateList[i].Code {
  960. rate = rateList[i].RMBRate
  961. break
  962. }
  963. }
  964. if rate <= 0 {
  965. resp.FailMsg("操作失败", "货币汇率信息有误", c)
  966. return
  967. }
  968. registerDate, e := time.Parse(utils.FormatDate, r.RegisterDate)
  969. if e != nil {
  970. resp.FailMsg("日期转换失败", "日期转换失败, Err: "+e.Error(), c)
  971. return
  972. }
  973. v := &fms.ContractInvoice{
  974. ContractInvoiceId: r.InvoiceId,
  975. ContractRegisterId: ob.ContractRegisterId,
  976. OriginAmount: r.Amount,
  977. CurrencyUnit: r.CurrencyUnit,
  978. InvoiceType: req.RegisterType,
  979. InvoiceDate: registerDate,
  980. AdminId: int(adminInfo.AdminId),
  981. AdminName: adminInfo.AdminName,
  982. Remark: r.Remark,
  983. ServiceProductId: r.ServiceProductId,
  984. IsPrePay: 1,
  985. StartDate: startDate,
  986. EndDate: endDate,
  987. TimeBase: base.TimeBase{},
  988. }
  989. v.TimeBase.ModifyTime = time.Now()
  990. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.OriginAmount/rate), 64)
  991. v.Amount = a
  992. sellerItem := sellerMap[r.SellerId]
  993. if sellerItem == nil {
  994. resp.Fail("销售信息异常", c)
  995. return
  996. }
  997. v.SellerId = sellerItem.SellerId
  998. v.SellerName = sellerItem.SellerName
  999. v.SellerGroupId = sellerItem.GroupId
  1000. v.SellerGroupName = sellerItem.GroupName
  1001. v.SellerTeamId = sellerItem.TeamId
  1002. v.SellerTeamName = sellerItem.TeamName
  1003. invoiceUpdateCols = append(invoiceUpdateCols, "SellerId", "SellerName", "SellerGroupId",
  1004. "SellerGroupName", "SellerTeamId", "SellerTeamName")
  1005. // 新增开票到款记录
  1006. if v.ContractInvoiceId != 0 {
  1007. //更新
  1008. invoiceHandleType = 1
  1009. invoiceUpdateCols = append(invoiceUpdateCols, "OriginAmount", "CurrencyUnit",
  1010. "InvoiceDate", "Remark", "ServiceProductId", "StartDate", "EndDate", "ModifyTime")
  1011. } else {
  1012. //新增
  1013. invoiceHandleType = 2
  1014. }
  1015. invoiceList = append(invoiceList, v)
  1016. //预登记列表
  1017. ppUpdateCols := make([]string, 0)
  1018. pp := &fms.ContractPreRegister{
  1019. PreRegisterId: r.PreRegisterId,
  1020. ContractRegisterId: ob.ContractRegisterId,
  1021. CompanyName: req.CompanyName,
  1022. InvoiceType: req.RegisterType,
  1023. InvoiceDate: registerDate,
  1024. AdminId: int(adminInfo.AdminId),
  1025. AdminName: adminInfo.AdminName,
  1026. ServiceProductId: r.ServiceProductId,
  1027. IsPrePay: 1,
  1028. StartDate: startDate,
  1029. EndDate: endDate,
  1030. TimeBase: base.TimeBase{},
  1031. }
  1032. pp.Set()
  1033. pp.CurrencyUnit = r.CurrencyUnit
  1034. if pp.InvoiceType == fms.ContractInvoiceTypePreMake {
  1035. //开票走开票字段
  1036. pp.Remark = r.Remark
  1037. pp.InvoiceDate = registerDate
  1038. pp.OriginAmount = r.Amount
  1039. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", pp.OriginAmount/rate), 64)
  1040. pp.Amount = a
  1041. sellerItem := sellerMap[r.SellerId]
  1042. if sellerItem == nil {
  1043. resp.Fail("销售信息异常", c)
  1044. return
  1045. }
  1046. pp.SellerId = sellerItem.SellerId
  1047. pp.SellerName = sellerItem.SellerName
  1048. pp.SellerGroupId = sellerItem.GroupId
  1049. pp.SellerGroupName = sellerItem.GroupName
  1050. pp.SellerTeamId = sellerItem.TeamId
  1051. pp.SellerTeamName = sellerItem.TeamName
  1052. pp.PaymentSellerId = sellerItem.SellerId
  1053. pp.PaymentSellerName = sellerItem.SellerName
  1054. pp.PaymentSellerGroupId = sellerItem.GroupId
  1055. pp.PaymentSellerGroupName = sellerItem.GroupName
  1056. pp.PaymentSellerTeamId = sellerItem.TeamId
  1057. pp.PaymentSellerTeamName = sellerItem.TeamName
  1058. ppUpdateCols = append(ppUpdateCols, "InvoiceDate", "Amount", "OriginAmount", "SellerId",
  1059. "SellerName", "SellerGroupId", "SellerGroupName", "SellerTeamId", "SellerTeamName", "Remark",
  1060. "PaymentSellerId", "PaymentSellerName", "PaymentSellerGroupId", "PaymentSellerGroupName", "PaymentSellerTeamId", "PaymentSellerTeamName")
  1061. } else {
  1062. //到款
  1063. pp.ArriveRemark = r.Remark
  1064. pp.ArriveDate = registerDate
  1065. pp.ArriveOriginAmount = r.Amount
  1066. a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", pp.ArriveOriginAmount/rate), 64)
  1067. pp.ArriveAmount = a
  1068. sellerItem := sellerMap[r.SellerId]
  1069. if sellerItem == nil {
  1070. resp.Fail("销售信息异常", c)
  1071. return
  1072. }
  1073. pp.PaymentSellerId = sellerItem.SellerId
  1074. pp.PaymentSellerName = sellerItem.SellerName
  1075. pp.PaymentSellerGroupId = sellerItem.GroupId
  1076. pp.PaymentSellerGroupName = sellerItem.GroupName
  1077. pp.PaymentSellerTeamId = sellerItem.TeamId
  1078. pp.PaymentSellerTeamName = sellerItem.TeamName
  1079. ppUpdateCols = append(ppUpdateCols, "ArriveRemark", "CurrencyUnit", "ArriveDate", "ArriveAmount", "ArriveOriginAmount",
  1080. "PaymentSellerId", "PaymentSellerName", "PaymentSellerGroupId", "PaymentSellerGroupName", "PaymentSellerTeamId", "PaymentSellerTeamName")
  1081. }
  1082. ppUpdateCols = append(ppUpdateCols,
  1083. "CompanyName", "AdminId", "AdminName", "ServiceProductId",
  1084. "CurrencyUnit", "StartDate", "EndDate", "ModifyTime")
  1085. if e = pp.Update(ppUpdateCols); e != nil {
  1086. resp.FailMsg("操作失败", "更新预登记记录失败, Err: "+e.Error(), c)
  1087. return
  1088. }
  1089. ppList = append(ppList, pp)
  1090. }
  1091. if e = fms.UpdateContractRegisterPre(ob, updateCols, serviceList, invoiceList, invoiceUpdateCols, invoiceHandleType, ppList); e != nil {
  1092. resp.FailMsg("操作失败", "新增合同及套餐失败, Err: "+e.Error(), c)
  1093. return
  1094. }
  1095. // 开票到款汇总
  1096. go fmsService.SummaryInvoicePaymentByContractRegisterId(ob.ContractRegisterId)
  1097. resp.Ok("操作成功", c)
  1098. }