Commit 01b610787755925349353c957eab38319037564d
1 parent
e7ee268a
✨ feat(transaction): 添加库存事务创建逻辑,支持虚拟线边库的出入库操作
新增库存事务创建逻辑,支持在虚拟线边库进行出库和入库操作。遍历消耗行、收货明细和退料明细时,自动生成相应的库存事务,确保库存记录的准确性和一致性。
Showing
5 changed files
with
191 additions
and
39 deletions
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/itemconsume/MesWmItemConsumeServiceImpl.java
| ... | ... | @@ -5,9 +5,19 @@ import cn.iocoder.yudao.module.mes.dal.dataobject.pro.feedback.MesProFeedbackDO; |
| 5 | 5 | import cn.iocoder.yudao.module.mes.dal.dataobject.pro.route.MesProRouteProductBomDO; |
| 6 | 6 | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.itemconsume.MesWmItemConsumeDO; |
| 7 | 7 | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.itemconsume.MesWmItemConsumeLineDO; |
| 8 | +import cn.iocoder.yudao.module.mes.dal.dataobject.wm.warehouse.MesWmWarehouseAreaDO; | |
| 9 | +import cn.iocoder.yudao.module.mes.dal.dataobject.wm.warehouse.MesWmWarehouseDO; | |
| 10 | +import cn.iocoder.yudao.module.mes.dal.dataobject.wm.warehouse.MesWmWarehouseLocationDO; | |
| 8 | 11 | import cn.iocoder.yudao.module.mes.dal.mysql.wm.itemconsume.MesWmItemConsumeMapper; |
| 9 | 12 | import cn.iocoder.yudao.module.mes.enums.wm.MesWmItemConsumeStatusEnum; |
| 13 | +import cn.iocoder.yudao.module.mes.enums.MesBizTypeConstants; | |
| 14 | +import cn.iocoder.yudao.module.mes.enums.wm.MesWmTransactionTypeEnum; | |
| 10 | 15 | import cn.iocoder.yudao.module.mes.service.pro.route.MesProRouteProductBomService; |
| 16 | +import cn.iocoder.yudao.module.mes.service.wm.transaction.MesWmTransactionService; | |
| 17 | +import cn.iocoder.yudao.module.mes.service.wm.transaction.dto.MesWmTransactionSaveReqDTO; | |
| 18 | +import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseAreaService; | |
| 19 | +import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseLocationService; | |
| 20 | +import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseService; | |
| 11 | 21 | import jakarta.annotation.Resource; |
| 12 | 22 | import org.springframework.stereotype.Service; |
| 13 | 23 | import org.springframework.validation.annotation.Validated; |
| ... | ... | @@ -32,10 +42,14 @@ public class MesWmItemConsumeServiceImpl implements MesWmItemConsumeService { |
| 32 | 42 | private MesWmItemConsumeLineService itemConsumeLineService; |
| 33 | 43 | @Resource |
| 34 | 44 | private MesProRouteProductBomService routeProductBomService; |
| 35 | - | |
| 36 | - // TODO @芋艿:待 MaterialStockService 补充 decreaseStock 后,注入并调用 | |
| 37 | - // @Resource | |
| 38 | - // private MesWmMaterialStockService materialStockService; | |
| 45 | + @Resource | |
| 46 | + private MesWmTransactionService wmTransactionService; | |
| 47 | + @Resource | |
| 48 | + private MesWmWarehouseService warehouseService; | |
| 49 | + @Resource | |
| 50 | + private MesWmWarehouseLocationService locationService; | |
| 51 | + @Resource | |
| 52 | + private MesWmWarehouseAreaService areaService; | |
| 39 | 53 | |
| 40 | 54 | @Override |
| 41 | 55 | public MesWmItemConsumeDO generateItemConsume(MesProFeedbackDO feedback) { |
| ... | ... | @@ -64,11 +78,22 @@ public class MesWmItemConsumeServiceImpl implements MesWmItemConsumeService { |
| 64 | 78 | |
| 65 | 79 | @Override |
| 66 | 80 | public void finishItemConsume(Long consumeId) { |
| 67 | - // TODO @AI(from codex):对齐,这里还需要按消耗明细扣减线边库库存,不能只把消耗单状态改为已完成。 | |
| 68 | - // List<MesWmItemConsumeLineDO> lines = itemConsumeLineService.getItemConsumeLineListByConsumeId(consumeId); | |
| 69 | - // for (MesWmItemConsumeLineDO line : lines) { | |
| 70 | - // materialStockService.decreaseStock(line.getItemId(), ..., line.getQuantity()); | |
| 71 | - // } | |
| 81 | + MesWmWarehouseDO virtualWarehouse = warehouseService.getWarehouseByCode(MesWmWarehouseDO.WIP_VIRTUAL_WAREHOUSE); | |
| 82 | + MesWmWarehouseLocationDO virtualLocation = locationService.getWarehouseLocationByCode(MesWmWarehouseLocationDO.WIP_VIRTUAL_LOCATION); | |
| 83 | + MesWmWarehouseAreaDO virtualArea = areaService.getWarehouseAreaByCode(MesWmWarehouseAreaDO.WIP_VIRTUAL_AREA); | |
| 84 | + | |
| 85 | + // 遍历消耗行,创建库存事务(从线边库扣减) | |
| 86 | + List<MesWmItemConsumeLineDO> lines = itemConsumeLineService.getItemConsumeLineListByConsumeId(consumeId); | |
| 87 | + for (MesWmItemConsumeLineDO line : lines) { | |
| 88 | + wmTransactionService.createTransaction(new MesWmTransactionSaveReqDTO() | |
| 89 | + .setType(MesWmTransactionTypeEnum.OUT.getType()).setItemId(line.getItemId()) | |
| 90 | + .setQuantity(line.getQuantity().negate()) // 库存减少 | |
| 91 | + .setWarehouseId(virtualWarehouse.getId()).setLocationId(virtualLocation.getId()).setAreaId(virtualArea.getId()) | |
| 92 | + .setCheckFlag(false) // 线边库允许负库存 | |
| 93 | + .setBizType(MesBizTypeConstants.WM_ITEM_CONSUME).setBizId(consumeId) | |
| 94 | + .setBizCode("").setBizLineId(line.getId())); | |
| 95 | + } | |
| 96 | + | |
| 72 | 97 | // 更新消耗单状态为已完成 |
| 73 | 98 | itemConsumeMapper.updateById(MesWmItemConsumeDO.builder() |
| 74 | 99 | .id(consumeId).status(MesWmItemConsumeStatusEnum.FINISHED.getStatus()).build()); | ... | ... |
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/productproduce/MesWmProductProduceServiceImpl.java
| ... | ... | @@ -14,8 +14,18 @@ import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productproduce.MesWmProduct |
| 14 | 14 | import cn.iocoder.yudao.module.mes.dal.mysql.wm.productproduce.MesWmProductProduceMapper; |
| 15 | 15 | import cn.iocoder.yudao.module.mes.enums.wm.MesWmProductProduceStatusEnum; |
| 16 | 16 | import cn.iocoder.yudao.module.mes.enums.wm.MesWmQualityStatusEnum; |
| 17 | +import cn.iocoder.yudao.module.mes.enums.MesBizTypeConstants; | |
| 18 | +import cn.iocoder.yudao.module.mes.enums.wm.MesWmTransactionTypeEnum; | |
| 19 | +import cn.iocoder.yudao.module.mes.dal.dataobject.wm.warehouse.MesWmWarehouseAreaDO; | |
| 20 | +import cn.iocoder.yudao.module.mes.dal.dataobject.wm.warehouse.MesWmWarehouseDO; | |
| 21 | +import cn.iocoder.yudao.module.mes.dal.dataobject.wm.warehouse.MesWmWarehouseLocationDO; | |
| 17 | 22 | import cn.iocoder.yudao.module.mes.service.pro.workorder.MesProWorkOrderService; |
| 18 | 23 | import cn.iocoder.yudao.module.mes.service.wm.batch.MesWmBatchService; |
| 24 | +import cn.iocoder.yudao.module.mes.service.wm.transaction.MesWmTransactionService; | |
| 25 | +import cn.iocoder.yudao.module.mes.service.wm.transaction.dto.MesWmTransactionSaveReqDTO; | |
| 26 | +import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseAreaService; | |
| 27 | +import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseLocationService; | |
| 28 | +import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseService; | |
| 19 | 29 | import jakarta.annotation.Resource; |
| 20 | 30 | import org.springframework.stereotype.Service; |
| 21 | 31 | import org.springframework.transaction.annotation.Transactional; |
| ... | ... | @@ -47,6 +57,14 @@ public class MesWmProductProduceServiceImpl implements MesWmProductProduceServic |
| 47 | 57 | private MesProWorkOrderService workOrderService; |
| 48 | 58 | @Resource |
| 49 | 59 | private MesWmBatchService batchService; |
| 60 | + @Resource | |
| 61 | + private MesWmTransactionService wmTransactionService; | |
| 62 | + @Resource | |
| 63 | + private MesWmWarehouseService warehouseService; | |
| 64 | + @Resource | |
| 65 | + private MesWmWarehouseLocationService locationService; | |
| 66 | + @Resource | |
| 67 | + private MesWmWarehouseAreaService areaService; | |
| 50 | 68 | |
| 51 | 69 | @Override |
| 52 | 70 | @Transactional(rollbackFor = Exception.class) |
| ... | ... | @@ -59,8 +77,17 @@ public class MesWmProductProduceServiceImpl implements MesWmProductProduceServic |
| 59 | 77 | throw exception(WM_PRODUCT_PRODUCE_NO_LINE); |
| 60 | 78 | } |
| 61 | 79 | |
| 62 | - // 2. 校验每行明细数量之和等于行数量 | |
| 80 | + // 2. 创建库存事务 | |
| 81 | + createTransactionList(id, lines); | |
| 82 | + | |
| 83 | + // 3. 更新入库单状态 | |
| 84 | + productProduceMapper.updateById(new MesWmProductProduceDO() | |
| 85 | + .setId(id).setStatus(MesWmProductProduceStatusEnum.FINISHED.getStatus())); | |
| 86 | + } | |
| 87 | + | |
| 88 | + private void createTransactionList(Long produceId, List<MesWmProductProduceLineDO> lines) { | |
| 63 | 89 | for (MesWmProductProduceLineDO line : lines) { |
| 90 | + // 1. 校验每行明细数量之和等于行数量 | |
| 64 | 91 | List<MesWmProductProduceDetailDO> details = productProduceDetailService.getProductProduceDetailListByLineId( |
| 65 | 92 | line.getId()); |
| 66 | 93 | BigDecimal totalDetailQty = CollectionUtils.getSumValue(details, |
| ... | ... | @@ -68,13 +95,17 @@ public class MesWmProductProduceServiceImpl implements MesWmProductProduceServic |
| 68 | 95 | if (line.getQuantity() != null && totalDetailQty.compareTo(line.getQuantity()) != 0) { |
| 69 | 96 | throw exception(WM_PRODUCT_PRODUCE_DETAIL_QUANTITY_MISMATCH); |
| 70 | 97 | } |
| 98 | + // 2. 按明细创建库存事务(产品产出到线边库) | |
| 99 | + for (MesWmProductProduceDetailDO detail : details) { | |
| 100 | + wmTransactionService.createTransaction(new MesWmTransactionSaveReqDTO() | |
| 101 | + .setType(MesWmTransactionTypeEnum.IN.getType()).setItemId(detail.getItemId()) | |
| 102 | + .setQuantity(detail.getQuantity()) | |
| 103 | + .setBatchId(detail.getBatchId()).setBatchCode(detail.getBatchCode()) | |
| 104 | + .setWarehouseId(detail.getWarehouseId()).setLocationId(detail.getLocationId()).setAreaId(detail.getAreaId()) | |
| 105 | + .setBizType(MesBizTypeConstants.WM_PRODUCT_PRODUCE).setBizId(produceId) | |
| 106 | + .setBizCode("").setBizLineId(line.getId())); | |
| 107 | + } | |
| 71 | 108 | } |
| 72 | - | |
| 73 | - // TODO @AI(from codex):对齐,这里还需要按产出明细执行库存入账,而不是只校验明细并更新单据状态。 | |
| 74 | - | |
| 75 | - // 3. 更新入库单状态 | |
| 76 | - productProduceMapper.updateById(new MesWmProductProduceDO() | |
| 77 | - .setId(id).setStatus(MesWmProductProduceStatusEnum.FINISHED.getStatus())); | |
| 78 | 109 | } |
| 79 | 110 | |
| 80 | 111 | @Override |
| ... | ... | @@ -106,6 +137,11 @@ public class MesWmProductProduceServiceImpl implements MesWmProductProduceServic |
| 106 | 137 | Long batchId = batch != null ? batch.getId() : null; |
| 107 | 138 | String batchCode = batch != null ? batch.getCode() : null; |
| 108 | 139 | |
| 140 | + // 1.5 获取虚拟线边库 | |
| 141 | + MesWmWarehouseDO virtualWarehouse = warehouseService.getWarehouseByCode(MesWmWarehouseDO.WIP_VIRTUAL_WAREHOUSE); | |
| 142 | + MesWmWarehouseLocationDO virtualLocation = locationService.getWarehouseLocationByCode(MesWmWarehouseLocationDO.WIP_VIRTUAL_LOCATION); | |
| 143 | + MesWmWarehouseAreaDO virtualArea = areaService.getWarehouseAreaByCode(MesWmWarehouseAreaDO.WIP_VIRTUAL_AREA); | |
| 144 | + | |
| 109 | 145 | // 3. 根据是否需要检验分支处理 |
| 110 | 146 | if (checkFlag) { |
| 111 | 147 | // 3.1 需要检验:创建一条行(质量状态=待检验),不生成明细 |
| ... | ... | @@ -123,7 +159,8 @@ public class MesWmProductProduceServiceImpl implements MesWmProductProduceServic |
| 123 | 159 | unqualifiedQty, MesWmQualityStatusEnum.FAIL.getStatus()); |
| 124 | 160 | productProduceLineService.createProductProduceLine(unqualifiedLine); |
| 125 | 161 | MesWmProductProduceDetailDO unqualifiedDetail = buildProduceDetail(produce, feedback, batchId, batchCode, |
| 126 | - unqualifiedLine.getId(), unqualifiedQty); | |
| 162 | + unqualifiedLine.getId(), unqualifiedQty, | |
| 163 | + virtualWarehouse.getId(), virtualLocation.getId(), virtualArea.getId()); | |
| 127 | 164 | productProduceDetailService.createProductProduceDetail(unqualifiedDetail); |
| 128 | 165 | } |
| 129 | 166 | // 3.2.2 合格品行 + 明细 |
| ... | ... | @@ -132,7 +169,8 @@ public class MesWmProductProduceServiceImpl implements MesWmProductProduceServic |
| 132 | 169 | qualifiedQty, MesWmQualityStatusEnum.PASS.getStatus()); |
| 133 | 170 | productProduceLineService.createProductProduceLine(qualifiedLine); |
| 134 | 171 | MesWmProductProduceDetailDO qualifiedDetail = buildProduceDetail(produce, feedback, batchId, batchCode, |
| 135 | - qualifiedLine.getId(), qualifiedQty); | |
| 172 | + qualifiedLine.getId(), qualifiedQty, | |
| 173 | + virtualWarehouse.getId(), virtualLocation.getId(), virtualArea.getId()); | |
| 136 | 174 | productProduceDetailService.createProductProduceDetail(qualifiedDetail); |
| 137 | 175 | } |
| 138 | 176 | } |
| ... | ... | @@ -153,12 +191,13 @@ public class MesWmProductProduceServiceImpl implements MesWmProductProduceServic |
| 153 | 191 | |
| 154 | 192 | private MesWmProductProduceDetailDO buildProduceDetail(MesWmProductProduceDO produce, MesProFeedbackDO feedback, |
| 155 | 193 | Long batchId, String batchCode, |
| 156 | - Long lineId, BigDecimal quantity) { | |
| 194 | + Long lineId, BigDecimal quantity, | |
| 195 | + Long warehouseId, Long locationId, Long areaId) { | |
| 157 | 196 | return MesWmProductProduceDetailDO.builder() |
| 158 | 197 | .produceId(produce.getId()).lineId(lineId) |
| 159 | 198 | .itemId(feedback.getItemId()).quantity(quantity) |
| 160 | 199 | .batchId(batchId).batchCode(batchCode) |
| 161 | - // TODO @AI(from codex):对齐 产出明细需要补齐虚拟仓/库区/库位(VIRTUAL_WH/VIRTUAL_WS/VIRTUAL_WA),否则后续库存入账定位不完整。 | |
| 200 | + .warehouseId(warehouseId).locationId(locationId).areaId(areaId) | |
| 162 | 201 | .build(); |
| 163 | 202 | } |
| 164 | 203 | ... | ... |
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/productreceipt/MesWmProductReceiptServiceImpl.java
| ... | ... | @@ -13,8 +13,19 @@ import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productreceipt.MesWmProduct |
| 13 | 13 | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productreceipt.MesWmProductReceiptDetailDO; |
| 14 | 14 | import cn.iocoder.yudao.module.mes.dal.dataobject.wm.productreceipt.MesWmProductReceiptLineDO; |
| 15 | 15 | import cn.iocoder.yudao.module.mes.dal.mysql.wm.productreceipt.MesWmProductReceiptMapper; |
| 16 | +import cn.iocoder.yudao.module.mes.dal.dataobject.wm.warehouse.MesWmWarehouseAreaDO; | |
| 17 | +import cn.iocoder.yudao.module.mes.dal.dataobject.wm.warehouse.MesWmWarehouseDO; | |
| 18 | +import cn.iocoder.yudao.module.mes.dal.dataobject.wm.warehouse.MesWmWarehouseLocationDO; | |
| 16 | 19 | import cn.iocoder.yudao.module.mes.enums.wm.MesWmProductReceiptStatusEnum; |
| 20 | +import cn.iocoder.yudao.module.mes.enums.MesBizTypeConstants; | |
| 21 | +import cn.iocoder.yudao.module.mes.enums.wm.MesWmTransactionTypeEnum; | |
| 22 | +import cn.iocoder.yudao.module.mes.service.pro.workorder.MesProWorkOrderService; | |
| 17 | 23 | import cn.iocoder.yudao.module.mes.service.wm.materialstock.MesWmMaterialStockService; |
| 24 | +import cn.iocoder.yudao.module.mes.service.wm.transaction.MesWmTransactionService; | |
| 25 | +import cn.iocoder.yudao.module.mes.service.wm.transaction.dto.MesWmTransactionSaveReqDTO; | |
| 26 | +import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseAreaService; | |
| 27 | +import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseLocationService; | |
| 28 | +import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseService; | |
| 18 | 29 | import jakarta.annotation.Resource; |
| 19 | 30 | import org.springframework.stereotype.Service; |
| 20 | 31 | import org.springframework.transaction.annotation.Transactional; |
| ... | ... | @@ -46,7 +57,16 @@ public class MesWmProductReceiptServiceImpl implements MesWmProductReceiptServic |
| 46 | 57 | private MesWmMaterialStockService materialStockService; |
| 47 | 58 | |
| 48 | 59 | @Resource |
| 49 | - private cn.iocoder.yudao.module.mes.service.pro.workorder.MesProWorkOrderService workOrderService; | |
| 60 | + private MesWmTransactionService wmTransactionService; | |
| 61 | + | |
| 62 | + @Resource | |
| 63 | + private MesProWorkOrderService workOrderService; | |
| 64 | + @Resource | |
| 65 | + private MesWmWarehouseService warehouseService; | |
| 66 | + @Resource | |
| 67 | + private MesWmWarehouseLocationService locationService; | |
| 68 | + @Resource | |
| 69 | + private MesWmWarehouseAreaService areaService; | |
| 50 | 70 | |
| 51 | 71 | @Override |
| 52 | 72 | public Long createProductReceipt(MesWmProductReceiptSaveReqVO createReqVO) { |
| ... | ... | @@ -160,20 +180,44 @@ public class MesWmProductReceiptServiceImpl implements MesWmProductReceiptServic |
| 160 | 180 | throw exception(WM_PRODUCT_RECPT_STATUS_ERROR); |
| 161 | 181 | } |
| 162 | 182 | |
| 163 | - // 遍历所有明细,更新库存台账 | |
| 164 | - // DONE @芋艿:【后面优化】(AI 未修复原因:标注为后续优化,需人工介入) | |
| 165 | - List<MesWmProductReceiptDetailDO> details = productReceiptDetailService.getProductReceiptDetailListByRecptId(id); | |
| 166 | - for (MesWmProductReceiptDetailDO detail : details) { | |
| 167 | - materialStockService.increaseStock( | |
| 168 | - detail.getItemId(), detail.getWarehouseId(), detail.getLocationId(), detail.getAreaId(), | |
| 169 | - detail.getBatchId(), detail.getQuantity(), null, null, null); | |
| 170 | - } | |
| 183 | + // 创建库存事务 | |
| 184 | + createTransactionList(receipt); | |
| 171 | 185 | |
| 172 | 186 | // 更新收货单状态 |
| 173 | 187 | productReceiptMapper.updateById(new MesWmProductReceiptDO() |
| 174 | 188 | .setId(id).setStatus(MesWmProductReceiptStatusEnum.FINISHED.getStatus())); |
| 175 | 189 | } |
| 176 | 190 | |
| 191 | + private void createTransactionList(MesWmProductReceiptDO receipt) { | |
| 192 | + // 1. 查询虚拟线边库 | |
| 193 | + MesWmWarehouseDO virtualWarehouse = warehouseService.getWarehouseByCode(MesWmWarehouseDO.WIP_VIRTUAL_WAREHOUSE); | |
| 194 | + MesWmWarehouseLocationDO virtualLocation = locationService.getWarehouseLocationByCode(MesWmWarehouseLocationDO.WIP_VIRTUAL_LOCATION); | |
| 195 | + MesWmWarehouseAreaDO virtualArea = areaService.getWarehouseAreaByCode(MesWmWarehouseAreaDO.WIP_VIRTUAL_AREA); | |
| 196 | + | |
| 197 | + // 2. 遍历明细,每条明细产生 OUT(虚拟线边库扣减)+ IN(实际仓库增加) | |
| 198 | + List<MesWmProductReceiptDetailDO> details = productReceiptDetailService.getProductReceiptDetailListByRecptId(receipt.getId()); | |
| 199 | + for (MesWmProductReceiptDetailDO detail : details) { | |
| 200 | + // 2.1 先从虚拟线边库出库(库存减少) | |
| 201 | + Long outTransactionId = wmTransactionService.createTransaction(new MesWmTransactionSaveReqDTO() | |
| 202 | + .setType(MesWmTransactionTypeEnum.OUT.getType()).setItemId(detail.getItemId()) | |
| 203 | + .setQuantity(detail.getQuantity().negate()) // 库存减少 | |
| 204 | + .setBatchId(detail.getBatchId()) | |
| 205 | + .setWarehouseId(virtualWarehouse.getId()).setLocationId(virtualLocation.getId()).setAreaId(virtualArea.getId()) | |
| 206 | + .setCheckFlag(false) // 线边库允许负库存 | |
| 207 | + .setBizType(MesBizTypeConstants.WM_PRODUCT_RECPT).setBizId(receipt.getId()) | |
| 208 | + .setBizCode(receipt.getCode()).setBizLineId(detail.getLineId())); | |
| 209 | + // 2.2 再入实际仓库(库存增加) | |
| 210 | + wmTransactionService.createTransaction(new MesWmTransactionSaveReqDTO() | |
| 211 | + .setType(MesWmTransactionTypeEnum.IN.getType()).setItemId(detail.getItemId()) | |
| 212 | + .setQuantity(detail.getQuantity()) // 库存增加 | |
| 213 | + .setBatchId(detail.getBatchId()) | |
| 214 | + .setWarehouseId(detail.getWarehouseId()).setLocationId(detail.getLocationId()).setAreaId(detail.getAreaId()) | |
| 215 | + .setBizType(MesBizTypeConstants.WM_PRODUCT_RECPT).setBizId(receipt.getId()) | |
| 216 | + .setBizCode(receipt.getCode()).setBizLineId(detail.getLineId()) | |
| 217 | + .setRelatedTransactionId(outTransactionId)); // 关联出库事务 | |
| 218 | + } | |
| 219 | + } | |
| 220 | + | |
| 177 | 221 | @Override |
| 178 | 222 | @Transactional(rollbackFor = Exception.class) |
| 179 | 223 | public void cancelProductReceipt(Long id) { | ... | ... |
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/returnissue/MesWmReturnIssueServiceImpl.java
| ... | ... | @@ -22,6 +22,12 @@ import cn.iocoder.yudao.module.mes.service.md.workstation.MesMdWorkstationServic |
| 22 | 22 | import cn.iocoder.yudao.module.mes.service.pro.workorder.MesProWorkOrderService; |
| 23 | 23 | import cn.iocoder.yudao.module.mes.service.wm.transaction.MesWmTransactionService; |
| 24 | 24 | import cn.iocoder.yudao.module.mes.service.wm.transaction.dto.MesWmTransactionSaveReqDTO; |
| 25 | +import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseAreaService; | |
| 26 | +import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseLocationService; | |
| 27 | +import cn.iocoder.yudao.module.mes.service.wm.warehouse.MesWmWarehouseService; | |
| 28 | +import cn.iocoder.yudao.module.mes.dal.dataobject.wm.warehouse.MesWmWarehouseAreaDO; | |
| 29 | +import cn.iocoder.yudao.module.mes.dal.dataobject.wm.warehouse.MesWmWarehouseDO; | |
| 30 | +import cn.iocoder.yudao.module.mes.dal.dataobject.wm.warehouse.MesWmWarehouseLocationDO; | |
| 25 | 31 | import jakarta.annotation.Resource; |
| 26 | 32 | import org.springframework.stereotype.Service; |
| 27 | 33 | import org.springframework.transaction.annotation.Transactional; |
| ... | ... | @@ -32,7 +38,6 @@ import java.util.List; |
| 32 | 38 | import java.util.Map; |
| 33 | 39 | |
| 34 | 40 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 35 | -import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; | |
| 36 | 41 | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.*; |
| 37 | 42 | |
| 38 | 43 | /** |
| ... | ... | @@ -57,6 +62,12 @@ public class MesWmReturnIssueServiceImpl implements MesWmReturnIssueService { |
| 57 | 62 | private MesMdItemService itemService; |
| 58 | 63 | @Resource |
| 59 | 64 | private MesWmTransactionService wmTransactionService; |
| 65 | + @Resource | |
| 66 | + private MesWmWarehouseService warehouseService; | |
| 67 | + @Resource | |
| 68 | + private MesWmWarehouseLocationService locationService; | |
| 69 | + @Resource | |
| 70 | + private MesWmWarehouseAreaService areaService; | |
| 60 | 71 | |
| 61 | 72 | @Override |
| 62 | 73 | @Transactional(rollbackFor = Exception.class) |
| ... | ... | @@ -194,14 +205,33 @@ public class MesWmReturnIssueServiceImpl implements MesWmReturnIssueService { |
| 194 | 205 | } |
| 195 | 206 | |
| 196 | 207 | private void createTransactionList(MesWmReturnIssueDO issue) { |
| 208 | + // 1. 查询虚拟线边库 | |
| 209 | + MesWmWarehouseDO virtualWarehouse = warehouseService.getWarehouseByCode(MesWmWarehouseDO.WIP_VIRTUAL_WAREHOUSE); | |
| 210 | + MesWmWarehouseLocationDO virtualLocation = locationService.getWarehouseLocationByCode(MesWmWarehouseLocationDO.WIP_VIRTUAL_LOCATION); | |
| 211 | + MesWmWarehouseAreaDO virtualArea = areaService.getWarehouseAreaByCode(MesWmWarehouseAreaDO.WIP_VIRTUAL_AREA); | |
| 212 | + | |
| 213 | + // 2. 遍历明细,每条明细产生 OUT(虚拟线边库扣减)+ IN(实际仓库增加) | |
| 197 | 214 | List<MesWmReturnIssueDetailDO> details = issueDetailService.getReturnIssueDetailListByIssueId(issue.getId()); |
| 198 | - wmTransactionService.createTransactionList(convertList(details, detail -> new MesWmTransactionSaveReqDTO() | |
| 199 | - .setType(MesWmTransactionTypeEnum.IN.getType()).setItemId(detail.getItemId()) | |
| 200 | - .setQuantity(detail.getQuantity()) // 入库数量为正数(退料回仓) | |
| 201 | - .setBatchId(detail.getBatchId()).setBatchCode(detail.getBatchCode()) | |
| 202 | - .setWarehouseId(detail.getWarehouseId()).setLocationId(detail.getLocationId()).setAreaId(detail.getAreaId()) | |
| 203 | - .setBizType(MesBizTypeConstants.WM_RETURN_ISSUE).setBizId(issue.getId()) | |
| 204 | - .setBizCode(issue.getCode()).setBizLineId(detail.getLineId()))); | |
| 215 | + for (MesWmReturnIssueDetailDO detail : details) { | |
| 216 | + // 2.1 先从虚拟线边库出库(库存减少) | |
| 217 | + Long outTransactionId = wmTransactionService.createTransaction(new MesWmTransactionSaveReqDTO() | |
| 218 | + .setType(MesWmTransactionTypeEnum.OUT.getType()).setItemId(detail.getItemId()) | |
| 219 | + .setQuantity(detail.getQuantity().negate()) // 库存减少 | |
| 220 | + .setBatchId(detail.getBatchId()).setBatchCode(detail.getBatchCode()) | |
| 221 | + .setWarehouseId(virtualWarehouse.getId()).setLocationId(virtualLocation.getId()).setAreaId(virtualArea.getId()) | |
| 222 | + .setCheckFlag(false) // 线边库允许负库存 | |
| 223 | + .setBizType(MesBizTypeConstants.WM_RETURN_ISSUE).setBizId(issue.getId()) | |
| 224 | + .setBizCode(issue.getCode()).setBizLineId(detail.getLineId())); | |
| 225 | + // 2.2 再入实际仓库(库存增加) | |
| 226 | + wmTransactionService.createTransaction(new MesWmTransactionSaveReqDTO() | |
| 227 | + .setType(MesWmTransactionTypeEnum.IN.getType()).setItemId(detail.getItemId()) | |
| 228 | + .setQuantity(detail.getQuantity()) // 库存增加(退料回仓) | |
| 229 | + .setBatchId(detail.getBatchId()).setBatchCode(detail.getBatchCode()) | |
| 230 | + .setWarehouseId(detail.getWarehouseId()).setLocationId(detail.getLocationId()).setAreaId(detail.getAreaId()) | |
| 231 | + .setBizType(MesBizTypeConstants.WM_RETURN_ISSUE).setBizId(issue.getId()) | |
| 232 | + .setBizCode(issue.getCode()).setBizLineId(detail.getLineId()) | |
| 233 | + .setRelatedTransactionId(outTransactionId)); // 关联出库事务 | |
| 234 | + } | |
| 205 | 235 | } |
| 206 | 236 | |
| 207 | 237 | @Override | ... | ... |
yudao-module-mes/src/main/java/cn/iocoder/yudao/module/mes/service/wm/transaction/MesWmTransactionServiceImpl.java
| ... | ... | @@ -68,6 +68,8 @@ public class MesWmTransactionServiceImpl implements MesWmTransactionService { |
| 68 | 68 | if (inbound) { |
| 69 | 69 | materialStockService.checkAreaMixingRule(reqDTO.getAreaId(), reqDTO.getItemId(), reqDTO.getBatchId()); |
| 70 | 70 | } |
| 71 | + // 1.5 关联事务校验 | |
| 72 | + checkRelatedTransaction(reqDTO); | |
| 71 | 73 | |
| 72 | 74 | // 2.1 获取或创建库存记录 |
| 73 | 75 | MesWmMaterialStockDO materialStock = materialStockService.getOrCreateMaterialStock( |
| ... | ... | @@ -143,6 +145,19 @@ public class MesWmTransactionServiceImpl implements MesWmTransactionService { |
| 143 | 145 | } |
| 144 | 146 | |
| 145 | 147 | /** |
| 148 | + * 关联事务校验:relatedTransactionId 不为空时,关联的事务必须存在 | |
| 149 | + */ | |
| 150 | + private void checkRelatedTransaction(MesWmTransactionSaveReqDTO reqDTO) { | |
| 151 | + if (reqDTO.getRelatedTransactionId() == null) { | |
| 152 | + return; | |
| 153 | + } | |
| 154 | + MesWmTransactionDO relatedTransaction = transactionMapper.selectById(reqDTO.getRelatedTransactionId()); | |
| 155 | + if (relatedTransaction == null) { | |
| 156 | + throw exception(WM_TRANSACTION_RELATED_NOT_EXISTS); | |
| 157 | + } | |
| 158 | + } | |
| 159 | + | |
| 160 | + /** | |
| 146 | 161 | * 冻结校验:检查仓库、库区、库位、库存记录是否被冻结 |
| 147 | 162 | * |
| 148 | 163 | * @param reqDTO 事务请求 DTO |
| ... | ... | @@ -167,8 +182,7 @@ public class MesWmTransactionServiceImpl implements MesWmTransactionService { |
| 167 | 182 | |
| 168 | 183 | // 2. 检查库存记录冻结 |
| 169 | 184 | if (Boolean.TRUE.equals(materialStock.getFrozen())) { |
| 170 | - throw exception(WM_TRANSACTION_STOCK_FROZEN, | |
| 171 | - warehouse.getName(), location.getName(), area.getName()); | |
| 185 | + throw exception(WM_TRANSACTION_STOCK_FROZEN, warehouse.getName(), location.getName(), area.getName()); | |
| 172 | 186 | } |
| 173 | 187 | } |
| 174 | 188 | ... | ... |