| | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.lang.Assert; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.invoice.ErpPurchaseInvoiceImportExcelVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.invoice.ErpPurchaseInvoiceImportReqVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.invoice.ErpPurchaseInvoiceImportRespVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.invoice.ErpPurchaseInvoicePageReqVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.invoice.ErpPurchaseInvoiceSaveReqVO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseInvoiceDO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseOrderDO; |
| | | import cn.iocoder.yudao.module.erp.dal.mysql.finance.ErpFinancePaymentMapper; |
| | | import cn.iocoder.yudao.module.erp.dal.mysql.purchase.ErpPurchaseInvoiceMapper; |
| | | import cn.iocoder.yudao.module.erp.dal.mysql.purchase.ErpPurchaseOrderMapper; |
| | | import cn.iocoder.yudao.module.erp.dal.redis.no.ErpNoRedisDAO; |
| | | import cn.iocoder.yudao.module.erp.enums.ErpPurchaseInvoiceAuditStatusEnum; |
| | | import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.getSumValue; |
| | | import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.*; |
| | | |
| | |
| | | private ErpPurchaseInvoiceMapper invoiceMapper; |
| | | |
| | | @Resource |
| | | private ErpFinancePaymentMapper financePaymentMapper; |
| | | |
| | | @Resource |
| | | private ErpNoRedisDAO noRedisDAO; |
| | | |
| | | @Resource |
| | | private ErpPurchaseOrderService purchaseOrderService; |
| | | |
| | | @Resource |
| | | private ErpPurchaseOrderMapper purchaseOrderMapper; |
| | | |
| | | @Resource |
| | | private StorageAttachmentApi storageAttachmentApi; |
| | |
| | | throw exception(PURCHASE_INVOICE_NO_EXISTS); |
| | | } |
| | | |
| | | // 2.1 插入来票(创建后即为未审核状态) |
| | | ErpPurchaseInvoiceDO invoice = BeanUtils.toBean(createReqVO, ErpPurchaseInvoiceDO.class) |
| | | .setNo(no).setAuditStatus(ErpPurchaseInvoiceAuditStatusEnum.PROCESS.getStatus()); |
| | | // 2.1 插入来票(创建后即为有效状态,无需审核) |
| | | ErpPurchaseInvoiceDO invoice = BeanUtils.toBean(createReqVO, ErpPurchaseInvoiceDO.class).setNo(no); |
| | | invoiceMapper.insert(invoice); |
| | | |
| | | // 2.2 绑定发票附件 |
| | |
| | | } |
| | | |
| | | private void validatePurchaseInvoicePriceExceedsLimit(ErpPurchaseInvoiceSaveReqVO reqVO) { |
| | | // 1. 计算剩余可开票金额,不包括 reqVO 自身(审批中也占用额度,防止累计来票超过采购订单金额) |
| | | // 1. 计算剩余可开票金额,不包括 reqVO 自身(所有有效来票都占用额度,防止累计来票超过采购订单金额) |
| | | ErpPurchaseOrderDO purchaseOrder = purchaseOrderService.getPurchaseOrder(reqVO.getPurchaseOrderId()); |
| | | List<ErpPurchaseInvoiceDO> invoices = invoiceMapper.selectListByOrderIdAndStatus(reqVO.getPurchaseOrderId(), |
| | | List.of(ErpPurchaseInvoiceAuditStatusEnum.DRAFT.getStatus(), |
| | | ErpPurchaseInvoiceAuditStatusEnum.PROCESS.getStatus(), |
| | | ErpPurchaseInvoiceAuditStatusEnum.APPROVE.getStatus())); |
| | | List<ErpPurchaseInvoiceDO> invoices = invoiceMapper.selectListByOrderId(reqVO.getPurchaseOrderId()); |
| | | if (reqVO.getId() != null) { |
| | | invoices.removeIf(invoice -> ObjectUtil.equal(invoice.getId(), reqVO.getId())); |
| | | } |
| | |
| | | public void updatePurchaseInvoice(ErpPurchaseInvoiceSaveReqVO updateReqVO) { |
| | | Assert.notNull(updateReqVO.getId(), "来票编号不能为空"); |
| | | // 1.1 校验存在 |
| | | ErpPurchaseInvoiceDO oldInvoice = validatePurchaseInvoiceExists(updateReqVO.getId()); |
| | | // 1.2 只有未审核可以编辑 |
| | | if (ObjectUtil.notEqual(oldInvoice.getAuditStatus(), ErpPurchaseInvoiceAuditStatusEnum.PROCESS.getStatus())) { |
| | | throw exception(PURCHASE_INVOICE_UPDATE_FAIL_EDITING_PROHIBITED, oldInvoice.getNo()); |
| | | } |
| | | // 1.3 校验关联数据 + 可开票金额 |
| | | validatePurchaseInvoiceExists(updateReqVO.getId()); |
| | | // 1.2 校验关联数据 + 可开票金额 |
| | | validateRelationDataExists(updateReqVO); |
| | | validatePurchaseInvoicePriceExceedsLimit(updateReqVO); |
| | | |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void deletePurchaseInvoice(Long id) { |
| | | // 1.1 校验存在 |
| | | ErpPurchaseInvoiceDO invoice = validatePurchaseInvoiceExists(id); |
| | | // 1.2 审批通过时,不允许删除 |
| | | if (ObjectUtil.equal(invoice.getAuditStatus(), ErpPurchaseInvoiceAuditStatusEnum.APPROVE.getStatus())) { |
| | | throw exception(PURCHASE_INVOICE_DELETE_FAIL_IS_APPROVE); |
| | | validatePurchaseInvoiceExists(id); |
| | | // 1.2 已有有效付款单关联的来票,不允许删除(保持来票与付款的关联完整性) |
| | | Map<Long, BigDecimal> paidMap = financePaymentMapper.selectPaymentPriceMapByInvoiceId(List.of(id)); |
| | | if (paidMap.getOrDefault(id, BigDecimal.ZERO).compareTo(BigDecimal.ZERO) > 0) { |
| | | throw exception(PURCHASE_INVOICE_DELETE_FAIL_HAS_PAYMENT); |
| | | } |
| | | |
| | | // 2.1 删除发票附件 |
| | | storageAttachmentApi.deleteAttachmentsByRecord(PURCHASE_INVOICE_RECORD_TYPE, id); |
| | | // 2.2 删除来票 |
| | | invoiceMapper.deleteById(id); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updatePurchaseInvoiceStatus(Long id, Integer status) { |
| | | boolean approve = ErpPurchaseInvoiceAuditStatusEnum.APPROVE.getStatus().equals(status); |
| | | // 1.1 校验存在 |
| | | ErpPurchaseInvoiceDO invoice = validatePurchaseInvoiceExists(id); |
| | | // 1.2 校验状态 |
| | | if (invoice.getAuditStatus().equals(status)) { |
| | | throw exception(approve ? PURCHASE_INVOICE_APPROVE_FAIL : PURCHASE_INVOICE_PROCESS_FAIL); |
| | | } |
| | | |
| | | // 2. 更新状态 |
| | | int updateCount = invoiceMapper.updateByIdAndStatus(id, invoice.getAuditStatus(), |
| | | new ErpPurchaseInvoiceDO().setAuditStatus(status)); |
| | | if (updateCount == 0) { |
| | | throw exception(approve ? PURCHASE_INVOICE_APPROVE_FAIL : PURCHASE_INVOICE_PROCESS_FAIL); |
| | | } |
| | | } |
| | | |
| | | private ErpPurchaseInvoiceDO validatePurchaseInvoiceExists(Long id) { |
| | |
| | | } |
| | | |
| | | @Override |
| | | public List<ErpPurchaseInvoiceDO> getPurchaseInvoiceListByOrderIdAndStatus(Long orderId, Collection<Integer> auditStatuses) { |
| | | return invoiceMapper.selectListByOrderIdAndStatus(orderId, auditStatuses); |
| | | public List<ErpPurchaseInvoiceDO> getPurchaseInvoiceListByOrderId(Long orderId) { |
| | | return invoiceMapper.selectListByOrderId(orderId); |
| | | } |
| | | |
| | | @Override |
| | | public List<ErpPurchaseInvoiceDO> getPurchaseInvoiceListBySupplierIdAndStatus(Long supplierId, Collection<Integer> auditStatuses) { |
| | | return invoiceMapper.selectListBySupplierIdAndStatus(supplierId, auditStatuses); |
| | | public List<ErpPurchaseInvoiceDO> getPurchaseInvoiceListBySupplierId(Long supplierId) { |
| | | return invoiceMapper.selectListBySupplierId(supplierId); |
| | | } |
| | | |
| | | @Override |
| | | public Long getAuditInvoiceCount() { |
| | | return invoiceMapper.selectCountByAudit(); |
| | | public ErpPurchaseInvoiceImportRespVO importPurchaseInvoiceList(List<ErpPurchaseInvoiceImportExcelVO> importInvoices, |
| | | ErpPurchaseInvoiceImportReqVO importReqVO) { |
| | | // 1. 校验非空(过滤完全空白的行) |
| | | importInvoices = filterList(importInvoices, item -> StrUtil.isNotBlank(item.getPurchaseOrderNo())); |
| | | if (CollUtil.isEmpty(importInvoices)) { |
| | | throw exception(PURCHASE_INVOICE_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | // 2. 逐行创建来票(一行一张,来票号由系统生成) |
| | | ErpPurchaseInvoiceImportRespVO respVO = ErpPurchaseInvoiceImportRespVO.builder() |
| | | .createPurchaseInvoiceNos(new ArrayList<>()) |
| | | .failurePurchaseInvoiceNos(new LinkedHashMap<>()) |
| | | .build(); |
| | | for (int i = 0; i < importInvoices.size(); i++) { |
| | | ErpPurchaseInvoiceImportExcelVO row = importInvoices.get(i); |
| | | String rowKey = StrUtil.isNotBlank(row.getInvoiceNo()) ? row.getInvoiceNo() : ("第" + (i + 1) + "行"); |
| | | try { |
| | | ErpPurchaseInvoiceSaveReqVO createReqVO = buildPurchaseInvoiceImportSaveReqVO(row); |
| | | Long invoiceId = createPurchaseInvoice(createReqVO); |
| | | respVO.getCreatePurchaseInvoiceNos().add(invoiceMapper.selectById(invoiceId).getNo()); |
| | | } catch (ServiceException ex) { |
| | | respVO.getFailurePurchaseInvoiceNos().put(rowKey, ex.getMessage()); |
| | | } |
| | | } |
| | | return respVO; |
| | | } |
| | | |
| | | /** |
| | | * 将一行来票转换成创建 Request VO |
| | | */ |
| | | private ErpPurchaseInvoiceSaveReqVO buildPurchaseInvoiceImportSaveReqVO(ErpPurchaseInvoiceImportExcelVO row) { |
| | | // 1. 校验来票日期与金额 |
| | | if (row.getInvoiceTime() == null) { |
| | | throw exception(PURCHASE_INVOICE_IMPORT_INVOICE_TIME_REQUIRED); |
| | | } |
| | | if (row.getPrice() == null) { |
| | | throw exception(PURCHASE_INVOICE_IMPORT_PRICE_REQUIRED); |
| | | } |
| | | // 2. 按采购订单号解析采购订单(供应商由 createPurchaseInvoice 以订单为准回填) |
| | | ErpPurchaseOrderDO purchaseOrder = purchaseOrderMapper.selectByNo(row.getPurchaseOrderNo()); |
| | | if (purchaseOrder == null) { |
| | | throw exception(PURCHASE_INVOICE_IMPORT_ORDER_NOT_EXISTS, row.getPurchaseOrderNo()); |
| | | } |
| | | // 3. 组装保存 VO |
| | | return new ErpPurchaseInvoiceSaveReqVO() |
| | | .setPurchaseOrderId(purchaseOrder.getId()) |
| | | .setInvoiceNo(row.getInvoiceNo()) |
| | | .setInvoiceTitle(row.getInvoiceTitle()) |
| | | .setPrice(row.getPrice()) |
| | | .setInvoiceTime(row.getInvoiceTime()) |
| | | .setRemark(row.getRemark()); |
| | | } |
| | | |
| | | } |