Просмотр исходного кода

修改采购退货入库:新建,编辑,作废
修改采购退货新建 退货的入库明细成本金额和单价由后台计算
销售出库编辑库存添加批次修改为大于等于本次修改数量,
添加库存,库存批采减去已退货数量,库存流水计算添加已退货数量
修改库存流水每次都是新建

koushanshan 1 год назад
Родитель
Сommit
88aaf5b8a2

+ 5 - 4
src/main/java/com/dk/mdm/service/ivt/inbound/InboundOtherService.java

@@ -305,8 +305,8 @@ public class InboundOtherService extends BaseService<Inbound> {
                         inboundItemVO.setInventoryType(Constant.InventoryType.INBOUND.getName());
                         inboundItemVO.setInventoryType(Constant.InventoryType.INBOUND.getName());
                         inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.OTHER_INBOUND.getValue());
                         inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.OTHER_INBOUND.getValue());
                         //编辑之前的数
                         //编辑之前的数
-                        inboundItemVO.setQtyBeforeUpdate(inboundItemResponse.getIntoQty());
-                        inboundItemVO.setAmtBeforeUpdate(inboundItemResponse.getIntoAmt());
+                        inboundItemVO.setQtyBeforeUpdate(inboundItemResponse.getIntoQty().subtract(inboundItemResponse.getReturnQty()));
+                        inboundItemVO.setAmtBeforeUpdate(inboundItemResponse.getIntoAmt().subtract(inboundItemResponse.getReturnAmt()));
                         //编辑之后的数
                         //编辑之后的数
                         inboundItemVO.setIntoQty(inboundItemVO.getIntoingQty());
                         inboundItemVO.setIntoQty(inboundItemVO.getIntoingQty());
                         inboundItemVO.setIntoAmt(inboundItemVO.getIntoingAmt());
                         inboundItemVO.setIntoAmt(inboundItemVO.getIntoingAmt());
@@ -624,9 +624,10 @@ public class InboundOtherService extends BaseService<Inbound> {
                 inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.OTHER_INBOUND.getValue());
                 inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.OTHER_INBOUND.getValue());
                 //编辑之前的数
                 //编辑之前的数
                 if (inboundItemResponse.getIntoQty().compareTo(BigDecimal.ZERO) > 0) {
                 if (inboundItemResponse.getIntoQty().compareTo(BigDecimal.ZERO) > 0) {
-                    inboundItemVO.setQtyBeforeUpdate(inboundItemResponse.getIntoQty());
-                    inboundItemVO.setAmtBeforeUpdate(inboundItemResponse.getIntoAmt());
+                    inboundItemVO.setQtyBeforeUpdate(inboundItemResponse.getIntoQty().subtract(inboundItemResponse.getReturnQty()));
+                    inboundItemVO.setAmtBeforeUpdate(inboundItemResponse.getIntoAmt().subtract(inboundItemResponse.getReturnAmt()));
                 }
                 }
+
                 inboundItemVO.setAddOrEditFlag(true);
                 inboundItemVO.setAddOrEditFlag(true);
                 //endregion
                 //endregion
             }
             }

+ 2 - 0
src/main/java/com/dk/mdm/service/ivt/inbound/InboundPurchaseReturnService.java

@@ -188,6 +188,7 @@ public class InboundPurchaseReturnService extends BaseService<Inbound> {
                     ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getMessage());
                     ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getMessage());
         }
         }
         for (InboundItemVO inboundItemVO : inboundVO.getItemList()) {
         for (InboundItemVO inboundItemVO : inboundVO.getItemList()) {
+
             //region 将库存需要的参数赋值
             //region 将库存需要的参数赋值
             inboundItemVO.setInventoryType(Constant.InventoryType.INBOUND.getName());
             inboundItemVO.setInventoryType(Constant.InventoryType.INBOUND.getName());
             inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_RETURN.getValue());
             inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_RETURN.getValue());
@@ -351,6 +352,7 @@ public class InboundPurchaseReturnService extends BaseService<Inbound> {
                 if (inboundVO.getDeleteItemList() != null && inboundVO.getDeleteItemList().size() > 0) {
                 if (inboundVO.getDeleteItemList() != null && inboundVO.getDeleteItemList().size() > 0) {
                     for (InboundItemVO inboundItemVO : inboundVO.getDeleteItemList()) {
                     for (InboundItemVO inboundItemVO : inboundVO.getDeleteItemList()) {
                         if (inboundItemVO.getItemId() != null) {
                         if (inboundItemVO.getItemId() != null) {
+
                             //region 将库存需要的参数赋值
                             //region 将库存需要的参数赋值
                             inboundItemVO.setInventoryType(Constant.InventoryType.INBOUND.getName());
                             inboundItemVO.setInventoryType(Constant.InventoryType.INBOUND.getName());
                             inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_RETURN.getValue());
                             inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_RETURN.getValue());

+ 5 - 5
src/main/java/com/dk/mdm/service/ivt/inbound/InboundPurchaseService.java

@@ -472,8 +472,8 @@ public class InboundPurchaseService extends BaseService<Inbound> {
                         inboundItemVO.setInventoryType(Constant.InventoryType.INBOUND.getName());
                         inboundItemVO.setInventoryType(Constant.InventoryType.INBOUND.getName());
                         inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_ORDER.getValue());
                         inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_ORDER.getValue());
                         //编辑之前的数
                         //编辑之前的数
-                        inboundItemVO.setQtyBeforeUpdate(inboundItemResponse.getIntoQty());
-                        inboundItemVO.setAmtBeforeUpdate(inboundItemResponse.getIntoAmt());
+                        inboundItemVO.setQtyBeforeUpdate(inboundItemResponse.getIntoQty().subtract(inboundItemResponse.getReturnQty()));
+                        inboundItemVO.setAmtBeforeUpdate(inboundItemResponse.getIntoAmt().subtract(inboundItemResponse.getReturnAmt()));
                         //编辑之后的数
                         //编辑之后的数
                         inboundItemVO.setIntoQty(inboundItemVO.getIntoingQty());
                         inboundItemVO.setIntoQty(inboundItemVO.getIntoingQty());
                         inboundItemVO.setIntoAmt(inboundItemVO.getIntoingAmt());
                         inboundItemVO.setIntoAmt(inboundItemVO.getIntoingAmt());
@@ -773,7 +773,7 @@ public class InboundPurchaseService extends BaseService<Inbound> {
         for (String fromId : purchaseIntoItemVOListMap.keySet()) {
         for (String fromId : purchaseIntoItemVOListMap.keySet()) {
             //有需要删除的修改数据
             //有需要删除的修改数据
             if(delPurchaseIntoItemVOListMap!=null  && delPurchaseIntoItemVOListMap.keySet().size()>0){
             if(delPurchaseIntoItemVOListMap!=null  && delPurchaseIntoItemVOListMap.keySet().size()>0){
-                //库明细删除汇总循环
+                //库明细删除汇总循环
                 for (String delFromId : delPurchaseIntoItemVOListMap.keySet()) {
                 for (String delFromId : delPurchaseIntoItemVOListMap.keySet()) {
                     //新建入库明细来源Id = 删除入库明细来源Id
                     //新建入库明细来源Id = 删除入库明细来源Id
                     if (fromId.equals(delFromId)) {
                     if (fromId.equals(delFromId)) {
@@ -1201,8 +1201,8 @@ public class InboundPurchaseService extends BaseService<Inbound> {
                 inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_ORDER.getValue());
                 inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.PURCHASE_ORDER.getValue());
                 //编辑之前的数
                 //编辑之前的数
                 if (inboundItemResponse.getIntoQty().compareTo(BigDecimal.ZERO) > 0) {
                 if (inboundItemResponse.getIntoQty().compareTo(BigDecimal.ZERO) > 0) {
-                    inboundItemVO.setQtyBeforeUpdate(inboundItemResponse.getIntoQty());
-                    inboundItemVO.setAmtBeforeUpdate(inboundItemResponse.getIntoAmt());
+                    inboundItemVO.setQtyBeforeUpdate(inboundItemResponse.getIntoQty().subtract(inboundItemResponse.getReturnQty()));
+                    inboundItemVO.setAmtBeforeUpdate(inboundItemResponse.getIntoAmt().subtract(inboundItemResponse.getReturnAmt()));
                 }
                 }
                 inboundItemVO.setAddOrEditFlag(true);
                 inboundItemVO.setAddOrEditFlag(true);
                 //endregion
                 //endregion

+ 82 - 51
src/main/java/com/dk/mdm/service/ivt/inventory/InventoryService.java

@@ -144,6 +144,16 @@ public class InventoryService extends BaseService<Inventory> {
             inOutRecordVO.setAccDate(LocalDate.now());
             inOutRecordVO.setAccDate(LocalDate.now());
             //可以输入负数标识  入库没有限制 所以这里给true 为了下面的校验库存
             //可以输入负数标识  入库没有限制 所以这里给true 为了下面的校验库存
             inOutRecordVO.setCanNegativeFlag(true);
             inOutRecordVO.setCanNegativeFlag(true);
+            //重新赋值入库数量和入库金额要考虑到退货的情况
+            if (Constant.InventoryType.INBOUND.getName().equals(inOutRecordVO.getInventoryType()) &&
+                    !Constant.InventoryDocCode.PURCHASE_RETURN.getValue().equals(inOutRecordVO.getInventoryDocCode())
+            ) {
+                InboundItemResponse inboundItemResponse = inboundItemMapper.selectById(inOutRecordVO.getItemId());
+                if (inboundItemResponse != null && inboundItemResponse.getReturnQty() != null) {
+                    inOutRecordVO.setIntoQty(inOutRecordVO.getIntoQty().subtract(inboundItemResponse.getReturnQty()));
+                    inOutRecordVO.setIntoAmt(inOutRecordVO.getIntoAmt().subtract(inboundItemResponse.getReturnAmt()));
+                }
+            }
         }
         }
         //region 校验是否传库存需要的数据
         //region 校验是否传库存需要的数据
         this.checkInventory(inOutRecordVOList);
         this.checkInventory(inOutRecordVOList);
@@ -410,7 +420,7 @@ public class InventoryService extends BaseService<Inventory> {
                 if (delOutList.size() > 0) {
                 if (delOutList.size() > 0) {
                     delOutList = delOutDetail.toJavaList(InOutRecordVO.class);
                     delOutList = delOutDetail.toJavaList(InOutRecordVO.class);
                     //赋值并校验库存流水数据
                     //赋值并校验库存流水数据
-                    delOutList = this.setInOutRecordMessageByInto(delOutList);
+                    delOutList = this.setInOutRecordMessageByOut(delOutList);
                     //库存相关反写
                     //库存相关反写
                     this.warehouseAdjustmentDelOut(delOutList);
                     this.warehouseAdjustmentDelOut(delOutList);
                 } else {
                 } else {
@@ -912,6 +922,7 @@ public class InventoryService extends BaseService<Inventory> {
             //实体转换
             //实体转换
             InOutRecord inOutRecordNeg = inOutRecordConvert.convertToPo(inOutRecordVO);
             InOutRecord inOutRecordNeg = inOutRecordConvert.convertToPo(inOutRecordVO);
             //新建
             //新建
+            inOutRecordNeg.setRecordId(null);
             inOutRecordMapper.insert(inOutRecordNeg);
             inOutRecordMapper.insert(inOutRecordNeg);
 
 
             //新建正数流水
             //新建正数流水
@@ -925,6 +936,7 @@ public class InventoryService extends BaseService<Inventory> {
             //业务单明细Id
             //业务单明细Id
             inOutRecord.setBiznisItemId(inOutRecordVO.getIntoItemId());
             inOutRecord.setBiznisItemId(inOutRecordVO.getIntoItemId());
             //新建
             //新建
+            inOutRecord.setRecordId(null);
             inOutRecordMapper.insert(inOutRecord);
             inOutRecordMapper.insert(inOutRecord);
 
 
         }
         }
@@ -935,12 +947,20 @@ public class InventoryService extends BaseService<Inventory> {
                     (!Constant.InventoryDocCode.PURCHASE_RETURN.getValue().equals(inOutRecordVO.getInventoryDocCode()) &&
                     (!Constant.InventoryDocCode.PURCHASE_RETURN.getValue().equals(inOutRecordVO.getInventoryDocCode()) &&
                             Constant.InventoryType.INBOUND.getName().equals(inOutRecordVO.getInventoryType()))
                             Constant.InventoryType.INBOUND.getName().equals(inOutRecordVO.getInventoryType()))
             ) {
             ) {
+                BigDecimal sumReturnQtyItem = BigDecimal.ZERO;
+                BigDecimal sumReturnAmtItem = BigDecimal.ZERO;
+                if (!Constant.InventoryDocCode.PURCHASE_RETURN.getValue().equals(inOutRecordVO.getInventoryDocCode())  &&
+                        !Constant.InventoryDocCode.SALE_RETURN.getValue().equals(inOutRecordVO.getInventoryDocCode()) ) {
+                    InboundItemResponse inboundItemResponse = inboundItemMapper.selectById(inOutRecordVO.getBiznisItemId());
+                    sumReturnQtyItem = inboundItemResponse.getReturnQty();
+                    sumReturnAmtItem = inboundItemResponse.getReturnAmt();
+                }
                 //入库价
                 //入库价
                 inOutRecordVO.setPriceInto(inOutRecordVO.getPriceInto().abs());
                 inOutRecordVO.setPriceInto(inOutRecordVO.getPriceInto().abs());
                 //入库数量
                 //入库数量
-                inOutRecordVO.setIntoQty(inOutRecordVO.getIntoQty().abs());
+                inOutRecordVO.setIntoQty(inOutRecordVO.getIntoQty().abs().add(sumReturnQtyItem));
                 //入库金额
                 //入库金额
-                inOutRecordVO.setIntoAmt(inOutRecordVO.getIntoAmt().abs());
+                inOutRecordVO.setIntoAmt(inOutRecordVO.getIntoAmt().abs().add(sumReturnAmtItem));
             }
             }
             //endregion
             //endregion
 
 
@@ -950,7 +970,7 @@ public class InventoryService extends BaseService<Inventory> {
                             Constant.InventoryType.OUTBOUND.getName().equals(inOutRecordVO.getInventoryType()))
                             Constant.InventoryType.OUTBOUND.getName().equals(inOutRecordVO.getInventoryType()))
             ) {
             ) {
                 //入库价
                 //入库价
-                inOutRecordVO.setPriceInto(inOutRecordVO.getPriceInto().abs().negate());
+                inOutRecordVO.setPriceInto(inOutRecordVO.getPriceInto().abs());
                 //入库数量
                 //入库数量
                 inOutRecordVO.setIntoQty(inOutRecordVO.getIntoQty().abs().negate());
                 inOutRecordVO.setIntoQty(inOutRecordVO.getIntoQty().abs().negate());
                 //入库金额
                 //入库金额
@@ -959,6 +979,7 @@ public class InventoryService extends BaseService<Inventory> {
             //endregion
             //endregion
             //实体转换
             //实体转换
             InOutRecord inOutRecord = inOutRecordConvert.convertToPo(inOutRecordVO);
             InOutRecord inOutRecord = inOutRecordConvert.convertToPo(inOutRecordVO);
+            inOutRecord.setRecordId(null);
             //新建
             //新建
             inOutRecordMapper.insert(inOutRecord);
             inOutRecordMapper.insert(inOutRecord);
         }
         }
@@ -1078,16 +1099,16 @@ public class InventoryService extends BaseService<Inventory> {
             //采购退货入库  把传入的负数转成正数
             //采购退货入库  把传入的负数转成正数
             if (Constant.InventoryDocCode.PURCHASE_RETURN.getValue().equals(inOutRecordVO.getInventoryDocCode())) {
             if (Constant.InventoryDocCode.PURCHASE_RETURN.getValue().equals(inOutRecordVO.getInventoryDocCode())) {
                 //出库数量
                 //出库数量
-                outboundItemCost.setOutQty(inOutRecordVO.getIntoQty().abs());
-                //出库单价
-                outboundItemCost.setCostPrice(inOutRecordVO.getPriceInto().abs());
+                outboundItemCost.setOutQty(qty);
                 //出库成本
                 //出库成本
-                outboundItemCost.setCostAmt(inOutRecordVO.getIntoAmt().abs());
+                outboundItemCost.setCostAmt(amount != null && amount.compareTo(BigDecimal.ZERO) > 0 ? amount : inOutRecordVO.getIntoAmt());
+                //出库单价
+                outboundItemCost.setCostPrice(amount != null && amount.compareTo(BigDecimal.ZERO) > 0 ? amount.divide(qty, 2, BigDecimal.ROUND_HALF_UP) : inOutRecordVO.getPriceInto());
             } else {
             } else {
                 //出库数量
                 //出库数量
                 outboundItemCost.setOutQty(qty);
                 outboundItemCost.setOutQty(qty);
                 //出库成本
                 //出库成本
-                outboundItemCost.setCostAmt(amount != null && amount.compareTo(BigDecimal.ZERO) > 0 ? amount : inOutRecordVO.getPriceInto());
+                outboundItemCost.setCostAmt(amount != null && amount.compareTo(BigDecimal.ZERO) > 0 ? amount : inOutRecordVO.getIntoAmt());
                 //出库单价
                 //出库单价
                 outboundItemCost.setCostPrice(amount != null && amount.compareTo(BigDecimal.ZERO) > 0 ? amount.divide(qty, 2, BigDecimal.ROUND_HALF_UP) : inOutRecordVO.getPriceInto());
                 outboundItemCost.setCostPrice(amount != null && amount.compareTo(BigDecimal.ZERO) > 0 ? amount.divide(qty, 2, BigDecimal.ROUND_HALF_UP) : inOutRecordVO.getPriceInto());
             }
             }
@@ -1128,11 +1149,10 @@ public class InventoryService extends BaseService<Inventory> {
                     //清零标识
                     //清零标识
                     inventoryBatchVO.setZeroFlag(true);
                     inventoryBatchVO.setZeroFlag(true);
                     inventoryBatchMapper.updateAmountAndQuantity(inventoryBatchVO);
                     inventoryBatchMapper.updateAmountAndQuantity(inventoryBatchVO);
-                    //单价
-                    inOutRecordVO.setPriceInto(inventoryBatchResponse.getCostPrice());
-                    //金额
-                    inOutRecordVO.setIntoAmt(inOutRecordVO.getIntoQty().multiply(inventoryBatchResponse.getCostPrice()));
-
+                    //存货单价
+                    inOutRecordVO.setCostPrice(inventoryBatchResponse.getCostPrice());
+                    //存货总额
+                    inOutRecordVO.setCostAmt(inventoryBatchResponse.getCostAmt());
                     //region  出库成本 todo  2024年5月20日15:46:01  库存批次有几条出库成本就有几条
                     //region  出库成本 todo  2024年5月20日15:46:01  库存批次有几条出库成本就有几条
                     //除了销售退货出库  其他类型出库要新建出库成本  采购退货 出库成本 正数
                     //除了销售退货出库  其他类型出库要新建出库成本  采购退货 出库成本 正数
                     if ((Constant.InventoryType.OUTBOUND.getName().equals(inOutRecordVO.getInventoryType()) &&
                     if ((Constant.InventoryType.OUTBOUND.getName().equals(inOutRecordVO.getInventoryType()) &&
@@ -1159,13 +1179,10 @@ public class InventoryService extends BaseService<Inventory> {
                         //清零标识
                         //清零标识
                         inventoryBatchVO.setZeroFlag(false);
                         inventoryBatchVO.setZeroFlag(false);
                         inventoryBatchMapper.updateAmountAndQuantity(inventoryBatchVO);
                         inventoryBatchMapper.updateAmountAndQuantity(inventoryBatchVO);
-                        //单价
-                        inOutRecordVO.setPriceInto(inventoryBatchResponse.getCostPrice());
-                        //金额
-                        inOutRecordVO.setIntoAmt(inOutRecordVO.getIntoQty().multiply(inventoryBatchResponse.getCostPrice()));
                         //金额每次更新完要重新运算
                         //金额每次更新完要重新运算
                         notEnoughAmount = notEnoughAmount.add(qty.multiply(inventoryBatchResponse.getCostPrice()));
                         notEnoughAmount = notEnoughAmount.add(qty.multiply(inventoryBatchResponse.getCostPrice()));
-
+                        //存货总额
+                        inOutRecordVO.setCostAmt(notEnoughAmount);
                         //region  出库成本 todo  2024年5月20日15:46:01  库存批次有几条出库成本就有几条
                         //region  出库成本 todo  2024年5月20日15:46:01  库存批次有几条出库成本就有几条
                         //除了销售退货出库  其他类型出库要新建出库成本  采购退货 出库成本 正数
                         //除了销售退货出库  其他类型出库要新建出库成本  采购退货 出库成本 正数
                         if ((Constant.InventoryType.OUTBOUND.getName().equals(inOutRecordVO.getInventoryType()) &&
                         if ((Constant.InventoryType.OUTBOUND.getName().equals(inOutRecordVO.getInventoryType()) &&
@@ -1279,8 +1296,8 @@ public class InventoryService extends BaseService<Inventory> {
                     //当前编辑的数据在库存批次中不存在
                     //当前编辑的数据在库存批次中不存在
                     throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_BATCH_DOES_NOT_EXIST.getMessage());
                     throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_BATCH_DOES_NOT_EXIST.getMessage());
                 } else {
                 } else {
-                    //数据库中库存批次数量和传入数量一致 可以更改
-                    if (inventoryBatchResponse.getInvQty().compareTo(inOutRecordVO.getQtyBeforeUpdate() != null ? inOutRecordVO.getQtyBeforeUpdate().abs() : inOutRecordVO.getIntoQty().abs()) == 0) {
+                    //数据库中库存批次数量和传入数量一致 可以更改 todo 2024年7月18日17:17:32 大于等于就行 和z沟通过
+                    if (inventoryBatchResponse.getInvQty().compareTo(inOutRecordVO.getQtyBeforeUpdate() != null ? inOutRecordVO.getQtyBeforeUpdate().abs() : inOutRecordVO.getIntoQty().abs()) >= 0) {
                         InventoryBatch inventoryBatch = new InventoryBatch();
                         InventoryBatch inventoryBatch = new InventoryBatch();
                         //来源单ID
                         //来源单ID
                         inventoryBatch.setFromId(inOutRecordVO.getBiznisId());
                         inventoryBatch.setFromId(inOutRecordVO.getBiznisId());
@@ -1472,9 +1489,9 @@ public class InventoryService extends BaseService<Inventory> {
             rollbackFor = {Exception.class}
             rollbackFor = {Exception.class}
     )
     )
     public List<InOutRecordVO> insertOrUpdateInbound(List<InOutRecordVO> inOutRecordVOList) {
     public List<InOutRecordVO> insertOrUpdateInbound(List<InOutRecordVO> inOutRecordVOList) {
-        BigDecimal sumOutQty = inOutRecordVOList.stream().map(InOutRecordVO::getIntoQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
         //新建
         //新建
         if (inOutRecordVOList.get(0).getAddOrEditFlag()) {
         if (inOutRecordVOList.get(0).getAddOrEditFlag()) {
+            BigDecimal sumOutQty = inOutRecordVOList.stream().map(InOutRecordVO::getIntoQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
             //region  总单
             //region  总单
             Inbound inbound = new Inbound();
             Inbound inbound = new Inbound();
             //获取 id/单号
             //获取 id/单号
@@ -1531,15 +1548,8 @@ public class InventoryService extends BaseService<Inventory> {
         }
         }
         //编辑
         //编辑
         else {
         else {
-            //region  编辑入库单数量
+            //region查询总单
             InboundResponse inboundResponse = inboundMapper.selectMessageByOtherQuery(new InboundQuery().setFromId(inOutRecordVOList.get(0).getBiznisId()));
             InboundResponse inboundResponse = inboundMapper.selectMessageByOtherQuery(new InboundQuery().setFromId(inOutRecordVOList.get(0).getBiznisId()));
-            Inbound inbound = new Inbound();
-            inbound.setIntoId(inboundResponse.getIntoId());
-            inbound.setIntoQty(sumOutQty.abs());
-            inboundMapper.update(inbound,
-                    new UpdateWrapper<Inbound>().lambda()
-                            .eq(Inbound::getIntoId, UUID.fromString(inbound.getIntoId()))
-            );
             //endregion
             //endregion
 
 
             //region 明细
             //region 明细
@@ -1562,6 +1572,18 @@ public class InventoryService extends BaseService<Inventory> {
                 inOutRecordVO.setIntoItemId(inboundItemResponse.getItemId());
                 inOutRecordVO.setIntoItemId(inboundItemResponse.getItemId());
             }
             }
             //endregion
             //endregion
+
+            //region  编辑入库单数量  编辑的时候把所有明细查出来  计算和在反写
+            List<InboundItemResponse> inboundItemResponses = inboundItemMapper.selectMessageByOtherCondition(new InboundItemQuery().setFromId(inOutRecordVOList.get(0).getBiznisId()));
+            BigDecimal sumOutQty = inboundItemResponses.stream().map(InboundItemResponse::getIntoQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
+            Inbound inbound = new Inbound();
+            inbound.setIntoId(inboundResponse.getIntoId());
+            inbound.setIntoQty(sumOutQty);
+            inboundMapper.update(inbound,
+                    new UpdateWrapper<Inbound>().lambda()
+                            .eq(Inbound::getIntoId, UUID.fromString(inbound.getIntoId()))
+            );
+            //endregion
         }
         }
 
 
         return inOutRecordVOList;
         return inOutRecordVOList;
@@ -1855,20 +1877,6 @@ public class InventoryService extends BaseService<Inventory> {
             rollbackFor = {Exception.class}
             rollbackFor = {Exception.class}
     )
     )
     public void updateIntobound(List<InOutRecordVO> inOutRecordVOList) {
     public void updateIntobound(List<InOutRecordVO> inOutRecordVOList) {
-        BigDecimal sumOutQty = inOutRecordVOList.stream().map(InOutRecordVO::getIntoQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
-        BigDecimal sumOutAmt = inOutRecordVOList.stream().map(InOutRecordVO::getIntoAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
-        //region  编辑入库单数量
-        InboundResponse inboundResponse = inboundMapper.selectMessageByOtherQuery(new InboundQuery().setIntoId(inOutRecordVOList.get(0).getBiznisId()));
-        Inbound inbound = new Inbound();
-        inbound.setIntoId(inboundResponse.getIntoId());
-        inbound.setIntoQty(sumOutQty);
-        inbound.setIntoAmt(sumOutAmt);
-        inboundMapper.update(inbound,
-                new UpdateWrapper<Inbound>().lambda()
-                        .eq(Inbound::getIntoId, UUID.fromString(inbound.getIntoId()))
-        );
-        //endregion
-
         //region 明细
         //region 明细
         for (InOutRecordVO inOutRecordVO : inOutRecordVOList) {
         for (InOutRecordVO inOutRecordVO : inOutRecordVOList) {
             //根据id查询入库明细,用于计算入库明细的库存数量
             //根据id查询入库明细,用于计算入库明细的库存数量
@@ -1881,21 +1889,44 @@ public class InventoryService extends BaseService<Inventory> {
             //已入库数量
             //已入库数量
             inboundItem.setIntoQty(inOutRecordVO.getIntoQty());
             inboundItem.setIntoQty(inOutRecordVO.getIntoQty());
             //已入库金额
             //已入库金额
-            inboundItem.setIntoAmt(inOutRecordVO.getIntoAmt());
-            //成本单价
-            inboundItem.setCostPrice(inOutRecordVO.getCostPrice());
+            inboundItem.setIntoAmt(inOutRecordVO.getIntoQty().multiply(inboundItemResponse.getPriceInto()));
             //成本金额
             //成本金额
-            inboundItem.setCostAmt(inOutRecordVO.getIntoAmt());
+            //采购退货特殊处理存负数
+            if(Constant.InventoryDocCode.PURCHASE_RETURN.getValue().equals(inOutRecordVOList.get(0).getInventoryDocCode())){
+                inboundItem.setCostAmt(inOutRecordVO.getCostAmt().negate());
+            }else{
+                inboundItem.setCostAmt(inOutRecordVO.getCostAmt());
+            }
+            //成本单价
+            inboundItem.setCostPrice(inOutRecordVO.getCostAmt().divide(inOutRecordVO.getIntoQty(),2, BigDecimal.ROUND_HALF_UP).abs());
             //库存id
             //库存id
             inboundItem.setInvId(inOutRecordVO.getInvId());
             inboundItem.setInvId(inOutRecordVO.getInvId());
             //库存数量 =  已入库数量-出库数量 -已入库退货数量
             //库存数量 =  已入库数量-出库数量 -已入库退货数量
-            inboundItem.setInvQty(inboundItem.getIntoQty().subtract(inboundItemResponse.getOutQty()).subtract(inboundItemResponse.getReturnQty()));
+            if (!Constant.InventoryDocCode.PURCHASE_RETURN.getValue().equals(inOutRecordVOList.get(0).getInventoryDocCode())) {
+                inboundItem.setInvQty(inboundItem.getIntoQty().subtract(inboundItemResponse.getOutQty()).subtract(inboundItemResponse.getReturnQty()));
+            }
             inboundItemMapper.update(inboundItem,
             inboundItemMapper.update(inboundItem,
                     new UpdateWrapper<InboundItem>().lambda()
                     new UpdateWrapper<InboundItem>().lambda()
                             .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
                             .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
             );
             );
         }
         }
         //endregion
         //endregion
+
+        //region  编辑入库单数量
+        InboundResponse inboundResponse = inboundMapper.selectMessageByOtherQuery(new InboundQuery().setIntoId(inOutRecordVOList.get(0).getBiznisId()));
+        List<InboundItemResponse> inboundItemResponses = inboundItemMapper.selectMessageByOtherCondition(new InboundItemQuery().setIntoId(inOutRecordVOList.get(0).getBiznisId()));
+        BigDecimal sumOutQty = inboundItemResponses.stream().map(InboundItemResponse::getIntoQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
+        BigDecimal sumOutAmt = inboundItemResponses.stream().map(InboundItemResponse::getIntoAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
+        Inbound inbound = new Inbound();
+        inbound.setIntoId(inboundResponse.getIntoId());
+        inbound.setIntoQty(sumOutQty);
+        inbound.setIntoAmt(sumOutAmt);
+        inboundMapper.update(inbound,
+                new UpdateWrapper<Inbound>().lambda()
+                        .eq(Inbound::getIntoId, UUID.fromString(inbound.getIntoId()))
+        );
+        //endregion
+
     }
     }
 
 
     /**
     /**
@@ -1948,8 +1979,8 @@ public class InventoryService extends BaseService<Inventory> {
                 //当前编辑的数据在库存批次中不存在
                 //当前编辑的数据在库存批次中不存在
                 throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_BATCH_DOES_NOT_EXIST.getMessage());
                 throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_BATCH_DOES_NOT_EXIST.getMessage());
             } else {
             } else {
-                //数据库中库存批次数量和传入数量一致 可以更改
-                if (inventoryResponse.getInvQty().compareTo(inOutRecordVO.getIntoQty().abs()) == 0) {
+                //数据库中库存批次数量和传入数量一致 可以更改  todo 2024年7月18日17:17:32 大于等于就行 和z沟通过
+                if (inventoryResponse.getInvQty().compareTo(inOutRecordVO.getIntoQty().abs()) >= 0) {
                     InventoryBatch inventoryBatch = new InventoryBatch();
                     InventoryBatch inventoryBatch = new InventoryBatch();
                     //来源单ID
                     //来源单ID
                     inventoryBatch.setFromId(inOutRecordVO.getBiznisId());
                     inventoryBatch.setFromId(inOutRecordVO.getBiznisId());