From d7946de5cec8e2cbfbdeb4da288bbbe7c48f203f Mon Sep 17 00:00:00 2001 From: aike <17730485278@139.com> Date: Mon, 22 Sep 2025 10:15:33 +0800 Subject: [PATCH] =?UTF-8?q?demand:=20=20TMS=E7=B3=BB=E7=BB=9F=20-=20?= =?UTF-8?q?=E5=AE=A2=E6=88=B7=E8=B4=A6=E5=8D=95=E7=AE=A1=E7=90=86=20-=20?= =?UTF-8?q?=E8=B4=A2=E5=8A=A1=E4=B8=AD=E5=BF=83=E9=94=80=E8=B4=A6=E5=AE=A1?= =?UTF-8?q?=E6=A0=B8=20-=E8=B4=A2=E5=8A=A1=E5=AE=A1=E6=A0=B8=E9=80=9A?= =?UTF-8?q?=E8=BF=87/=E6=80=BB=E9=83=A8=E9=A9=B3=E5=9B=9E=E4=BB=A5?= =?UTF-8?q?=E5=90=8E=E6=9B=B4=E6=96=B0=E5=8F=91=E7=A5=A8=E6=94=B6=E6=AC=BE?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E3=80=81=E5=B7=B2=E6=94=B6=E9=87=91=E9=A2=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit committer: heyu --- .../impl/EmisWriteoffApplyServiceImpl.java | 134 ++++++++++++++++-- 1 file changed, 124 insertions(+), 10 deletions(-) diff --git a/emis-biz/src/main/java/com/xdadan/erp/emis/service/impl/EmisWriteoffApplyServiceImpl.java b/emis-biz/src/main/java/com/xdadan/erp/emis/service/impl/EmisWriteoffApplyServiceImpl.java index 65024830c..51e7f1577 100644 --- a/emis-biz/src/main/java/com/xdadan/erp/emis/service/impl/EmisWriteoffApplyServiceImpl.java +++ b/emis-biz/src/main/java/com/xdadan/erp/emis/service/impl/EmisWriteoffApplyServiceImpl.java @@ -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 detailList, + boolean isPass) { + try { + if (CollectionUtils.isEmpty(detailList)) { + log.warn("申请明细为空,无法更新开票记录付款状态,申请ID: {}", apply.getId()); + return; + } + + log.info("开始更新开票记录付款状态,申请ID: {}, 是否通过: {}", apply.getId(), isPass); + + // 按账单号分组处理,避免重复更新同一账单 + Map> billGroups = detailList.stream() + .filter(detail -> StringUtil.isNotBlank(detail.getSettleBillNo())) + .collect(java.util.stream.Collectors.groupingBy(EmisWriteoffApplyDetail::getSettleBillNo)); + + for (Map.Entry> entry : billGroups.entrySet()) { + String settleBillNo = entry.getKey(); + List 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 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); + // 不抛出异常,避免影响主流程 + } + } }