| | |
| | | package cn.iocoder.yudao.module.erp.service.finance; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | 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.finance.vo.payment.ErpFinancePaymentPageReqVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.finance.vo.payment.ErpFinancePaymentSaveReqVO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.finance.ErpFinancePaymentDO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.finance.ErpFinancePaymentItemDO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseInvoiceDO; |
| | | import cn.iocoder.yudao.module.erp.dal.mysql.finance.ErpFinancePaymentItemMapper; |
| | | import cn.iocoder.yudao.module.erp.dal.mysql.finance.ErpFinancePaymentMapper; |
| | | import cn.iocoder.yudao.module.erp.dal.redis.no.ErpNoRedisDAO; |
| | | import cn.iocoder.yudao.module.erp.enums.ErpAuditStatus; |
| | | import cn.iocoder.yudao.module.erp.enums.ErpPurchaseInvoiceAuditStatusEnum; |
| | | import cn.iocoder.yudao.module.erp.service.purchase.ErpPurchaseInvoiceService; |
| | | import cn.iocoder.yudao.module.erp.service.purchase.ErpPurchaseInvoiceServiceImpl; |
| | | import cn.iocoder.yudao.module.erp.service.purchase.ErpSupplierService; |
| | | import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi; |
| | | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| | |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | | 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.*; |
| | |
| | | private ErpSupplierService supplierService; |
| | | @Resource |
| | | private ErpAccountService accountService; |
| | | @Resource |
| | | private ErpPurchaseInvoiceService invoiceService; |
| | | |
| | | @Resource |
| | | private AdminUserApi adminUserApi; |
| | |
| | | if (createReqVO.getFinanceUserId() != null) { |
| | | adminUserApi.validateUser(createReqVO.getFinanceUserId()); |
| | | } |
| | | // 1.5 生成付款单号,并校验唯一性 |
| | | // 1.5 校验关联来票 |
| | | validateInvoiceForPayment(createReqVO.getInvoiceId(), createReqVO.getSupplierId()); |
| | | // 1.6 校验付款金额不超过该来票剩余可付金额 |
| | | validatePaymentPriceExceedsInvoiceRemaining(createReqVO.getInvoiceId(), |
| | | getSumValue(paymentItems, ErpFinancePaymentItemDO::getPaymentPrice, BigDecimal::add, BigDecimal.ZERO) |
| | | .subtract(createReqVO.getDiscountPrice()), null); |
| | | // 1.7 生成付款单号,并校验唯一性 |
| | | String no = noRedisDAO.generate(ErpNoRedisDAO.FINANCE_PAYMENT_NO_PREFIX); |
| | | if (financePaymentMapper.selectByNo(no) != null) { |
| | | throw exception(FINANCE_PAYMENT_NO_EXISTS); |
| | |
| | | // 1.5 校验付款单项的有效性 |
| | | List<ErpFinancePaymentItemDO> paymentItems = validateFinancePaymentItems( |
| | | updateReqVO.getSupplierId(), updateReqVO.getItems()); |
| | | // 1.6 校验关联来票 |
| | | validateInvoiceForPayment(updateReqVO.getInvoiceId(), updateReqVO.getSupplierId()); |
| | | // 1.7 校验付款金额不超过该来票剩余可付金额(排除该付款单自身当前已付金额) |
| | | validatePaymentPriceExceedsInvoiceRemaining(updateReqVO.getInvoiceId(), |
| | | getSumValue(paymentItems, ErpFinancePaymentItemDO::getPaymentPrice, BigDecimal::add, BigDecimal.ZERO) |
| | | .subtract(updateReqVO.getDiscountPrice()), payment.getPaymentPrice()); |
| | | |
| | | // 2.1 更新付款单 |
| | | ErpFinancePaymentDO updateObj = BeanUtils.toBean(updateReqVO, ErpFinancePaymentDO.class); |
| | |
| | | return payment; |
| | | } |
| | | |
| | | /** |
| | | * 校验付款单关联的来票:存在、审批通过、已上传发票附件、供应商与来票一致 |
| | | * |
| | | * @param invoiceId 来票编号 |
| | | * @param supplierId 供应商编号 |
| | | */ |
| | | private void validateInvoiceForPayment(Long invoiceId, Long supplierId) { |
| | | // 1. 校验来票存在 |
| | | ErpPurchaseInvoiceDO invoice = invoiceService.getPurchaseInvoice(invoiceId); |
| | | if (invoice == null) { |
| | | throw exception(FINANCE_PAYMENT_CREATE_FAIL_INVOICE_NOT_EXISTS); |
| | | } |
| | | // 2. 校验来票审批通过 |
| | | if (ObjectUtil.notEqual(invoice.getAuditStatus(), ErpPurchaseInvoiceAuditStatusEnum.APPROVE.getStatus())) { |
| | | throw exception(FINANCE_PAYMENT_CREATE_FAIL_INVOICE_NOT_APPROVE); |
| | | } |
| | | // 3. 校验已上传发票附件 |
| | | if (CollUtil.isEmpty(storageAttachmentApi.listAttachments( |
| | | ErpPurchaseInvoiceServiceImpl.PURCHASE_INVOICE_RECORD_TYPE, invoiceId))) { |
| | | throw exception(FINANCE_PAYMENT_CREATE_FAIL_INVOICE_NOT_ATTACHMENT); |
| | | } |
| | | // 4. 校验供应商与来票一致(防脏数据) |
| | | if (ObjectUtil.notEqual(invoice.getSupplierId(), supplierId)) { |
| | | throw exception(FINANCE_PAYMENT_CREATE_FAIL_INVOICE_SUPPLIER_MISMATCH); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 校验付款金额不超过该来票剩余可付金额(来票金额 - 该来票已付款金额) |
| | | * |
| | | * @param invoiceId 来票编号 |
| | | * @param paymentPrice 本次付款金额 |
| | | * @param oldPaymentPrice 更新场景下,该付款单自身当前的付款金额(用于排除自身),创建场景传 null |
| | | */ |
| | | private void validatePaymentPriceExceedsInvoiceRemaining(Long invoiceId, BigDecimal paymentPrice, BigDecimal oldPaymentPrice) { |
| | | ErpPurchaseInvoiceDO invoice = invoiceService.getPurchaseInvoice(invoiceId); |
| | | BigDecimal paidPrice = financePaymentMapper.selectPaymentPriceMapByInvoiceId(List.of(invoiceId)) |
| | | .getOrDefault(invoiceId, BigDecimal.ZERO); |
| | | if (oldPaymentPrice != null) { |
| | | paidPrice = paidPrice.subtract(oldPaymentPrice); // 排除该付款单自身当前已付金额 |
| | | } |
| | | BigDecimal remainingPrice = invoice.getPrice().subtract(paidPrice); |
| | | if (paymentPrice.compareTo(remainingPrice) > 0) { |
| | | throw exception(FINANCE_PAYMENT_CREATE_FAIL_INVOICE_PRICE_EXCEEDS, remainingPrice); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public ErpFinancePaymentDO getFinancePayment(Long id) { |
| | | return financePaymentMapper.selectById(id); |
| | |
| | | return financePaymentMapper.selectPage(pageReqVO); |
| | | } |
| | | |
| | | @Override |
| | | public Map<Long, BigDecimal> getPaymentPriceMapByInvoiceId(Collection<Long> invoiceIds) { |
| | | return financePaymentMapper.selectPaymentPriceMapByInvoiceId(invoiceIds); |
| | | } |
| | | |
| | | // ==================== 付款单项 ==================== |
| | | |
| | | @Override |