> entry : purchaseSnMap.entrySet()) {
+ String purchaseNo = entry.getKey();
+ int newSnCount = entry.getValue().size();
+ OmsPurchaseOrder purchaseOrder = omsPurchaseOrderService.selectOmsPurchaseOrderByNo(purchaseNo);
+ if (purchaseOrder == null) {
+ throw new ServiceException(String.format("采购单[%s]信息不存在", purchaseNo));
+ }
+ OmsPurchaseOrderMap purchaseOrderMap = omsPurchaseOrderMapMapper.selectByOrderIdAndPurchaseId(orderInfo.getId(), purchaseOrder.getId(), inventoryDelivery.getProductCode());
+ if (purchaseOrderMap == null || purchaseOrderMap.getBindNum() == null || purchaseOrderMap.getBindNum() <= 0) {
+ throw new ServiceException(String.format("当前订单与采购单[%s]未建立绑定关系或绑定数量为0", purchaseNo));
+ }
+ int totalBindNum = purchaseOrderMap.getBindNum();
+ int alreadyBindNum = inventoryInfoMapper.countByOrderCodeAndPurchaseNo(inventoryDelivery.getOrderCode(), purchaseNo, inventoryDelivery.getProductCode());
+ int remainingNum = totalBindNum - alreadyBindNum;
+ if (newSnCount > remainingNum) {
+ throw new ServiceException(String.format("采购单[%s]新增SN码数量(%d)超过剩余可新增数量(%d),总绑定数(%d),已绑定数(%d)",
+ purchaseNo, newSnCount, remainingNum, totalBindNum, alreadyBindNum));
+ }
+ }
+ return snPurchaseMap;
+ }
+
+ /**
+ * 解析每个SN归属的采购单。
+ *
+ * 前端按采购单页签提交的SN优先,已入库SN再用库存入库单的实际采购单做一致性校验;
+ * 前端未声明时使用库存实际归属;两者都取不到时,只有在能确定本次只涉及一个采购单的
+ * 情况下才回落到本次提交的采购单号,多采购单场景直接报错,避免整批SN被归到同一采购单。
+ *
+ * @param snSet 本次发货涉及的SN
+ * @param productSnDataList 导入清单
+ * @param assignSnDataList 指定发货清单
+ * @param defaultPurchaseNo 本次提交的采购单号
+ * @return SN与采购单号的关系,每个SN都有值
+ */
+ private Map resolveSnPurchaseNoMap(Set snSet, List productSnDataList,
+ List assignSnDataList, String defaultPurchaseNo) {
+ Map snPurchaseMap = new LinkedHashMap<>();
+ if (CollUtil.isEmpty(snSet)) {
+ return snPurchaseMap;
+ }
+ Map declaredPurchaseMap = new LinkedHashMap<>();
+ collectSnPurchaseNo(snSet, productSnDataList, declaredPurchaseMap);
+ collectSnPurchaseNo(snSet, assignSnDataList, declaredPurchaseMap);
+ Map inventoryPurchaseMap = loadInventoryPurchaseNoMap(snSet);
+ Set knownPurchaseNoSet = new LinkedHashSet<>(declaredPurchaseMap.values());
+ knownPurchaseNoSet.addAll(inventoryPurchaseMap.values());
+ // 没有任何采购单线索时(历史库存未关联采购单、纯手工发货),保持原有行为不做采购配额校验
+ if (knownPurchaseNoSet.isEmpty() && StringUtils.isEmpty(defaultPurchaseNo)) {
+ return snPurchaseMap;
+ }
+ // 只有已知采购单为空或仅等于本次提交的采购单时,才能认为缺失归属的SN也属于该采购单;
+ // 多采购单一起发货时提交的 purchaseNo 只是最后操作的页签,不能作为整批兜底
+ boolean canUseDefaultPurchaseNo = StringUtils.isNotEmpty(defaultPurchaseNo)
+ && (knownPurchaseNoSet.isEmpty()
+ || (knownPurchaseNoSet.size() == 1 && knownPurchaseNoSet.contains(defaultPurchaseNo)));
+ for (String productSn : snSet) {
+ String declaredPurchaseNo = declaredPurchaseMap.get(productSn);
+ String inventoryPurchaseNo = inventoryPurchaseMap.get(productSn);
+ if (StringUtils.isNotEmpty(declaredPurchaseNo)) {
+ if (StringUtils.isNotEmpty(inventoryPurchaseNo) && !inventoryPurchaseNo.equals(declaredPurchaseNo)) {
+ throw new ServiceException(String.format("SN码[%s]所属采购单为[%s],与页签选择的采购单[%s]不一致,请确认后重试",
+ productSn, inventoryPurchaseNo, declaredPurchaseNo));
+ }
+ snPurchaseMap.put(productSn, declaredPurchaseNo);
+ } else if (StringUtils.isNotEmpty(inventoryPurchaseNo)) {
+ snPurchaseMap.put(productSn, inventoryPurchaseNo);
+ } else if (canUseDefaultPurchaseNo) {
+ snPurchaseMap.put(productSn, defaultPurchaseNo);
+ } else {
+ throw new ServiceException(String.format("SN码[%s]未携带采购单号,无法确定所属采购单,请重新选择发货清单", productSn));
+ }
+ }
+ return snPurchaseMap;
+ }
+
+ /**
+ * 查询已入库SN的实际采购单归属。
+ *
+ * @param snSet 本次发货涉及的SN
+ * @return SN与库存入库单采购单号的关系,未入库或入库单无采购单号的SN不在结果中
+ */
+ private Map loadInventoryPurchaseNoMap(Set snSet) {
+ Map snPurchaseMap = new HashMap<>();
+ if (CollUtil.isEmpty(snSet)) {
+ return snPurchaseMap;
+ }
+ List inventoryInfoList = inventoryInfoMapper.listByProductSnList(new ArrayList<>(snSet));
+ if (CollUtil.isEmpty(inventoryInfoList)) {
+ return snPurchaseMap;
+ }
+ List innerCodeList = inventoryInfoList.stream().map(InventoryInfo::getInnerCode)
+ .filter(StringUtils::isNotEmpty).distinct().collect(Collectors.toList());
+ Map innerPurchaseMap = new HashMap<>();
+ if (CollUtil.isNotEmpty(innerCodeList)) {
+ for (OmsInventoryInner inner : omsInventoryInnerMapper.selectOmsInventoryInnerByInnerCodeList(innerCodeList)) {
+ if (StringUtils.isNotEmpty(inner.getInnerCode()) && StringUtils.isNotEmpty(inner.getPurchaseNo())) {
+ innerPurchaseMap.put(inner.getInnerCode(), inner.getPurchaseNo());
+ }
+ }
+ }
+ for (InventoryInfo info : inventoryInfoList) {
+ String purchaseNo = innerPurchaseMap.get(info.getInnerCode());
+ if (StringUtils.isEmpty(info.getProductSn()) || StringUtils.isEmpty(purchaseNo)) {
+ continue;
+ }
+ String existingPurchaseNo = snPurchaseMap.get(info.getProductSn());
+ if (StringUtils.isNotEmpty(existingPurchaseNo) && !existingPurchaseNo.equals(purchaseNo)) {
+ throw new ServiceException(String.format("SN码[%s]在库存中同时归属采购单[%s]和[%s],请确认后重试",
+ info.getProductSn(), existingPurchaseNo, purchaseNo));
+ }
+ snPurchaseMap.put(info.getProductSn(), purchaseNo);
+ }
+ return snPurchaseMap;
+ }
+
+ /**
+ * 收集前端清单中明确携带的SN采购单归属。
+ *
+ * @param snSet 本次发货涉及的SN
+ * @param snDataList 导入或指定发货清单
+ * @param snPurchaseMap SN与采购单号的关系
+ */
+ private void collectSnPurchaseNo(Set snSet, List snDataList, Map snPurchaseMap) {
+ if (CollUtil.isEmpty(snDataList)) {
+ return;
+ }
+ for (InventoryInfo info : snDataList) {
+ String productSn = info.getProductSn();
+ if (StringUtils.isEmpty(productSn) || StringUtils.isEmpty(info.getPurchaseNo()) || !snSet.contains(productSn)) {
+ continue;
+ }
+ String existingPurchaseNo = snPurchaseMap.get(productSn);
+ if (StringUtils.isNotEmpty(existingPurchaseNo) && !existingPurchaseNo.equals(info.getPurchaseNo())) {
+ throw new ServiceException(String.format("SN码[%s]同时归属采购单[%s]和[%s],请确认后重试",
+ productSn, existingPurchaseNo, info.getPurchaseNo()));
+ }
+ snPurchaseMap.put(productSn, info.getPurchaseNo());
+ }
+ }
+
+ /**
+ * 按采购明细剩余可入库数量拆分本次入库量。
+ *
+ * 同一采购单同一产品可能存在多条明细,单条明细剩余不足但合计足够时,
+ * 需要拆成多张入库单分别回写对应明细,不能只取一条明细。
+ * 优先使用前端带入的明细,再按明细顺序补足。
+ *
+ * @param purchaseNo 采购单号
+ * @param productCode 产品编码
+ * @param groupSize 本次该采购单需要入库的SN数量
+ * @param preferredItemId 前端指定的采购明细ID,可为空
+ * @return 采购明细ID与本次入库数量的关系,按拆分顺序排列
+ */
+ private Map allocatePurchaseItemQuantities(String purchaseNo, String productCode, int groupSize,
+ Long preferredItemId) {
+ OmsPurchaseOrderItemDto itemQuery = new OmsPurchaseOrderItemDto();
+ itemQuery.setPurchaseNo(purchaseNo);
+ itemQuery.setProductCode(productCode);
+ List itemList = omsPurchaseOrderService.listItem(itemQuery);
+ if (CollUtil.isEmpty(itemList)) {
+ throw new ServiceException(String.format("采购单[%s]未找到产品[%s]的采购明细", purchaseNo, productCode));
+ }
+ List candidates = new ArrayList<>();
+ if (preferredItemId != null) {
+ for (OmsPurchaseOrderItemDto item : itemList) {
+ if (preferredItemId.equals(item.getItemId())) {
+ if (getRemainingInnerQuantity(item) > 0) {
+ candidates.add(item);
+ }
+ break;
+ }
+ }
+ }
+ for (OmsPurchaseOrderItemDto item : itemList) {
+ if (item.getItemId() == null || item.getItemId().equals(preferredItemId)
+ || getRemainingInnerQuantity(item) <= 0) {
+ continue;
+ }
+ candidates.add(item);
+ }
+
+ Map allocationMap = new LinkedHashMap<>();
+ int remaining = groupSize;
+ for (OmsPurchaseOrderItemDto item : candidates) {
+ if (remaining <= 0) {
+ break;
+ }
+ int take = Math.min(remaining, getRemainingInnerQuantity(item));
+ if (take > 0) {
+ allocationMap.put(item.getItemId(), take);
+ remaining -= take;
+ }
+ }
+ if (remaining > 0) {
+ throw new ServiceException(String.format("采购单[%s]产品[%s]剩余可入库数量不足,本次需入库%d",
+ purchaseNo, productCode, groupSize));
+ }
+ return allocationMap;
+ }
+
+ /**
+ * 采购明细剩余可入库数量。
+ *
+ * @param item 采购明细
+ * @return 剩余可入库数量
+ */
+ private int getRemainingInnerQuantity(OmsPurchaseOrderItemDto item) {
+ int quantity = item.getQuantity() == null ? 0 : item.getQuantity().intValue();
+ int innerQuantity = item.getInnerQuantity() == null ? 0 : item.getInnerQuantity().intValue();
+ return quantity - innerQuantity;
+ }
}
diff --git a/ruoyi-sip/src/main/java/com/ruoyi/sip/service/impl/OmsInventoryInnerServiceImpl.java b/ruoyi-sip/src/main/java/com/ruoyi/sip/service/impl/OmsInventoryInnerServiceImpl.java
index 5e56d899..54559546 100644
--- a/ruoyi-sip/src/main/java/com/ruoyi/sip/service/impl/OmsInventoryInnerServiceImpl.java
+++ b/ruoyi-sip/src/main/java/com/ruoyi/sip/service/impl/OmsInventoryInnerServiceImpl.java
@@ -225,7 +225,10 @@ public class OmsInventoryInnerServiceImpl implements IOmsInventoryInnerService {
}
BigDecimal finalTaxRate = taxRate == null ? new BigDecimal(defaultTax) : taxRate;
- BigDecimal withoutTax = inventoryInfoList.stream().reduce(BigDecimal.ZERO, (a, b) -> a.add(b.getInnerPrice().divide(BigDecimal.ONE.add(finalTaxRate), 2, RoundingMode.HALF_UP)), BigDecimal::add);
+ BigDecimal withoutTax = inventoryInfoList.stream()
+ .map(InventoryInfo::getInnerPrice)
+ .filter(Objects::nonNull)
+ .reduce(BigDecimal.ZERO, (a, b) -> a.add(b.divide(BigDecimal.ONE.add(finalTaxRate), 2, RoundingMode.HALF_UP)), BigDecimal::add);
payableBill.setTotalPriceWithoutTax(withoutTax);
payableBill.setTaxRate(finalTaxRate);
payableBill.setTaxAmount(totalPriceWithTax.subtract(payableBill.getTotalPriceWithoutTax()));
@@ -322,7 +325,8 @@ public class OmsInventoryInnerServiceImpl implements IOmsInventoryInnerService {
omsInventoryInner.setProductCode(productCode);
omsInventoryInner.setProductType(productInfos.get(0).getType());
omsInventoryInner.setPurchaseNo(purchaseNo);
- omsInventoryInner.setTotalAmount(inventoryInfoList.stream().map(InventoryInfo::getInnerPrice).reduce(BigDecimal.ZERO, BigDecimal::add));
+ omsInventoryInner.setTotalAmount(inventoryInfoList.stream().map(InventoryInfo::getInnerPrice)
+ .filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add));
omsInventoryInner.setTaxRate(inventoryInfoList.get(0).getTaxRate());
// omsInventoryInner.setTaxTotal(inventoryInfoList.stream().map(item->
// item.getInnerPrice().divide(BigDecimal.ONE.add(item.getTaxRate()),2, RoundingMode.HALF_UP)
diff --git a/ruoyi-sip/src/main/resources/mapper/sip/OmsPurchaseOrderMapper.xml b/ruoyi-sip/src/main/resources/mapper/sip/OmsPurchaseOrderMapper.xml
index 2b20ade1..5b43a195 100644
--- a/ruoyi-sip/src/main/resources/mapper/sip/OmsPurchaseOrderMapper.xml
+++ b/ruoyi-sip/src/main/resources/mapper/sip/OmsPurchaseOrderMapper.xml
@@ -246,7 +246,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
#{item}
- order by t2.create_time desc
+ order by t2.create_time desc, t1.id asc