fix: 预结算列表按付款申请关联行归集预付申请与已付金额
多选预结算单时付款申请主表只存第一张单,原汇总逻辑按主表 pre_settlement_id 归集, 导致第二张及之后单据的预付申请金额恒为 0。改为按 blade_payment_application_settlement 关联行逐单归集,已付金额按关联行顺序分摊,与金蝶预付建档口径一致;无关联行的历史单据仍按主表兜底。
This commit is contained in:
+49
-13
@@ -44,6 +44,7 @@ import org.springblade.transport.mapper.PreSettlementDetailMapper;
|
||||
import org.springblade.transport.mapper.PreSettlementMapper;
|
||||
import org.springblade.transport.mapper.PreSettlementSummaryFeeMapper;
|
||||
import org.springblade.transport.mapper.PaymentApplicationMapper;
|
||||
import org.springblade.transport.mapper.PaymentApplicationSettlementMapper;
|
||||
import org.springblade.transport.mapper.ReceivablePayableCargoFeeMapper;
|
||||
import org.springblade.transport.mapper.ReceivablePayableDetailMapper;
|
||||
import org.springblade.transport.pojo.dto.PreSettlementAdvanceRequest;
|
||||
@@ -59,6 +60,7 @@ import org.springblade.transport.pojo.entity.PreSettlementDetail;
|
||||
import org.springblade.transport.pojo.entity.PreSettlementDetailFee;
|
||||
import org.springblade.transport.pojo.entity.PreSettlementSummaryFee;
|
||||
import org.springblade.transport.pojo.entity.PaymentApplication;
|
||||
import org.springblade.transport.pojo.entity.PaymentApplicationSettlement;
|
||||
import org.springblade.transport.pojo.entity.ProjectApply;
|
||||
import org.springblade.transport.pojo.entity.ReceivablePayableCargoFee;
|
||||
import org.springblade.transport.pojo.entity.ReceivablePayableDetail;
|
||||
@@ -118,6 +120,7 @@ public class PreSettlementServiceImpl extends BaseServiceImpl<PreSettlementMappe
|
||||
private final PreSettlementSummaryFeeMapper summaryFeeMapper;
|
||||
private final PreSettlementAdvanceMapper advanceMapper;
|
||||
private final PaymentApplicationMapper paymentApplicationMapper;
|
||||
private final PaymentApplicationSettlementMapper settlementRelationMapper;
|
||||
private final PreSettlementChangeRecordMapper changeRecordMapper;
|
||||
private final ReceivablePayableDetailMapper sourceDetailMapper;
|
||||
private final ReceivablePayableCargoFeeMapper sourceFeeMapper;
|
||||
@@ -140,21 +143,54 @@ public class PreSettlementServiceImpl extends BaseServiceImpl<PreSettlementMappe
|
||||
Set<Long> preSettlementIds = records.stream().map(PreSettlementVO::getId)
|
||||
.filter(Objects::nonNull).collect(Collectors.toSet());
|
||||
if (preSettlementIds.isEmpty()) return;
|
||||
List<PaymentApplication> applications = paymentApplicationMapper.selectList(
|
||||
Wrappers.<PaymentApplication>lambdaQuery()
|
||||
.select(PaymentApplication::getPreSettlementId, PaymentApplication::getAppliedAmount,
|
||||
PaymentApplication::getPaidAmount)
|
||||
.eq(PaymentApplication::getPaymentType, "progress_advance")
|
||||
.in(PaymentApplication::getPreSettlementId, preSettlementIds)
|
||||
.eq(PaymentApplication::getApprovalStatus, STATUS_APPROVED)
|
||||
.eq(PaymentApplication::getIsDeleted, 0));
|
||||
Map<Long, BigDecimal> appliedAmounts = new HashMap<>();
|
||||
Map<Long, BigDecimal> paidAmounts = new HashMap<>();
|
||||
applications.forEach(application -> {
|
||||
Long preSettlementId = application.getPreSettlementId();
|
||||
appliedAmounts.merge(preSettlementId, money(application.getAppliedAmount()), BigDecimal::add);
|
||||
paidAmounts.merge(preSettlementId, money(application.getPaidAmount()), BigDecimal::add);
|
||||
});
|
||||
// 多选预结算单时付款申请主表只存第一张,按关联行逐单归集;已付金额按关联行顺序分摊实付金额,
|
||||
// 与同步金蝶预付建档(syncPreSettlementAdvance)同一口径,避免第二张及之后的单据金额恒为0
|
||||
List<PaymentApplicationSettlement> relations = settlementRelationMapper.selectList(
|
||||
Wrappers.<PaymentApplicationSettlement>lambdaQuery()
|
||||
.in(PaymentApplicationSettlement::getPreSettlementId, preSettlementIds)
|
||||
.eq(PaymentApplicationSettlement::getIsDeleted, 0)
|
||||
.orderByAsc(PaymentApplicationSettlement::getId));
|
||||
Set<Long> relationPaymentIds = relations.stream()
|
||||
.map(PaymentApplicationSettlement::getPaymentApplicationId).filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
Map<Long, PaymentApplication> relationPayments = relationPaymentIds.isEmpty() ? new HashMap<>()
|
||||
: paymentApplicationMapper.selectBatchIds(relationPaymentIds).stream()
|
||||
.filter(application -> !Objects.equals(application.getIsDeleted(), 1))
|
||||
.filter(application -> TransportBusiConstant.APPROVED.equals(application.getApprovalStatus()))
|
||||
.filter(application -> "progress_advance".equals(application.getPaymentType()))
|
||||
.collect(Collectors.toMap(PaymentApplication::getId, Function.identity(), (left, right) -> left));
|
||||
relations.stream()
|
||||
.filter(relation -> relationPayments.containsKey(relation.getPaymentApplicationId()))
|
||||
.collect(Collectors.groupingBy(PaymentApplicationSettlement::getPaymentApplicationId,
|
||||
LinkedHashMap::new, Collectors.toList()))
|
||||
.forEach((paymentId, paymentRelations) -> {
|
||||
BigDecimal remainingPaid = money(relationPayments.get(paymentId).getPaidAmount());
|
||||
for (int index = 0; index < paymentRelations.size(); index++) {
|
||||
PaymentApplicationSettlement relation = paymentRelations.get(index);
|
||||
BigDecimal applied = money(relation.getAppliedAmount());
|
||||
BigDecimal paid = index == paymentRelations.size() - 1 ? remainingPaid
|
||||
: applied.min(remainingPaid);
|
||||
remainingPaid = remainingPaid.subtract(paid);
|
||||
appliedAmounts.merge(relation.getPreSettlementId(), applied, BigDecimal::add);
|
||||
paidAmounts.merge(relation.getPreSettlementId(), paid, BigDecimal::add);
|
||||
}
|
||||
});
|
||||
// 历史仅写主表、无关联行的付款申请按主表兜底
|
||||
paymentApplicationMapper.selectList(
|
||||
Wrappers.<PaymentApplication>lambdaQuery()
|
||||
.select(PaymentApplication::getId, PaymentApplication::getPreSettlementId,
|
||||
PaymentApplication::getAppliedAmount, PaymentApplication::getPaidAmount)
|
||||
.in(PaymentApplication::getPreSettlementId, preSettlementIds)
|
||||
.eq(PaymentApplication::getPaymentType, "progress_advance")
|
||||
.eq(PaymentApplication::getApprovalStatus, STATUS_APPROVED)
|
||||
.eq(PaymentApplication::getIsDeleted, 0)
|
||||
.notIn(!relationPaymentIds.isEmpty(), PaymentApplication::getId, relationPaymentIds))
|
||||
.forEach(application -> {
|
||||
appliedAmounts.merge(application.getPreSettlementId(), money(application.getAppliedAmount()), BigDecimal::add);
|
||||
paidAmounts.merge(application.getPreSettlementId(), money(application.getPaidAmount()), BigDecimal::add);
|
||||
});
|
||||
records.forEach(record -> {
|
||||
record.setAdvanceAppliedAmount(money(appliedAmounts.get(record.getId())));
|
||||
record.setAdvancePaidAmount(money(paidAmounts.get(record.getId())));
|
||||
|
||||
Reference in New Issue
Block a user