| | |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjUtil; |
| | | 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.collection.CollectionUtils; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; |
| | | import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi; |
| | | import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.returnvendor.vo.MesWmReturnVendorPageReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.returnvendor.vo.MesWmReturnVendorSaveReqVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.returnvendor.vo.line.MesWmReturnVendorLineSaveReqVO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.returnvendor.MesWmReturnVendorDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.returnvendor.MesWmReturnVendorDetailDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.returnvendor.MesWmReturnVendorLineDO; |
| | | import cn.iocoder.yudao.module.mes.dal.mysql.wm.returnvendor.MesWmReturnVendorMapper; |
| | | import cn.iocoder.yudao.module.mes.enums.MesBizTypeConstants; |
| | | import cn.iocoder.yudao.module.mes.enums.wm.MesWmReturnVendorHandleTypeEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.wm.MesWmReturnVendorStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.wm.MesWmTransactionTypeEnum; |
| | | import cn.iocoder.yudao.module.erp.api.finance.ErpFinancePaymentApi; |
| | | import cn.iocoder.yudao.module.erp.api.finance.dto.ErpFinancePaymentRedFlushCreateReqDTO; |
| | | import cn.iocoder.yudao.module.erp.api.purchase.ErpPurchaseOrderApi; |
| | | import cn.iocoder.yudao.module.erp.api.purchase.dto.ErpPurchaseOrderItemRespDTO; |
| | | import cn.iocoder.yudao.module.erp.api.purchase.dto.ErpPurchaseOrderRespDTO; |
| | | import cn.iocoder.yudao.module.srm.api.supplier.SrmSupplierApi; |
| | | import cn.iocoder.yudao.module.mes.service.wm.arrivalnotice.MesWmArrivalNoticeService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.transaction.MesWmTransactionService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.transaction.dto.MesWmTransactionSaveReqDTO; |
| | | import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.security.core.Authentication; |
| | | import org.springframework.security.core.context.SecurityContextHolder; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDateTime; |
| | | import java.util.HashMap; |
| | | import java.util.HashSet; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| | |
| | | @Validated |
| | | public class MesWmReturnVendorServiceImpl implements MesWmReturnVendorService { |
| | | |
| | | /** |
| | | * 附件业务类型:MES 供应商退货单 |
| | | */ |
| | | public static final String RETURN_VENDOR_RECORD_TYPE = "mes_wm_return_vendor"; |
| | | |
| | | @Resource |
| | | private MesWmReturnVendorMapper returnVendorMapper; |
| | | |
| | |
| | | private SrmSupplierApi srmSupplierApi; |
| | | @Resource |
| | | private MesWmTransactionService wmTransactionService; |
| | | @Resource |
| | | private ErpPurchaseOrderApi purchaseOrderApi; |
| | | @Resource |
| | | private ErpFinancePaymentApi financePaymentApi; |
| | | @Resource |
| | | private MesWmArrivalNoticeService arrivalNoticeService; |
| | | @Resource |
| | | private StorageAttachmentApi storageAttachmentApi; |
| | | @Resource |
| | | private BpmProcessInstanceApi processInstanceApi; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long createReturnVendor(MesWmReturnVendorSaveReqVO createReqVO) { |
| | | // 1. 校验数据 |
| | | validateReturnVendorSaveData(createReqVO); |
| | | // 1.1. 若采购订单号为空但关联了采购订单,则从 ERP 采购订单补齐订单号、合同号 |
| | | fillFromPurchaseOrder(createReqVO); |
| | | |
| | | // 2. 插入主表 |
| | | MesWmReturnVendorDO returnVendor = BeanUtils.toBean(createReqVO, MesWmReturnVendorDO.class); |
| | | returnVendor.setStatus(MesWmReturnVendorStatusEnum.PREPARE.getStatus()); |
| | | returnVendorMapper.insert(returnVendor); |
| | | |
| | | // 2.1. 关联了采购订单:自动把订单物料带入退货行(无可退物料时静默,不影响单据创建) |
| | | if (returnVendor.getPurchaseOrderId() != null) { |
| | | autoImportLinesFromLinkedOrder(returnVendor.getId(), false); |
| | | } |
| | | // 2.2. 绑定附件(质量部发起退货时上传的检验报告、照片等) |
| | | storageAttachmentApi.bindAttachments("file", RETURN_VENDOR_RECORD_TYPE, |
| | | returnVendor.getId(), createReqVO.getBlobIds()); |
| | | return returnVendor.getId(); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateReturnVendor(MesWmReturnVendorSaveReqVO updateReqVO) { |
| | | // 1. 校验数据 |
| | | validateReturnVendorSaveData(updateReqVO); |
| | | // 1. 校验数据(含存在 + 草稿状态) |
| | | MesWmReturnVendorDO oldReturnVendor = validateReturnVendorSaveData(updateReqVO); |
| | | // 1.1. 若采购订单号为空但关联了采购订单,则从 ERP 采购订单补齐订单号、合同号 |
| | | fillFromPurchaseOrder(updateReqVO); |
| | | |
| | | Long oldPurchaseOrderId = oldReturnVendor != null ? oldReturnVendor.getPurchaseOrderId() : null; |
| | | Long newPurchaseOrderId = updateReqVO.getPurchaseOrderId(); |
| | | boolean purchaseOrderChanged = !ObjUtil.equal(oldPurchaseOrderId, newPurchaseOrderId); |
| | | |
| | | // 2. 更新主表 |
| | | MesWmReturnVendorDO updateObj = BeanUtils.toBean(updateReqVO, MesWmReturnVendorDO.class); |
| | | returnVendorMapper.updateById(updateObj); |
| | | |
| | | // 3. 关联了采购订单:自动同步物料行,保证行物料与该采购订单一致 |
| | | // 3.1 更换了采购订单 → 先清空旧订单带入的行,再按新订单重新带入 |
| | | // 3.2 采购订单未变但当前无行 → 兜底自动带入(历史数据或带料被中断的场景) |
| | | if (newPurchaseOrderId != null) { |
| | | autoImportLinesFromLinkedOrder(updateReqVO.getId(), purchaseOrderChanged); |
| | | } |
| | | |
| | | // 4. 增量更新附件(只处理新增/删除,未传 blobIds 时不动现有附件) |
| | | storageAttachmentApi.updateAttachments("file", RETURN_VENDOR_RECORD_TYPE, |
| | | updateReqVO.getId(), updateReqVO.getBlobIds()); |
| | | } |
| | | |
| | | /** |
| | | * 关联了采购订单时,从 ERP 采购订单补齐采购订单号、合同号(合同号只做快照,不允许手工生成) |
| | | */ |
| | | private void fillFromPurchaseOrder(MesWmReturnVendorSaveReqVO reqVO) { |
| | | if (reqVO == null || reqVO.getPurchaseOrderId() == null) { |
| | | return; |
| | | } |
| | | ErpPurchaseOrderRespDTO purchaseOrder = |
| | | purchaseOrderApi.getPurchaseOrder(reqVO.getPurchaseOrderId()).getCheckedData(); |
| | | if (purchaseOrder == null) { |
| | | return; |
| | | } |
| | | if (StrUtil.isBlank(reqVO.getPurchaseOrderCode())) { |
| | | reqVO.setPurchaseOrderCode(purchaseOrder.getNo()); |
| | | } |
| | | if (StrUtil.isBlank(reqVO.getContractNo())) { |
| | | reqVO.setContractNo(purchaseOrder.getContractNo()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | returnVendorDetailService.deleteReturnVendorDetailByReturnId(id); |
| | | // 2.2 级联删除行 |
| | | returnVendorLineService.deleteReturnVendorLineByReturnId(id); |
| | | // 2.3 删除主表 |
| | | // 2.3 删除附件 |
| | | storageAttachmentApi.deleteAttachmentsByRecord(RETURN_VENDOR_RECORD_TYPE, id); |
| | | // 2.4 删除主表 |
| | | returnVendorMapper.deleteById(id); |
| | | } |
| | | |
| | |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void submitReturnVendorApproval(Long id, String processDefinitionKey) { |
| | | // 1. 校验存在 + 草稿状态 + 至少一条行 |
| | | validateReturnVendorExistsAndPrepare(id); |
| | | if (CollUtil.isEmpty(returnVendorLineService.getReturnVendorLineListByReturnId(id))) { |
| | | throw exception(WM_RETURN_VENDOR_NO_LINE); |
| | | } |
| | | |
| | | // 2. 全局免审批:直接置为「待拣货」,无需流程模型 |
| | | if (processInstanceApi.isGlobalAutoApprove()) { |
| | | returnVendorMapper.updateById(new MesWmReturnVendorDO() |
| | | .setId(id) |
| | | .setStatus(MesWmReturnVendorStatusEnum.APPROVING.getStatus()) |
| | | .setProcessDefinitionKey(processDefinitionKey) |
| | | .setProcessInstanceId("")); |
| | | return; |
| | | } |
| | | |
| | | // 3. 创建 BPM 流程实例;流程未发布时给出明确提示 |
| | | String processInstanceId; |
| | | try { |
| | | processInstanceId = processInstanceApi.createProcessInstance(getCurrentUserId(), |
| | | new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(processDefinitionKey) |
| | | .setBusinessKey(String.valueOf(id))); |
| | | } catch (ServiceException ex) { |
| | | if (cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.PROCESS_DEFINITION_NOT_EXISTS |
| | | .getCode().equals(ex.getCode())) { |
| | | throw exception(WM_RETURN_VENDOR_APPROVE_PROCESS_NOT_PUBLISHED, processDefinitionKey); |
| | | } |
| | | throw ex; |
| | | } |
| | | |
| | | // 4. 更新退货单状态为审批中 |
| | | returnVendorMapper.updateById(new MesWmReturnVendorDO() |
| | | .setId(id) |
| | | .setStatus(MesWmReturnVendorStatusEnum.BPM_APPROVING.getStatus()) |
| | | .setProcessDefinitionKey(processDefinitionKey) |
| | | .setProcessInstanceId(processInstanceId)); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateReturnVendorAuditStatus(Long id, Integer bpmResult) { |
| | | // 1. 校验存在 + 审批中 |
| | | MesWmReturnVendorDO returnVendor = validateReturnVendorExists(id); |
| | | if (!MesWmReturnVendorStatusEnum.BPM_APPROVING.getStatus().equals(returnVendor.getStatus())) { |
| | | return; |
| | | } |
| | | |
| | | // 2. 根据审批结果更新状态:通过 -> 待拣货;驳回 -> 草稿;取消 -> 已取消 |
| | | Integer newStatus = convertBpmResultToReturnVendorStatus(bpmResult); |
| | | if (newStatus != null) { |
| | | returnVendorMapper.updateById(new MesWmReturnVendorDO().setId(id).setStatus(newStatus)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转换 BPM 审批结果为供应商退货单状态 |
| | | */ |
| | | private Integer convertBpmResultToReturnVendorStatus(Integer bpmResult) { |
| | | if (cn.iocoder.yudao.module.bpm.enums.task.BpmProcessInstanceStatusEnum.APPROVE.getStatus().equals(bpmResult)) { |
| | | return MesWmReturnVendorStatusEnum.APPROVING.getStatus(); |
| | | } |
| | | if (cn.iocoder.yudao.module.bpm.enums.task.BpmProcessInstanceStatusEnum.REJECT.getStatus().equals(bpmResult)) { |
| | | return MesWmReturnVendorStatusEnum.PREPARE.getStatus(); |
| | | } |
| | | if (cn.iocoder.yudao.module.bpm.enums.task.BpmProcessInstanceStatusEnum.CANCEL.getStatus().equals(bpmResult)) { |
| | | return MesWmReturnVendorStatusEnum.CANCELED.getStatus(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void stockReturnVendor(Long id) { |
| | | // 校验存在 |
| | | MesWmReturnVendorDO returnVendor = validateReturnVendorExists(id); |
| | |
| | | throw exception(WM_RETURN_VENDOR_NO_DETAIL); |
| | | } |
| | | |
| | | // 1.3 校验处理方式(退款 / 换货) |
| | | if (!MesWmReturnVendorHandleTypeEnum.isValid(returnVendor.getHandleType())) { |
| | | throw exception(WM_RETURN_VENDOR_HANDLE_TYPE_INVALID, returnVendor.getHandleType()); |
| | | } |
| | | |
| | | // 2. 遍历所有明细,创建库存事务(扣减库存 + 记录流水) |
| | | createTransactionList(returnVendor, details); |
| | | |
| | | // 3. 更新退货单状态 |
| | | // 3. 回写采购订单行的已退货数量(按物料匹配采购订单明细) |
| | | writeBackPurchaseOrderReturnCount(returnVendor); |
| | | |
| | | // 4. 先置为已完成,保证重复调用会被状态校验拦截(幂等) |
| | | returnVendorMapper.updateById(new MesWmReturnVendorDO() |
| | | .setId(id).setStatus(MesWmReturnVendorStatusEnum.FINISHED.getStatus())); |
| | | |
| | | // 5. 费用处理:退款 -> 负数冲红付款单;换货 -> 换货到货通知单(重新入库 + 重新质检) |
| | | if (MesWmReturnVendorHandleTypeEnum.REFUND.getType().equals(returnVendor.getHandleType())) { |
| | | createRedFlushPayment(id); |
| | | } else if (MesWmReturnVendorHandleTypeEnum.REPLACEMENT.getType().equals(returnVendor.getHandleType())) { |
| | | createReplacementNotice(id); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 把退货数量增量回写到关联采购订单的明细行,并同步订单级退货数量 |
| | | */ |
| | | private void writeBackPurchaseOrderReturnCount(MesWmReturnVendorDO returnVendor) { |
| | | if (returnVendor.getPurchaseOrderId() == null) { |
| | | return; |
| | | } |
| | | List<MesWmReturnVendorLineDO> lines = returnVendorLineService.getReturnVendorLineListByReturnId(returnVendor.getId()); |
| | | if (CollUtil.isEmpty(lines)) { |
| | | return; |
| | | } |
| | | List<ErpPurchaseOrderItemRespDTO> orderItems = purchaseOrderApi |
| | | .getPurchaseOrderItemList(returnVendor.getPurchaseOrderId()).getCheckedData(); |
| | | if (CollUtil.isEmpty(orderItems)) { |
| | | return; |
| | | } |
| | | Map<Long, Long> purchaseOrderItemIdMap = new HashMap<>(); |
| | | orderItems.stream() |
| | | .filter(item -> item.getProductId() != null) |
| | | .forEach(item -> purchaseOrderItemIdMap.putIfAbsent(item.getProductId(), item.getId())); |
| | | for (MesWmReturnVendorLineDO line : lines) { |
| | | Long purchaseOrderItemId = purchaseOrderItemIdMap.get(line.getItemId()); |
| | | if (purchaseOrderItemId == null || line.getQuantity() == null) { |
| | | continue; |
| | | } |
| | | purchaseOrderApi.updateOrderItemReturnCount(purchaseOrderItemId, line.getQuantity()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long createRedFlushPayment(Long id) { |
| | | // 1. 校验存在 + 已完成 + 处理方式为退款 |
| | | MesWmReturnVendorDO returnVendor = validateReturnVendorFinished(id); |
| | | if (!MesWmReturnVendorHandleTypeEnum.REFUND.getType().equals(returnVendor.getHandleType())) { |
| | | throw exception(WM_RETURN_VENDOR_HANDLE_TYPE_INVALID, returnVendor.getHandleType()); |
| | | } |
| | | // 2. 幂等:已生成过冲红单直接返回 |
| | | if (returnVendor.getRedFlushPaymentId() != null) { |
| | | return returnVendor.getRedFlushPaymentId(); |
| | | } |
| | | // 3. 计算退款金额(未填写时按采购单价 × 退货数量) |
| | | BigDecimal refundAmount = resolveRefundAmount(returnVendor); |
| | | |
| | | // 4. 生成负数冲红付款单 |
| | | Long paymentId = financePaymentApi.createRedFlushPayment(new ErpFinancePaymentRedFlushCreateReqDTO() |
| | | .setSupplierId(returnVendor.getVendorId()) |
| | | .setRefundAmount(refundAmount) |
| | | .setSourceId(returnVendor.getId()) |
| | | .setSourceNo(returnVendor.getCode()) |
| | | .setFinanceUserId(getCurrentUserId()) |
| | | .setRemark("供应商退货单[" + returnVendor.getCode() + "]退款冲红,合同号[" + returnVendor.getContractNo() + "]")) |
| | | .getCheckedData(); |
| | | |
| | | // 5. 回写冲红单号与最终退款金额 |
| | | returnVendorMapper.updateById(new MesWmReturnVendorDO() |
| | | .setId(id).setRefundAmount(refundAmount).setRedFlushPaymentId(paymentId)); |
| | | return paymentId; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Long createReplacementNotice(Long id) { |
| | | // 1. 校验存在 + 已完成 + 处理方式为换货 |
| | | MesWmReturnVendorDO returnVendor = validateReturnVendorFinished(id); |
| | | if (!MesWmReturnVendorHandleTypeEnum.REPLACEMENT.getType().equals(returnVendor.getHandleType())) { |
| | | throw exception(WM_RETURN_VENDOR_HANDLE_TYPE_INVALID, returnVendor.getHandleType()); |
| | | } |
| | | // 2. 幂等:已生成过换货到货单直接返回 |
| | | if (returnVendor.getReplacementNoticeId() != null) { |
| | | return returnVendor.getReplacementNoticeId(); |
| | | } |
| | | |
| | | // 3. 生成换货到货通知单(待质检,行自动创建 IQC) |
| | | Long noticeId = arrivalNoticeService.createReplacementArrivalNotice(id); |
| | | |
| | | // 4. 回写到货通知单编号 |
| | | returnVendorMapper.updateById(new MesWmReturnVendorDO() |
| | | .setId(id).setReplacementNoticeId(noticeId)); |
| | | return noticeId; |
| | | } |
| | | |
| | | /** |
| | | * 解析退款金额:优先取单据填写值;未填写时按「采购单价 × 退货数量」逐行累加 |
| | | */ |
| | | private BigDecimal resolveRefundAmount(MesWmReturnVendorDO returnVendor) { |
| | | if (returnVendor.getRefundAmount() != null |
| | | && returnVendor.getRefundAmount().compareTo(BigDecimal.ZERO) > 0) { |
| | | return returnVendor.getRefundAmount(); |
| | | } |
| | | if (returnVendor.getPurchaseOrderId() == null) { |
| | | throw exception(WM_RETURN_VENDOR_REFUND_AMOUNT_REQUIRED, |
| | | returnVendor.getCode(), "未关联采购订单"); |
| | | } |
| | | List<ErpPurchaseOrderItemRespDTO> orderItems = purchaseOrderApi |
| | | .getPurchaseOrderItemList(returnVendor.getPurchaseOrderId()).getCheckedData(); |
| | | Map<Long, BigDecimal> priceMap = new HashMap<>(); |
| | | if (CollUtil.isNotEmpty(orderItems)) { |
| | | orderItems.stream().filter(item -> item.getProductId() != null) |
| | | .forEach(item -> priceMap.putIfAbsent(item.getProductId(), item.getProductPrice())); |
| | | } |
| | | List<MesWmReturnVendorLineDO> lines = returnVendorLineService.getReturnVendorLineListByReturnId(returnVendor.getId()); |
| | | if (CollUtil.isEmpty(lines)) { |
| | | throw exception(WM_RETURN_VENDOR_NO_LINE); |
| | | } |
| | | BigDecimal total = BigDecimal.ZERO; |
| | | for (MesWmReturnVendorLineDO line : lines) { |
| | | BigDecimal price = priceMap.get(line.getItemId()); |
| | | if (price == null) { |
| | | throw exception(WM_RETURN_VENDOR_REFUND_AMOUNT_REQUIRED, |
| | | returnVendor.getCode(), "物料ID=" + line.getItemId() + " 未匹配到采购单价"); |
| | | } |
| | | BigDecimal quantity = line.getQuantity() != null ? line.getQuantity() : BigDecimal.ZERO; |
| | | total = total.add(price.multiply(quantity)); |
| | | } |
| | | if (total.compareTo(BigDecimal.ZERO) <= 0) { |
| | | throw exception(WM_RETURN_VENDOR_REFUND_AMOUNT_REQUIRED, |
| | | returnVendor.getCode(), "采购单价 × 退货数量合计为 0"); |
| | | } |
| | | return total; |
| | | } |
| | | |
| | | /** |
| | | * 校验退货单存在且已完成(费用类操作仅在完成后退货单上执行) |
| | | */ |
| | | private MesWmReturnVendorDO validateReturnVendorFinished(Long id) { |
| | | MesWmReturnVendorDO returnVendor = validateReturnVendorExists(id); |
| | | if (!MesWmReturnVendorStatusEnum.FINISHED.getStatus().equals(returnVendor.getStatus())) { |
| | | throw exception(WM_RETURN_VENDOR_FINISHED_REQUIRED, returnVendor.getCode(), |
| | | resolveStatusName(returnVendor.getStatus())); |
| | | } |
| | | return returnVendor; |
| | | } |
| | | |
| | | /** |
| | | * 状态值转名称,解析失败回退为 ID 字符串 |
| | | */ |
| | | private String resolveStatusName(Integer status) { |
| | | for (MesWmReturnVendorStatusEnum value : MesWmReturnVendorStatusEnum.values()) { |
| | | if (value.getStatus().equals(status)) { |
| | | return value.getName(); |
| | | } |
| | | } |
| | | return "ID=" + status; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前用户编号(BPM 流程发起人、财务人员) |
| | | */ |
| | | private Long getCurrentUserId() { |
| | | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| | | if (authentication != null && authentication.getPrincipal() instanceof Long) { |
| | | return (Long) authentication.getPrincipal(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | private void createTransactionList(MesWmReturnVendorDO returnVendor, List<MesWmReturnVendorDetailDO> details) { |
| | |
| | | |
| | | /** |
| | | * 校验退货单创建/修改的公共数据 |
| | | * |
| | | * @return 修改时返回旧单据(用于比较采购订单是否变更);创建时返回 null |
| | | */ |
| | | private void validateReturnVendorSaveData(MesWmReturnVendorSaveReqVO reqVO) { |
| | | private MesWmReturnVendorDO validateReturnVendorSaveData(MesWmReturnVendorSaveReqVO reqVO) { |
| | | // 校验存在 + 草稿状态(仅修改时) |
| | | MesWmReturnVendorDO oldReturnVendor = null; |
| | | if (reqVO.getId() != null) { |
| | | validateReturnVendorExistsAndPrepare(reqVO.getId()); |
| | | oldReturnVendor = validateReturnVendorExistsAndPrepare(reqVO.getId()); |
| | | } |
| | | // 校验供应商存在 |
| | | srmSupplierApi.getSupplier(reqVO.getVendorId()); |
| | | // 校验编码唯一 |
| | | validateCodeUnique(reqVO.getId(), reqVO.getCode()); |
| | | return oldReturnVendor; |
| | | } |
| | | |
| | | /** |
| | | * 把关联采购订单的物料自动带入退货单行(仅草稿状态且已关联采购订单时生效)。 |
| | | * |
| | | * @param id 退货单编号 |
| | | * @param clearFirst 是否先清空已有行再带入;更换采购订单时传 true |
| | | */ |
| | | private void autoImportLinesFromLinkedOrder(Long id, boolean clearFirst) { |
| | | MesWmReturnVendorDO returnVendor = returnVendorMapper.selectById(id); |
| | | if (returnVendor == null || returnVendor.getPurchaseOrderId() == null) { |
| | | return; |
| | | } |
| | | List<MesWmReturnVendorLineDO> existingLines = |
| | | returnVendorLineService.getReturnVendorLineListByReturnId(id); |
| | | if (clearFirst) { |
| | | if (CollUtil.isNotEmpty(existingLines)) { |
| | | // 级联删除旧明细与行 |
| | | returnVendorDetailService.deleteReturnVendorDetailByReturnId(id); |
| | | returnVendorLineService.deleteReturnVendorLineByReturnId(id); |
| | | } |
| | | } else if (CollUtil.isNotEmpty(existingLines)) { |
| | | // 已有行且未更换采购订单:不重复带入,避免覆盖用户已调整的数量 |
| | | return; |
| | | } |
| | | try { |
| | | importPurchaseOrderItems(id); |
| | | } catch (ServiceException ignore) { |
| | | // 采购订单无明细 / 无可退物料等业务异常:单据主体仍正常保存,可在物料区另行处理 |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | return returnVendorMapper.selectCountByVendorId(vendorId); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int importPurchaseOrderItems(Long id) { |
| | | // 1. 校验退货单存在且为草稿状态 |
| | | MesWmReturnVendorDO returnVendor = validateReturnVendorExistsAndPrepare(id); |
| | | if (returnVendor.getPurchaseOrderId() == null) { |
| | | throw exception(WM_RETURN_VENDOR_PURCHASE_ORDER_REQUIRED); |
| | | } |
| | | |
| | | // 2. 获取采购订单及明细 |
| | | ErpPurchaseOrderRespDTO purchaseOrder = purchaseOrderApi.getPurchaseOrder(returnVendor.getPurchaseOrderId()).getCheckedData(); |
| | | if (purchaseOrder == null) { |
| | | throw exception(WM_PURCHASE_ORDER_NOT_EXISTS); |
| | | } |
| | | List<ErpPurchaseOrderItemRespDTO> items = purchaseOrderApi.getPurchaseOrderItemList(returnVendor.getPurchaseOrderId()).getCheckedData(); |
| | | if (CollUtil.isEmpty(items)) { |
| | | throw exception(WM_RETURN_VENDOR_PURCHASE_ORDER_NO_ITEM); |
| | | } |
| | | |
| | | // 3. 已存在的行物料集合(按物料去重,避免重复带入) |
| | | Set<Long> existsItemIds = new HashSet<>(); |
| | | List<MesWmReturnVendorLineDO> existingLines = returnVendorLineService.getReturnVendorLineListByReturnId(id); |
| | | if (CollUtil.isNotEmpty(existingLines)) { |
| | | existsItemIds.addAll(convertList(existingLines, MesWmReturnVendorLineDO::getItemId)); |
| | | } |
| | | |
| | | // 4. 遍历采购订单明细,生成退货行(可退数量 = 已入库 - 已退货) |
| | | int importedCount = 0; |
| | | for (ErpPurchaseOrderItemRespDTO item : items) { |
| | | if (item.getProductId() == null || existsItemIds.contains(item.getProductId())) { |
| | | continue; |
| | | } |
| | | BigDecimal inCount = item.getInCount() != null ? item.getInCount() : BigDecimal.ZERO; |
| | | BigDecimal returnCount = item.getReturnCount() != null ? item.getReturnCount() : BigDecimal.ZERO; |
| | | BigDecimal quantity = inCount.subtract(returnCount); |
| | | if (quantity.compareTo(BigDecimal.ZERO) <= 0) { |
| | | continue; |
| | | } |
| | | MesWmReturnVendorLineSaveReqVO lineReqVO = new MesWmReturnVendorLineSaveReqVO(); |
| | | lineReqVO.setReturnId(id); |
| | | lineReqVO.setItemId(item.getProductId()); |
| | | lineReqVO.setQuantity(quantity); |
| | | lineReqVO.setRemark(item.getRemark()); |
| | | returnVendorLineService.createReturnVendorLine(lineReqVO); |
| | | importedCount++; |
| | | } |
| | | return importedCount; |
| | | } |
| | | |
| | | } |