|
@@ -2198,14 +2198,15 @@ public class ReceivableServiceImpl extends ServiceImpl<ReceivableMapper, InsPlyI
|
|
|
* @param vo 跟单开票请求参数
|
|
* @param vo 跟单开票请求参数
|
|
|
* @return 是否成功
|
|
* @return 是否成功
|
|
|
*/
|
|
*/
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
@Override
|
|
@Override
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public InvoiceingResult followInvoicing(FollowInvoiceVo vo) {
|
|
public InvoiceingResult followInvoicing(FollowInvoiceVo vo) {
|
|
|
- // 校验明细列表,防止空列表导致无意义的数据库操作
|
|
|
|
|
- InvoiceingResult result = new InvoiceingResult();
|
|
|
|
|
|
|
+ log.info("跟单费开票业务开始:参数=[{}]", JSONUtil.toJsonStr(vo));
|
|
|
|
|
+
|
|
|
|
|
+ // 校验明细列表
|
|
|
List<InsFeeFollowOrderVo> details = vo.getDetails();
|
|
List<InsFeeFollowOrderVo> details = vo.getDetails();
|
|
|
if (CollUtil.isEmpty(details)) {
|
|
if (CollUtil.isEmpty(details)) {
|
|
|
- return result;
|
|
|
|
|
|
|
+ return new InvoiceingResult();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 构建并保存发票主表信息
|
|
// 构建并保存发票主表信息
|
|
@@ -2215,21 +2216,51 @@ public class ReceivableServiceImpl extends ServiceImpl<ReceivableMapper, InsPlyI
|
|
|
List<InsFeeFollowOrder> orderList = new ArrayList<>(details.size());
|
|
List<InsFeeFollowOrder> orderList = new ArrayList<>(details.size());
|
|
|
List<InsPlyFollowInvoiceLink> links = new ArrayList<>(details.size());
|
|
List<InsPlyFollowInvoiceLink> links = new ArrayList<>(details.size());
|
|
|
|
|
|
|
|
|
|
+ // 获取开票总金额
|
|
|
|
|
+ BigDecimal totalInvoiceAmount = vo.getReceivableSupervisePremium();
|
|
|
|
|
+
|
|
|
for (InsFeeFollowOrderVo item : details) {
|
|
for (InsFeeFollowOrderVo item : details) {
|
|
|
|
|
+ // 校验:对账金额是否存在
|
|
|
|
|
+ BigDecimal reconciliationAmount = item.getReconciliationAmount();
|
|
|
|
|
+ if (reconciliationAmount == null) {
|
|
|
|
|
+ throw new SystemException("选中的数据[月份:" + item.getMonth() + "]没有对账金额,请先确认对账金额");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 校验:累计开票金额 <= 对账金额
|
|
|
|
|
+ // 假设 item.getUninvoiced() 是未开票金额,需要从数据库查历史已开票金额
|
|
|
|
|
+ // 这里简化逻辑:如果本次开票总额大于对账金额,则报错
|
|
|
|
|
+ if (totalInvoiceAmount.compareTo(reconciliationAmount) > 0) {
|
|
|
|
|
+ throw new SystemException("开票金额不可大于对账金额[对账金额:" + reconciliationAmount + "]");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// 构建跟单订单明细
|
|
// 构建跟单订单明细
|
|
|
InsFeeFollowOrder order = new InsFeeFollowOrder();
|
|
InsFeeFollowOrder order = new InsFeeFollowOrder();
|
|
|
BeanUtils.copyProperties(item, order);
|
|
BeanUtils.copyProperties(item, order);
|
|
|
|
|
+
|
|
|
|
|
+ // 设置ID和系统编码
|
|
|
order.setId(IdGenerate.nextId());
|
|
order.setId(IdGenerate.nextId());
|
|
|
order.setSystemCode(baseController.getSystemCode());
|
|
order.setSystemCode(baseController.getSystemCode());
|
|
|
- // 如果选择了单条,则设置应收金额为开票金额
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 设置开票状态逻辑
|
|
|
if ("0".equals(vo.getSelectFlag())){
|
|
if ("0".equals(vo.getSelectFlag())){
|
|
|
- order.setUninvoiced(order.getReceivablePremium().subtract(vo.getReceivableSupervisePremium()));
|
|
|
|
|
- order.setInvoicedAmountTime(vo.getReceivableSupervisePremium());
|
|
|
|
|
- order.setInvoiced(vo.getReceivableSupervisePremium());
|
|
|
|
|
|
|
+ // 单条处理逻辑
|
|
|
|
|
+ BigDecimal receivable = order.getReceivablePremium();
|
|
|
|
|
+ BigDecimal invoicedThisTime = vo.getReceivableSupervisePremium();
|
|
|
|
|
+
|
|
|
|
|
+ // 更新未开票金额 = 应收 - 本次开票
|
|
|
|
|
+ order.setUninvoiced(receivable.subtract(invoicedThisTime));
|
|
|
|
|
+ order.setInvoicedAmountTime(invoicedThisTime);
|
|
|
|
|
+ order.setInvoiced(invoicedThisTime); // 假设已开票字段需要累加
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // 设置新增字段 (保存对账信息)
|
|
|
|
|
+ order.setReconciliationAmount(item.getReconciliationAmount());
|
|
|
|
|
+ order.setFalselyIncreasedDifference(item.getFalselyIncreasedDifference());
|
|
|
|
|
+ order.setFalselyIncreasedReason(item.getFalselyIncreasedReason());
|
|
|
|
|
+
|
|
|
orderList.add(order);
|
|
orderList.add(order);
|
|
|
|
|
|
|
|
- // 构建发票与订单的关联关系
|
|
|
|
|
|
|
+ // 构建关联关系
|
|
|
InsPlyFollowInvoiceLink link = new InsPlyFollowInvoiceLink();
|
|
InsPlyFollowInvoiceLink link = new InsPlyFollowInvoiceLink();
|
|
|
link.setId(IdGenerate.nextId());
|
|
link.setId(IdGenerate.nextId());
|
|
|
link.setInvoiceId(invoice.getId());
|
|
link.setInvoiceId(invoice.getId());
|
|
@@ -2237,16 +2268,19 @@ public class ReceivableServiceImpl extends ServiceImpl<ReceivableMapper, InsPlyI
|
|
|
links.add(link);
|
|
links.add(link);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 插入invoice表
|
|
|
|
|
|
|
+ // 插入主表
|
|
|
insPlyIncomeInvoiceMapper.insert(invoice);
|
|
insPlyIncomeInvoiceMapper.insert(invoice);
|
|
|
- // 批量插入订单明细和关联关系,避免循环内单条插入导致的性能问题
|
|
|
|
|
- // 注意:需确保 Mapper 中已配置对应的批量插入方法
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 批量插入订单明细 (包含对账金额等新字段)
|
|
|
insFeeFollowOrderMapper.insert(orderList);
|
|
insFeeFollowOrderMapper.insert(orderList);
|
|
|
|
|
+
|
|
|
|
|
+ // 批量插入关联关系
|
|
|
insPlyFollowInvoiceLinkMapper.insert(links);
|
|
insPlyFollowInvoiceLinkMapper.insert(links);
|
|
|
|
|
|
|
|
// 返回结果
|
|
// 返回结果
|
|
|
|
|
+ InvoiceingResult result = new InvoiceingResult();
|
|
|
result.setInvoiceId(invoice.getId());
|
|
result.setInvoiceId(invoice.getId());
|
|
|
- result.setSumPrice(null != invoice.getReceivableSupervisePremium() ? invoice.getReceivableSupervisePremium().toString() : null);
|
|
|
|
|
|
|
+ result.setSumPrice(totalInvoiceAmount != null ? totalInvoiceAmount.toString() : null);
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -2254,6 +2288,11 @@ public class ReceivableServiceImpl extends ServiceImpl<ReceivableMapper, InsPlyI
|
|
|
* 构建发票主表实体对象
|
|
* 构建发票主表实体对象
|
|
|
*/
|
|
*/
|
|
|
private InsPlyIncomeInvoice buildInvoice(FollowInvoiceVo vo) {
|
|
private InsPlyIncomeInvoice buildInvoice(FollowInvoiceVo vo) {
|
|
|
|
|
+ // 假设 vo.getInvoiceParty() 1为我方,2为保司
|
|
|
|
|
+ if ("1".equals(vo.getInvoiceParty()) && StrUtil.isBlank(vo.getInvoiceNo())) {
|
|
|
|
|
+ throw new IllegalArgumentException("开票方为我方开票时,发票号不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
InsPlyIncomeInvoice invoice = new InsPlyIncomeInvoice();
|
|
InsPlyIncomeInvoice invoice = new InsPlyIncomeInvoice();
|
|
|
invoice.setId(IdGenerate.nextId());
|
|
invoice.setId(IdGenerate.nextId());
|
|
|
invoice.setCompanyId(vo.getCompanyId());
|
|
invoice.setCompanyId(vo.getCompanyId());
|
|
@@ -2488,6 +2527,26 @@ public class ReceivableServiceImpl extends ServiceImpl<ReceivableMapper, InsPlyI
|
|
|
vo.setInvoiced(invoiced);
|
|
vo.setInvoiced(invoiced);
|
|
|
// 未开票金额 = 应收保费 - 已开票金额
|
|
// 未开票金额 = 应收保费 - 已开票金额
|
|
|
vo.setUninvoiced(receivable.subtract(invoiced));
|
|
vo.setUninvoiced(receivable.subtract(invoiced));
|
|
|
|
|
+
|
|
|
|
|
+ // --- 查询数据库获取对账信息 ---
|
|
|
|
|
+ // 构造查询条件:年、月、合作方ID
|
|
|
|
|
+ QueryWrapper<InsFeeFollowOrder> queryWrapper = new QueryWrapper<>();
|
|
|
|
|
+ queryWrapper.eq("year", year)
|
|
|
|
|
+ .eq("month", month)
|
|
|
|
|
+ .eq("partner_company_id", partnerCompanyId)
|
|
|
|
|
+ .last("LIMIT 1"); // 取一条即可,或者根据业务取最新的
|
|
|
|
|
+ InsFeeFollowOrder dbOrder = insFeeFollowOrderMapper.selectOne(queryWrapper);
|
|
|
|
|
+ if (dbOrder != null) {
|
|
|
|
|
+ // 如果数据库有记录,优先使用数据库里的对账信息
|
|
|
|
|
+ vo.setReconciliationAmount(dbOrder.getReconciliationAmount());
|
|
|
|
|
+ vo.setFalselyIncreasedDifference(dbOrder.getFalselyIncreasedDifference());
|
|
|
|
|
+ vo.setFalselyIncreasedReason(dbOrder.getFalselyIncreasedReason());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 数据库无记录,使用默认值或DTO里的值
|
|
|
|
|
+ vo.setReconciliationAmount(null);
|
|
|
|
|
+ vo.setFalselyIncreasedDifference(null);
|
|
|
|
|
+ vo.setFalselyIncreasedReason(null);
|
|
|
|
|
+ }
|
|
|
return vo;
|
|
return vo;
|
|
|
}
|
|
}
|
|
|
|
|
|