| | |
| | | import cn.iocoder.yudao.module.mes.enums.MesBizTypeConstants; |
| | | import cn.iocoder.yudao.module.mes.enums.wm.MesWmItemReceiptStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.enums.wm.MesWmTransactionTypeEnum; |
| | | import cn.iocoder.yudao.module.bpm.enums.task.BpmTaskStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.service.qc.iqc.MesQcIqcService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.arrivalnotice.MesWmArrivalNoticeService; |
| | | import cn.iocoder.yudao.module.mes.service.wm.arrivalnotice.MesWmArrivalNoticeLineService; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.arrivalnotice.MesWmArrivalNoticeLineDO; |
| | | 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.bpm.api.task.BpmProcessInstanceApi; |
| | | import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO; |
| | | import cn.iocoder.yudao.module.erp.api.purchase.ErpPurchaseOrderApi; |
| | | import cn.iocoder.yudao.module.erp.api.purchase.ErpSupplierApi; |
| | | import cn.iocoder.yudao.module.erp.api.purchase.PurchaseOrderApi; |
| | | import cn.iocoder.yudao.module.srm.api.supplier.SrmSupplierApi; |
| | | import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi; |
| | | import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO; |
| | | import org.springframework.security.core.Authentication; |
| | | import org.springframework.security.core.context.SecurityContextHolder; |
| | | import cn.iocoder.yudao.module.system.api.approval.ApprovalConfigApi; |
| | | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| | | import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | import java.util.Collection; |
| | | 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; |
| | | import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*; |
| | | |
| | | /** |
| | |
| | | @Validated |
| | | @Slf4j |
| | | public class MesWmItemReceiptServiceImpl implements MesWmItemReceiptService { |
| | | |
| | | /** |
| | | * 采购入库单审批的业务类型编码 |
| | | * |
| | | * 与 system_approval_config.biz_type 对应,审核人在「系统管理 - 审批配置」中维护。 |
| | | */ |
| | | private static final String ITEM_RECEIPT_APPROVE_BIZ_TYPE = "item_receipt_approve"; |
| | | |
| | | @Resource |
| | | private MesWmItemReceiptMapper itemReceiptMapper; |
| | |
| | | private MesWmArrivalNoticeService arrivalNoticeService; |
| | | @Resource |
| | | @Lazy |
| | | private ErpSupplierApi supplierApi; |
| | | private SrmSupplierApi srmSupplierApi; |
| | | @Resource |
| | | @Lazy |
| | | private MesQcIqcService iqcService; |
| | |
| | | private MesWmArrivalNoticeLineService arrivalNoticeLineService; |
| | | |
| | | @Resource |
| | | private BpmProcessInstanceApi processInstanceApi; |
| | | private AdminUserApi adminUserApi; |
| | | |
| | | @Resource |
| | | private ErpPurchaseOrderApi purchaseOrderApi; |
| | | private ApprovalConfigApi approvalConfigApi; |
| | | |
| | | @Resource |
| | | private PurchaseOrderApi purchaseOrderApi; |
| | | |
| | | @Resource |
| | | private MdmItemApi mdmItemApi; |
| | |
| | | // 校验编码唯一 |
| | | validateCodeUnique(reqVO.getId(), reqVO.getCode()); |
| | | // 校验供应商存在(采购入库使用 ERP 供应商) |
| | | supplierApi.validateSupplier(reqVO.getVendorId()); |
| | | srmSupplierApi.getSupplier(reqVO.getVendorId()); |
| | | // 校验到货通知单存在 |
| | | if (reqVO.getNoticeId() != null) { |
| | | MesWmArrivalNoticeDO notice = arrivalNoticeService.validateArrivalNoticeReadyForReceipt(reqVO.getNoticeId()); |
| | |
| | | // 校验物料批次管理:如果物料开启批次管理,则明细的批次号不能为空 |
| | | validateBatchManagement(lines); |
| | | |
| | | // 提交(草稿 → 待上架) |
| | | itemReceiptMapper.updateById(new MesWmItemReceiptDO() |
| | | .setId(id).setStatus(MesWmItemReceiptStatusEnum.APPROVING.getStatus())); |
| | | // 校验审批配置已启用且配置了有效审核人(未配置时抛出带明确提示的业务异常) |
| | | approvalConfigApi.validateApprovalEnabledAndGetApprovers(ITEM_RECEIPT_APPROVE_BIZ_TYPE); |
| | | |
| | | // 提交(草稿 → 待上架),并清空上一轮审核结果 |
| | | itemReceiptMapper.submitAndResetAuditInfo(id, MesWmItemReceiptStatusEnum.APPROVING.getStatus()); |
| | | } |
| | | |
| | | @Override |
| | | public Set<Long> getItemReceiptApproverUserIds() { |
| | | return approvalConfigApi.getApproverUserIds(ITEM_RECEIPT_APPROVE_BIZ_TYPE); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void auditItemReceipt(Long id, Boolean pass, String reviewRemark) { |
| | | // 1. 校验存在 |
| | | MesWmItemReceiptDO receipt = validateItemReceiptExists(id); |
| | | // 2. 校验处于待上架状态 |
| | | if (!MesWmItemReceiptStatusEnum.APPROVING.getStatus().equals(receipt.getStatus())) { |
| | | throw exception(WM_ITEM_RECEIPT_NOT_APPROVING); |
| | | } |
| | | // 3. 校验当前登录用户是该业务类型的审核人(或签:任一人均可审核) |
| | | Long userId = getLoginUserId(); |
| | | approvalConfigApi.validateApprover(ITEM_RECEIPT_APPROVE_BIZ_TYPE, userId); |
| | | // 4. 审核不通过:必须填写原因,退回草稿可修改后重新提交 |
| | | if (!Boolean.TRUE.equals(pass)) { |
| | | if (StrUtil.isBlank(reviewRemark)) { |
| | | throw exception(WM_ITEM_RECEIPT_REJECT_REASON_REQUIRED); |
| | | } |
| | | itemReceiptMapper.auditItemReceipt(id, MesWmItemReceiptStatusEnum.PREPARE.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark); |
| | | return; |
| | | } |
| | | // 5. 审核通过:记录审核人与审核意见,状态置为待执行入库 |
| | | itemReceiptMapper.auditItemReceipt(id, MesWmItemReceiptStatusEnum.APPROVED.getStatus(), |
| | | userId, getUserNickname(userId), reviewRemark); |
| | | } |
| | | |
| | | /** |
| | | * 获取用户昵称 |
| | | * |
| | | * @param userId 用户编号 |
| | | * @return 昵称,用户不存在时返回 null |
| | | */ |
| | | private String getUserNickname(Long userId) { |
| | | if (userId == null) { |
| | | return null; |
| | | } |
| | | AdminUserRespDTO user = adminUserApi.getUser(userId); |
| | | return user == null ? null : user.getNickname(); |
| | | } |
| | | |
| | | /** |
| | |
| | | if (item == null) { |
| | | continue; |
| | | } |
| | | // 如果物料开启批次管理,检查行上的批次号是否为空 |
| | | if (Boolean.TRUE.equals(item.getIsBatchManaged()) && line.getBatchId() == null) { |
| | | throw exception(WM_ITEM_RECEIPT_BATCH_REQUIRED, item.getCode(), item.getName()); |
| | | // 如果物料开启批次管理,行或其上架明细必须携带批次号,否则无法生成库存事务 |
| | | if (Boolean.TRUE.equals(item.getIsBatchManaged())) { |
| | | boolean hasBatch = line.getBatchId() != null |
| | | || hasDetailBatch(itemReceiptDetailService.getItemReceiptDetailListByLineId(line.getId())); |
| | | if (!hasBatch) { |
| | | throw exception(WM_ITEM_RECEIPT_BATCH_REQUIRED, item.getCode(), item.getName()); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | private boolean hasDetailBatch(List<MesWmItemReceiptDetailDO> details) { |
| | | return details.stream().anyMatch(detail -> detail.getBatchId() != null); |
| | | } |
| | | |
| | | @Override |
| | |
| | | * 回写 ERP 采购订单的入库数量 |
| | | */ |
| | | private void updateErpPurchaseOrderInCount(MesWmItemReceiptDO receipt) { |
| | | // 获取入库单行 |
| | | List<MesWmItemReceiptLineDO> lines = itemReceiptLineService.getItemReceiptLineListByReceiptId(receipt.getId()); |
| | | if (CollUtil.isEmpty(lines)) { |
| | | return; |
| | | // 1. 确定采购订单 ID:优先从入库单头取(从到货通知单带入),其次从行反查 |
| | | Long purchaseOrderId = receipt.getPurchaseOrderId(); |
| | | if (purchaseOrderId == null && receipt.getNoticeId() != null) { |
| | | // 从关联的到货通知单获取采购订单 ID |
| | | MesWmArrivalNoticeDO notice = arrivalNoticeService.getArrivalNotice(receipt.getNoticeId()); |
| | | if (notice != null) { |
| | | purchaseOrderId = notice.getPurchaseOrderId(); |
| | | } |
| | | } |
| | | // 用于存储采购订单 ID,避免重复更新 |
| | | Long purchaseOrderId = null; |
| | | // 遍历行,按 purchaseOrderItemId 更新入库数量 |
| | | for (MesWmItemReceiptLineDO line : lines) { |
| | | if (line.getPurchaseOrderItemId() != null && line.getReceivedQuantity() != null) { |
| | | purchaseOrderApi.updateOrderItemInCount(line.getPurchaseOrderItemId(), line.getReceivedQuantity()); |
| | | // 获取采购订单 ID(从第一个有效的明细项获取) |
| | | if (purchaseOrderId == null) { |
| | | cn.iocoder.yudao.module.erp.api.purchase.dto.ErpPurchaseOrderItemRespDTO itemDTO = |
| | | purchaseOrderApi.getPurchaseOrderItem(line.getPurchaseOrderItemId()).getCheckedData(); |
| | | if (itemDTO != null && itemDTO.getOrderId() != null) { |
| | | purchaseOrderId = itemDTO.getOrderId(); |
| | | |
| | | // 2. 获取入库单行,按 purchaseOrderItemId 更新入库数量 |
| | | List<MesWmItemReceiptLineDO> lines = itemReceiptLineService.getItemReceiptLineListByReceiptId(receipt.getId()); |
| | | if (CollUtil.isNotEmpty(lines)) { |
| | | for (MesWmItemReceiptLineDO line : lines) { |
| | | if (line.getPurchaseOrderItemId() != null && line.getReceivedQuantity() != null) { |
| | | purchaseOrderApi.updateOrderItemInCount(line.getPurchaseOrderItemId(), line.getReceivedQuantity()); |
| | | // 如果还没拿到 purchaseOrderId,从明细项反查 |
| | | if (purchaseOrderId == null) { |
| | | cn.iocoder.yudao.module.erp.api.purchase.dto.ErpPurchaseOrderItemRespDTO itemDTO = |
| | | purchaseOrderApi.getPurchaseOrderItem(line.getPurchaseOrderItemId()).getCheckedData(); |
| | | if (itemDTO != null && itemDTO.getOrderId() != null) { |
| | | purchaseOrderId = itemDTO.getOrderId(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } else if (receipt.getNoticeId() != null) { |
| | | // 兜底:入库行为空时,从到货通知单行回写入库数量 |
| | | List<MesWmArrivalNoticeLineDO> noticeLines = arrivalNoticeLineService |
| | | .getArrivalNoticeLineListByNoticeId(receipt.getNoticeId()); |
| | | for (MesWmArrivalNoticeLineDO noticeLine : noticeLines) { |
| | | if (noticeLine.getPurchaseOrderItemId() != null) { |
| | | // 使用合格数量(有 IQC 时为合格数量,否则为到货数量) |
| | | BigDecimal qty = noticeLine.getQualifiedQuantity() != null |
| | | ? noticeLine.getQualifiedQuantity() : noticeLine.getArrivalQuantity(); |
| | | if (qty != null && qty.compareTo(BigDecimal.ZERO) > 0) { |
| | | purchaseOrderApi.updateOrderItemInCount(noticeLine.getPurchaseOrderItemId(), qty); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | // 更新采购订单的入库状态 |
| | | |
| | | // 3. 更新采购订单的入库状态 |
| | | if (purchaseOrderId != null) { |
| | | purchaseOrderApi.updateOrderInStatus(purchaseOrderId); |
| | | } |
| | |
| | | receipt.setCode(code); |
| | | receipt.setName(notice.getName() + "入库单"); |
| | | receipt.setNoticeId(arrivalNoticeId); |
| | | receipt.setPurchaseOrderId(notice.getPurchaseOrderId()); |
| | | receipt.setPurchaseOrderCode(notice.getPurchaseOrderCode()); |
| | | receipt.setVendorId(notice.getVendorId()); |
| | | receipt.setReceiptDate(java.time.LocalDateTime.now()); // 入库时间为当前时间 |
| | |
| | | } |
| | | |
| | | return receipt.getId(); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void submitItemReceiptApproval(Long id, String processDefinitionKey) { |
| | | // 1. 校验存在 + 草稿状态 |
| | | validateItemReceiptExistsAndDraft(id); |
| | | |
| | | // 2. 创建 BPM 流程实例 |
| | | Long userId = getCurrentUserId(); |
| | | String processInstanceId = processInstanceApi.createProcessInstance(userId, |
| | | new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(processDefinitionKey) |
| | | .setBusinessKey(String.valueOf(id))); |
| | | |
| | | // 3. 更新入库单状态为审批中 |
| | | itemReceiptMapper.updateById(new MesWmItemReceiptDO() |
| | | .setId(id) |
| | | .setStatus(MesWmItemReceiptStatusEnum.APPROVING.getStatus()) |
| | | .setProcessInstanceId(processInstanceId)); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateItemReceiptAuditStatus(Long id, Integer bpmResult) { |
| | | // 1. 校验存在 |
| | | MesWmItemReceiptDO receipt = validateItemReceiptExists(id); |
| | | if (!MesWmItemReceiptStatusEnum.APPROVING.getStatus().equals(receipt.getStatus())) { |
| | | log.warn("[updateItemReceiptAuditStatus] 入库单({}) 不处于审批中状态", id); |
| | | return; |
| | | } |
| | | |
| | | // 2. 根据审批结果更新状态 |
| | | Integer newStatus = convertBpmResultToReceiptStatus(bpmResult); |
| | | if (newStatus != null) { |
| | | itemReceiptMapper.updateById(new MesWmItemReceiptDO() |
| | | .setId(id).setStatus(newStatus)); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 转换 BPM 审批结果为入库单状态 |
| | | */ |
| | | private Integer convertBpmResultToReceiptStatus(Integer bpmResult) { |
| | | if (BpmTaskStatusEnum.APPROVE.getStatus().equals(bpmResult)) { |
| | | return MesWmItemReceiptStatusEnum.APPROVED.getStatus(); |
| | | } else if (BpmTaskStatusEnum.REJECT.getStatus().equals(bpmResult)) { |
| | | return MesWmItemReceiptStatusEnum.PREPARE.getStatus(); |
| | | } else if (BpmTaskStatusEnum.CANCEL.getStatus().equals(bpmResult)) { |
| | | return MesWmItemReceiptStatusEnum.CANCELED.getStatus(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前用户 ID |
| | | */ |
| | | private Long getCurrentUserId() { |
| | | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| | | if (authentication != null && authentication.getPrincipal() instanceof Long) { |
| | | return (Long) authentication.getPrincipal(); |
| | | } |
| | | return 1L; // 默认用户 |
| | | } |
| | | |
| | | /** |