demand: TMS系统 - 客户账单管理 - 财务中心销账审核 -财务审核通过/总部驳回以后更新发票收款状态、已收金额

committer: heyu
This commit is contained in:
aike 2025-09-22 10:15:33 +08:00
parent 846c5e0e5b
commit d7946de5ce

View File

@ -69,6 +69,9 @@ public class EmisWriteoffApplyServiceImpl extends EmisBaseService implements IEm
@Autowired
private IEmisCreditSalesmenService emisCreditSalesmenService;
@Autowired
private IEmisSettleInvoiceRecordService emisSettleInvoiceRecordService;
/**
* 查询销账申请
*
@ -134,11 +137,11 @@ public class EmisWriteoffApplyServiceImpl extends EmisBaseService implements IEm
// 验证:申请金额 + 已收金额 <= 运单应收金额
BigDecimal totalAmount = detail.getApplyMoney().add(receivedAmount);
// if (totalAmount.compareTo(detail.getEmisWaybill().getFreight()) > 0) {
// throw new EmisBizError("运单号 " + detail.getBillCode() + " 的申请金额(" +
// detail.getApplyMoney() + ") + 已收金额(" + receivedAmount + ") = " +
// totalAmount + " 超过运单应收金额(" + detail.getEmisWaybill().getFreight() + ")");
// }
// if (totalAmount.compareTo(detail.getEmisWaybill().getFreight()) > 0) {
// throw new EmisBizError("运单号 " + detail.getBillCode() + " 的申请金额(" +
// detail.getApplyMoney() + ") + 已收金额(" + receivedAmount + ") = " +
// totalAmount + " 超过运单应收金额(" + detail.getEmisWaybill().getFreight() + ")");
// }
}
// 验证总销账金额
@ -349,6 +352,9 @@ public class EmisWriteoffApplyServiceImpl extends EmisBaseService implements IEm
// 重算并更新月结客户额度和现金客户额度
updateCreditLimits(existingApply, detailList);
// 更新相关开票记录的收款金额和付款状态
updateInvoiceRecordPaymentStatus(existingApply, detailList, true);
// 所有业务数据更新成功后,再更新审核状态
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
emisWriteoffApply.setBlCenterConfirmed(WriteoffApplyStatus.CENTER_APPROVED.statusCode);
@ -491,6 +497,9 @@ public class EmisWriteoffApplyServiceImpl extends EmisBaseService implements IEm
// 重算并更新月结客户额度和现金客户额度
updateCreditLimits(existingApply, detailList);
// 更新相关开票记录的收款金额和付款状态(回退)
updateInvoiceRecordPaymentStatus(existingApply, detailList, false);
// 所有业务数据回退成功后,再更新审核状态
SysUser currentUser = SecurityUtils.getLoginUser().getUser();
emisWriteoffApply.setBlHeadquartersConfirmed(WriteoffApplyStatus.HEADQUARTERS_REJECTED.statusCode);
@ -683,10 +692,11 @@ public class EmisWriteoffApplyServiceImpl extends EmisBaseService implements IEm
: BigDecimal.ZERO; // 已收金额
// 验证:申请金额 + 已收金额 <= 运单金额
// BigDecimal totalAmount = applyAmount.add(receivedAmount);
// if (totalAmount.compareTo(waybillAmount) > 0) {
// throw new EmisBizError("申请金额+已收金额(" + totalAmount + ")超过运单应收金额(" + waybillAmount + ")");
// }
// BigDecimal totalAmount = applyAmount.add(receivedAmount);
// if (totalAmount.compareTo(waybillAmount) > 0) {
// throw new EmisBizError("申请金额+已收金额(" + totalAmount + ")超过运单应收金额(" +
// waybillAmount + ")");
// }
// 直接使用明细中的子账单信息,无需额外查询
if (detail.getEmisSettleSubBill() == null) {
@ -1121,7 +1131,7 @@ public class EmisWriteoffApplyServiceImpl extends EmisBaseService implements IEm
*/
private void sendWriteoffApplyNotification(EmisWriteoffApply apply, String actionType, String rejectReason) {
try {
if (apply == null || StringUtils.isEmpty(apply.getApplyManCode())) {
if (apply == null || StringUtil.isBlank(apply.getApplyManCode())) {
log.warn("销账申请或申请人代码为空,无法发送通知");
return;
}
@ -1280,4 +1290,108 @@ public class EmisWriteoffApplyServiceImpl extends EmisBaseService implements IEm
// 不抛出异常,避免影响主流程
}
}
/**
* 更新开票记录的收款金额和付款状态
*
* @param apply 销账申请
* @param detailList 申请明细列表
* @param isPass 是否审核通过
*/
private void updateInvoiceRecordPaymentStatus(EmisWriteoffApply apply, List<EmisWriteoffApplyDetail> detailList,
boolean isPass) {
try {
if (CollectionUtils.isEmpty(detailList)) {
log.warn("申请明细为空,无法更新开票记录付款状态,申请ID: {}", apply.getId());
return;
}
log.info("开始更新开票记录付款状态,申请ID: {}, 是否通过: {}", apply.getId(), isPass);
// 按账单号分组处理,避免重复更新同一账单
Map<String, List<EmisWriteoffApplyDetail>> billGroups = detailList.stream()
.filter(detail -> StringUtil.isNotBlank(detail.getSettleBillNo()))
.collect(java.util.stream.Collectors.groupingBy(EmisWriteoffApplyDetail::getSettleBillNo));
for (Map.Entry<String, List<EmisWriteoffApplyDetail>> entry : billGroups.entrySet()) {
String settleBillNo = entry.getKey();
List<EmisWriteoffApplyDetail> billDetails = entry.getValue();
try {
// 计算该账单的总申请金额
BigDecimal totalApplyMoney = billDetails.stream()
.map(detail -> detail.getApplyMoney() != null ? detail.getApplyMoney() : BigDecimal.ZERO)
.reduce(BigDecimal.ZERO, BigDecimal::add);
// 获取账单的当前已收金额
EmisWriteoffApplyDetail firstDetail = billDetails.get(0);
BigDecimal currentRecedMoney = BigDecimal.ZERO;
if (firstDetail.getEmisSettleBill() != null
&& firstDetail.getEmisSettleBill().getRecedMoney() != null) {
currentRecedMoney = firstDetail.getEmisSettleBill().getRecedMoney();
}
// 计算新的已收金额
BigDecimal newRecedMoney;
if (isPass) {
// 审核通过:增加申请金额
newRecedMoney = currentRecedMoney.add(totalApplyMoney);
} else {
// 审核驳回:减少申请金额
newRecedMoney = currentRecedMoney.subtract(totalApplyMoney);
if (newRecedMoney.compareTo(BigDecimal.ZERO) < 0) {
newRecedMoney = BigDecimal.ZERO;
}
}
// 计算付款状态
BigDecimal recMoney = BigDecimal.ZERO;
if (firstDetail.getEmisSettleBill() != null
&& firstDetail.getEmisSettleBill().getRecMoney() != null) {
recMoney = firstDetail.getEmisSettleBill().getRecMoney();
}
String paymentStatus;
if (newRecedMoney.compareTo(BigDecimal.ZERO) == 0) {
paymentStatus = "0"; // 未付款
} else if (newRecedMoney.compareTo(recMoney) >= 0) {
paymentStatus = "1"; // 已付款
} else {
paymentStatus = "2"; // 部分付款
}
// 查询该账单号对应的开票记录
List<EmisSettleInvoiceRecord> invoiceRecords = emisSettleInvoiceRecordService
.selectEmisSettleInvoiceRecordList(new EmisSettleInvoiceRecord() {
{
setSettleBillNo(settleBillNo);
}
});
// 更新每个开票记录
int updateCount = 0;
for (EmisSettleInvoiceRecord invoiceRecord : invoiceRecords) {
invoiceRecord.setRecedMoney(newRecedMoney);
invoiceRecord.setPaymentStatus(paymentStatus);
int result = emisSettleInvoiceRecordService.updateEmisSettleInvoiceRecord(invoiceRecord);
if (result > 0) {
updateCount++;
}
}
log.info("开票记录付款状态更新成功,账单号: {}, 更新记录数: {}, 新已收金额: {}, 付款状态: {}, 申请金额: {}",
settleBillNo, updateCount, newRecedMoney, paymentStatus, totalApplyMoney);
} catch (Exception e) {
log.error("更新开票记录付款状态失败,账单号: {}", settleBillNo, e);
// 继续处理其他账单,不抛出异常
}
}
log.info("开票记录付款状态更新完成,申请ID: {}", apply.getId());
} catch (Exception e) {
log.error("更新开票记录付款状态失败,申请ID: {}", apply.getId(), e);
// 不抛出异常,避免影响主流程
}
}
}