| | |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| | | import cn.iocoder.yudao.module.erp.api.finance.dto.ErpFinancePaymentRedFlushCreateReqDTO; |
| | | import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi; |
| | | import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO; |
| | | import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmCategoryDO; |
| | |
| | | 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.common.ErpBizTypeEnum; |
| | | import cn.iocoder.yudao.module.erp.enums.finance.ErpFinancePaymentSourceTypeEnum; |
| | | 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 org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDateTime; |
| | | import java.util.*; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long createRedFlushPayment(ErpFinancePaymentRedFlushCreateReqDTO reqDTO) { |
| | | // 1.1 校验来源单据:冲红单必须能追溯到退货单 |
| | | if (reqDTO.getSourceId() == null || StrUtil.isBlank(reqDTO.getSourceNo())) { |
| | | throw exception(FINANCE_PAYMENT_RED_FLUSH_SOURCE_REQUIRED); |
| | | } |
| | | // 1.2 校验供应商 |
| | | if (reqDTO.getSupplierId() == null) { |
| | | throw exception(FINANCE_PAYMENT_RED_FLUSH_SUPPLIER_REQUIRED); |
| | | } |
| | | supplierService.validateSupplier(reqDTO.getSupplierId()); |
| | | // 1.3 校验退款金额 |
| | | if (reqDTO.getRefundAmount() == null || reqDTO.getRefundAmount().compareTo(BigDecimal.ZERO) <= 0) { |
| | | throw exception(FINANCE_PAYMENT_RED_FLUSH_AMOUNT_INVALID, reqDTO.getRefundAmount()); |
| | | } |
| | | // 1.4 校验财务人员 |
| | | if (reqDTO.getFinanceUserId() != null) { |
| | | adminUserApi.validateUser(reqDTO.getFinanceUserId()); |
| | | } |
| | | // 1.5 幂等:同一来源退货单只允许生成一张冲红单 |
| | | ErpFinancePaymentDO existing = financePaymentMapper.selectOne(new LambdaQueryWrapperX<ErpFinancePaymentDO>() |
| | | .eq(ErpFinancePaymentDO::getSourceType, |
| | | ErpFinancePaymentSourceTypeEnum.PURCHASE_RETURN_RED_FLUSH.getType()) |
| | | .eq(ErpFinancePaymentDO::getSourceId, reqDTO.getSourceId())); |
| | | if (existing != null) { |
| | | log.info("[createRedFlushPayment][退货单({}) 已存在冲红单({}),直接复用]", |
| | | reqDTO.getSourceNo(), existing.getNo()); |
| | | return existing.getId(); |
| | | } |
| | | |
| | | // 2.1 生成付款单号 |
| | | String no = noRedisDAO.generate(ErpNoRedisDAO.FINANCE_PAYMENT_NO_PREFIX); |
| | | if (financePaymentMapper.selectByNo(no) != null) { |
| | | throw exception(FINANCE_PAYMENT_NO_EXISTS); |
| | | } |
| | | // 2.2 金额按负数入账 |
| | | BigDecimal negativeAmount = reqDTO.getRefundAmount().negate(); |
| | | ErpFinancePaymentDO payment = new ErpFinancePaymentDO() |
| | | .setNo(no) |
| | | .setStatus(ErpAuditStatus.APPROVE.getStatus()) |
| | | .setPaymentTime(LocalDateTime.now()) |
| | | .setFinanceUserId(reqDTO.getFinanceUserId()) |
| | | .setSupplierId(reqDTO.getSupplierId()) |
| | | .setAccountName(reqDTO.getAccountName()) |
| | | .setTotalPrice(negativeAmount) |
| | | .setDiscountPrice(BigDecimal.ZERO) |
| | | .setPaymentPrice(negativeAmount) |
| | | .setRemark(StrUtil.blankToDefault(reqDTO.getRemark(), "采购退货冲红")) |
| | | .setSourceType(ErpFinancePaymentSourceTypeEnum.PURCHASE_RETURN_RED_FLUSH.getType()) |
| | | .setSourceId(reqDTO.getSourceId()) |
| | | .setSourceNo(reqDTO.getSourceNo()); |
| | | financePaymentMapper.insert(payment); |
| | | |
| | | // 3. 插入付款项(业务类型:采购退货) |
| | | financePaymentItemMapper.insert(new ErpFinancePaymentItemDO() |
| | | .setPaymentId(payment.getId()) |
| | | .setBizType(ErpBizTypeEnum.PURCHASE_RETURN.getType()) |
| | | .setBizId(reqDTO.getSourceId()) |
| | | .setBizNo(reqDTO.getSourceNo()) |
| | | .setTotalPrice(negativeAmount) |
| | | .setPaidPrice(BigDecimal.ZERO) |
| | | .setPaymentPrice(negativeAmount) |
| | | .setRemark(StrUtil.blankToDefault(reqDTO.getRemark(), "采购退货冲红"))); |
| | | return payment.getId(); |
| | | } |
| | | |
| | | @Override |
| | | public ErpFinancePaymentDO getFinancePayment(Long id) { |
| | | return financePaymentMapper.selectById(id); |
| | | } |