yzc преди 1 година
родител
ревизия
567a81df55

+ 29 - 64
hx-item/src/main/java/com/fjhx/item/service/product/impl/ProductInfoServiceImpl.java

@@ -389,42 +389,22 @@ public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, Produ
                 //计算可用库存
                 if (requiredQuantity.compareTo(BigDecimal.ZERO) < 0) {
                     //需要采购的数量<0 可用库存 = 可用库存 - 合同量
-                    productStockInfoService.update(q -> q
-                            .eq(ProductStockInfo::getId, productStockInfoId)
-                            .setSql("available_quantity = available_quantity - " + quantity)
-                            .set(BasePo::getUpdateTime, new Date())
-                            .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
-                    );
+                    availableQuantity = availableQuantity.subtract(quantity);
                 } else {
                     //需要采购的数量>=0 可用库存 = 安全库存
-                    productStockInfoService.update(q -> q
-                            .eq(ProductStockInfo::getId, productStockInfoId)
-                            .setSql("available_quantity = " + stockThreshold)
-                            .set(BasePo::getUpdateTime, new Date())
-                            .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
-                    );
+                    availableQuantity = stockThreshold;
                 }
             }
             //销售合同作废          可用库存=可用库存-((待采购量-已采购量)-合同量)
             //申购的-采购合同作废    可用库存 = 当前可用库存 - 采购明细数量
             //手动出库             可用库存 = 当前可用库存 - 出库数量
             else if (ProductAvailableRecordType.SALE_CANCEL.equals(businessType) || ProductAvailableRecordType.PURCHASE_CANCEL.equals(businessType) || ProductAvailableRecordType.HAND_OUT.equals(businessType)) {
-                productStockInfoService.update(q -> q
-                        .eq(ProductStockInfo::getId, productStockInfoId)
-                        .setSql("available_quantity = available_quantity - " + quantity)
-                        .set(BasePo::getUpdateTime, new Date())
-                        .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
-                );
+                availableQuantity = availableQuantity.subtract(quantity);
             }
             //申购的-采购合同通过    可用库存 = 当前可用库存 + 采购明细数量
             //手动入库             可用库存 = 当前可用库存 + 入库数量
             else if (ProductAvailableRecordType.PURCHASE_PASS.equals(businessType) || ProductAvailableRecordType.HAND_IN.equals(businessType)) {
-                productStockInfoService.update(q -> q
-                        .eq(ProductStockInfo::getId, productStockInfoId)
-                        .setSql("available_quantity = available_quantity + " + quantity)
-                        .set(BasePo::getUpdateTime, new Date())
-                        .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
-                );
+                availableQuantity = availableQuantity.add(quantity);
             }
             //采购变更
             else if (ProductAvailableRecordType.PURCHASE_UPDATE.equals(businessType)) {
@@ -432,44 +412,25 @@ public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, Produ
 
                 if (ObjectUtils.isEmpty(quantity)) {
                     //新数据直接添加
-                    productStockInfoService.update(q -> q
-                            .eq(ProductStockInfo::getId, productStockInfoId)
-                            .setSql("available_quantity = available_quantity + " + newQuantity)
-                            .set(BasePo::getUpdateTime, new Date())
-                            .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
-                    );
+                    availableQuantity = availableQuantity.add(newQuantity);
                 } else if (ObjectUtils.isEmpty(newQuantity)) {
                     //原数据被删,已入库数量大于0
                     if (inOutBo.getInStockQuantity().compareTo(BigDecimal.ZERO) > 0) {
                         throw new ServiceException("该采购存在已入库数据禁止变更!");
                     }
                     //直接回滚
-                    productStockInfoService.update(q -> q
-                            .eq(ProductStockInfo::getId, productStockInfoId)
-                            .setSql("available_quantity = available_quantity - " + quantity)
-                            .set(BasePo::getUpdateTime, new Date())
-                            .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
-                    );
+                    availableQuantity = availableQuantity.subtract(quantity);
                 } else if (quantity.compareTo(newQuantity) < 0) {
                     //新数量比原数量多直接添加 差额
                     BigDecimal subtract = newQuantity.subtract(quantity);
-                    productStockInfoService.update(q -> q
-                            .eq(ProductStockInfo::getId, productStockInfoId)
-                            .setSql("available_quantity = available_quantity + " + subtract)
-                            .set(BasePo::getUpdateTime, new Date())
-                            .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
-                    );
+
+                    availableQuantity = availableQuantity.add(subtract);
                 } else if (quantity.compareTo(newQuantity) > 0) {
                     //新数量比原数量少检查已入库数量,如果比新数量大直接报错
                     if (newQuantity.compareTo(inOutBo.getInStockQuantity()) >= 0) {
                         BigDecimal subtract = quantity.subtract(newQuantity);
                         //新数量大等于已入库数量
-                        productStockInfoService.update(q -> q
-                                .eq(ProductStockInfo::getId, productStockInfoId)
-                                .setSql("available_quantity = available_quantity - " + subtract)
-                                .set(BasePo::getUpdateTime, new Date())
-                                .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
-                        );
+                        availableQuantity = availableQuantity.subtract(subtract);
                     } else {
                         //新数量<已入库数量
                         throw new ServiceException("新采购量小于已入库数据,禁止变更!");
@@ -483,18 +444,17 @@ public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, Produ
                 //后>前 可用=可用+(后-前)
                 if (newQuantity.compareTo(inOutBo.getQuantity()) > 0) {
                     BigDecimal subtract = newQuantity.subtract(inOutBo.getQuantity());
-                    productStockInfoService.update(q -> q
-                            .eq(ProductStockInfo::getId, productStockInfoId)
-                            .setSql("available_quantity = available_quantity + " + subtract)
-                            .set(BasePo::getUpdateTime, new Date())
-                            .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
-                    );
+                    availableQuantity = availableQuantity.add(subtract);
+
+                    //计数需要采购的数量(需采购量 = 安全库存 - (可用库存 - 合同量)若 需采购量<0,则按0算不采购)
+                    BigDecimal subtract1 = availableQuantity.subtract(subtract);
+                    BigDecimal requiredQuantity = stockThreshold.subtract(subtract1);
+                    inOutBo.setNewQuantity(requiredQuantity);
+
                 }
                 //后<前
                 else if (newQuantity.compareTo(inOutBo.getQuantity()) < 0) {
 
-                    BigDecimal count = BigDecimal.ZERO;
-
                     //后>已采购 可用=可用-(后-已采)
                     if (newQuantity.compareTo(inOutBo.getInStockQuantity()) > 0) {
                         BigDecimal subtract = newQuantity.subtract(inOutBo.getInStockQuantity());
@@ -503,7 +463,7 @@ public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, Produ
                         availableQuantity = availableQuantity.subtract(subtract);
                     }
                     //后<已采购 可用=可用-(前-已采)
-                    if (newQuantity.compareTo(inOutBo.getInStockQuantity()) < 0) {
+                    else if (newQuantity.compareTo(inOutBo.getInStockQuantity()) < 0) {
                         BigDecimal subtract = inOutBo.getQuantity().subtract(inOutBo.getInStockQuantity());
 
                         //计算可用库存
@@ -514,18 +474,23 @@ public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, Produ
                         availableQuantity = BigDecimal.ZERO;
                     }
 
-                    BigDecimal finalAvailableQuantity = availableQuantity;
-                    productStockInfoService.update(q -> q
-                            .eq(ProductStockInfo::getId, productStockInfoId)
-                            .set(ProductStockInfo::getAvailableQuantity, finalAvailableQuantity)
-                            .set(BasePo::getUpdateTime, new Date())
-                            .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
-                    );
+                    //需采购量直接为0
+                    inOutBo.setNewQuantity(BigDecimal.ZERO);
+
                 }
             } else {
                 throw new ServiceException("未知操作类型,请联系管理员!");
             }
 
+            //保存可用库存
+            BigDecimal finalAvailableQuantity = availableQuantity;
+            productStockInfoService.update(q -> q
+                    .eq(ProductStockInfo::getId, productStockInfoId)
+                    .set(ProductStockInfo::getAvailableQuantity, finalAvailableQuantity)
+                    .set(BasePo::getUpdateTime, new Date())
+                    .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
+            );
+
         }
 
 

+ 0 - 2
hx-purchase/src/main/java/com/fjhx/purchase/service/subscribe/impl/SubscribeDetailServiceImpl.java

@@ -114,8 +114,6 @@ public class SubscribeDetailServiceImpl extends ServiceImpl<SubscribeDetailMappe
             wrapper.eq("t2", SubscribeDetail::getCompanyId, dto.getCompanyId());
         }
 
-        //按类型倒序 先显示合同数据 再显示申购数据
-        wrapper.orderByDesc("t2", SubscribeDetail::getDataType);
         wrapper.orderByDesc("t2", SubscribeDetail::getCreateTime);
 
         //过滤作废的数据

+ 83 - 36
hx-sale/src/main/java/com/fjhx/sale/flow/ContractUpdateFlow.java

@@ -261,32 +261,32 @@ public class ContractUpdateFlow extends FlowDelegate {
             availableStockBoList.add(availableStockBo);
 
 
-            //操作待采购数据
-            SubscribeDetail subscribeDetail = subscribeDetailService.getOne(q -> q.eq(SubscribeDetail::getContractId, oldContractId)
-                    .eq(SubscribeDetail::getProductId, materialId)
-            );
-            if (ObjectUtils.isEmpty(newQuantity)) {
-                //原数据被删,检查已采购数量
-                if (ObjectUtils.isEmpty(purchaseQuantity)) {
-                    subscribeDetailService.removeById(subscribeDetail);
-                } else {
-                    subscribeDetailService.update(q -> q
-                            .eq(SubscribeDetail::getContractId, oldContractId)
-                            .eq(SubscribeDetail::getProductId, materialId)
-                            .set(SubscribeDetail::getCount, BigDecimal.ZERO)
-                    );
-                }
-            } else {
-                subscribeDetail.setCount(newQuantity);
-
-                //计算状态
-                int status = purchaseQuantity.compareTo(newQuantity) >= 0 ? 20 : 30;
-                if (purchaseQuantity.compareTo(BigDecimal.ZERO) == 0) {
-                    status = 15;
-                }
-                subscribeDetail.setStatus(status);
-                subscribeDetailService.updateById(subscribeDetail);
-            }
+//            //操作待采购数据
+//            SubscribeDetail subscribeDetail = subscribeDetailService.getOne(q -> q.eq(SubscribeDetail::getContractId, oldContractId)
+//                    .eq(SubscribeDetail::getProductId, materialId)
+//            );
+//            if (ObjectUtils.isEmpty(newQuantity)) {
+//                //原数据被删,检查已采购数量
+//                if (ObjectUtils.isEmpty(purchaseQuantity)) {
+//                    subscribeDetailService.removeById(subscribeDetail);
+//                } else {
+//                    subscribeDetailService.update(q -> q
+//                            .eq(SubscribeDetail::getContractId, oldContractId)
+//                            .eq(SubscribeDetail::getProductId, materialId)
+//                            .set(SubscribeDetail::getCount, BigDecimal.ZERO)
+//                    );
+//                }
+//            } else {
+//                subscribeDetail.setCount(newQuantity);
+//
+//                //计算状态
+//                int status = purchaseQuantity.compareTo(newQuantity) >= 0 ? 20 : 30;
+//                if (purchaseQuantity.compareTo(BigDecimal.ZERO) == 0) {
+//                    status = 15;
+//                }
+//                subscribeDetail.setStatus(status);
+//                subscribeDetailService.updateById(subscribeDetail);
+//            }
 
         }
 
@@ -310,22 +310,69 @@ public class ContractUpdateFlow extends FlowDelegate {
             availableStockBoList.add(availableStockBo);
 
 
-            //生成待采购
-            SubscribeDetail subscribeDetail = new SubscribeDetail();
-            subscribeDetail.setProductId(materialId);
-            subscribeDetail.setCount(newPurchaseProduct.getQuantity());
-            subscribeDetail.setStatus(15);//待采购
-            subscribeDetail.setContractId(oldContractId);
-            subscribeDetail.setDataType(1);
-            subscribeDetail.setCompanyId(oldContract.getCompanyId());
-
-            subscribeDetailService.save(subscribeDetail);
+//            //生成待采购
+//            SubscribeDetail subscribeDetail = new SubscribeDetail();
+//            subscribeDetail.setProductId(materialId);
+//            subscribeDetail.setCount(newPurchaseProduct.getQuantity());
+//            subscribeDetail.setStatus(15);//待采购
+//            subscribeDetail.setContractId(oldContractId);
+//            subscribeDetail.setDataType(1);
+//            subscribeDetail.setCompanyId(oldContract.getCompanyId());
+//            subscribeDetailService.save(subscribeDetail);
         }
 
 
         //修改可用库存
         productInfoService.editAvailableQuantity(availableStockBoList, businessId, ProductAvailableRecordType.SALE_UPDATE, oldContract.getCompanyId());
 
+
+        //修该待采购量为需采购量
+        for (AvailableStockBo availableStockBo : availableStockBoList) {
+
+            Long materialId = availableStockBo.getProductId();
+            SubscribeDetail subscribeDetail = subscribeDetailService.getOne(q -> q.eq(SubscribeDetail::getContractId, oldContractId)
+                    .eq(SubscribeDetail::getProductId, materialId)
+            );
+
+            BigDecimal requiredQuantity = availableStockBo.getNewQuantity();
+
+            //需采购=已采购 虚采购量=0
+            if (requiredQuantity.compareTo(availableStockBo.getInStockQuantity()) == 0 && requiredQuantity.compareTo(BigDecimal.ZERO) == 0) {
+                if (ObjectUtils.isNotEmpty(subscribeDetail)) {
+                    subscribeDetailService.removeById(subscribeDetail);
+                }
+            }
+
+            //需采购量>0
+            else if (requiredQuantity.compareTo(BigDecimal.ZERO) > 0) {
+                if (ObjectUtils.isEmpty(subscribeDetail)) {
+                    //生成待采购
+                    subscribeDetail = new SubscribeDetail();
+                    subscribeDetail.setProductId(materialId);
+                    subscribeDetail.setCount(BigDecimal.ZERO);
+                    subscribeDetail.setStatus(15);//待采购
+                    subscribeDetail.setContractId(oldContractId);
+                    subscribeDetail.setDataType(1);
+                    subscribeDetail.setCompanyId(oldContract.getCompanyId());
+                }
+
+                //修改待采购量
+                subscribeDetail.setCount(requiredQuantity);
+
+                //计算状态
+                BigDecimal purchaseQuantity = availableStockBo.getInStockQuantity();
+                int status = purchaseQuantity.compareTo(requiredQuantity) >= 0 ? 20 : 30;
+                if (purchaseQuantity.compareTo(BigDecimal.ZERO) == 0) {
+                    status = 15;
+                }
+                subscribeDetail.setStatus(status);
+
+                subscribeDetailService.saveOrUpdate(subscribeDetail);
+            }
+
+
+        }
+
         //---------------------------------------------------------------
     }