| | |
| | | package com.ruoyi.cwgl.service.impl; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.math.BigDecimal; |
| | | import java.util.*; |
| | | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import cn.hutool.http.HttpUtil; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.ruoyi.common.core.domain.AjaxResult; |
| | | import com.ruoyi.common.enums.SystemDataNoEnum; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.DateUtils; |
| | | import javax.annotation.Resource; |
| | | |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.cwgl.domain.InvoiceDetail; |
| | | import com.ruoyi.cwgl.domain.*; |
| | | import com.ruoyi.cwgl.domain.dto.CallbackReceiptDto; |
| | | import com.ruoyi.cwgl.mapper.*; |
| | | import com.ruoyi.cwgl.service.IInvoiceBillDetailService; |
| | | import com.ruoyi.cwgl.service.IInvoiceBusinessService; |
| | | import com.ruoyi.system.service.ISystemDataNoService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.scheduling.annotation.Async; |
| | |
| | | import com.ruoyi.common.enums.DataSourceType; |
| | | import com.ruoyi.common.core.service.BaseService; |
| | | |
| | | import com.ruoyi.cwgl.mapper.InvoiceManageMapper; |
| | | import com.ruoyi.cwgl.domain.InvoiceManage; |
| | | import com.ruoyi.cwgl.domain.InvoiceManageLog; |
| | | import com.ruoyi.cwgl.service.IInvoiceManageService; |
| | | import com.ruoyi.cwgl.service.IInvoiceManageLogService; |
| | | import com.ruoyi.common.core.text.Convert; |
| | |
| | | */ |
| | | @Service |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public class InvoiceManageServiceImpl extends BaseService<InvoiceManageMapper, InvoiceManage> implements IInvoiceManageService |
| | | { |
| | | public class InvoiceManageServiceImpl extends BaseService<InvoiceManageMapper, InvoiceManage> implements IInvoiceManageService { |
| | | protected final Logger logger = LoggerFactory.getLogger(getClass()); |
| | | @Resource |
| | | private InvoiceManageMapper invoiceManageMapper; |
| | | |
| | | |
| | | @Resource |
| | | private InvoiceBillDetailMapper invoiceBillDetailMapper; |
| | | |
| | | @Resource |
| | | private IInvoiceBillDetailService invoiceBillDetailService; |
| | | |
| | | @Resource |
| | | private IInvoiceManageLogService invoiceManageLogService; |
| | | |
| | | |
| | | @Autowired |
| | | private ISystemDataNoService systemDataNoService; |
| | | @Autowired |
| | | private IInvoiceBusinessService businessService; |
| | | @Value("${custom.gdpaasApi.extensionNumber}") |
| | | private String extensionNumber; |
| | | |
| | | |
| | | @Value("${custom.gdpaasApi.clerk}") |
| | | private String clerk; |
| | | @Value("${custom.gdpaasApi.invoicingAddress}") |
| | | private String invoicingAddress; |
| | | @Value("${custom.gdpaasApi.returnAddress}") |
| | | private String returnAddress; |
| | | @Autowired |
| | | private IInvoiceBusinessService invoiceBusinessService; |
| | | |
| | | @Resource |
| | | private ReceivableInvoiceBusinessMapper receivableInvoiceBusinessMapper; |
| | | @Resource |
| | | private ReceivableBillManagementMapper receivableBillManagementMapper; |
| | | @Resource |
| | | private PayableInvoiceBusinessMapper payableInvoiceBusinessMapper; |
| | | @Resource |
| | | private PayableBillManagementMapper payableBillManagementMapper; |
| | | /** |
| | | * 查询发票管理 |
| | | * |
| | |
| | | */ |
| | | @DataSource(DataSourceType.SLAVE) |
| | | @Override |
| | | public InvoiceManage selectInvoiceManageById(Integer id) |
| | | { |
| | | public InvoiceManage selectInvoiceManageById(Integer id) { |
| | | return invoiceManageMapper.selectInvoiceManageById(id); |
| | | } |
| | | |
| | |
| | | */ |
| | | @DataSource(DataSourceType.SLAVE) |
| | | @Override |
| | | public int selectInvoiceManageCount(InvoiceManage invoiceManage) |
| | | { |
| | | public int selectInvoiceManageCount(InvoiceManage invoiceManage) { |
| | | return invoiceManageMapper.selectInvoiceManageCount(invoiceManage); |
| | | } |
| | | |
| | |
| | | */ |
| | | @DataSource(DataSourceType.SLAVE) |
| | | @Override |
| | | public List<InvoiceManage> selectInvoiceManageList(InvoiceManage invoiceManage) |
| | | { |
| | | public List<InvoiceManage> selectInvoiceManageList(InvoiceManage invoiceManage) { |
| | | return invoiceManageMapper.selectInvoiceManageList(invoiceManage); |
| | | } |
| | | |
| | |
| | | * 查询发票管理列表 异步 导出 |
| | | * |
| | | * @param invoiceManage 发票管理 |
| | | * @param exportKey 导出功能的唯一标识 |
| | | * @param exportKey 导出功能的唯一标识 |
| | | * @return 发票管理集合 |
| | | */ |
| | | @DataSource(DataSourceType.SLAVE) |
| | | @Async |
| | | @Override |
| | | public void export(InvoiceManage invoiceManage,String exportKey) { |
| | | public void export(InvoiceManage invoiceManage, String exportKey) { |
| | | |
| | | super.export(InvoiceManage.class,exportKey,"invoiceManageData",(pageNum)->{ |
| | | super.export(InvoiceManage.class, exportKey, "invoiceManageData", (pageNum) -> { |
| | | PageUtils.startPage(pageNum, Constants.EXPORT_PATE_SIZE); |
| | | return selectInvoiceManageList(invoiceManage); |
| | | }); |
| | |
| | | * @param invoiceManage 发票管理 |
| | | * @return 结果 |
| | | */ |
| | | @Transactional |
| | | @Override |
| | | public int insertInvoiceManage(InvoiceManage invoiceManage) |
| | | { |
| | | public int insertInvoiceManage(InvoiceManage invoiceManage) { |
| | | invoiceManage.setCreateTime(DateUtils.getNowDate()); |
| | | invoiceManage.setOrderNo(systemDataNoService.getNoByKey(SystemDataNoEnum.FP)); |
| | | int rows = invoiceManageMapper.insertInvoiceManage(invoiceManage); |
| | | insertInvoiceDetail(invoiceManage); |
| | | |
| | | |
| | | // 记录新增发票管理日志 |
| | | if (rows > 0) { |
| | | recordInvoiceManageLog(invoiceManage, "新增发票管理"); |
| | | } |
| | | |
| | | |
| | | return rows; |
| | | } |
| | | |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public int insertInvoiceManageBatch(List<InvoiceManage> invoiceManages) |
| | | { |
| | | public int insertInvoiceManageBatch(List<InvoiceManage> invoiceManages) { |
| | | int rows = invoiceManageMapper.insertInvoiceManageBatch(invoiceManages); |
| | | |
| | | |
| | | // 记录批量新增发票管理日志 |
| | | if (rows > 0 && invoiceManages != null && !invoiceManages.isEmpty()) { |
| | | for (InvoiceManage invoiceManage : invoiceManages) { |
| | | recordInvoiceManageLog(invoiceManage, "批量新增发票管理"); |
| | | } |
| | | } |
| | | |
| | | |
| | | return rows; |
| | | } |
| | | |
| | |
| | | */ |
| | | @Transactional |
| | | @Override |
| | | public int updateInvoiceManage(InvoiceManage invoiceManage) |
| | | { |
| | | public int updateInvoiceManage(InvoiceManage invoiceManage) { |
| | | invoiceManage.setUpdateTime(DateUtils.getNowDate()); |
| | | // invoiceManageMapper.deleteInvoiceDetailByInvoiceManageId(invoiceManage.getId()); |
| | | // insertInvoiceDetail(invoiceManage); |
| | | // 删除原有的发票明细 |
| | | invoiceManageMapper.deleteInvoiceDetailByInvoiceManageId(invoiceManage.getId()); |
| | | // 删除原有的发票业务商品明细 |
| | | invoiceBillDetailService.deleteInvoiceBillDetailByInvoiceManageId(invoiceManage.getId()); |
| | | // 插入新的明细记录 |
| | | insertInvoiceDetail(invoiceManage); |
| | | int rows = invoiceManageMapper.updateInvoiceManage(invoiceManage); |
| | | |
| | | |
| | | // 记录修改发票管理日志 |
| | | if (rows > 0) { |
| | | recordInvoiceManageLog(invoiceManage, "修改发票管理"); |
| | | } |
| | | |
| | | |
| | | return rows; |
| | | } |
| | | |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public int updateInvoiceManageBatch(List<InvoiceManage> invoiceManages){ |
| | | public int updateInvoiceManageBatch(List<InvoiceManage> invoiceManages) { |
| | | int rows = invoiceManageMapper.updateInvoiceManageBatch(invoiceManages); |
| | | |
| | | |
| | | // 记录批量修改发票管理日志 |
| | | if (rows > 0 && invoiceManages != null && !invoiceManages.isEmpty()) { |
| | | for (InvoiceManage invoiceManage : invoiceManages) { |
| | | recordInvoiceManageLog(invoiceManage, "批量修改发票管理"); |
| | | } |
| | | } |
| | | |
| | | |
| | | return rows; |
| | | } |
| | | |
| | |
| | | */ |
| | | @Transactional |
| | | @Override |
| | | public int deleteInvoiceManageByIds(String ids) |
| | | { |
| | | public int deleteInvoiceManageByIds(String ids) { |
| | | return deleteInvoiceManageByIds(Convert.toIntArray(ids)); |
| | | } |
| | | |
| | | /** |
| | | * 删除发票管理对象 |
| | | * |
| | | * |
| | | * @param ids 需要删除的数据ID |
| | | * @return 结果 |
| | | */ |
| | | @Transactional |
| | | @Override |
| | | public int deleteInvoiceManageByIds(Integer[] ids) |
| | | { |
| | | public int deleteInvoiceManageByIds(Integer[] ids) { |
| | | invoiceManageMapper.deleteInvoiceDetailByInvoiceManageIds(ids); |
| | | return invoiceManageMapper.deleteInvoiceManageByIds(ids); |
| | | } |
| | |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public int deleteInvoiceManageById(Integer id) |
| | | { |
| | | public int deleteInvoiceManageById(Integer id) { |
| | | invoiceManageMapper.deleteInvoiceDetailByInvoiceManageId(id); |
| | | return invoiceManageMapper.deleteInvoiceManageById(id); |
| | | } |
| | |
| | | * @param invoiceManage 发票管理对象 |
| | | */ |
| | | @Override |
| | | public void insertInvoiceDetail(InvoiceManage invoiceManage) |
| | | { |
| | | public void insertInvoiceDetail(InvoiceManage invoiceManage) { |
| | | List<InvoiceDetail> invoiceDetailList = invoiceManage.getInvoiceDetailList(); |
| | | Integer id = invoiceManage.getId(); |
| | | if (StringUtils.isNotNull(invoiceDetailList)) |
| | | { |
| | | if (StringUtils.isNotNull(invoiceDetailList)) { |
| | | List<InvoiceDetail> list = new ArrayList<InvoiceDetail>(); |
| | | for (InvoiceDetail invoiceDetail : invoiceDetailList) |
| | | { |
| | | for (InvoiceDetail invoiceDetail : invoiceDetailList) { |
| | | invoiceDetail.setInvoiceManageId(id); |
| | | invoiceDetail.setCreateTime(new Date()); |
| | | list.add(invoiceDetail); |
| | | } |
| | | if (list.size() > 0) |
| | | { |
| | | if (list.size() > 0) { |
| | | invoiceManageMapper.batchInvoiceDetail(list); |
| | | } |
| | | } |
| | | |
| | | // 新增发票业务商品明细信息 |
| | | List<InvoiceBillDetail> invoiceBillDetails = invoiceManage.getInvoiceBillDetails(); |
| | | if (StringUtils.isNotNull(invoiceBillDetails)) { |
| | | List<InvoiceBillDetail> billDetailList = new ArrayList<InvoiceBillDetail>(); |
| | | for (InvoiceBillDetail invoiceBillDetail : invoiceBillDetails) { |
| | | invoiceBillDetail.setInvoiceManageId(id); |
| | | invoiceBillDetail.setCreateTime(new Date()); |
| | | billDetailList.add(invoiceBillDetail); |
| | | } |
| | | if (billDetailList.size() > 0) { |
| | | invoiceBillDetailMapper.insertInvoiceBillDetailBatch(billDetailList); |
| | | } |
| | | } |
| | | } |
| | |
| | | * @param invoiceManage 发票管理对象 |
| | | * @param operationType 操作类型 |
| | | */ |
| | | private void recordInvoiceManageLog(InvoiceManage invoiceManage, String operationType) |
| | | { |
| | | private void recordInvoiceManageLog(InvoiceManage invoiceManage, String operationType) { |
| | | try { |
| | | InvoiceManageLog log = new InvoiceManageLog(); |
| | | log.setInvoiceManageId(invoiceManage.getId()); |
| | | log.setOperator(invoiceManage.getCreateBy()); |
| | | log.setOperator(SecurityUtils.getUsername()); |
| | | log.setOperationTime(DateUtils.getNowDate()); |
| | | |
| | | |
| | | // 构建操作描述,包含开票金额、关联账单编号和对应开票金额 |
| | | StringBuilder descBuilder = new StringBuilder(); |
| | | descBuilder.append(operationType).append(": "); |
| | | |
| | | |
| | | if (invoiceManage.getInvoiceAmount() != null) { |
| | | descBuilder.append("开票金额=").append(invoiceManage.getInvoiceAmount()).append("; "); |
| | | } |
| | | |
| | | |
| | | // 获取关联账单信息 |
| | | if (invoiceManage.getInvoiceDetailList() != null && !invoiceManage.getInvoiceDetailList().isEmpty()) { |
| | | descBuilder.append("关联账单信息: "); |
| | |
| | | descBuilder.append("; "); |
| | | } |
| | | } |
| | | |
| | | |
| | | log.setOperationDesc(descBuilder.toString()); |
| | | log.setCreateTime(DateUtils.getNowDate()); |
| | | |
| | | |
| | | invoiceManageLogService.insertInvoiceManageLog(log); |
| | | } catch (Exception e) { |
| | | logger.error("记录发票管理操作日志失败: {}", e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public AjaxResult applyInvoice(Integer id) { |
| | | InvoiceManage invoiceManage = selectInvoiceManageById(id); |
| | | if (invoiceManage == null) { |
| | | throw new ServiceException("发票记录不存在"); |
| | | } |
| | | if (invoiceManage.getStatus().equals(1)) { |
| | | throw new ServiceException("请勿重复开票"); |
| | | |
| | | } |
| | | List<InvoiceBillDetail> invoiceBillDetails = invoiceManage.getInvoiceBillDetails(); |
| | | if (CollectionUtil.isEmpty(invoiceBillDetails)) { |
| | | throw new ServiceException("商品明细无数据无法开发票"); |
| | | |
| | | } |
| | | |
| | | // 记录申请开票日志 |
| | | recordApplyInvoiceLog(invoiceManage); |
| | | |
| | | return componentInvoice(invoiceManage.getInvoiceNo(),"FP",invoiceManage,invoiceBillDetails); |
| | | } |
| | | |
| | | public AjaxResult componentInvoice(String orderNo, String businessType, InvoiceManage invoiceManage, List<InvoiceBillDetail> invoiceBillDetails) { |
| | | |
| | | InvoicingVo invoicingVo = new InvoicingVo(); |
| | | Order order = new Order(); |
| | | order.setSalerTaxNum(invoiceManage.getInvoiceSellerCreditCode()); |
| | | order.setSalerTel(invoiceManage.getInvoiceSellerPhone()); |
| | | order.setSalerAddress(invoiceManage.getInvoiceSellerAddress()); |
| | | order.setInvoiceDate(DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD_HH_MM_SS)); |
| | | order.setClerk(clerk); |
| | | order.setInvoiceType("1"); |
| | | order.setPushMode("0"); |
| | | order.setExtensionNumber(extensionNumber); |
| | | |
| | | order.setOrderNo(orderNo); |
| | | order.setEmail(invoiceManage.getInvoiceOperatingLicenseEmail()); |
| | | order.setBuyerName(invoiceManage.getInvoiceCompanyName()); |
| | | order.setInvoiceLine(invoiceManage.getInvoiceType()); |
| | | order.setBuyerPhone(invoiceManage.getInvoiceOperatingLicensePhone()); |
| | | order.setBuyerTaxNum(invoiceManage.getInvoiceCreditCode()); |
| | | order.setBuyerTel(invoiceManage.getInvoiceOperatingLicensePhone()); |
| | | if (StringUtils.isNotBlank(invoiceManage.getInvoiceBankNo()) && |
| | | StringUtils.isNotBlank(invoiceManage.getInvoiceBankName())) { |
| | | order.setBuyerAccount(invoiceManage.getInvoiceBankName() + " " + invoiceManage.getInvoiceBankNo()); |
| | | } |
| | | |
| | | List<InvoiceDetailApi> invoiceDetailList = new ArrayList<>(); |
| | | //商品明细 |
| | | for (InvoiceBillDetail billingDetail : invoiceBillDetails) { |
| | | InvoiceDetailApi invoiceDetail = new InvoiceDetailApi(); |
| | | |
| | | invoiceDetail.setGoodsCode(billingDetail.getGoodsCode()); |
| | | invoiceDetail.setGoodsName(billingDetail.getGoodsName()); |
| | | |
| | | invoiceDetail.setWithTaxFlag(billingDetail.getWithTaxFlag().toString()); |
| | | invoiceDetail.setTaxRate("0.06"); |
| | | invoiceDetail.setUnit("项"); |
| | | invoiceDetail.setNum("1"); |
| | | invoiceDetail.setPrice(billingDetail.getPrice().toString()); // 初始价格为0 |
| | | invoiceDetailList.add(invoiceDetail); |
| | | } |
| | | |
| | | |
| | | order.setInvoiceDetail(invoiceDetailList); |
| | | invoicingVo.setOrder(order); |
| | | String body = JSONObject.toJSONString(invoicingVo); |
| | | String urlString = invoicingAddress + "?businessType=" + businessType + "&returnUrl=" + returnAddress; |
| | | logger.info("请求body{}",body); |
| | | logger.info("请求地址{}",urlString); |
| | | String json = HttpUtil.post(urlString, body); |
| | | |
| | | if (json == null) { |
| | | logger.info("开票失败"); |
| | | return AjaxResult.error("开票失败"); |
| | | } |
| | | JSONObject jsonObject = JSONObject.parseObject(json); |
| | | String code = jsonObject.getString("code"); |
| | | logger.info("开票返回{}",jsonObject); |
| | | String invoiceSerialNum = null; |
| | | InvoiceBusiness record = new InvoiceBusiness(); |
| | | record.setReqData(JSONObject.toJSONString(invoicingVo)); |
| | | record.setResData(json); |
| | | record.setCreateTime(new Date()); |
| | | if ("0".equals(code)) { |
| | | String data = jsonObject.getString("data"); |
| | | |
| | | JSONObject dataJson = JSONObject.parseObject(data); |
| | | invoiceSerialNum = dataJson.getString("invoiceSerialNum"); |
| | | logger.info("开票成功发票序列号为{}", invoiceSerialNum); |
| | | record.setInvoiceSerialNum(invoiceSerialNum); |
| | | record.setInvoiceManageId(invoiceManage.getId()); |
| | | invoiceBusinessService.insertInvoiceBusiness(record); |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | |
| | | return AjaxResult.error("开票失败"); |
| | | } |
| | | |
| | | /** |
| | | * 应收账单申请开票 |
| | | * |
| | | * @param invoiceManage 发票管理对象 |
| | | * @param id 应收账单ID |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public AjaxResult applyReceivableInvoice(InvoiceManage invoiceManage, Integer id) { |
| | | List<InvoiceBillDetail> invoiceReceivableBillDetails = invoiceManage.getInvoiceBillDetails(); |
| | | if (CollectionUtil.isEmpty(invoiceReceivableBillDetails)) { |
| | | throw new ServiceException("应收账单商品明细无数据无法开发票"); |
| | | } |
| | | |
| | | // 获取应收账单信息 |
| | | ReceivableBillManagement receivableBill = receivableBillManagementMapper.selectReceivableBillManagementById(id); |
| | | if (receivableBill == null) { |
| | | throw new ServiceException("应收账单记录不存在"); |
| | | } |
| | | |
| | | // 查询应收账单的应结算金额 |
| | | BigDecimal totalAmount = receivableBill.getTotalAmount(); |
| | | if (totalAmount == null || totalAmount.compareTo(BigDecimal.ZERO) <= 0) { |
| | | throw new ServiceException("应收账单应结算金额无效"); |
| | | } |
| | | |
| | | // 查询receivable_invoice_business表中的开票金额 |
| | | ReceivableInvoiceBusiness query = new ReceivableInvoiceBusiness(); |
| | | query.setHeadId(id); |
| | | List<ReceivableInvoiceBusiness> existingInvoices = receivableInvoiceBusinessMapper.selectReceivableInvoiceBusinessList(query); |
| | | |
| | | // 计算已开票金额 |
| | | BigDecimal invoicedAmount = existingInvoices.stream() |
| | | .map(ReceivableInvoiceBusiness::getInvoicingAmount) |
| | | .filter(Objects::nonNull) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | |
| | | // 计算本次开票金额 |
| | | BigDecimal currentInvoiceAmount = invoiceReceivableBillDetails.stream() |
| | | .map(InvoiceBillDetail::getPrice) |
| | | .filter(Objects::nonNull) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | |
| | | // 校验开票金额不能超过应结算金额 |
| | | BigDecimal totalInvoicedAmount = invoicedAmount.add(currentInvoiceAmount); |
| | | if (totalInvoicedAmount.compareTo(totalAmount) > 0) { |
| | | throw new ServiceException("开票金额" + currentInvoiceAmount + "加上已开票金额" + invoicedAmount + |
| | | "超过应结算金额" + totalAmount + ",无法开票"); |
| | | } |
| | | |
| | | String noByKey = systemDataNoService.getNoByKey(SystemDataNoEnum.YSFP); |
| | | |
| | | // 在receivable_invoice_business表中添加开票数据 |
| | | ReceivableInvoiceBusiness receivableInvoiceBusiness = new ReceivableInvoiceBusiness(); |
| | | receivableInvoiceBusiness.setHeadId(id); |
| | | receivableInvoiceBusiness.setOrderSn(noByKey); // 使用noByKey作为orderSn |
| | | receivableInvoiceBusiness.setInvoiceType("应收发票"); |
| | | receivableInvoiceBusiness.setInvoiceCompanyName(receivableBill.getCustomerName()); |
| | | receivableInvoiceBusiness.setInvoicingAmount(currentInvoiceAmount); |
| | | receivableInvoiceBusiness.setStatus(0); // 初始状态 |
| | | receivableInvoiceBusiness.setCreateTime(new java.util.Date()); |
| | | |
| | | // 保存到receivable_invoice_business表 |
| | | receivableInvoiceBusinessMapper.insertReceivableInvoiceBusiness(receivableInvoiceBusiness); |
| | | |
| | | // 记录应收账单申请开票日志 |
| | | recordReceivableApplyInvoiceLog(receivableBill, invoiceReceivableBillDetails); |
| | | |
| | | return componentInvoice(noByKey, "FP_RECEIVABLE", invoiceManage, invoiceReceivableBillDetails); |
| | | } |
| | | |
| | | /** |
| | | * 应付账单申请开票 |
| | | * |
| | | * @param invoiceManage 发票管理对象 |
| | | * @param id 应付账单ID |
| | | * @return 结果 |
| | | */ |
| | | @Override |
| | | public AjaxResult applyPayableInvoice(InvoiceManage invoiceManage, Integer id) { |
| | | List<InvoiceBillDetail> invoiceBillDetails = invoiceManage.getInvoiceBillDetails(); |
| | | if (CollectionUtil.isEmpty(invoiceBillDetails)) { |
| | | throw new ServiceException("应付账单商品明细无数据无法开发票"); |
| | | } |
| | | |
| | | // 获取应付账单信息 |
| | | PayableBillManagement payableBill = payableBillManagementMapper.selectPayableBillManagementById(id); |
| | | if (payableBill == null) { |
| | | throw new ServiceException("应付账单记录不存在"); |
| | | } |
| | | |
| | | // 查询应付账单的应结算金额 |
| | | BigDecimal totalAmount = payableBill.getTotalAmount(); |
| | | if (totalAmount == null || totalAmount.compareTo(BigDecimal.ZERO) <= 0) { |
| | | throw new ServiceException("应付账单应结算金额无效"); |
| | | } |
| | | |
| | | // 查询payable_invoice_business表中的开票金额 |
| | | PayableInvoiceBusiness query = new PayableInvoiceBusiness(); |
| | | query.setHeadId(id); |
| | | List<PayableInvoiceBusiness> existingInvoices = payableInvoiceBusinessMapper.selectPayableInvoiceBusinessList(query); |
| | | |
| | | // 计算已开票金额 |
| | | BigDecimal invoicedAmount = existingInvoices.stream() |
| | | .map(PayableInvoiceBusiness::getInvoicingAmount) |
| | | .filter(Objects::nonNull) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | |
| | | // 计算本次开票金额 |
| | | BigDecimal currentInvoiceAmount = invoiceBillDetails.stream() |
| | | .map(InvoiceBillDetail::getPrice) |
| | | .filter(Objects::nonNull) |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | |
| | | // 校验开票金额不能超过应结算金额 |
| | | BigDecimal totalInvoicedAmount = invoicedAmount.add(currentInvoiceAmount); |
| | | if (totalInvoicedAmount.compareTo(totalAmount) > 0) { |
| | | throw new ServiceException("开票金额" + currentInvoiceAmount + "加上已开票金额" + invoicedAmount + |
| | | "超过应结算金额" + totalAmount + ",无法开票"); |
| | | } |
| | | |
| | | String noByKey = systemDataNoService.getNoByKey(SystemDataNoEnum.YFFP); |
| | | |
| | | // 在payable_invoice_business表中添加开票数据 |
| | | PayableInvoiceBusiness payableInvoiceBusiness = new PayableInvoiceBusiness(); |
| | | payableInvoiceBusiness.setHeadId(id); |
| | | payableInvoiceBusiness.setOrderSn(noByKey); // 使用noByKey作为orderSn |
| | | payableInvoiceBusiness.setInvoiceType("pc"); |
| | | payableInvoiceBusiness.setInvoiceCompanyName(payableBill.getSupplierName()); |
| | | payableInvoiceBusiness.setInvoicingAmount(currentInvoiceAmount); |
| | | payableInvoiceBusiness.setStatus(0); // 初始状态 |
| | | payableInvoiceBusiness.setCreateTime(new java.util.Date()); |
| | | |
| | | // 保存到payable_invoice_business表 |
| | | payableInvoiceBusinessMapper.insertPayableInvoiceBusiness(payableInvoiceBusiness); |
| | | |
| | | recordPayableApplyInvoiceLog(payableBill,invoiceBillDetails); |
| | | // 调用现有的 componentInvoice 接口 |
| | | return componentInvoice(noByKey, "FP_PAYABLE", invoiceManage, invoiceBillDetails); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 记录应收账单申请开票日志 |
| | | * |
| | | * @param receivableBill 应收账单对象 |
| | | * @param invoiceBillDetails 应收账单商品明细列表 |
| | | */ |
| | | private void recordReceivableApplyInvoiceLog(ReceivableBillManagement receivableBill,List<InvoiceBillDetail> invoiceBillDetails) { |
| | | try { |
| | | InvoiceManageLog log = new InvoiceManageLog(); |
| | | log.setInvoiceManageId(receivableBill.getId()); |
| | | log.setOperator(SecurityUtils.getUsername()); |
| | | log.setOperationTime(new java.util.Date()); |
| | | |
| | | // 构建操作描述,包含开票金额、关联应收账单编号和对应开票金额 |
| | | StringBuilder descBuilder = new StringBuilder(); |
| | | descBuilder.append("应收账单申请开票: "); |
| | | |
| | | if (receivableBill.getTotalAmount() != null) { |
| | | descBuilder.append("账单总金额=").append(receivableBill.getTotalAmount()).append("; "); |
| | | } |
| | | // 获取关联应收账单信息 |
| | | if (invoiceBillDetails != null && !invoiceBillDetails.isEmpty()) { |
| | | |
| | | for (InvoiceBillDetail detail : invoiceBillDetails) { |
| | | if (detail.getGoodsName() != null) { |
| | | descBuilder.append("商品名称=").append(detail.getGoodsName()); |
| | | } |
| | | if (detail.getPrice() != null) { |
| | | descBuilder.append(", 商品金额=").append(detail.getPrice()); |
| | | } |
| | | descBuilder.append("; "); |
| | | } |
| | | } |
| | | |
| | | log.setOperationDesc(descBuilder.toString()); |
| | | log.setCreateTime(new java.util.Date()); |
| | | |
| | | invoiceManageLogService.insertInvoiceManageLog(log); |
| | | } catch (Exception e) { |
| | | logger.error("记录应收账单申请开票日志失败: {}", e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 记录应付账单申请开票日志 |
| | | * |
| | | * @param payableBill 应付账单对象 |
| | | * @param invoiceBillDetails 应付账单商品明细列表 |
| | | */ |
| | | private void recordPayableApplyInvoiceLog(PayableBillManagement payableBill, List<InvoiceBillDetail> invoiceBillDetails) { |
| | | try { |
| | | InvoiceManageLog log = new InvoiceManageLog(); |
| | | log.setInvoiceManageId(payableBill.getId()); |
| | | log.setOperator(SecurityUtils.getUsername()); |
| | | log.setOperationTime(new java.util.Date()); |
| | | |
| | | // 构建操作描述,包含开票金额、关联应付账单编号和对应开票金额 |
| | | StringBuilder descBuilder = new StringBuilder(); |
| | | descBuilder.append("应付账单申请开票: "); |
| | | |
| | | if (payableBill.getTotalAmount() != null) { |
| | | descBuilder.append("账单总金额=").append(payableBill.getTotalAmount()).append("; "); |
| | | } |
| | | |
| | | // 获取关联应付账单信息 |
| | | if (invoiceBillDetails != null && !invoiceBillDetails.isEmpty()) { |
| | | for (InvoiceBillDetail detail : invoiceBillDetails) { |
| | | if (detail.getGoodsName() != null) { |
| | | descBuilder.append("商品名称=").append(detail.getGoodsName()); |
| | | } |
| | | if (detail.getPrice() != null) { |
| | | descBuilder.append(", 商品金额=").append(detail.getPrice()); |
| | | } |
| | | descBuilder.append("; "); |
| | | } |
| | | } |
| | | |
| | | log.setOperationDesc(descBuilder.toString()); |
| | | log.setCreateTime(new java.util.Date()); |
| | | |
| | | invoiceManageLogService.insertInvoiceManageLog(log); |
| | | } catch (Exception e) { |
| | | logger.error("记录应付账单申请开票日志失败: {}", e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 记录申请开票日志 |
| | | * |
| | | * @param invoiceManage 发票管理对象 |
| | | */ |
| | | private void recordApplyInvoiceLog(InvoiceManage invoiceManage) { |
| | | try { |
| | | InvoiceManageLog log = new InvoiceManageLog(); |
| | | log.setInvoiceManageId(invoiceManage.getId()); |
| | | log.setOperator(SecurityUtils.getUsername()); |
| | | log.setOperationTime(new java.util.Date()); |
| | | |
| | | // 构建操作描述,包含开票金额、关联账单编号和对应开票金额 |
| | | StringBuilder descBuilder = new StringBuilder(); |
| | | descBuilder.append("申请开票: "); |
| | | |
| | | if (invoiceManage.getInvoiceAmount() != null) { |
| | | descBuilder.append("开票金额=").append(invoiceManage.getInvoiceAmount()).append("; "); |
| | | } |
| | | |
| | | // 获取关联账单信息 |
| | | if (invoiceManage.getInvoiceDetailList() != null && !invoiceManage.getInvoiceDetailList().isEmpty()) { |
| | | descBuilder.append("关联账单信息: "); |
| | | for (com.ruoyi.cwgl.domain.InvoiceDetail detail : invoiceManage.getInvoiceDetailList()) { |
| | | if (detail.getReceivableBillNo() != null) { |
| | | descBuilder.append("账单编号=").append(detail.getReceivableBillNo()); |
| | | } |
| | | if (detail.getCurrentBilledAmount() != null) { |
| | | descBuilder.append(", 本次开票金额=").append(detail.getCurrentBilledAmount()); |
| | | } |
| | | descBuilder.append("; "); |
| | | } |
| | | } |
| | | |
| | | log.setOperationDesc(descBuilder.toString()); |
| | | log.setCreateTime(new java.util.Date()); |
| | | |
| | | invoiceManageLogService.insertInvoiceManageLog(log); |
| | | } catch (Exception e) { |
| | | logger.error("记录申请开票日志失败: {}", e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void invoicingSuccess(CallbackReceiptDto callbackReceiptDto) { |
| | | InvoiceManage invoiceBusiness = invoiceManageMapper.selectInvoiceManageLogByOrderNo(callbackReceiptDto.getOrderno()); |
| | | if (invoiceBusiness == null) { |
| | | return; |
| | | } |
| | | String content = callbackReceiptDto.getContent(); |
| | | if (StringUtils.isEmpty(content)) { |
| | | return; |
| | | } |
| | | JSONObject jsonObject = JSONObject.parseObject(content); |
| | | |
| | | int c_status = Integer.parseInt(jsonObject.getString("c_status")); |
| | | invoiceBusiness.setStatus(c_status); |
| | | if (c_status == 1) { |
| | | |
| | | |
| | | String invoiceNumStr = jsonObject.getString("c_fphm"); |
| | | |
| | | InvoiceManage invoiceManageUpdate = new InvoiceManage(); |
| | | invoiceManageUpdate.setInvoiceNo(invoiceNumStr); |
| | | invoiceManageUpdate.setId(invoiceBusiness.getId()); |
| | | invoiceManageUpdate.setStatus(2); |
| | | invoiceManageMapper.updateById(invoiceBusiness); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | |