|
@@ -1,5 +1,7 @@
|
|
|
package com.sd.business.service.inventory.impl;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
@@ -19,6 +21,7 @@ import com.sd.business.entity.inventory.vo.QuantityByWarehouseVo;
|
|
|
import com.sd.business.mapper.inventory.InventoryMapper;
|
|
|
import com.sd.business.service.bom.BomSpecService;
|
|
|
import com.sd.business.service.in.InOutStorageDetailsService;
|
|
|
+import com.sd.business.service.in.impl.InOutStorageServiceImpl;
|
|
|
import com.sd.business.service.inventory.InventoryService;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -96,7 +99,7 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
for (InOutFun inOutFun : list) {
|
|
|
Long bomSpecId = inOutFun.getBomSpecId();
|
|
|
BigDecimal quantity = inOutFun.getQuantity();
|
|
|
- BigDecimal unitPrice = inOutFun.getUnitPrice();
|
|
|
+ BigDecimal unitPrice = InOutStorageServiceImpl.UNITPRICE;
|
|
|
Inventory inventory = map.get(bomSpecId);
|
|
|
|
|
|
if (inventory != null) {
|
|
@@ -113,9 +116,13 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
|
|
|
// 根据bom规格id和仓库id获取结存金额和结存数量
|
|
|
BalancePriceNum priceAndNum = baseMapper.getPriceAndNum(bomSpecId, warehouseId);
|
|
|
+ if (priceAndNum == null){
|
|
|
+ throw new ServiceException("没有该数据");
|
|
|
+ }
|
|
|
BigDecimal balanceNum = priceAndNum.getBalanceNum();
|
|
|
BigDecimal balancePrice = priceAndNum.getBalancePrice();
|
|
|
|
|
|
+
|
|
|
// 计算最新的结存数量和结存金额
|
|
|
balanceNum = balanceNum.add(quantity);
|
|
|
BigDecimal balancep = quantity.multiply(unitPrice);
|
|
@@ -151,83 +158,73 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
|
|
|
//根据仓库id和规格id获取出入详细表数据
|
|
|
List<InOutStorageDetails> detailsList = getDetailsList(warehouseId, bomSpecIdList);
|
|
|
- List<InOutStorageDetails> updateList = new ArrayList<>();
|
|
|
- List<InOutStorageDetails> outDetailsList = new ArrayList<>();
|
|
|
- Map<Long,BigDecimal> outPriceMap = new HashMap<>();
|
|
|
-
|
|
|
- for (int i = 0; i < detailsList.size(); i++) {
|
|
|
- InOutStorageDetails details = detailsList.get(i);
|
|
|
+ if (CollUtil.isEmpty(detailsList)){
|
|
|
+ throw new ServiceException("");
|
|
|
}
|
|
|
-
|
|
|
- for (InOutStorageDetails details : detailsList) {
|
|
|
- //如果数量足够减扣
|
|
|
- BigDecimal outNum = map.get(details.getBomSpecId());
|
|
|
- if (details.getQuantity().compareTo(outNum) >= 0){
|
|
|
- //对数量进行减扣
|
|
|
- details.setQuantity(details.getQuantity().subtract(outNum));
|
|
|
-
|
|
|
- //将这次出库的金额存储到map里面
|
|
|
- BigDecimal outPrice = details.getInUnitPrice().multiply(outNum);
|
|
|
- outPriceMap.put(details.getBomSpecId(),outPrice);
|
|
|
-
|
|
|
- //将减扣数量后的对象修改回去
|
|
|
- updateList.add(details);
|
|
|
- //创建一个新的对象
|
|
|
- InOutStorageDetails outStorageDetails = new InOutStorageDetails();
|
|
|
- //将数据进行拷贝
|
|
|
- BeanUtils.copyProperties(details,outStorageDetails);
|
|
|
- //修改需要修改的值
|
|
|
- outStorageDetails.setQuantity(outNum);
|
|
|
- outStorageDetails.setId(null);
|
|
|
- outStorageDetails.setType(0L);
|
|
|
- //添加记录
|
|
|
- outDetailsList.add(outStorageDetails);
|
|
|
- break;
|
|
|
+ //存储需要修改的入库记录
|
|
|
+ List<InOutStorageDetails> updateList = new ArrayList<>();
|
|
|
+ //存储这次出库的金额
|
|
|
+ Map<Long, BigDecimal> outPriceMap = new HashMap<>();
|
|
|
+
|
|
|
+ int j = 0;
|
|
|
+ BigDecimal quantity;
|
|
|
+ do {
|
|
|
+ InOutStorageDetails details = detailsList.get(j);
|
|
|
+ BigDecimal num = map.get(details.getBomSpecId());
|
|
|
+ quantity = details.getQuantity().subtract(num);
|
|
|
+ if (quantity.compareTo(BigDecimal.ZERO) < 0){
|
|
|
+ throw new ServiceException("出库失败,库存不足");
|
|
|
}
|
|
|
- // 数量不够减扣
|
|
|
- // 获取剩余的数量
|
|
|
- BigDecimal remainderNum = outNum.subtract(details.getQuantity());
|
|
|
- if (remainderNum.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
- details.setQuantity(BigDecimal.ZERO);
|
|
|
- updateList.add(details);
|
|
|
+ //对数量进行减扣
|
|
|
+ BigDecimal detailsNum = details.getQuantity().subtract(num);
|
|
|
+ details.setQuantity(detailsNum);
|
|
|
+
|
|
|
+ //数量减少了,那金额也得减少
|
|
|
+ details.setInSumPrice(detailsNum.multiply(details.getInUnitPrice()));
|
|
|
+
|
|
|
+ //将这次出库的金额存储到map里面
|
|
|
+ BigDecimal outPrice = details.getInUnitPrice().multiply(num);
|
|
|
+ outPriceMap.put(details.getBomSpecId(), outPrice);
|
|
|
+ //将减扣数量后的对象修改回去
|
|
|
+ updateList.add(details);
|
|
|
+ j++;
|
|
|
+
|
|
|
+ }while (quantity.compareTo(BigDecimal.ZERO) > 0);
|
|
|
+
|
|
|
+
|
|
|
+ inOutStorageDetailsService.updateBatchById(updateList);
|
|
|
+ /*inOutStorageDetailsService.saveBatch(outDetailsList);*/
|
|
|
+
|
|
|
+ for (Inventory inventory : inventoryList) {
|
|
|
+ // 获取之前的结存金额
|
|
|
+ BigDecimal balancePrice = inventory.getQuantity().multiply(inventory.getBalanceUnitPrice());
|
|
|
+ // 减去出库金额后的结存金额
|
|
|
+ BigDecimal newBalancePrice = balancePrice.subtract(outPriceMap.get(inventory.getBomSpecId()));
|
|
|
+ // 减去出库数量后的结存数量
|
|
|
+ inventory.setQuantity(inventory.getQuantity().subtract(map.get(inventory.getBomSpecId())));
|
|
|
+ // 获取最后的结存单价
|
|
|
+ BigDecimal balanceUnitPrice = newBalancePrice.divide(inventory.getQuantity(), 2, BigDecimal.ROUND_HALF_UP);
|
|
|
+ //将数据存入对象
|
|
|
+ inventory.setBalanceUnitPrice(balanceUnitPrice);
|
|
|
}
|
|
|
- // TODO 这时候需要顺延到下一条记录,暂时没想好怎么顺
|
|
|
- // 上面也许可以不用增强for,普通for试试
|
|
|
- }
|
|
|
- inOutStorageDetailsService.updateBatchById(updateList);
|
|
|
- inOutStorageDetailsService.saveBatch(outDetailsList);
|
|
|
-
|
|
|
- for (Inventory inventory : inventoryList) {
|
|
|
- // 获取之前的结存金额
|
|
|
- BigDecimal balancePrice = inventory.getQuantity().multiply(inventory.getBalanceUnitPrice());
|
|
|
- // 减去出库金额后的结存金额
|
|
|
- BigDecimal newBalancePrice = balancePrice.subtract(outPriceMap.get(inventory.getBomSpecId()));
|
|
|
- // 减去出库数量后的结存数量
|
|
|
- inventory.setQuantity(inventory.getQuantity().subtract(map.get(inventory.getBomSpecId())));
|
|
|
- // 获取最后的结存单价
|
|
|
- BigDecimal balanceUnitPrice = newBalancePrice.divide(inventory.getQuantity(), 2, BigDecimal.ROUND_HALF_UP);
|
|
|
- //将数据存入对象
|
|
|
- inventory.setBalanceUnitPrice(balanceUnitPrice);
|
|
|
+ updateBatchById(inventoryList);
|
|
|
}
|
|
|
- updateBatchById(inventoryList);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
|
|
|
- private List<Inventory> getInventoryList(Long departmentId, Long warehouseId, List<Long> bomSpecIdList) {
|
|
|
- return list(q -> q
|
|
|
- .eq(Inventory::getDepartmentId, departmentId)
|
|
|
- .eq(Inventory::getWarehouseId, warehouseId)
|
|
|
- .in(Inventory::getBomSpecId, bomSpecIdList)
|
|
|
- );
|
|
|
}
|
|
|
+ private List<Inventory> getInventoryList (Long departmentId, Long warehouseId, List < Long > bomSpecIdList){
|
|
|
+ return list(q -> q
|
|
|
+ .eq(Inventory::getDepartmentId, departmentId)
|
|
|
+ .eq(Inventory::getWarehouseId, warehouseId)
|
|
|
+ .in(Inventory::getBomSpecId, bomSpecIdList)
|
|
|
+ );
|
|
|
+ }
|
|
|
|
|
|
- private List<InOutStorageDetails> getDetailsList(Long warehouseId, List<Long> bomSpecIdList) {
|
|
|
- return inOutStorageDetailsService.list(q -> q
|
|
|
- .eq(InOutStorageDetails::getWarehouseId, warehouseId)
|
|
|
- .in(InOutStorageDetails::getBomSpecId, bomSpecIdList)
|
|
|
- .isNotNull(InOutStorageDetails::getQuantity)
|
|
|
- );
|
|
|
- }
|
|
|
+ private List<InOutStorageDetails> getDetailsList (Long warehouseId, List < Long > bomSpecIdList){
|
|
|
+ return inOutStorageDetailsService.list(q -> q
|
|
|
+ .eq(InOutStorageDetails::getWarehouseId, warehouseId)
|
|
|
+ .in(InOutStorageDetails::getBomSpecId, bomSpecIdList)
|
|
|
+ .isNotNull(InOutStorageDetails::getQuantity)
|
|
|
+ );
|
|
|
+ }
|
|
|
|
|
|
}
|