| | |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | 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.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.md.vendor.MesMdVendorService; |
| | | 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.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.srm.api.supplier.SrmSupplierApi; |
| | | import cn.iocoder.yudao.module.system.api.notify.NotifyMessageSendApi; |
| | | import cn.iocoder.yudao.module.system.api.notify.dto.NotifySendSingleToUserReqDTO; |
| | | 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 jakarta.annotation.Resource; |
| | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.Collection; |
| | | import java.util.HashMap; |
| | | import java.util.LinkedHashSet; |
| | | 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; |
| | |
| | | @Lazy |
| | | private MesWmArrivalNoticeService arrivalNoticeService; |
| | | @Resource |
| | | private MesMdVendorService vendorService; |
| | | @Lazy |
| | | private SrmSupplierApi srmSupplierApi; |
| | | @Resource |
| | | @Lazy |
| | | private MesQcIqcService iqcService; |
| | |
| | | |
| | | @Resource |
| | | private ErpPurchaseOrderApi purchaseOrderApi; |
| | | |
| | | @Resource |
| | | private MdmItemApi mdmItemApi; |
| | | |
| | | @Resource |
| | | private NotifyMessageSendApi notifyMessageSendApi; |
| | | |
| | | @Override |
| | | public Long createItemReceipt(MesWmItemReceiptSaveReqVO createReqVO) { |
| | |
| | | private void validateItemReceiptSaveData(MesWmItemReceiptSaveReqVO reqVO) { |
| | | // 校验编码唯一 |
| | | validateCodeUnique(reqVO.getId(), reqVO.getCode()); |
| | | // 校验供应商存在 |
| | | vendorService.validateVendorExistsAndEnable(reqVO.getVendorId()); |
| | | // 校验供应商存在(采购入库使用 ERP 供应商) |
| | | srmSupplierApi.getSupplier(reqVO.getVendorId()); |
| | | // 校验到货通知单存在 |
| | | if (reqVO.getNoticeId() != null) { |
| | | MesWmArrivalNoticeDO notice = arrivalNoticeService.validateArrivalNoticeReadyForReceipt(reqVO.getNoticeId()); |
| | |
| | | throw exception(WM_ITEM_RECEIPT_NO_LINE); |
| | | } |
| | | |
| | | // 校验物料批次管理:如果物料开启批次管理,则明细的批次号不能为空 |
| | | validateBatchManagement(lines); |
| | | |
| | | // 提交(草稿 → 待上架) |
| | | itemReceiptMapper.updateById(new MesWmItemReceiptDO() |
| | | .setId(id).setStatus(MesWmItemReceiptStatusEnum.APPROVING.getStatus())); |
| | | } |
| | | |
| | | /** |
| | | * 校验物料批次管理:如果物料开启批次管理,则行的批次号不能为空 |
| | | */ |
| | | private void validateBatchManagement(List<MesWmItemReceiptLineDO> lines) { |
| | | // 获取所有物料ID |
| | | List<Long> itemIds = CollectionUtils.convertList(lines, MesWmItemReceiptLineDO::getItemId); |
| | | Map<Long, MdmItemRespDTO> itemMap = mdmItemApi.getItemMap(itemIds); |
| | | |
| | | // 遍历每一行,检查物料是否开启批次管理 |
| | | for (MesWmItemReceiptLineDO line : lines) { |
| | | MdmItemRespDTO item = itemMap.get(line.getItemId()); |
| | | if (item == null) { |
| | | continue; |
| | | } |
| | | // 如果物料开启批次管理,检查行上的批次号是否为空 |
| | | if (Boolean.TRUE.equals(item.getIsBatchManaged()) && line.getBatchId() == null) { |
| | | throw exception(WM_ITEM_RECEIPT_BATCH_REQUIRED, item.getCode(), item.getName()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | |
| | | // 5. 回写 ERP 采购订单的入库数量 |
| | | updateErpPurchaseOrderInCount(receipt); |
| | | Long purchaseOrderId = updateErpPurchaseOrderInCount(receipt); |
| | | |
| | | // 6. 需求 5.2①:采购入库完成后触发付款流程(站内信提醒财务生成付款单) |
| | | notifyPaymentRemind(receipt, purchaseOrderId); |
| | | } |
| | | |
| | | /** 付款提醒站内信模板编码 */ |
| | | private static final String PAYMENT_REMIND_TEMPLATE_CODE = "purchase_receipt_payment_remind"; |
| | | |
| | | /** |
| | | * 采购入库完成后,提醒相关人凭采购订单生成付款单 |
| | | * |
| | | * <p>需求 5.2①:采购入库确认收货后触发付款流程。系统不自动创建付款单—— |
| | | * ERP 付款单必须关联来票(发票),入库时通常尚未取票,自动建单会生成无效单据; |
| | | * 因此这里以站内信方式提醒,由财务在「付款单」页面按来票补录。</p> |
| | | */ |
| | | private void notifyPaymentRemind(MesWmItemReceiptDO receipt, Long purchaseOrderId) { |
| | | try { |
| | | // 1. 采购订单号与订单金额 |
| | | String purchaseOrderCode = receipt.getPurchaseOrderCode(); |
| | | BigDecimal totalPrice = null; |
| | | if (purchaseOrderId != null) { |
| | | cn.iocoder.yudao.module.erp.api.purchase.dto.ErpPurchaseOrderRespDTO order = |
| | | purchaseOrderApi.getPurchaseOrder(purchaseOrderId).getCheckedData(); |
| | | if (order != null) { |
| | | if (StrUtil.isBlank(purchaseOrderCode)) { |
| | | purchaseOrderCode = order.getNo(); |
| | | } |
| | | totalPrice = order.getTotalPrice(); |
| | | } |
| | | } |
| | | Map<String, Object> params = new HashMap<>(); |
| | | params.put("receiptCode", receipt.getCode()); |
| | | params.put("purchaseOrderCode", StrUtil.isNotBlank(purchaseOrderCode) ? purchaseOrderCode : "-"); |
| | | params.put("totalPrice", totalPrice != null |
| | | ? totalPrice.setScale(2, RoundingMode.HALF_UP).toPlainString() : "-"); |
| | | // 2. 接收人:入库单创建人(仓管)+ 管理员,去重后逐个发送 |
| | | Set<Long> recipientIds = new LinkedHashSet<>(); |
| | | if (StrUtil.isNotBlank(receipt.getCreator())) { |
| | | try { |
| | | recipientIds.add(Long.parseLong(receipt.getCreator())); |
| | | } catch (NumberFormatException ignored) { |
| | | log.warn("采购入库单({}) 创建人({}) 不是合法用户编号,付款提醒将只发给管理员", |
| | | receipt.getId(), receipt.getCreator()); |
| | | } |
| | | } |
| | | recipientIds.add(1L); |
| | | for (Long userId : recipientIds) { |
| | | NotifySendSingleToUserReqDTO message = new NotifySendSingleToUserReqDTO(); |
| | | message.setUserId(userId); |
| | | message.setTemplateCode(PAYMENT_REMIND_TEMPLATE_CODE); |
| | | message.setTemplateParams(params); |
| | | notifyMessageSendApi.sendSingleMessageToAdmin(message); |
| | | } |
| | | } catch (Exception ex) { |
| | | // 提醒失败不影响入库业务 |
| | | log.warn("采购入库单({}) 付款提醒发送失败", receipt.getId(), ex); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 回写 ERP 采购订单的入库数量 |
| | | */ |
| | | private void updateErpPurchaseOrderInCount(MesWmItemReceiptDO receipt) { |
| | | // 获取入库单行 |
| | | List<MesWmItemReceiptLineDO> lines = itemReceiptLineService.getItemReceiptLineListByReceiptId(receipt.getId()); |
| | | if (CollUtil.isEmpty(lines)) { |
| | | return; |
| | | private Long updateErpPurchaseOrderInCount(MesWmItemReceiptDO receipt) { |
| | | // 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); |
| | | } |
| | | return purchaseOrderId; |
| | | } |
| | | |
| | | private void createTransactionList(MesWmItemReceiptDO receipt) { |
| | | List<MesWmItemReceiptDetailDO> details = itemReceiptDetailService.getItemReceiptDetailListByReceiptId(receipt.getId()); |
| | | wmTransactionService.createTransactionList(convertList(details, detail -> new MesWmTransactionSaveReqDTO() |
| | | .setType(MesWmTransactionTypeEnum.IN.getType()).setItemId(detail.getItemId()) |
| | | .setQuantity(detail.getQuantity()) // 入库数量为正数 |
| | | .setBatchId(detail.getBatchId()) |
| | | .setWarehouseId(detail.getWarehouseId()).setLocationId(detail.getLocationId()).setAreaId(detail.getAreaId()) |
| | | .setVendorId(receipt.getVendorId()).setReceiptTime(receipt.getReceiptDate()) |
| | | .setBizType(MesBizTypeConstants.WM_ITEM_RECEIPT_IN).setBizId(receipt.getId()) |
| | | .setBizCode(receipt.getCode()).setBizLineId(detail.getLineId()))); |
| | | // 获取所有行信息,用于获取批次号 |
| | | List<MesWmItemReceiptLineDO> lines = itemReceiptLineService.getItemReceiptLineListByReceiptId(receipt.getId()); |
| | | Map<Long, MesWmItemReceiptLineDO> lineMap = CollectionUtils.convertMap(lines, MesWmItemReceiptLineDO::getId); |
| | | |
| | | wmTransactionService.createTransactionList(convertList(details, detail -> { |
| | | // 从行上获取批次信息(如果明细没有批次号) |
| | | MesWmItemReceiptLineDO line = lineMap.get(detail.getLineId()); |
| | | Long batchId = detail.getBatchId() != null ? detail.getBatchId() : |
| | | (line != null ? line.getBatchId() : null); |
| | | String batchCode = line != null ? line.getBatchCode() : null; |
| | | |
| | | return new MesWmTransactionSaveReqDTO() |
| | | .setType(MesWmTransactionTypeEnum.IN.getType()).setItemId(detail.getItemId()) |
| | | .setQuantity(detail.getQuantity()) |
| | | .setBatchId(batchId).setBatchCode(batchCode) |
| | | .setWarehouseId(detail.getWarehouseId()).setLocationId(detail.getLocationId()).setAreaId(detail.getAreaId()) |
| | | .setVendorId(receipt.getVendorId()).setReceiptTime(receipt.getReceiptDate()) |
| | | .setBizType(MesBizTypeConstants.WM_ITEM_RECEIPT_IN).setBizId(receipt.getId()) |
| | | .setBizCode(receipt.getCode()).setBizLineId(detail.getLineId()); |
| | | })); |
| | | } |
| | | |
| | | @Override |
| | |
| | | // 1. 校验到货通知单存在且状态为待入库 |
| | | MesWmArrivalNoticeDO notice = arrivalNoticeService.validateArrivalNoticeReadyForReceipt(arrivalNoticeId); |
| | | |
| | | // 2. 获取到货通知单行 |
| | | // 2. 校验是否已生成过入库单(防止重复生成) |
| | | MesWmItemReceiptDO existReceipt = itemReceiptMapper.selectByNoticeId(arrivalNoticeId); |
| | | if (existReceipt != null) { |
| | | throw exception(WM_ITEM_RECEIPT_ALREADY_EXISTS); |
| | | } |
| | | |
| | | // 3. 获取到货通知单行 |
| | | List<MesWmArrivalNoticeLineDO> lines = arrivalNoticeLineService.getArrivalNoticeLineListByNoticeId(arrivalNoticeId); |
| | | if (CollUtil.isEmpty(lines)) { |
| | | throw exception(WM_ARRIVAL_NOTICE_NO_LINE); |
| | | } |
| | | |
| | | // 3. 生成入库单编码 |
| | | // 4. 生成入库单编码 |
| | | String code = generateItemReceiptCode(); |
| | | |
| | | // 4. 创建入库单主表 |
| | | // 5. 创建入库单主表 |
| | | MesWmItemReceiptDO receipt = new MesWmItemReceiptDO(); |
| | | 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()); // 入库时间为当前时间 |
| | | receipt.setStatus(MesWmItemReceiptStatusEnum.PREPARE.getStatus()); |
| | | itemReceiptMapper.insert(receipt); |
| | | |
| | | // 5. 遍历行创建入库单行 |
| | | // 6. 遍历行创建入库单行 |
| | | for (MesWmArrivalNoticeLineDO noticeLine : lines) { |
| | | MesWmItemReceiptLineDO receiptLine = new MesWmItemReceiptLineDO(); |
| | | receiptLine.setReceiptId(receipt.getId()); |
| | |
| | | |
| | | // 2. 创建 BPM 流程实例 |
| | | Long userId = getCurrentUserId(); |
| | | // 全局免审批:业务校验通过后直接执行审批通过逻辑,无需流程模型。 |
| | | if (processInstanceApi.isGlobalAutoApprove()) { |
| | | itemReceiptMapper.updateById(new MesWmItemReceiptDO() |
| | | .setId(id) |
| | | .setStatus(MesWmItemReceiptStatusEnum.APPROVING.getStatus()) |
| | | .setProcessInstanceId("")); |
| | | updateItemReceiptAuditStatus(id, cn.iocoder.yudao.module.bpm.enums.task.BpmProcessInstanceStatusEnum.APPROVE.getStatus()); |
| | | return; |
| | | } |
| | | |
| | | String processInstanceId = processInstanceApi.createProcessInstance(userId, |
| | | new BpmProcessInstanceCreateReqDTO() |
| | | .setProcessDefinitionKey(processDefinitionKey) |