|
@ -457,148 +457,85 @@ public class PurchaseOrderServiceImpl implements IPurchaseOrderService |
|
|
|
|
|
|
|
|
for (PurchasePlanSelectMaterialVo purchasePlanSelectMaterialVo : purchasePlanSelectMaterialVoList) { |
|
|
for (PurchasePlanSelectMaterialVo purchasePlanSelectMaterialVo : purchasePlanSelectMaterialVoList) { |
|
|
String materialNo = purchasePlanSelectMaterialVo.getMaterialNo(); |
|
|
String materialNo = purchasePlanSelectMaterialVo.getMaterialNo(); |
|
|
// Integer thisPurchaseNum = purchasePlanSelectMaterialVo.getThisPurchaseNum();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//共享库存占用数
|
|
|
|
|
|
Integer sharedInventoryOccupancyNum = purchasePlanSelectMaterialVo.getSharedInventoryOccupancyNum(); |
|
|
|
|
|
//库存查询
|
|
|
|
|
|
WarehouseInventoryInquiry warehouseInventoryInquiry = inventoryInquiryMapper.selectWarehouseInventoryInquiryByMaterialNo(materialNo); |
|
|
|
|
|
if ( sharedInventoryOccupancyNum != null && sharedInventoryOccupancyNum > 0){ |
|
|
|
|
|
Integer sharedAvailableStockNum = Optional.ofNullable(warehouseInventoryInquiry.getSharedAvailableStockNum()).orElse(0); |
|
|
|
|
|
|
|
|
|
|
|
Integer availableStockNum = Optional.ofNullable(warehouseInventoryInquiry.getAvailableStockNum()).orElse(0); |
|
|
|
|
|
Integer lockStockNum = Optional.ofNullable(warehouseInventoryInquiry.getLockStockNum()).orElse(0); |
|
|
|
|
|
warehouseInventoryInquiry.setAvailableStockNum(availableStockNum - sharedInventoryOccupancyNum); |
|
|
|
|
|
warehouseInventoryInquiry.setSharedAvailableStockNum(sharedAvailableStockNum - sharedInventoryOccupancyNum); |
|
|
|
|
|
warehouseInventoryInquiry.setLockStockNum(lockStockNum + sharedInventoryOccupancyNum); |
|
|
|
|
|
warehouseInventoryInquiry.setUpdateTime(new Date()); |
|
|
|
|
|
warehouseInventoryInquiry.setUpdateBy(ShiroUtils.getLoginName()); |
|
|
|
|
|
int updateInventoryInquiry = inventoryInquiryMapper.updateWarehouseInventoryInquiry(warehouseInventoryInquiry); |
|
|
|
|
|
if (updateInventoryInquiry <= 0){ |
|
|
|
|
|
throw new BusinessException("更新库存查询记录失败"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//本次采购数
|
|
|
|
|
|
Integer thisPurchaseNum = purchasePlanSelectMaterialVo.getThisPurchaseNum(); |
|
|
Integer thisPurchaseNum = purchasePlanSelectMaterialVo.getThisPurchaseNum(); |
|
|
|
|
|
Integer sharedInventoryOccupancyNum = purchasePlanSelectMaterialVo.getSharedInventoryOccupancyNum(); |
|
|
|
|
|
|
|
|
//共享可用库存
|
|
|
// 检查是否有共享库存占用数,没有的话直接使用thisPurchaseNum
|
|
|
Integer sharedAvailableInventoryNum = purchasePlanSelectMaterialVo.getSharedAvailableInventoryNum(); |
|
|
Integer remainingSharedInventoryOccupancyNum = 0; |
|
|
|
|
|
if (sharedInventoryOccupancyNum == null || sharedInventoryOccupancyNum <= 0) { |
|
|
//待采购数
|
|
|
remainingSharedInventoryOccupancyNum = 0; |
|
|
Integer purchaseNum = purchasePlanSelectMaterialVo.getPurchaseNum(); |
|
|
} else { |
|
|
|
|
|
remainingSharedInventoryOccupancyNum = sharedInventoryOccupancyNum; |
|
|
if (purchaseNum < thisPurchaseNum){ |
|
|
|
|
|
throw new BusinessException("本次采购数量不能大于待采购数量"); |
|
|
|
|
|
} |
|
|
} |
|
|
// 获取旧数据中相同物料编号的数据
|
|
|
|
|
|
List<PurchasePlanChild> oldChildren = oldDataGroupedByMaterialNo.get(materialNo); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取旧数据中相同物料编号的数据,并排序
|
|
|
|
|
|
List<PurchasePlanChild> oldChildren = Optional.ofNullable(oldDataGroupedByMaterialNo.get(materialNo)).orElse(Collections.emptyList()); |
|
|
|
|
|
oldChildren.sort(Comparator.comparing(PurchasePlanChild::getCreateTime)); |
|
|
|
|
|
|
|
|
// 优先使用本次占用共享库存
|
|
|
// 遍历旧数据,分配剩余的共享库存占用数和本次采购数
|
|
|
Integer remainingSharedInventoryOccupancyNum = Math.min(sharedInventoryOccupancyNum, thisPurchaseNum); |
|
|
|
|
|
|
|
|
|
|
|
if (!CollectionUtils.isEmpty(oldChildren)) { |
|
|
|
|
|
// 按照订单创建时间远离当前时间排序(即由远及近)
|
|
|
|
|
|
oldChildren.sort(Comparator.comparing(oldChild -> oldChild.getCreateTime().getTime())); |
|
|
|
|
|
|
|
|
|
|
|
for (PurchasePlanChild oldChild : oldChildren) { |
|
|
for (PurchasePlanChild oldChild : oldChildren) { |
|
|
|
|
|
if (remainingSharedInventoryOccupancyNum <= 0 && thisPurchaseNum <= 0) { |
|
|
// Integer oldPurchaseNum = Optional.ofNullable(oldChild.getPurchaseNum()).orElse(0);//待采购数
|
|
|
|
|
|
Integer planPurchaseNum = Optional.ofNullable(oldChild.getPlanPurchaseNum()).orElse(0);//计划采购数
|
|
|
|
|
|
Integer oldActualPurchaseNum = Optional.ofNullable(oldChild.getActualPurchaseNum()).orElse(0);//实际采购数
|
|
|
|
|
|
Integer cancelNum = Optional.ofNullable(oldChild.getCancelNum()).orElse(0);//作废数
|
|
|
|
|
|
Integer oldSharedInventoryOccupancyNum = Optional.ofNullable(oldChild.getSharedInventoryOccupancyNum()).orElse(0);//共享库存占用数
|
|
|
|
|
|
|
|
|
|
|
|
if (remainingSharedInventoryOccupancyNum <= 0){ |
|
|
|
|
|
if (purchaseNum <= 0){ |
|
|
|
|
|
// 更新状态
|
|
|
|
|
|
updateChildPurchasePlanStatus(oldChild,sharedInventoryOccupancyNum,thisPurchaseNum,purchaseNum); |
|
|
|
|
|
// 更新其他字段
|
|
|
|
|
|
oldChild.setUpdateTime(new Date()); |
|
|
|
|
|
oldChild.setUpdateBy(ShiroUtils.getLoginName()); |
|
|
|
|
|
//oldChild.setThisPurchaseNum(purchasePlanSelectMaterialVo.getThisPurchaseNum());
|
|
|
|
|
|
oldChild.setMaterialNoRmbSum(purchasePlanSelectMaterialVo.getMaterialNoRmbSum()); |
|
|
|
|
|
oldChild.setMaterialRmbSum(purchasePlanSelectMaterialVo.getMaterialRmbSum()); |
|
|
|
|
|
oldChild.setLatestQuoteRmb(purchasePlanSelectMaterialVo.getLatestQuoteRmb()); |
|
|
|
|
|
oldChild.setThisPurchaseNum(thisPurchaseNum); |
|
|
|
|
|
int result = purchasePlanChildMapper.updatePurchasePlanChild(oldChild); |
|
|
|
|
|
if (result <= 0){ |
|
|
|
|
|
throw new BusinessException("更新采购计划子表数据失败"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
break; // 如果本次采购数和共享库存都已分配完毕则退出循环
|
|
|
break; // 如果本次采购数和共享库存都已分配完毕则退出循环
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Integer planPurchaseNum = Optional.ofNullable(oldChild.getPlanPurchaseNum()).orElse(0); |
|
|
|
|
|
Integer oldActualPurchaseNum = Optional.ofNullable(oldChild.getActualPurchaseNum()).orElse(0); |
|
|
|
|
|
Integer cancelNum = Optional.ofNullable(oldChild.getCancelNum()).orElse(0); |
|
|
|
|
|
Integer oldSharedInventoryOccupancyNum = Optional.ofNullable(oldChild.getSharedInventoryOccupancyNum()).orElse(0); |
|
|
|
|
|
|
|
|
// 更新状态
|
|
|
// 更新共享库存占用数和实际采购数
|
|
|
updateChildPurchasePlanStatus(oldChild,sharedInventoryOccupancyNum,thisPurchaseNum,purchaseNum); |
|
|
int useSharedInventory = 0; |
|
|
|
|
|
if (remainingSharedInventoryOccupancyNum > 0) { |
|
|
if (oldSharedInventoryOccupancyNum == 0){ |
|
|
// 先用完剩余的共享库存占用数
|
|
|
oldChild.setSharedInventoryOccupancyNum(sharedInventoryOccupancyNum); |
|
|
useSharedInventory = Math.min(remainingSharedInventoryOccupancyNum, planPurchaseNum - oldActualPurchaseNum - cancelNum); |
|
|
}else { |
|
|
remainingSharedInventoryOccupancyNum -= useSharedInventory; |
|
|
oldChild.setSharedInventoryOccupancyNum(oldSharedInventoryOccupancyNum + sharedInventoryOccupancyNum); |
|
|
oldChild.setSharedInventoryOccupancyNum(oldSharedInventoryOccupancyNum + useSharedInventory); |
|
|
|
|
|
oldChild.setActualPurchaseNum(oldActualPurchaseNum + useSharedInventory); |
|
|
} |
|
|
} |
|
|
if (oldActualPurchaseNum == 0){ |
|
|
|
|
|
oldChild.setActualPurchaseNum(thisPurchaseNum); |
|
|
int useThisPurchase = 0; |
|
|
}else { |
|
|
if (thisPurchaseNum > 0) { |
|
|
oldChild.setActualPurchaseNum(oldActualPurchaseNum + thisPurchaseNum); |
|
|
// 然后用本次采购数补充
|
|
|
|
|
|
useThisPurchase = Math.min(thisPurchaseNum, planPurchaseNum - oldActualPurchaseNum - cancelNum - useSharedInventory); |
|
|
|
|
|
thisPurchaseNum -= useThisPurchase; |
|
|
|
|
|
oldChild.setActualPurchaseNum(oldActualPurchaseNum + useThisPurchase); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 更新其他字段
|
|
|
// 更新其他字段和状态
|
|
|
|
|
|
updateChildPurchasePlanStatus(oldChild); |
|
|
oldChild.setUpdateTime(new Date()); |
|
|
oldChild.setUpdateTime(new Date()); |
|
|
oldChild.setUpdateBy(ShiroUtils.getLoginName()); |
|
|
oldChild.setUpdateBy(ShiroUtils.getLoginName()); |
|
|
//oldChild.setThisPurchaseNum(purchasePlanSelectMaterialVo.getThisPurchaseNum());
|
|
|
|
|
|
oldChild.setMaterialNoRmbSum(purchasePlanSelectMaterialVo.getMaterialNoRmbSum()); |
|
|
oldChild.setMaterialNoRmbSum(purchasePlanSelectMaterialVo.getMaterialNoRmbSum()); |
|
|
oldChild.setMaterialRmbSum(purchasePlanSelectMaterialVo.getMaterialRmbSum()); |
|
|
oldChild.setMaterialRmbSum(purchasePlanSelectMaterialVo.getMaterialRmbSum()); |
|
|
oldChild.setLatestQuoteRmb(purchasePlanSelectMaterialVo.getLatestQuoteRmb()); |
|
|
oldChild.setLatestQuoteRmb(purchasePlanSelectMaterialVo.getLatestQuoteRmb()); |
|
|
oldChild.setThisPurchaseNum(thisPurchaseNum); |
|
|
|
|
|
// 收集更新后的子表数据
|
|
|
|
|
|
purchasePlanChildren.add(oldChild); |
|
|
purchasePlanChildren.add(oldChild); |
|
|
|
|
|
// 更新数据库
|
|
|
int result = purchasePlanChildMapper.updatePurchasePlanChild(oldChild); |
|
|
int result = purchasePlanChildMapper.updatePurchasePlanChild(oldChild); |
|
|
if (result <= 0) { |
|
|
if (result <= 0) { |
|
|
throw new BusinessException("更新采购计划子表数据失败"); |
|
|
throw new BusinessException("更新采购计划子表数据失败"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
return purchasePlanChildren; |
|
|
return purchasePlanChildren; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 定义一个方法来更新采购计划状态
|
|
|
// 定义一个方法来更新采购计划状态
|
|
|
private void updateChildPurchasePlanStatus(PurchasePlanChild oldChild,Integer sharedInventoryOccupancyNum,Integer thisPurchaseNum,Integer purchaseNum) { |
|
|
private void updateChildPurchasePlanStatus(PurchasePlanChild oldChild) { |
|
|
|
|
|
|
|
|
/* |
|
|
/* |
|
|
|
|
|
* 待申请:实际采购数=共享库存占用数=作废数=0,作废数<计划采购数; |
|
|
* 部分申请:0<(实际采购数+共享库存占用数+作废数)<计划采购数;可添加采购订单 |
|
|
* 部分申请:0<(实际采购数+共享库存占用数+作废数)<计划采购数;可添加采购订单 |
|
|
* 全部申请:0<(实际采购数+共享库存占用数+作废数)=计划采购数,作废数<计划采购数;不可添加采购订单 |
|
|
* 全部申请:0<(实际采购数+共享库存占用数+作废数)=计划采购数,作废数<计划采购数;不可添加采购订单 |
|
|
* */ |
|
|
*/ |
|
|
|
|
|
|
|
|
Integer oldSharedInventoryOccupancyNum = Optional.ofNullable(oldChild.getSharedInventoryOccupancyNum()).orElse(0);//共享库存占用数
|
|
|
|
|
|
Integer planPurchaseNum = Optional.ofNullable(oldChild.getPlanPurchaseNum()).orElse(0);//计划采购数
|
|
|
|
|
|
Integer actualPurchaseNum = Optional.ofNullable(oldChild.getActualPurchaseNum()).orElse(0);//实际采购数
|
|
|
|
|
|
Integer cancelNum = Optional.ofNullable(oldChild.getCancelNum()).orElse(0);//作废数
|
|
|
|
|
|
|
|
|
|
|
|
if (oldSharedInventoryOccupancyNum == 0){ |
|
|
|
|
|
oldSharedInventoryOccupancyNum = sharedInventoryOccupancyNum; |
|
|
|
|
|
}else { |
|
|
|
|
|
oldSharedInventoryOccupancyNum += sharedInventoryOccupancyNum; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (purchaseNum <= 0){ |
|
|
|
|
|
actualPurchaseNum = thisPurchaseNum; |
|
|
|
|
|
}else { |
|
|
|
|
|
actualPurchaseNum += thisPurchaseNum; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Integer actualPurchaseNum = Optional.ofNullable(oldChild.getActualPurchaseNum()).orElse(0); |
|
|
|
|
|
Integer oldSharedInventoryOccupancyNum = Optional.ofNullable(oldChild.getSharedInventoryOccupancyNum()).orElse(0); |
|
|
|
|
|
Integer cancelNum = Optional.ofNullable(oldChild.getCancelNum()).orElse(0); |
|
|
|
|
|
Integer planPurchaseNum = Optional.ofNullable(oldChild.getPlanPurchaseNum()).orElse(0); |
|
|
|
|
|
|
|
|
|
|
|
// totalApplied 应该只包含已经分配给当前子项的数量
|
|
|
Integer totalApplied = actualPurchaseNum + oldSharedInventoryOccupancyNum + cancelNum; |
|
|
Integer totalApplied = actualPurchaseNum + oldSharedInventoryOccupancyNum + cancelNum; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (totalApplied == 0 && cancelNum < planPurchaseNum) { |
|
|
if (totalApplied == 0 && cancelNum < planPurchaseNum) { |
|
|
oldChild.setPurchasePlanStatus("0"); // 待申请
|
|
|
oldChild.setPurchasePlanStatus("0"); // 待申请
|
|
|
} else if (0 < totalApplied && totalApplied < planPurchaseNum) { |
|
|
} else if (0 < totalApplied && totalApplied < planPurchaseNum) { |
|
|