| | |
| | | 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.bpm.enums.task.BpmTaskStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.returnvendor.vo.MesWmReturnVendorImportExcelVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.wm.returnvendor.vo.MesWmReturnVendorImportRespVO; |
| | | 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.dal.dataobject.wm.returnvendor.MesWmReturnVendorDO; |
| | |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.ArrayList; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| | |
| | | */ |
| | | private static final String RETURN_VENDOR_APPROVE_BIZ_TYPE = "return_vendor_approve"; |
| | | |
| | | /** |
| | | * 供应商退货单审批的 BPM 流程定义 Key |
| | | */ |
| | | private static final String RETURN_VENDOR_APPROVE_PROCESS_KEY = "return_vendor_approve"; |
| | | |
| | | @Resource |
| | | private MesWmReturnVendorMapper returnVendorMapper; |
| | | |
| | | @Resource |
| | | private BpmProcessInstanceApi bpmProcessInstanceApi; |
| | | |
| | | @Resource |
| | | private MesWmReturnVendorLineService returnVendorLineService; |
| | |
| | | approvalConfigApi.validateApprovalEnabledAndGetApprovers(RETURN_VENDOR_APPROVE_BIZ_TYPE); |
| | | // 4. 提交审批(草稿 → 待拣货),并清空上一轮审核结果 |
| | | returnVendorMapper.submitAndResetAuditInfo(id, MesWmReturnVendorStatusEnum.APPROVING.getStatus()); |
| | | // 5. 创建 BPM 流程实例,使审批出现在「协同办公 - 我的流程」 |
| | | bpmProcessInstanceApi.createProcessInstance(getLoginUserId(), |
| | | new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(RETURN_VENDOR_APPROVE_PROCESS_KEY) |
| | | .setBusinessKey(String.valueOf(id))); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateReturnVendorAuditStatus(Long id, Integer bpmStatus, String reason) { |
| | | // BPM 审批结果映射为单据状态:通过=待执行退货 / 不通过=草稿 / 取消=已取消 |
| | | Integer status; |
| | | if (BpmTaskStatusEnum.APPROVE.getStatus().equals(bpmStatus)) { |
| | | status = MesWmReturnVendorStatusEnum.APPROVED.getStatus(); |
| | | } else if (BpmTaskStatusEnum.REJECT.getStatus().equals(bpmStatus)) { |
| | | status = MesWmReturnVendorStatusEnum.PREPARE.getStatus(); |
| | | } else if (BpmTaskStatusEnum.CANCEL.getStatus().equals(bpmStatus)) { |
| | | status = MesWmReturnVendorStatusEnum.CANCELED.getStatus(); |
| | | } else { |
| | | return; |
| | | } |
| | | if (returnVendorMapper.updateAuditStatusByBpm(id, status, reason) != 1) { |
| | | log.warn("[updateReturnVendorAuditStatus] 供应商退货单({}) 不处于审批中状态,忽略 BPM 结果({})", id, bpmStatus); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void stockReturnVendor(Long id) { |
| | | // 校验存在 |
| | | // 校验存在 + 已审批(状态只能由 audit 推进:APPROVING → APPROVED) |
| | | MesWmReturnVendorDO returnVendor = validateReturnVendorExists(id); |
| | | if (ObjUtil.notEqual(MesWmReturnVendorStatusEnum.APPROVING.getStatus(), returnVendor.getStatus())) { |
| | | if (ObjUtil.notEqual(MesWmReturnVendorStatusEnum.APPROVED.getStatus(), returnVendor.getStatus())) { |
| | | throw exception(WM_RETURN_VENDOR_STATUS_INVALID); |
| | | } |
| | | // 执行拣货(待拣货 -> 待执行退货) |
| | | returnVendorMapper.updateById(new MesWmReturnVendorDO() |
| | | .setId(id).setStatus(MesWmReturnVendorStatusEnum.APPROVED.getStatus())); |
| | | // 仅校验行数量与明细数量一致,不修改单据状态 |
| | | if (!Boolean.TRUE.equals(checkReturnVendorQuantity(id))) { |
| | | throw exception(WM_RETURN_VENDOR_DETAIL_QUANTITY_MISMATCH); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | if (CollUtil.isEmpty(details)) { |
| | | throw exception(WM_RETURN_VENDOR_NO_DETAIL); |
| | | } |
| | | // 1.3 校验每行明细数量之和等于行退货数量,避免出库数量与单据不一致 |
| | | if (!Boolean.TRUE.equals(checkReturnVendorQuantity(id))) { |
| | | throw exception(WM_RETURN_VENDOR_DETAIL_QUANTITY_MISMATCH); |
| | | } |
| | | |
| | | // 2. 遍历所有明细,创建库存事务(扣减库存 + 记录流水) |
| | | createTransactionList(returnVendor, details); |
| | |
| | | private void createTransactionList(MesWmReturnVendorDO returnVendor, List<MesWmReturnVendorDetailDO> details) { |
| | | wmTransactionService.createTransactionList(convertList(details, detail -> new MesWmTransactionSaveReqDTO() |
| | | .setType(MesWmTransactionTypeEnum.OUT.getType()).setItemId(detail.getItemId()) |
| | | .setQuantity(detail.getQuantity().negate()) // 出库数量为负数 |
| | | .setQuantity(negateQuantity(detail.getQuantity())) // 出库数量为负数 |
| | | .setBatchId(detail.getBatchId()).setBatchCode(detail.getBatchCode()) |
| | | .setWarehouseId(detail.getWarehouseId()).setLocationId(detail.getLocationId()).setAreaId(detail.getAreaId()) |
| | | .setVendorId(returnVendor.getVendorId()) |
| | | .setBizType(MesBizTypeConstants.WM_RETURN_VENDOR).setBizId(returnVendor.getId()) |
| | | .setBizCode(returnVendor.getCode()).setBizLineId(detail.getLineId()))); |
| | | } |
| | | |
| | | /** |
| | | * 出库数量取负:数量为空时抛出明确的业务异常,避免 NPE |
| | | */ |
| | | private BigDecimal negateQuantity(BigDecimal quantity) { |
| | | if (quantity == null) { |
| | | throw exception(WM_RETURN_VENDOR_DETAIL_QUANTITY_INVALID); |
| | | } |
| | | return quantity.negate(); |
| | | } |
| | | |
| | | @Override |
| | |
| | | return returnVendorMapper.selectCountByVendorId(vendorId); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public MesWmReturnVendorImportRespVO importReturnVendorList(List<MesWmReturnVendorImportExcelVO> importList, boolean updateSupport) { |
| | | if (CollUtil.isEmpty(importList)) { |
| | | throw exception(WM_RETURN_VENDOR_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | MesWmReturnVendorImportRespVO respVO = MesWmReturnVendorImportRespVO.builder() |
| | | .createCodes(new ArrayList<>()).updateCodes(new ArrayList<>()) |
| | | .failureCodes(new LinkedHashMap<>()).build(); |
| | | AtomicInteger index = new AtomicInteger(1); |
| | | importList.forEach(importItem -> { |
| | | int currentIndex = index.getAndIncrement(); |
| | | String code = importItem.getCode(); |
| | | String key = StrUtil.blankToDefault(code, "第 " + currentIndex + " 行"); |
| | | if (StrUtil.isBlank(code)) { |
| | | respVO.getFailureCodes().put(key, "单号不能为空"); |
| | | return; |
| | | } |
| | | if (StrUtil.isBlank(importItem.getName())) { |
| | | respVO.getFailureCodes().put(key, "名称不能为空"); |
| | | return; |
| | | } |
| | | MesWmReturnVendorDO existing = returnVendorMapper.selectByCode(code); |
| | | if (existing != null && updateSupport) { |
| | | if (!MesWmReturnVendorStatusEnum.PREPARE.getStatus().equals(existing.getStatus())) { |
| | | respVO.getFailureCodes().put(key, "单据非草稿状态,不允许覆盖更新"); |
| | | return; |
| | | } |
| | | MesWmReturnVendorDO updateObj = BeanUtils.toBean(importItem, MesWmReturnVendorDO.class); |
| | | updateObj.setId(existing.getId()); |
| | | updateObj.setStatus(existing.getStatus()); |
| | | returnVendorMapper.updateById(updateObj); |
| | | respVO.getUpdateCodes().add(code); |
| | | } else if (existing != null) { |
| | | respVO.getFailureCodes().put(key, "单号已存在:" + code); |
| | | } else { |
| | | MesWmReturnVendorDO createObj = BeanUtils.toBean(importItem, MesWmReturnVendorDO.class); |
| | | createObj.setId(null); |
| | | createObj.setStatus(MesWmReturnVendorStatusEnum.PREPARE.getStatus()); |
| | | returnVendorMapper.insert(createObj); |
| | | respVO.getCreateCodes().add(code); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | } |