Quellcode durchsuchen

Merge remote-tracking branch 'origin/dev1.0' into dev1.0

liutao vor 8 Monaten
Ursprung
Commit
ce68c1601b

+ 1 - 15
src/main/java/com/sunxung/factoring/entity/ledger/LedgerFundGoodsAssign.java

@@ -133,7 +133,7 @@ public class LedgerFundGoodsAssign extends BaseEntity {
     private BigDecimal financingPrincipal;
 
     /**
-     * 是否分配融资利息
+     * 是否分配融资利息和管理费
      */
     @TableField("has_assign_interest")
     private Boolean hasAssignInterest;
@@ -144,12 +144,6 @@ public class LedgerFundGoodsAssign extends BaseEntity {
     @TableField("financing_interest")
     private BigDecimal financingInterest;
 
-    /**
-     * 是否分配管理费
-     */
-    @TableField("has_assign_management_fee")
-    private Boolean hasAssignManagementFee;
-
     /**
      * 融资管理费(2023.3.21新增)
      */
@@ -619,14 +613,6 @@ public class LedgerFundGoodsAssign extends BaseEntity {
         this.hasAssignInterest = hasAssignInterest;
     }
 
-    public Boolean getHasAssignManagementFee() {
-        return hasAssignManagementFee;
-    }
-
-    public void setHasAssignManagementFee(Boolean hasAssignManagementFee) {
-        this.hasAssignManagementFee = hasAssignManagementFee;
-    }
-
     public Boolean getHasAssignOverdueInterest() {
         return hasAssignOverdueInterest;
     }

+ 103 - 112
src/main/java/com/sunxung/factoring/service/ledger/impl/LedgerManagementServiceImpl.java

@@ -1131,7 +1131,7 @@ public class LedgerManagementServiceImpl implements LedgerManagementService {
                             //1.查看计划是否有逾期利息未分完的,存在先分逾期利息
                             if (goodsAssignVo.getHasAssignOverdueInterest()) {
                                 List<PaymentCollectionPlanFactoring> overdueInterestList = planFactoringList.stream()
-                                        .filter(f -> f.getAmountType().getCode().equals("overdue_interest") && f.getPlanDate().before(ledgerDate))
+                                        .filter(f -> f.getAmountType().getCode().equals("overdue_interest"))
                                         .sorted(Comparator.comparing(PaymentCollectionPlanFactoring::getPlanDate)).collect(Collectors.toList());
                                 if (overdueInterestList != null && overdueInterestList.size() > 0) {
                                     for (PaymentCollectionPlanFactoring factoring : overdueInterestList) {
@@ -1167,8 +1167,16 @@ public class LedgerManagementServiceImpl implements LedgerManagementService {
                             BigDecimal actualManagementTotal = getActualAmountTotal(allPlanFactoringList, managementFeeDict);
                             //4.获取所有的已经收款的利息之和
                             BigDecimal actualInterestTotal = getActualAmountTotal(allPlanFactoringList, interestDict);
+
+                            Boolean assignOverdueInterest = hasAssignOverdueInterest(allPlanFactoringList, goodsAssignVo.getHasAssignOverdueInterest());
                             //5.获取截至到分账日期的新的本金计划
-                            List<PaymentCollectionPlanFactoring> newPrincipalFactoringList = getPrincipalFactoringByLedgerDate(principalFactoringList, ledgerDate, returnedAmount);
+                            List<PaymentCollectionPlanFactoring> newPrincipalFactoringList = new ArrayList<>();
+                            if (assignOverdueInterest) {
+                                //判断是否存在逾期利息不豁免且需要分逾期利息的场景,如果存在分账日期取系统当前日期
+                                newPrincipalFactoringList = getPrincipalFactoringByLedgerDate(principalFactoringList, new Date(), returnedAmount);
+                            } else {
+                                newPrincipalFactoringList = getPrincipalFactoringByLedgerDate(principalFactoringList, ledgerDate, returnedAmount);
+                            }
                             //6.计算截至到分账日期计划应收管理费总和
                             BigDecimal planManagementTotal = getAllocatedAmountByPlanFactoring(newPrincipalFactoringList, managementFeeDict,
                                     goodsAssignVo, paymentCalculateVo);
@@ -1180,87 +1188,12 @@ public class LedgerManagementServiceImpl implements LedgerManagementService {
                             //剩余未分配利息
                             BigDecimal unAssignInterest = BigDecimal.ZERO;
 
-                            //用已收利息之和减去计划应收、判断需不需要退还
-                            refundInterest = actualInterestTotal.subtract(planInterestTotal);
-                            //获取所有的利息收款计划
-                            List<PaymentCollectionPlanFactoring> interestFactoringList = getFactoringByAmountType(allPlanFactoringList, interestDict);
-                            if (refundInterest.compareTo(BigDecimal.ZERO) > 0) {
-                                //说明利息分多了,需要将多的退掉
-//                                ledgerPlanFactoringAssignList.add(addPlanFactoringInsertAssign(ledgerDate, refundInterest.negate(), refundInterest.negate(),
-//                                        IncomeExpenditureTypeDict.ChildEnum.INTEREST.getCode(), goodsAssignVo, 0));
-                                unAssignInterest = refundInterest.negate();
-                                //删除未分配的利息
-                                deleteUnAssignList(interestFactoringList, ledgerPlanFactoringAssignList, goodsAssignVo);
-                                //将部分分配的利息计划金额改为实收金额
-                                updatePartAssignList(interestFactoringList, ledgerPlanFactoringAssignList, goodsAssignVo);
-                            } else if (refundInterest.compareTo(BigDecimal.ZERO) == 0) {
-                                //删除未分配的利息
-                                deleteUnAssignList(interestFactoringList, ledgerPlanFactoringAssignList, goodsAssignVo);
-                                //将部分分配的利息计划金额改为实收金额
-                                updatePartAssignList(interestFactoringList, ledgerPlanFactoringAssignList, goodsAssignVo);
-                            } else if (refundInterest.compareTo(BigDecimal.ZERO) < 0) {
-                                //说明利息还需要补
-                                BigDecimal toAssignInterest = refundInterest.negate();
-                                if (assignAmount.compareTo(BigDecimal.ZERO) > 0) {
-                                    if (assignAmount.compareTo(toAssignInterest) >= 0) {
-                                        //说明本次分的钱可以把管理费分完,增加一条利息分配记录(应分=实分)
-                                        if (goodsAssignVo.getHasAssignInterest()) {
-                                            ledgerPlanFactoringAssignList.add(addPlanFactoringInsertAssign(ledgerDate, toAssignInterest, toAssignInterest,
-                                                    IncomeExpenditureTypeDict.ChildEnum.INTEREST.getCode(), goodsAssignVo, 0));
-                                            financeInterest = financeInterest.add(toAssignInterest);
-                                            assignAmount = assignAmount.subtract(toAssignInterest);
-                                        } else {
-                                            ledgerPlanFactoringAssignList.add(addPlanFactoringInsertAssign(ledgerDate, toAssignInterest, BigDecimal.ZERO,
-                                                    IncomeExpenditureTypeDict.ChildEnum.INTEREST.getCode(), goodsAssignVo, 0));
-                                        }
-                                    } else if (assignAmount.compareTo(toAssignInterest) < 0) {
-                                        //说明本次分的钱把管理费分不完,增加一条利息分配记录(应分>实分)
-                                        if (goodsAssignVo.getHasAssignInterest()) {
-                                            //如果本次选择分利息
-                                            ledgerPlanFactoringAssignList.add(addPlanFactoringInsertAssign(ledgerDate, assignAmount, assignAmount,
-                                                    IncomeExpenditureTypeDict.ChildEnum.INTEREST.getCode(), goodsAssignVo, 0));
-                                            financeInterest = financeInterest.add(assignAmount);
-                                            assignAmount = BigDecimal.ZERO;
-                                        } else {
-                                            //如果选择不分利息
-                                            ledgerPlanFactoringAssignList.add(addPlanFactoringInsertAssign(ledgerDate, assignAmount, BigDecimal.ZERO,
-                                                    IncomeExpenditureTypeDict.ChildEnum.INTEREST.getCode(), goodsAssignVo, 0));
-                                        }
-                                        //剩余未分配利息
-                                        unAssignInterest = toAssignInterest.subtract(assignAmount);
-
-                                        // 获取最后一期利息收款计划
-//                                        PaymentCollectionPlanFactoring interestFactoring = getLastPrincipalFactoring(planFactoringList, interestDict);
-//                                        if (interestFactoring != null) {
-//                                            List<PayCollPlanFactoringAmountAllocation> allocationList = interestFactoring.getAmountAllocationList();
-//                                            BigDecimal allocationAmount = BigDecimal.ZERO;
-//                                            if (CollectionUtil.isNotEmpty(allocationList)) {
-//                                                allocationAmount = allocationList.stream().filter(a -> a.getActualAmount() != null)
-//                                                        .map(PayCollPlanFactoringAmountAllocation::getActualAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
-//                                            }
-//                                            if (interestFactoring.getAmount().compareTo(allocationAmount) > 0) {
-//                                                //说明是未完成的计划
-//                                                interestFactoring.setAmount(unAssignInterest.add(interestFactoring.getAmount()));
-//                                                ledgerPlanFactoringAssignList.add(addPlanFactoringUpdateAssign(interestFactoring.getPlanDate(), BigDecimal.ZERO,
-//                                                        interestFactoring, goodsAssignVo, 0));
-//                                            }
-//                                        } else {
-//                                            ledgerPlanFactoringAssignList.add(addPlanFactoringInsertAssign(ledgerDate, unAssignInterest, BigDecimal.ZERO,
-//                                                    IncomeExpenditureTypeDict.ChildEnum.INTEREST.getCode(), goodsAssignVo, 0));
-//                                        }
-                                    }
-
-                                }
-                            }
-
                             //用已收管理费之和减去计划应收、判断需不需要退还
                             refundManagement = actualManagementTotal.subtract(planManagementTotal);
                             //获取所有的管理费收款计划
                             List<PaymentCollectionPlanFactoring> managementFeeFactoringList = getFactoringByAmountType(allPlanFactoringList, managementFeeDict);
                             if (refundManagement.compareTo(BigDecimal.ZERO) > 0) {
                                 //说明管理费分多了,需要将多的退掉
-//                                ledgerPlanFactoringAssignList.add(addPlanFactoringInsertAssign(ledgerDate, refundManagement.negate(), refundManagement.negate(),
-//                                        IncomeExpenditureTypeDict.ChildEnum.MANAGEMENT_FEE.getCode(), goodsAssignVo, 0));
                                 unAssignManagement = refundManagement.negate();
                                 //删除未分配的管理费
                                 deleteUnAssignList(managementFeeFactoringList, ledgerPlanFactoringAssignList, goodsAssignVo);
@@ -1278,7 +1211,7 @@ public class LedgerManagementServiceImpl implements LedgerManagementService {
                                 if (assignAmount.compareTo(BigDecimal.ZERO) > 0) {
                                     if (assignAmount.compareTo(toAssignManagement) >= 0) {
                                         //说明本次分的钱可以把管理费分完,增加一条管理费分配记录
-                                        if (goodsAssignVo.getHasAssignManagementFee()) {
+                                        if (goodsAssignVo.getHasAssignInterest()) {
                                             //如果本次选择分管理费
                                             ledgerPlanFactoringAssignList.add(addPlanFactoringInsertAssign(ledgerDate, toAssignManagement, toAssignManagement,
                                                     IncomeExpenditureTypeDict.ChildEnum.MANAGEMENT_FEE.getCode(), goodsAssignVo, 0));
@@ -1291,7 +1224,7 @@ public class LedgerManagementServiceImpl implements LedgerManagementService {
                                         }
                                     } else if (assignAmount.compareTo(toAssignManagement) < 0) {
                                         //说明本次分的钱把管理费分不完,增加一条管理费分配记录
-                                        if (goodsAssignVo.getHasAssignManagementFee()) {
+                                        if (goodsAssignVo.getHasAssignInterest()) {
                                             //如果本次选择分管理费
                                             ledgerPlanFactoringAssignList.add(addPlanFactoringInsertAssign(ledgerDate, assignAmount, assignAmount,
                                                     IncomeExpenditureTypeDict.ChildEnum.MANAGEMENT_FEE.getCode(), goodsAssignVo, 0));
@@ -1304,28 +1237,60 @@ public class LedgerManagementServiceImpl implements LedgerManagementService {
                                         }
                                         //剩余未分配管理费
                                         unAssignManagement = toAssignManagement.subtract(assignAmount);
-                                        // 获取最后一期管理费收款计划
-//                                        PaymentCollectionPlanFactoring managementFactoring = getLastPrincipalFactoring(planFactoringList, managementFeeDict);
-//                                        if (managementFactoring != null) {
-//                                            List<PayCollPlanFactoringAmountAllocation> allocationList = managementFactoring.getAmountAllocationList();
-//                                            BigDecimal allocationAmount = BigDecimal.ZERO;
-//                                            if (CollectionUtil.isNotEmpty(allocationList)) {
-//                                                allocationAmount = allocationList.stream().filter(a -> a.getActualAmount() != null)
-//                                                        .map(PayCollPlanFactoringAmountAllocation::getActualAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
-//                                            }
-//                                            if (managementFactoring.getAmount().compareTo(allocationAmount) > 0) {
-//                                                //说明是未完成的计划
-//                                                managementFactoring.setAmount(unAssignManagement.add(managementFactoring.getAmount()));
-//                                                ledgerPlanFactoringAssignList.add(addPlanFactoringUpdateAssign(managementFactoring.getPlanDate(), BigDecimal.ZERO,
-//                                                        managementFactoring, goodsAssignVo, 0));
-//                                            }
-//                                        } else {
-//                                            ledgerPlanFactoringAssignList.add(addPlanFactoringInsertAssign(ledgerDate, unAssignManagement, BigDecimal.ZERO,
-//                                                    IncomeExpenditureTypeDict.ChildEnum.MANAGEMENT_FEE.getCode(), goodsAssignVo, 0));
-//                                        }
                                     }
                                 }
                             }
+
+                            //用已收利息之和减去计划应收、判断需不需要退还
+                            refundInterest = actualInterestTotal.subtract(planInterestTotal);
+                            //获取所有的利息收款计划
+                            List<PaymentCollectionPlanFactoring> interestFactoringList = getFactoringByAmountType(allPlanFactoringList, interestDict);
+                            if (refundInterest.compareTo(BigDecimal.ZERO) > 0) {
+                                //说明利息分多了,需要将多的退掉
+                                unAssignInterest = refundInterest.negate();
+                                //删除未分配的利息
+                                deleteUnAssignList(interestFactoringList, ledgerPlanFactoringAssignList, goodsAssignVo);
+                                //将部分分配的利息计划金额改为实收金额
+                                updatePartAssignList(interestFactoringList, ledgerPlanFactoringAssignList, goodsAssignVo);
+                            } else if (refundInterest.compareTo(BigDecimal.ZERO) == 0) {
+                                //删除未分配的利息
+                                deleteUnAssignList(interestFactoringList, ledgerPlanFactoringAssignList, goodsAssignVo);
+                                //将部分分配的利息计划金额改为实收金额
+                                updatePartAssignList(interestFactoringList, ledgerPlanFactoringAssignList, goodsAssignVo);
+                            } else if (refundInterest.compareTo(BigDecimal.ZERO) < 0) {
+                                //说明利息还需要补
+                                BigDecimal toAssignInterest = refundInterest.negate();
+                                if (assignAmount.compareTo(BigDecimal.ZERO) > 0) {
+                                    if (assignAmount.compareTo(toAssignInterest) >= 0) {
+                                        //说明本次分的钱可以把管理费分完,增加一条利息分配记录(应分=实分)
+                                        if (goodsAssignVo.getHasAssignInterest()) {
+                                            ledgerPlanFactoringAssignList.add(addPlanFactoringInsertAssign(ledgerDate, toAssignInterest, toAssignInterest,
+                                                    IncomeExpenditureTypeDict.ChildEnum.INTEREST.getCode(), goodsAssignVo, 0));
+                                            financeInterest = financeInterest.add(toAssignInterest);
+                                            assignAmount = assignAmount.subtract(toAssignInterest);
+                                        } else {
+                                            ledgerPlanFactoringAssignList.add(addPlanFactoringInsertAssign(ledgerDate, toAssignInterest, BigDecimal.ZERO,
+                                                    IncomeExpenditureTypeDict.ChildEnum.INTEREST.getCode(), goodsAssignVo, 0));
+                                        }
+                                    } else if (assignAmount.compareTo(toAssignInterest) < 0) {
+                                        //说明本次分的钱把管理费分不完,增加一条利息分配记录(应分>实分)
+                                        if (goodsAssignVo.getHasAssignInterest()) {
+                                            //如果本次选择分利息
+                                            ledgerPlanFactoringAssignList.add(addPlanFactoringInsertAssign(ledgerDate, assignAmount, assignAmount,
+                                                    IncomeExpenditureTypeDict.ChildEnum.INTEREST.getCode(), goodsAssignVo, 0));
+                                            financeInterest = financeInterest.add(assignAmount);
+                                            assignAmount = BigDecimal.ZERO;
+                                        } else {
+                                            //如果选择不分利息
+                                            ledgerPlanFactoringAssignList.add(addPlanFactoringInsertAssign(ledgerDate, assignAmount, BigDecimal.ZERO,
+                                                    IncomeExpenditureTypeDict.ChildEnum.INTEREST.getCode(), goodsAssignVo, 0));
+                                        }
+                                        //剩余未分配利息
+                                        unAssignInterest = toAssignInterest.subtract(assignAmount);
+                                    }
+
+                                }
+                            }
                             if (assignAmount.compareTo(BigDecimal.ZERO) > 0) {
                                 //分配完还有钱去分配本金
                                 if (CollectionUtil.isNotEmpty(principalFactoringList)) {
@@ -1354,23 +1319,30 @@ public class LedgerManagementServiceImpl implements LedgerManagementService {
                                                 financePrincipal = financePrincipal.add(planFactoring.getAmount());
                                             } else {
                                                 //说明本金不够分了
-                                                if (ledgerDate.before(planFactoring.getPlanDate())) {
-                                                    //分账日期早于计划日期
-                                                    planFactoring.setPlanDate(ledgerDate);
+                                                //判断本金状态是否逾期,如果逾期,本金不能拆分
+                                                if (PaymentCollectionPlanStatusDict.ChildEnum.OVERDUE.getCode().equals(planFactoring.getStatus().getCode())) {
+                                                    //计划已逾期
+                                                    ledgerPlanFactoringAssignList.add(addPlanFactoringUpdateAssign(ledgerDate, assignAmount,
+                                                            planFactoring, goodsAssignVo, 1));
+                                                } else {
+                                                    if (ledgerDate.before(planFactoring.getPlanDate())) {
+                                                        //分账日期早于计划日期
+                                                        planFactoring.setPlanDate(ledgerDate);
+                                                    }
+                                                    planFactoring.setAmount(assignAmount);
+                                                    planFactoring.setActualCashFlow(assignAmount);
+                                                    //修改一笔本金收款记录
+                                                    ledgerPlanFactoringAssignList.add(addPlanFactoringUpdateAssign(ledgerDate, assignAmount,
+                                                            planFactoring, goodsAssignVo, 1));
+                                                    financePrincipal = financePrincipal.add(assignAmount);
+                                                    Date lastPlanDate = goodsAssignVo.getLastPlanDate();
+                                                    //增加一笔剩余本金收款记录
+                                                    ledgerPlanFactoringAssignList.add(addPlanFactoringInsertAssign(lastPlanDate, returnedAmount.subtract(assignAmount), BigDecimal.ZERO,
+                                                            IncomeExpenditureTypeDict.ChildEnum.PRINCIPAL.getCode(), goodsAssignVo, 1));
                                                 }
-                                                planFactoring.setAmount(assignAmount);
-                                                planFactoring.setActualCashFlow(assignAmount);
-
-                                                //修改一笔本金收款记录
-                                                ledgerPlanFactoringAssignList.add(addPlanFactoringUpdateAssign(ledgerDate, assignAmount,
-                                                        planFactoring, goodsAssignVo, 1));
-                                                financePrincipal = financePrincipal.add(assignAmount);
-                                                Date lastPlanDate = goodsAssignVo.getLastPlanDate();
-                                                //增加一笔剩余本金收款记录
-                                                ledgerPlanFactoringAssignList.add(addPlanFactoringInsertAssign(lastPlanDate, returnedAmount.subtract(assignAmount), BigDecimal.ZERO,
-                                                        IncomeExpenditureTypeDict.ChildEnum.PRINCIPAL.getCode(), goodsAssignVo, 1));
                                                 returnedAmount = returnedAmount.subtract(assignAmount);
                                                 assignAmount = BigDecimal.ZERO;
+                                                flag = true;
                                             }
                                         }
                                     }
@@ -1720,6 +1692,25 @@ public class LedgerManagementServiceImpl implements LedgerManagementService {
         return ledgerAccountInfoVo;
     }
 
+    /**
+     * 判断是否需要分配逾期利息
+     *
+     * @param allPlanFactoringList
+     * @param hasAssignOverdueInterest
+     */
+    private Boolean hasAssignOverdueInterest(List<PaymentCollectionPlanFactoring> allPlanFactoringList, Boolean hasAssignOverdueInterest) {
+        Boolean flag = false;
+        if (CollectionUtil.isNotEmpty(allPlanFactoringList)) {
+            boolean exist = allPlanFactoringList.stream().anyMatch(planFactoring ->
+                    IncomeExpenditureTypeDict.ChildEnum.OVERDUE_INTEREST.getCode().equals(planFactoring.getAmountType().getCode())
+                            && !PaymentCollectionPlanStatusDict.ChildEnum.NOTNEED.getCode().equals(planFactoring.getStatus().getCode()));
+            if (exist && hasAssignOverdueInterest) {
+                flag = true;
+            }
+        }
+        return flag;
+    }
+
     /**
      * 收放款计划校验
      *