修复应收应付明细单价生成不对的问题

This commit is contained in:
2026-08-18 21:48:39 +08:00
parent 154cc4a7cb
commit 83fb67a142
@@ -357,11 +357,13 @@ public class ReceivablePayableDetailServiceImpl
try {
String planId = matchedPlanId(waybill, contract);
if (Func.isEmpty(planId)) continue;
// 自动生成只按合同计费方案计算,matchOnly=true 禁止回退读取运单自身其他费用。
List<ReceivablePayableCargoFee> matchedFees = calculatedFees(waybill, contract, planId, true);
if (matchedFees.isEmpty()) continue;
BigDecimal contractUnitPrice = resolveContractUnitPrice(matchedFees);
for (String settlementType : List.of("payable", "receivable")) {
if (existsByWaybill(waybill.getId(), settlementType)) continue;
ReceivablePayableDetail detail = buildDetail(waybill, contract, settlementType, matchedFees);
ReceivablePayableDetail detail = buildDetail(waybill, contract, settlementType, matchedFees, contractUnitPrice);
save(detail);
for (ReceivablePayableCargoFee fee : matchedFees) {
ReceivablePayableCargoFee copy = BeanUtil.copyProperties(fee, ReceivablePayableCargoFee.class);
@@ -510,6 +512,11 @@ public class ReceivablePayableDetailServiceImpl
}
private ReceivablePayableDetail buildDetail(Waybill waybill, ContractManage contract, String settlementType, List<ReceivablePayableCargoFee> fees) {
return buildDetail(waybill, contract, settlementType, fees, resolveContractUnitPrice(fees));
}
private ReceivablePayableDetail buildDetail(Waybill waybill, ContractManage contract, String settlementType,
List<ReceivablePayableCargoFee> fees, BigDecimal unitPrice) {
BigDecimal freight = fees.stream().filter(this::isFreight).map(ReceivablePayableCargoFee::getAfterAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal total = fees.stream().map(ReceivablePayableCargoFee::getAfterAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal other = total.subtract(freight).setScale(2, RoundingMode.HALF_UP);
@@ -538,7 +545,7 @@ public class ReceivablePayableDetailServiceImpl
detail.setQuantityUnit(waybill.getQuantityUnit());
detail.setMileage(waybill.getMileage());
detail.setBatchNo(waybill.getBatchNo());
detail.setUnitPrice(waybill.getUnitPrice());
detail.setUnitPrice(unitPrice);
detail.setCurrency("RMB");
detail.setFreightAmount(freight);
detail.setOtherFeeAmount(other);
@@ -549,6 +556,19 @@ public class ReceivablePayableDetailServiceImpl
return detail;
}
private BigDecimal resolveContractUnitPrice(List<ReceivablePayableCargoFee> fees) {
return fees.stream()
.filter(this::isFreight)
.map(ReceivablePayableCargoFee::getUnitPrice)
.filter(Objects::nonNull)
.findFirst()
.orElseGet(() -> fees.stream()
.map(ReceivablePayableCargoFee::getUnitPrice)
.filter(Objects::nonNull)
.findFirst()
.orElse(BigDecimal.ZERO));
}
private ReceivablePayableCargoFee buildCargoFee(Long detailId, Waybill waybill) {
BigDecimal quantity = money(waybill.getQuantity());
BigDecimal unitPrice = money(waybill.getUnitPrice());
@@ -807,6 +827,7 @@ public class ReceivablePayableDetailServiceImpl
detail.setFreightAmount(freight);
detail.setOtherFeeAmount(total.subtract(freight));
detail.setTotalAmount(total);
detail.setUnitPrice(resolveContractUnitPrice(fees));
detail.setFeeItemsJson(JsonUtil.toJson(fees.stream().collect(HashMap<String, BigDecimal>::new, (map, fee) -> map.put(fee.getCargoName(), fee.getAfterAmount()), HashMap::putAll)));
updateById(detail);
}