pre_register.go 38 KB

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