| | |
| | | import cn.iocoder.yudao.module.bpm.service.definition.BpmCategoryService; |
| | | import cn.iocoder.yudao.module.bpm.service.definition.BpmProcessDefinitionService; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.plan.ErpPurchasePlanPageReqVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.plan.ErpPurchasePlanRespVO; |
| | | import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.plan.ErpPurchasePlanSaveReqVO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchasePlanDO; |
| | | import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchasePlanItemDO; |
| | |
| | | import cn.iocoder.yudao.module.erp.dal.redis.no.ErpNoRedisDAO; |
| | | import cn.iocoder.yudao.module.erp.enums.ErpPurchasePlanStatusEnum; |
| | | import cn.iocoder.yudao.module.erp.enums.ErpPurchaseRequestStatusEnum; |
| | | import cn.iocoder.yudao.module.mes.api.item.MesMdItemApi; |
| | | import cn.iocoder.yudao.module.mes.api.item.dto.MesMdItemSafetyStockRespDTO; |
| | | import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi; |
| | | import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO; |
| | | import cn.iocoder.yudao.module.mes.api.wms.MesWmStockApi; |
| | | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| | | import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; |
| | |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.util.*; |
| | | |
| | |
| | | private AdminUserApi adminUserApi; |
| | | |
| | | @Resource |
| | | private MesMdItemApi mesMdItemApi; |
| | | private MdmItemApi mdmItemApi; |
| | | @Resource |
| | | private MesWmStockApi mesWmStockApi; |
| | | |
| | |
| | | } |
| | | |
| | | @Override |
| | | public List<ErpPurchasePlanRespVO.Item> getStockAlertSuggestItems() { |
| | | // 1. 获取启用的物料基础数据(包含默认仓库、安全/最低/最高库存) |
| | | List<MdmItemRespDTO> items = mdmItemApi.getItemListByStatus(0).getCheckedData(); |
| | | if (CollUtil.isEmpty(items)) { |
| | | throw exception(PURCHASE_PLAN_STOCK_ALERT_NO_ITEM); |
| | | } |
| | | // 2. 按默认仓库库存规划建议采购量 |
| | | List<ErpPurchasePlanRespVO.Item> planItems = new ArrayList<>(); |
| | | for (MdmItemRespDTO item : items) { |
| | | ErpPurchasePlanRespVO.Item planItem = buildStockAlertSuggestItem(item); |
| | | if (planItem != null) { |
| | | planItems.add(planItem); |
| | | } |
| | | } |
| | | if (CollUtil.isEmpty(planItems)) { |
| | | throw exception(PURCHASE_PLAN_STOCK_ALERT_NO_ITEM); |
| | | } |
| | | return planItems; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public ErpPurchasePlanDO generatePlanFromStockAlert(String name, Long supplierId) { |
| | | // 1. 获取启用安全库存的物料 |
| | | List<MesMdItemSafetyStockRespDTO> safetyStockItems = mesMdItemApi.getSafetyStockItemList(); |
| | | if (CollUtil.isEmpty(safetyStockItems)) { |
| | | throw exception(PURCHASE_PLAN_ITEM_EMPTY); |
| | | // 1. 获取库存预警建议采购明细 |
| | | List<ErpPurchasePlanRespVO.Item> suggestItems = getStockAlertSuggestItems(); |
| | | if (CollUtil.isEmpty(suggestItems)) { |
| | | throw exception(PURCHASE_PLAN_STOCK_ALERT_NO_ITEM); |
| | | } |
| | | // 2. 计算缺量,生成计划明细 |
| | | List<ErpPurchasePlanSaveReqVO.Item> items = new ArrayList<>(); |
| | | for (MesMdItemSafetyStockRespDTO item : safetyStockItems) { |
| | | if (item.getMinStock() == null) { |
| | | continue; |
| | | } |
| | | BigDecimal stock = mesWmStockApi.getStockQuantity(item.getId()); |
| | | BigDecimal shortage = item.getMinStock().subtract(stock == null ? BigDecimal.ZERO : stock); |
| | | if (shortage.compareTo(BigDecimal.ZERO) <= 0) { |
| | | continue; |
| | | } |
| | | ErpPurchasePlanSaveReqVO.Item planItem = new ErpPurchasePlanSaveReqVO.Item(); |
| | | planItem.setProductId(item.getMdmItemId()); |
| | | planItem.setProductUnitId(item.getUnitMeasureId()); |
| | | planItem.setCount(shortage); |
| | | items.add(planItem); |
| | | } |
| | | if (CollUtil.isEmpty(items)) { |
| | | throw exception(PURCHASE_PLAN_ITEM_EMPTY); |
| | | } |
| | | // 2. 转换为保存明细 |
| | | List<ErpPurchasePlanSaveReqVO.Item> planItems = convertList(suggestItems, item -> |
| | | BeanUtils.toBean(item, ErpPurchasePlanSaveReqVO.Item.class)); |
| | | // 3. 创建草稿采购计划 |
| | | ErpPurchasePlanSaveReqVO createReqVO = new ErpPurchasePlanSaveReqVO(); |
| | | createReqVO.setName(name != null ? name : "库存预警采购计划"); |
| | | createReqVO.setDemandSource(DEMAND_SOURCE_STOCK_ALERT); |
| | | createReqVO.setPlanDate(LocalDateTime.now()); |
| | | createReqVO.setSupplierId(supplierId); |
| | | createReqVO.setItems(items); |
| | | createReqVO.setItems(planItems); |
| | | Long planId = createPurchasePlan(createReqVO); |
| | | return purchasePlanMapper.selectById(planId); |
| | | } |
| | | |
| | | /** |
| | | * 根据单个物料的默认仓库库存构建建议采购明细 |
| | | * |
| | | * @param item 物料基础数据 |
| | | * @return 建议采购明细;无需补货时返回 null |
| | | */ |
| | | private ErpPurchasePlanRespVO.Item buildStockAlertSuggestItem(MdmItemRespDTO item) { |
| | | // 1. 未配置默认仓库或最低库存(无补货基准)的物料不参与规划 |
| | | if (item.getWarehouseId() == null || item.getMinStock() == null) { |
| | | return null; |
| | | } |
| | | // 2. 查询默认仓库的当前库存 |
| | | BigDecimal currentStock = mesWmStockApi.getStockQuantity(item.getId(), item.getWarehouseId()); |
| | | if (currentStock == null) { |
| | | currentStock = BigDecimal.ZERO; |
| | | } |
| | | // 3. 默认仓库库存不低于最低库存时不触发补货 |
| | | if (currentStock.compareTo(item.getMinStock()) >= 0) { |
| | | return null; |
| | | } |
| | | // 4. 补货目标:优先补到安全库存,未配置则回落最高库存,最后取最低库存 |
| | | BigDecimal targetStock = item.getSafetyStock() != null ? item.getSafetyStock() |
| | | : (item.getMaxStock() != null ? item.getMaxStock() : item.getMinStock()); |
| | | BigDecimal count = targetStock.subtract(currentStock); |
| | | if (count.compareTo(BigDecimal.ZERO) <= 0) { |
| | | return null; |
| | | } |
| | | ErpPurchasePlanRespVO.Item planItem = new ErpPurchasePlanRespVO.Item(); |
| | | planItem.setProductId(item.getId()); |
| | | planItem.setProductName(item.getName()); |
| | | planItem.setProductUnitId(item.getUnitMeasureId()); |
| | | planItem.setCount(count); |
| | | planItem.setDemandTime(LocalDate.now()); |
| | | planItem.setRemark(String.format("默认仓库库存%s,安全库存%s,最低库存%s,最高库存%s,建议采购量%s", |
| | | currentStock, item.getSafetyStock(), item.getMinStock(), item.getMaxStock(), count)); |
| | | return planItem; |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public List<Long> generatePurchaseRequests(Long id) { |