invoice_payment.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. package fms
  2. import (
  3. "fmt"
  4. "github.com/shopspring/decimal"
  5. "hongze/fms_api/models/crm"
  6. "hongze/fms_api/models/fms"
  7. "hongze/fms_api/services/alarm_msg"
  8. "hongze/fms_api/utils"
  9. "strconv"
  10. "time"
  11. )
  12. // CalculateContractPaymentType 计算到款登记付款方式
  13. // 计算公式: 到款金额 / ( 合同金额 / round( (合同结束日期 - 合同开始日期) / 365) )
  14. func CalculateContractPaymentType(paymentAmount, contractAmount, dayDiff float64) (payEnum int) {
  15. payEnum = fms.ContractPaymentPayTypeAbnormal // 默认异常
  16. if paymentAmount <= 0 || contractAmount <= 0 || dayDiff <= 0 {
  17. return
  18. }
  19. // 年份四舍五入
  20. days := decimal.NewFromFloat(dayDiff)
  21. yearDays := decimal.NewFromFloat(365)
  22. yearRound := days.DivRound(yearDays, 2)
  23. if yearRound.IsZero() {
  24. return
  25. }
  26. years := yearRound.Add(decimal.NewFromFloat(0.5)).Floor()
  27. if years.IsZero() {
  28. return
  29. }
  30. // 分母
  31. contractDec := decimal.NewFromFloat(contractAmount)
  32. contractDeno := contractDec.DivRound(years, 2)
  33. if contractDeno.IsZero() {
  34. return
  35. }
  36. paymentDec := decimal.NewFromFloat(paymentAmount)
  37. // 结果
  38. resultDec := paymentDec.DivRound(contractDeno, 2)
  39. // 标准比例
  40. yearPay := decimal.NewFromFloat(1)
  41. halfYearPay := decimal.NewFromFloat(0.5)
  42. quarterPay := decimal.NewFromFloat(0.25)
  43. zeroPay := decimal.NewFromFloat(0)
  44. payEnum = fms.ContractPaymentPayTypeOther
  45. // 异常
  46. if yearPay.LessThan(resultDec) || resultDec.LessThanOrEqual(zeroPay) {
  47. payEnum = fms.ContractPaymentPayTypeAbnormal
  48. return
  49. }
  50. // 年付
  51. if resultDec.Equal(yearPay) {
  52. payEnum = fms.ContractPaymentPayTypeYear
  53. return
  54. }
  55. // 半年付
  56. if resultDec.Equal(halfYearPay) {
  57. payEnum = fms.ContractPaymentPayTypeHalfYear
  58. return
  59. }
  60. // 季付
  61. if resultDec.Equal(quarterPay) {
  62. payEnum = fms.ContractPaymentPayTypeQuarter
  63. return
  64. }
  65. return
  66. }
  67. // MergeInvoiceList2InvoicePaymentCensusInfo 合同登记-合并开票到款列表
  68. func MergeInvoiceList2InvoicePaymentCensusInfo(invoiceList []*fms.ContractInvoice, paymentList []*fms.ContractInvoice, serviceList []*fms.ContractService, serviceAmountList []*fms.ContractPaymentServiceAmount) (mergeList []*fms.InvoicePaymentCensusInfo) {
  69. invoiceLen := len(invoiceList)
  70. paymentLen := len(paymentList)
  71. if invoiceLen == 0 && paymentLen == 0 {
  72. return
  73. }
  74. // 到款套餐分配信息
  75. amountMap := make(map[string]*fms.ContractPaymentServiceAmount)
  76. for i := range serviceAmountList {
  77. k := fmt.Sprintf("%d-%d", serviceAmountList[i].ContractPaymentId, serviceAmountList[i].ServiceTemplateId)
  78. amountMap[k] = serviceAmountList[i]
  79. }
  80. serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmountItem)
  81. for i := range paymentList {
  82. l := make([]*fms.ContractPaymentServiceAmountItem, 0)
  83. for ii := range serviceList {
  84. v := new(fms.ContractPaymentServiceAmountItem)
  85. v.ServiceTemplateId = serviceList[ii].ServiceTemplateId
  86. v.ServiceTemplateName = serviceList[ii].Title
  87. k := fmt.Sprintf("%d-%d", paymentList[i].ContractInvoiceId, serviceList[ii].ServiceTemplateId)
  88. a := amountMap[k]
  89. if a != nil {
  90. v.ContractPaymentServiceAmountId = a.ContractPaymentServiceAmountId
  91. v.ContractPaymentId = a.ContractPaymentId
  92. v.Amount = a.Amount
  93. }
  94. l = append(l, v)
  95. }
  96. serviceAmountMap[paymentList[i].ContractInvoiceId] = l
  97. }
  98. // 开票和到款列表, 条数多的为最后的合并条数
  99. mergeList = make([]*fms.InvoicePaymentCensusInfo, 0)
  100. if invoiceLen >= paymentLen {
  101. for i := range invoiceList {
  102. v := new(fms.InvoicePaymentCensusInfo)
  103. v.InvoiceId = invoiceList[i].ContractInvoiceId
  104. v.InvoiceDate = invoiceList[i].InvoiceDate.Format(utils.FormatDate)
  105. v.InvoiceAmount = invoiceList[i].OriginAmount
  106. v.SellerId = invoiceList[i].SellerId
  107. v.SellerName = invoiceList[i].SellerName
  108. v.SellerGroupId = invoiceList[i].SellerGroupId
  109. v.SellerGroupName = invoiceList[i].SellerGroupName
  110. // 直接取对应键的到款列表
  111. if i+1 <= paymentLen {
  112. payItem := paymentList[i]
  113. if payItem != nil {
  114. v.PaymentId = payItem.ContractInvoiceId
  115. v.PaymentDate = payItem.InvoiceDate.Format(utils.FormatDate)
  116. v.PaymentAmount = payItem.OriginAmount
  117. v.PayType = payItem.PayType
  118. v.ServiceAmountList = serviceAmountMap[payItem.ContractInvoiceId]
  119. }
  120. }
  121. mergeList = append(mergeList, v)
  122. }
  123. return
  124. }
  125. // 到款多于开票
  126. if paymentLen > invoiceLen {
  127. for i := range paymentList {
  128. v := new(fms.InvoicePaymentCensusInfo)
  129. v.PaymentId = paymentList[i].ContractInvoiceId
  130. v.PaymentDate = paymentList[i].InvoiceDate.Format(utils.FormatDate)
  131. v.PaymentAmount = paymentList[i].OriginAmount
  132. v.PayType = paymentList[i].PayType
  133. v.ServiceAmountList = serviceAmountMap[paymentList[i].ContractInvoiceId]
  134. // 直接取对应键的开票
  135. if i+1 <= invoiceLen {
  136. invoiceItem := invoiceList[i]
  137. if invoiceItem != nil {
  138. v.InvoiceId = invoiceItem.ContractInvoiceId
  139. v.InvoiceDate = invoiceItem.InvoiceDate.Format(utils.FormatDate)
  140. v.InvoiceAmount = invoiceItem.OriginAmount
  141. v.SellerId = invoiceItem.SellerId
  142. v.SellerName = invoiceItem.SellerName
  143. v.SellerGroupId = invoiceList[i].SellerGroupId
  144. v.SellerGroupName = invoiceList[i].SellerGroupName
  145. }
  146. }
  147. mergeList = append(mergeList, v)
  148. }
  149. return
  150. }
  151. return
  152. }
  153. // SummaryInvoicePaymentByContractRegisterId 汇总合同登记的开票到款, 即一一进行对应存入汇总表
  154. func SummaryInvoicePaymentByContractRegisterId(registerId int) {
  155. var err error
  156. defer func() {
  157. if err != nil {
  158. alarm_msg.SendAlarmMsg(fmt.Sprintf("汇总开票到款失败, ErrMsg: \n%s", err.Error()), 3)
  159. }
  160. }()
  161. // 获取开票到款信息
  162. cond := `contract_register_id = ?`
  163. pars := make([]interface{}, 0)
  164. pars = append(pars, registerId)
  165. list, e := fms.GetContractInvoiceItemList(cond, pars)
  166. if e != nil {
  167. err = fmt.Errorf("获取开票到款列表失败, Err: %s", e.Error())
  168. return
  169. }
  170. ficcInvoiceIds := make([]int, 0)
  171. ficcPaymentIds := make([]int, 0)
  172. raiInvoiceIds := make([]int, 0)
  173. raiPaymentIds := make([]int, 0)
  174. noProductPaymentIds := make([]int, 0)
  175. for i := range list {
  176. if list[i].InvoiceType == fms.ContractInvoiceTypeMake {
  177. if list[i].ServiceProductId == crm.CompanyProductFicc {
  178. ficcInvoiceIds = append(ficcInvoiceIds, list[i].ContractInvoiceId)
  179. } else if list[i].ServiceProductId == crm.CompanyProductRai {
  180. raiInvoiceIds = append(raiInvoiceIds, list[i].ContractInvoiceId)
  181. }
  182. continue
  183. }
  184. if list[i].InvoiceType == fms.ContractInvoiceTypePay {
  185. if list[i].ServiceProductId == crm.CompanyProductFicc {
  186. ficcPaymentIds = append(ficcPaymentIds, list[i].ContractInvoiceId)
  187. } else if list[i].ServiceProductId == crm.CompanyProductRai {
  188. raiPaymentIds = append(raiPaymentIds, list[i].ContractInvoiceId)
  189. }
  190. continue
  191. }
  192. if list[i].InvoiceType == fms.ContractInvoiceTypePreMake {
  193. if list[i].ServiceProductId == crm.CompanyProductFicc {
  194. ficcInvoiceIds = append(ficcInvoiceIds, list[i].ContractInvoiceId)
  195. } else if list[i].ServiceProductId == crm.CompanyProductRai {
  196. raiInvoiceIds = append(raiInvoiceIds, list[i].ContractInvoiceId)
  197. }
  198. continue
  199. }
  200. if list[i].InvoiceType == fms.ContractInvoiceTypePrePay {
  201. if list[i].ServiceProductId == crm.CompanyProductFicc {
  202. ficcPaymentIds = append(ficcPaymentIds, list[i].ContractInvoiceId)
  203. } else if list[i].ServiceProductId == crm.CompanyProductRai {
  204. raiPaymentIds = append(raiPaymentIds, list[i].ContractInvoiceId)
  205. } else {
  206. noProductPaymentIds = append(noProductPaymentIds, list[i].ContractInvoiceId)
  207. }
  208. continue
  209. }
  210. }
  211. ficcInvoiceLen := len(ficcInvoiceIds)
  212. raiInvoiceLen := len(raiInvoiceIds)
  213. ficcPaymentLen := len(ficcPaymentIds)
  214. raiPaymentLen := len(raiPaymentIds)
  215. noProductPaymentLen := len(noProductPaymentIds)
  216. // 汇总数据
  217. nowTime := time.Now().Local()
  218. summaryList := make([]*fms.InvoicePaymentSummary, 0)
  219. // ficc
  220. if ficcInvoiceLen >= ficcPaymentLen {
  221. for i := range ficcInvoiceIds {
  222. v := new(fms.InvoicePaymentSummary)
  223. v.RegisterId = registerId
  224. v.InvoiceId = ficcInvoiceIds[i]
  225. v.ServiceProductId = crm.CompanyProductFicc
  226. v.CreateTime = nowTime
  227. v.ModifyTime = nowTime
  228. // 取对应key的到款ID
  229. if i+1 <= ficcPaymentLen {
  230. v.PaymentId = ficcPaymentIds[i]
  231. }
  232. summaryList = append(summaryList, v)
  233. }
  234. }
  235. if ficcPaymentLen > ficcInvoiceLen {
  236. for i := range ficcPaymentIds {
  237. v := new(fms.InvoicePaymentSummary)
  238. v.RegisterId = registerId
  239. v.PaymentId = ficcPaymentIds[i]
  240. v.ServiceProductId = crm.CompanyProductFicc
  241. v.CreateTime = nowTime
  242. v.ModifyTime = nowTime
  243. // 取对应key的开票ID
  244. if i+1 <= ficcInvoiceLen {
  245. v.InvoiceId = ficcInvoiceIds[i]
  246. }
  247. summaryList = append(summaryList, v)
  248. }
  249. }
  250. // rai
  251. if raiInvoiceLen >= raiPaymentLen {
  252. for i := range raiInvoiceIds {
  253. v := new(fms.InvoicePaymentSummary)
  254. v.RegisterId = registerId
  255. v.InvoiceId = raiInvoiceIds[i]
  256. v.ServiceProductId = crm.CompanyProductRai
  257. v.CreateTime = nowTime
  258. v.ModifyTime = nowTime
  259. // 取对应key的到款ID
  260. if i+1 <= raiPaymentLen {
  261. v.PaymentId = raiPaymentIds[i]
  262. }
  263. summaryList = append(summaryList, v)
  264. }
  265. }
  266. if raiPaymentLen > raiInvoiceLen {
  267. for i := range raiPaymentIds {
  268. v := new(fms.InvoicePaymentSummary)
  269. v.RegisterId = registerId
  270. v.PaymentId = raiPaymentIds[i]
  271. v.ServiceProductId = crm.CompanyProductRai
  272. v.CreateTime = nowTime
  273. v.ModifyTime = nowTime
  274. // 取对应key的开票ID
  275. if i+1 <= raiInvoiceLen {
  276. v.InvoiceId = raiInvoiceIds[i]
  277. }
  278. summaryList = append(summaryList, v)
  279. }
  280. }
  281. if noProductPaymentLen > 0 {
  282. for i := range noProductPaymentIds {
  283. v := new(fms.InvoicePaymentSummary)
  284. v.RegisterId = registerId
  285. v.PaymentId = noProductPaymentIds[i]
  286. v.CreateTime = nowTime
  287. v.ModifyTime = nowTime
  288. summaryList = append(summaryList, v)
  289. }
  290. }
  291. // 删除并新增汇总数据
  292. summaryOB := new(fms.InvoicePaymentSummary)
  293. if e = summaryOB.DeleteAndCreate(registerId, summaryList); e != nil {
  294. err = fmt.Errorf("新增汇总数据失败, Err: %s", e.Error())
  295. }
  296. return
  297. }
  298. // CalculatePaymentServiceAmount
  299. func CalculatePaymentServiceAmount(registerId int) (err error) {
  300. // todo 判断是否符合均分金额的条件,如果符合,需要生成金额分配记录表
  301. // 查询所有的到款记录,如果没有到款记录则无需分配到款金额
  302. cond := `contract_register_id = ? and invoice_type=2 and service_product_id=2`
  303. pars := make([]interface{}, 0)
  304. pars = append(pars, registerId)
  305. paymentList, err := fms.GetContractInvoiceItemList(cond, pars)
  306. if err != nil {
  307. err = fmt.Errorf("获取开票到款列表失败, Err: %s", err.Error())
  308. return
  309. }
  310. if len(paymentList) == 0 {
  311. return
  312. }
  313. var contractPaymentIds []int
  314. for _, v := range paymentList {
  315. contractPaymentIds = append(contractPaymentIds, v.ContractInvoiceId)
  316. }
  317. // 获取服务套餐
  318. servicesList, err := fms.GetContractServiceAndDetailList(registerId)
  319. if err != nil {
  320. err = fmt.Errorf("获取服务套餐失败, Err: " + err.Error())
  321. return
  322. }
  323. industrySlice := []string{
  324. "行业套餐",
  325. "医药",
  326. "消费",
  327. "科技",
  328. "智造",
  329. "策略",
  330. "主观",
  331. "客观",
  332. }
  333. unNeedAsign := false
  334. industryFlag := false
  335. servicesListMap := make(map[int]*fms.ContractServiceAndDetail)
  336. // 判断当前套餐是否符合分配规则
  337. for _, v := range servicesList {
  338. if v.ProductId == crm.CompanyProductRai {
  339. industryFlag = true
  340. }
  341. servicesListMap[v.ServiceTemplateId] = v
  342. if v.ProductId == crm.CompanyProductRai && !utils.InArray(v.Title, industrySlice) {
  343. unNeedAsign = true
  344. }
  345. }
  346. if unNeedAsign || !industryFlag {
  347. //不符合自动分配规则,则直接删除之前的自动分配记录
  348. err = fms.DeletePaymentServiceAmountByRegisterId(registerId)
  349. if err != nil {
  350. return
  351. }
  352. return
  353. } else {
  354. //遍历所有的到款记录,筛选出需要删除并新增的到款记录ID
  355. _, serviceFormatMap, e := GetContractServiceNameFormat([]int{registerId})
  356. if e != nil {
  357. err = fmt.Errorf("获取合同套餐失败, Err: %s", e.Error())
  358. return
  359. }
  360. servicesFormatList, ok := serviceFormatMap[registerId]
  361. if !ok {
  362. err = fmt.Errorf("查不到合同套餐")
  363. return
  364. }
  365. servicesFormatStr := ""
  366. length := 0
  367. for _, v := range servicesFormatList {
  368. if servicesListMap[v.ServiceTemplateId].ProductId == crm.CompanyProductRai {
  369. length++
  370. servicesFormatStr += fmt.Sprintf("%d_", v.ServiceTemplateId)
  371. }
  372. }
  373. if length == 0 {
  374. return
  375. }
  376. payAverageAmountMap := make(map[int]float64)
  377. for _, v := range paymentList {
  378. payAverageAmountMap[v.ContractInvoiceId] = v.Amount / float64(length)
  379. payAverageAmountMap[v.ContractInvoiceId], _ = strconv.ParseFloat(fmt.Sprintf("%.2f", payAverageAmountMap[v.ContractInvoiceId]), 64)
  380. }
  381. //查询原来的金额分配记录
  382. serviceAmountCond := `contract_payment_id = ? and contract_payment_id in ?`
  383. serviceAmountPars := make([]interface{}, 0)
  384. serviceAmountPars = append(serviceAmountPars, registerId, contractPaymentIds)
  385. serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  386. serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  387. if e != nil {
  388. err = fmt.Errorf("获取到款套餐分配列表失败, Err: %s", e.Error())
  389. return
  390. }
  391. oldPaymentServiceMap := make(map[int]string)
  392. oldPaymentAmountMap := make(map[int]float64)
  393. for _, v := range serviceAmountList {
  394. oldPaymentServiceMap[v.ContractPaymentId] += fmt.Sprintf("%d_", v.ServiceTemplateId)
  395. oldPaymentAmountMap[v.ContractPaymentId] = v.Amount
  396. }
  397. var newPaymentIds []int
  398. for _, v := range paymentList {
  399. if _, ok1 := oldPaymentServiceMap[v.ContractInvoiceId]; !ok1 {
  400. newPaymentIds = append(newPaymentIds, v.ContractInvoiceId)
  401. continue
  402. }
  403. if oldPaymentServiceMap[v.ContractInvoiceId] != servicesFormatStr {
  404. newPaymentIds = append(newPaymentIds, v.ContractInvoiceId)
  405. continue
  406. }
  407. if oldPaymentAmountMap[v.ContractInvoiceId] != payAverageAmountMap[v.ContractInvoiceId] {
  408. newPaymentIds = append(newPaymentIds, v.ContractInvoiceId)
  409. continue
  410. }
  411. }
  412. // 删除旧的金额分配记录并生成新的自动分配记录
  413. if len(newPaymentIds) == 0 {
  414. return
  415. }
  416. for _, v := range newPaymentIds {
  417. addList := make([]*fms.ContractPaymentServiceAmount, 0)
  418. for _, f := range servicesFormatList {
  419. if servicesListMap[f.ServiceTemplateId].ProductId == crm.CompanyProductRai {
  420. t := &fms.ContractPaymentServiceAmount{
  421. ContractRegisterId: registerId,
  422. ContractPaymentId: v,
  423. ServiceTemplatePid: f.ServiceTemplatePid,
  424. ServiceTemplateId: f.ServiceTemplateId,
  425. Amount: payAverageAmountMap[v],
  426. InitType: 1,
  427. }
  428. t.Set()
  429. addList = append(addList, t)
  430. }
  431. }
  432. if e := fms.CreatePaymentServiceAmount(registerId, v, addList); e != nil {
  433. err = fmt.Errorf("新增到款套餐金额失败, Err: " + e.Error())
  434. return
  435. }
  436. }
  437. }
  438. return
  439. }
  440. // SummaryInvoicePaymentByPreRegister 预登记时汇总
  441. func SummaryInvoicePaymentByPreRegister(registerId int) {
  442. var err error
  443. defer func() {
  444. if err != nil {
  445. alarm_msg.SendAlarmMsg(fmt.Sprintf("汇总开票到款失败, ErrMsg: \n%s", err.Error()), 3)
  446. }
  447. }()
  448. // 获取开票到款信息
  449. cond := `contract_register_id = ?`
  450. pars := make([]interface{}, 0)
  451. pars = append(pars, registerId)
  452. list, e := fms.GetContractPreRegisterItemList(cond, pars)
  453. if e != nil {
  454. err = fmt.Errorf("获取开票到款列表失败, Err: %s", e.Error())
  455. return
  456. }
  457. ficcInvoiceIds := make([]int, 0)
  458. ficcPaymentIds := make([]int, 0)
  459. raiInvoiceIds := make([]int, 0)
  460. raiPaymentIds := make([]int, 0)
  461. for i := range list {
  462. if list[i].InvoiceType == fms.ContractInvoiceTypePreMake {
  463. if list[i].ServiceProductId == crm.CompanyProductFicc {
  464. ficcInvoiceIds = append(ficcInvoiceIds, list[i].PreRegisterId)
  465. } else if list[i].ServiceProductId == crm.CompanyProductRai {
  466. raiInvoiceIds = append(raiInvoiceIds, list[i].PreRegisterId)
  467. }
  468. continue
  469. }
  470. if list[i].InvoiceType == fms.ContractInvoiceTypePrePay {
  471. if list[i].ServiceProductId == crm.CompanyProductFicc {
  472. ficcPaymentIds = append(ficcPaymentIds, list[i].PreRegisterId)
  473. } else if list[i].ServiceProductId == crm.CompanyProductRai {
  474. raiPaymentIds = append(raiPaymentIds, list[i].PreRegisterId)
  475. }
  476. continue
  477. }
  478. }
  479. ficcInvoiceLen := len(ficcInvoiceIds)
  480. raiInvoiceLen := len(raiInvoiceIds)
  481. ficcPaymentLen := len(ficcPaymentIds)
  482. raiPaymentLen := len(raiPaymentIds)
  483. // 汇总数据
  484. nowTime := time.Now().Local()
  485. summaryList := make([]*fms.InvoicePaymentSummary, 0)
  486. // ficc
  487. if ficcInvoiceLen >= ficcPaymentLen {
  488. for i := range ficcInvoiceIds {
  489. v := new(fms.InvoicePaymentSummary)
  490. v.RegisterId = registerId
  491. v.InvoiceId = ficcInvoiceIds[i]
  492. v.ServiceProductId = crm.CompanyProductFicc
  493. v.CreateTime = nowTime
  494. v.ModifyTime = nowTime
  495. // 取对应key的到款ID
  496. if i+1 <= ficcPaymentLen {
  497. v.PaymentId = ficcPaymentIds[i]
  498. }
  499. summaryList = append(summaryList, v)
  500. }
  501. }
  502. if ficcPaymentLen > ficcInvoiceLen {
  503. for i := range ficcPaymentIds {
  504. v := new(fms.InvoicePaymentSummary)
  505. v.RegisterId = registerId
  506. v.PaymentId = ficcPaymentIds[i]
  507. v.ServiceProductId = crm.CompanyProductFicc
  508. v.CreateTime = nowTime
  509. v.ModifyTime = nowTime
  510. // 取对应key的开票ID
  511. if i+1 <= ficcInvoiceLen {
  512. v.InvoiceId = ficcInvoiceIds[i]
  513. }
  514. summaryList = append(summaryList, v)
  515. }
  516. }
  517. // rai
  518. if raiInvoiceLen >= raiPaymentLen {
  519. for i := range raiInvoiceIds {
  520. v := new(fms.InvoicePaymentSummary)
  521. v.RegisterId = registerId
  522. v.InvoiceId = raiInvoiceIds[i]
  523. v.ServiceProductId = crm.CompanyProductRai
  524. v.CreateTime = nowTime
  525. v.ModifyTime = nowTime
  526. // 取对应key的到款ID
  527. if i+1 <= raiPaymentLen {
  528. v.PaymentId = raiPaymentIds[i]
  529. }
  530. summaryList = append(summaryList, v)
  531. }
  532. }
  533. if raiPaymentLen > raiInvoiceLen {
  534. for i := range raiPaymentIds {
  535. v := new(fms.InvoicePaymentSummary)
  536. v.RegisterId = registerId
  537. v.PaymentId = raiPaymentIds[i]
  538. v.ServiceProductId = crm.CompanyProductRai
  539. v.CreateTime = nowTime
  540. v.ModifyTime = nowTime
  541. // 取对应key的开票ID
  542. if i+1 <= raiInvoiceLen {
  543. v.InvoiceId = raiInvoiceIds[i]
  544. }
  545. summaryList = append(summaryList, v)
  546. }
  547. }
  548. // 删除并新增汇总数据
  549. summaryOB := new(fms.InvoicePaymentSummary)
  550. if e = summaryOB.DeleteAndCreate(registerId, summaryList); e != nil {
  551. err = fmt.Errorf("新增汇总数据失败, Err: %s", e.Error())
  552. }
  553. return
  554. }