invoice_payment.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. for i := range list {
  175. if list[i].InvoiceType == fms.ContractInvoiceTypeMake {
  176. if list[i].ServiceProductId == crm.CompanyProductFicc {
  177. ficcInvoiceIds = append(ficcInvoiceIds, list[i].ContractInvoiceId)
  178. } else if list[i].ServiceProductId == crm.CompanyProductRai {
  179. raiInvoiceIds = append(raiInvoiceIds, list[i].ContractInvoiceId)
  180. }
  181. continue
  182. }
  183. if list[i].InvoiceType == fms.ContractInvoiceTypePay {
  184. if list[i].ServiceProductId == crm.CompanyProductFicc {
  185. ficcPaymentIds = append(ficcPaymentIds, list[i].ContractInvoiceId)
  186. } else if list[i].ServiceProductId == crm.CompanyProductRai {
  187. raiPaymentIds = append(raiPaymentIds, list[i].ContractInvoiceId)
  188. }
  189. continue
  190. }
  191. if list[i].InvoiceType == fms.ContractInvoiceTypePreMake {
  192. if list[i].ServiceProductId == crm.CompanyProductFicc {
  193. ficcInvoiceIds = append(ficcInvoiceIds, list[i].ContractInvoiceId)
  194. } else if list[i].ServiceProductId == crm.CompanyProductRai {
  195. raiInvoiceIds = append(raiInvoiceIds, list[i].ContractInvoiceId)
  196. }
  197. continue
  198. }
  199. if list[i].InvoiceType == fms.ContractInvoiceTypePrePay {
  200. if list[i].ServiceProductId == crm.CompanyProductFicc {
  201. ficcPaymentIds = append(ficcPaymentIds, list[i].ContractInvoiceId)
  202. } else if list[i].ServiceProductId == crm.CompanyProductRai {
  203. raiPaymentIds = append(raiPaymentIds, list[i].ContractInvoiceId)
  204. }
  205. continue
  206. }
  207. }
  208. ficcInvoiceLen := len(ficcInvoiceIds)
  209. raiInvoiceLen := len(raiInvoiceIds)
  210. ficcPaymentLen := len(ficcPaymentIds)
  211. raiPaymentLen := len(raiPaymentIds)
  212. // 汇总数据
  213. nowTime := time.Now().Local()
  214. summaryList := make([]*fms.InvoicePaymentSummary, 0)
  215. // ficc
  216. if ficcInvoiceLen >= ficcPaymentLen {
  217. for i := range ficcInvoiceIds {
  218. v := new(fms.InvoicePaymentSummary)
  219. v.RegisterId = registerId
  220. v.InvoiceId = ficcInvoiceIds[i]
  221. v.ServiceProductId = crm.CompanyProductFicc
  222. v.CreateTime = nowTime
  223. v.ModifyTime = nowTime
  224. // 取对应key的到款ID
  225. if i+1 <= ficcPaymentLen {
  226. v.PaymentId = ficcPaymentIds[i]
  227. }
  228. summaryList = append(summaryList, v)
  229. }
  230. }
  231. if ficcPaymentLen > ficcInvoiceLen {
  232. for i := range ficcPaymentIds {
  233. v := new(fms.InvoicePaymentSummary)
  234. v.RegisterId = registerId
  235. v.PaymentId = ficcPaymentIds[i]
  236. v.ServiceProductId = crm.CompanyProductFicc
  237. v.CreateTime = nowTime
  238. v.ModifyTime = nowTime
  239. // 取对应key的开票ID
  240. if i+1 <= ficcInvoiceLen {
  241. v.InvoiceId = ficcInvoiceIds[i]
  242. }
  243. summaryList = append(summaryList, v)
  244. }
  245. }
  246. // rai
  247. if raiInvoiceLen >= raiPaymentLen {
  248. for i := range raiInvoiceIds {
  249. v := new(fms.InvoicePaymentSummary)
  250. v.RegisterId = registerId
  251. v.InvoiceId = raiInvoiceIds[i]
  252. v.ServiceProductId = crm.CompanyProductRai
  253. v.CreateTime = nowTime
  254. v.ModifyTime = nowTime
  255. // 取对应key的到款ID
  256. if i+1 <= raiPaymentLen {
  257. v.PaymentId = raiPaymentIds[i]
  258. }
  259. summaryList = append(summaryList, v)
  260. }
  261. }
  262. if raiPaymentLen > raiInvoiceLen {
  263. for i := range raiPaymentIds {
  264. v := new(fms.InvoicePaymentSummary)
  265. v.RegisterId = registerId
  266. v.PaymentId = raiPaymentIds[i]
  267. v.ServiceProductId = crm.CompanyProductRai
  268. v.CreateTime = nowTime
  269. v.ModifyTime = nowTime
  270. // 取对应key的开票ID
  271. if i+1 <= raiInvoiceLen {
  272. v.InvoiceId = raiInvoiceIds[i]
  273. }
  274. summaryList = append(summaryList, v)
  275. }
  276. }
  277. // 删除并新增汇总数据
  278. summaryOB := new(fms.InvoicePaymentSummary)
  279. if e = summaryOB.DeleteAndCreate(registerId, summaryList); e != nil {
  280. err = fmt.Errorf("新增汇总数据失败, Err: %s", e.Error())
  281. }
  282. return
  283. }
  284. // CalculatePaymentServiceAmount
  285. func CalculatePaymentServiceAmount(registerId int) (err error) {
  286. // todo 判断是否符合均分金额的条件,如果符合,需要生成金额分配记录表
  287. // 查询所有的到款记录,如果没有到款记录则无需分配到款金额
  288. cond := `contract_register_id = ? and invoice_type=2 and service_product_id=2`
  289. pars := make([]interface{}, 0)
  290. pars = append(pars, registerId)
  291. paymentList, err := fms.GetContractInvoiceItemList(cond, pars)
  292. if err != nil {
  293. err = fmt.Errorf("获取开票到款列表失败, Err: %s", err.Error())
  294. return
  295. }
  296. if len(paymentList) == 0 {
  297. return
  298. }
  299. var contractPaymentIds []int
  300. for _, v := range paymentList {
  301. contractPaymentIds = append(contractPaymentIds, v.ContractInvoiceId)
  302. }
  303. // 获取服务套餐
  304. servicesList, err := fms.GetContractServiceAndDetailList(registerId)
  305. if err != nil {
  306. err = fmt.Errorf("获取服务套餐失败, Err: " + err.Error())
  307. return
  308. }
  309. industrySlice := []string{
  310. "行业套餐",
  311. "医药",
  312. "消费",
  313. "科技",
  314. "智造",
  315. "策略",
  316. "主观",
  317. "客观",
  318. }
  319. unNeedAsign := false
  320. industryFlag := false
  321. servicesListMap := make(map[int]*fms.ContractServiceAndDetail)
  322. // 判断当前套餐是否符合分配规则
  323. for _, v := range servicesList {
  324. if v.ProductId == crm.CompanyProductRai {
  325. industryFlag = true
  326. }
  327. servicesListMap[v.ServiceTemplateId] = v
  328. if v.ProductId == crm.CompanyProductRai && !utils.InArray(v.Title, industrySlice) {
  329. unNeedAsign = true
  330. }
  331. }
  332. if unNeedAsign || !industryFlag {
  333. //不符合自动分配规则,则直接删除之前的自动分配记录
  334. err = fms.DeletePaymentServiceAmountByRegisterId(registerId)
  335. if err != nil {
  336. return
  337. }
  338. return
  339. } else {
  340. //遍历所有的到款记录,筛选出需要删除并新增的到款记录ID
  341. _, serviceFormatMap, e := GetContractServiceNameFormat([]int{registerId})
  342. if e != nil {
  343. err = fmt.Errorf("获取合同套餐失败, Err: %s", e.Error())
  344. return
  345. }
  346. servicesFormatList, ok := serviceFormatMap[registerId]
  347. if !ok {
  348. err = fmt.Errorf("查不到合同套餐")
  349. return
  350. }
  351. servicesFormatStr := ""
  352. length := 0
  353. for _, v := range servicesFormatList {
  354. if servicesListMap[v.ServiceTemplateId].ProductId == crm.CompanyProductRai {
  355. length++
  356. servicesFormatStr += fmt.Sprintf("%d_", v.ServiceTemplateId)
  357. }
  358. }
  359. if length == 0 {
  360. return
  361. }
  362. payAverageAmountMap := make(map[int]float64)
  363. for _, v := range paymentList {
  364. payAverageAmountMap[v.ContractInvoiceId] = v.Amount / float64(length)
  365. payAverageAmountMap[v.ContractInvoiceId], _ = strconv.ParseFloat(fmt.Sprintf("%.2f", payAverageAmountMap[v.ContractInvoiceId]), 64)
  366. }
  367. //查询原来的金额分配记录
  368. serviceAmountCond := `contract_payment_id = ? and contract_payment_id in ?`
  369. serviceAmountPars := make([]interface{}, 0)
  370. serviceAmountPars = append(serviceAmountPars, registerId, contractPaymentIds)
  371. serviceAmountOB := new(fms.ContractPaymentServiceAmount)
  372. serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
  373. if e != nil {
  374. err = fmt.Errorf("获取到款套餐分配列表失败, Err: %s", e.Error())
  375. return
  376. }
  377. oldPaymentServiceMap := make(map[int]string)
  378. oldPaymentAmountMap := make(map[int]float64)
  379. for _, v := range serviceAmountList {
  380. oldPaymentServiceMap[v.ContractPaymentId] += fmt.Sprintf("%d_", v.ServiceTemplateId)
  381. oldPaymentAmountMap[v.ContractPaymentId] = v.Amount
  382. }
  383. var newPaymentIds []int
  384. for _, v := range paymentList {
  385. if _, ok1 := oldPaymentServiceMap[v.ContractInvoiceId]; !ok1 {
  386. newPaymentIds = append(newPaymentIds, v.ContractInvoiceId)
  387. continue
  388. }
  389. if oldPaymentServiceMap[v.ContractInvoiceId] != servicesFormatStr {
  390. newPaymentIds = append(newPaymentIds, v.ContractInvoiceId)
  391. continue
  392. }
  393. if oldPaymentAmountMap[v.ContractInvoiceId] != payAverageAmountMap[v.ContractInvoiceId] {
  394. newPaymentIds = append(newPaymentIds, v.ContractInvoiceId)
  395. continue
  396. }
  397. }
  398. // 删除旧的金额分配记录并生成新的自动分配记录
  399. if len(newPaymentIds) == 0 {
  400. return
  401. }
  402. for _, v := range newPaymentIds {
  403. addList := make([]*fms.ContractPaymentServiceAmount, 0)
  404. for _, f := range servicesFormatList {
  405. if servicesListMap[f.ServiceTemplateId].ProductId == crm.CompanyProductRai {
  406. t := &fms.ContractPaymentServiceAmount{
  407. ContractRegisterId: registerId,
  408. ContractPaymentId: v,
  409. ServiceTemplatePid: f.ServiceTemplatePid,
  410. ServiceTemplateId: f.ServiceTemplateId,
  411. Amount: payAverageAmountMap[v],
  412. InitType: 1,
  413. }
  414. t.Set()
  415. addList = append(addList, t)
  416. }
  417. }
  418. if e := fms.CreatePaymentServiceAmount(registerId, v, addList); e != nil {
  419. err = fmt.Errorf("新增到款套餐金额失败, Err: " + e.Error())
  420. return
  421. }
  422. }
  423. }
  424. return
  425. }
  426. // SummaryInvoicePaymentByPreRegister 预登记时汇总
  427. func SummaryInvoicePaymentByPreRegister(registerId int) {
  428. var err error
  429. defer func() {
  430. if err != nil {
  431. alarm_msg.SendAlarmMsg(fmt.Sprintf("汇总开票到款失败, ErrMsg: \n%s", err.Error()), 3)
  432. }
  433. }()
  434. // 获取开票到款信息
  435. cond := `contract_register_id = ?`
  436. pars := make([]interface{}, 0)
  437. pars = append(pars, registerId)
  438. list, e := fms.GetContractPreRegisterItemList(cond, pars)
  439. if e != nil {
  440. err = fmt.Errorf("获取开票到款列表失败, Err: %s", e.Error())
  441. return
  442. }
  443. ficcInvoiceIds := make([]int, 0)
  444. ficcPaymentIds := make([]int, 0)
  445. raiInvoiceIds := make([]int, 0)
  446. raiPaymentIds := make([]int, 0)
  447. for i := range list {
  448. if list[i].InvoiceType == fms.ContractInvoiceTypePreMake {
  449. if list[i].ServiceProductId == crm.CompanyProductFicc {
  450. ficcInvoiceIds = append(ficcInvoiceIds, list[i].PreRegisterId)
  451. } else if list[i].ServiceProductId == crm.CompanyProductRai {
  452. raiInvoiceIds = append(raiInvoiceIds, list[i].PreRegisterId)
  453. }
  454. continue
  455. }
  456. if list[i].InvoiceType == fms.ContractInvoiceTypePrePay {
  457. if list[i].ServiceProductId == crm.CompanyProductFicc {
  458. ficcPaymentIds = append(ficcPaymentIds, list[i].PreRegisterId)
  459. } else if list[i].ServiceProductId == crm.CompanyProductRai {
  460. raiPaymentIds = append(raiPaymentIds, list[i].PreRegisterId)
  461. }
  462. continue
  463. }
  464. }
  465. ficcInvoiceLen := len(ficcInvoiceIds)
  466. raiInvoiceLen := len(raiInvoiceIds)
  467. ficcPaymentLen := len(ficcPaymentIds)
  468. raiPaymentLen := len(raiPaymentIds)
  469. // 汇总数据
  470. nowTime := time.Now().Local()
  471. summaryList := make([]*fms.InvoicePaymentSummary, 0)
  472. // ficc
  473. if ficcInvoiceLen >= ficcPaymentLen {
  474. for i := range ficcInvoiceIds {
  475. v := new(fms.InvoicePaymentSummary)
  476. v.RegisterId = registerId
  477. v.InvoiceId = ficcInvoiceIds[i]
  478. v.ServiceProductId = crm.CompanyProductFicc
  479. v.CreateTime = nowTime
  480. v.ModifyTime = nowTime
  481. // 取对应key的到款ID
  482. if i+1 <= ficcPaymentLen {
  483. v.PaymentId = ficcPaymentIds[i]
  484. }
  485. summaryList = append(summaryList, v)
  486. }
  487. }
  488. if ficcPaymentLen > ficcInvoiceLen {
  489. for i := range ficcPaymentIds {
  490. v := new(fms.InvoicePaymentSummary)
  491. v.RegisterId = registerId
  492. v.PaymentId = ficcPaymentIds[i]
  493. v.ServiceProductId = crm.CompanyProductFicc
  494. v.CreateTime = nowTime
  495. v.ModifyTime = nowTime
  496. // 取对应key的开票ID
  497. if i+1 <= ficcInvoiceLen {
  498. v.InvoiceId = ficcInvoiceIds[i]
  499. }
  500. summaryList = append(summaryList, v)
  501. }
  502. }
  503. // rai
  504. if raiInvoiceLen >= raiPaymentLen {
  505. for i := range raiInvoiceIds {
  506. v := new(fms.InvoicePaymentSummary)
  507. v.RegisterId = registerId
  508. v.InvoiceId = raiInvoiceIds[i]
  509. v.ServiceProductId = crm.CompanyProductRai
  510. v.CreateTime = nowTime
  511. v.ModifyTime = nowTime
  512. // 取对应key的到款ID
  513. if i+1 <= raiPaymentLen {
  514. v.PaymentId = raiPaymentIds[i]
  515. }
  516. summaryList = append(summaryList, v)
  517. }
  518. }
  519. if raiPaymentLen > raiInvoiceLen {
  520. for i := range raiPaymentIds {
  521. v := new(fms.InvoicePaymentSummary)
  522. v.RegisterId = registerId
  523. v.PaymentId = raiPaymentIds[i]
  524. v.ServiceProductId = crm.CompanyProductRai
  525. v.CreateTime = nowTime
  526. v.ModifyTime = nowTime
  527. // 取对应key的开票ID
  528. if i+1 <= raiInvoiceLen {
  529. v.InvoiceId = raiInvoiceIds[i]
  530. }
  531. summaryList = append(summaryList, v)
  532. }
  533. }
  534. // 删除并新增汇总数据
  535. summaryOB := new(fms.InvoicePaymentSummary)
  536. if e = summaryOB.DeleteAndCreate(registerId, summaryList); e != nil {
  537. err = fmt.Errorf("新增汇总数据失败, Err: %s", e.Error())
  538. }
  539. return
  540. }