|
@@ -11,6 +11,7 @@ import com.sd.business.entity.in.po.InOutStorageDetails;
|
|
|
import com.sd.business.entity.inventory.bo.InOutFun;
|
|
|
import com.sd.business.entity.inventory.dto.InventorySelectDto;
|
|
|
import com.sd.business.entity.inventory.dto.QuantityByWarehouseDto;
|
|
|
+import com.sd.business.entity.inventory.po.BalancePriceNum;
|
|
|
import com.sd.business.entity.inventory.po.Inventory;
|
|
|
import com.sd.business.entity.inventory.po.InventoryBackup;
|
|
|
import com.sd.business.entity.inventory.vo.InventoryVo;
|
|
@@ -28,6 +29,7 @@ import org.springframework.stereotype.Service;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.function.Function;
|
|
@@ -75,8 +77,6 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
Page<InventoryVo> page = this.baseMapper.getPage(dto.getPage(), wrapper, tableName);
|
|
|
return page;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
@Override
|
|
|
public List<QuantityByWarehouseVo> getQuantityByWarehouse(QuantityByWarehouseDto dto) {
|
|
|
return baseMapper.getQuantityByWarehouse(dto);
|
|
@@ -112,25 +112,26 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
inventoryList.add(inventory);
|
|
|
map.put(bomSpecId, inventory);
|
|
|
}
|
|
|
- List<InOutStorageDetails> detailsList = getInOutStorageDetailsMap(warehouseId, bomSpecIdList);
|
|
|
- if (CollUtil.isEmpty(detailsList)){
|
|
|
- for (InOutStorageDetails details : detailsList) {
|
|
|
- details.setInSumPrice(BigDecimal.ZERO);
|
|
|
- details.setQuantity(BigDecimal.ZERO);
|
|
|
- }
|
|
|
- }
|
|
|
- BigDecimal sumPrice = detailsList.stream().map(InOutStorageDetails::getInSumPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
- BigDecimal sumQuantity = detailsList.stream().map(InOutStorageDetails::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ Map<Long, List<InOutStorageDetails>> inOutStorageDetailsMap = getInOutStorageDetailsMap(warehouseId, bomSpecIdList);
|
|
|
+
|
|
|
+ //获取结存数量
|
|
|
+ BigDecimal sumQuantity = inOutStorageDetailsMap.getOrDefault(inOutFun.getBomSpecId(), Collections.emptyList())
|
|
|
+ .stream().map(InOutStorageDetails::getQuantity)
|
|
|
+ .filter(item -> item.compareTo(BigDecimal.ZERO) > 0)
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+
|
|
|
+ BigDecimal sumPrice = inOutStorageDetailsMap.getOrDefault(inOutFun.getBomSpecId(), Collections.emptyList()).stream()
|
|
|
+ .filter(item -> item.getQuantity().compareTo(BigDecimal.ZERO) > 0)
|
|
|
+ .map(item -> item.getQuantity().multiply(item.getInUnitPrice()))
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
|
|
|
// 计算最新的结存数量和结存金额
|
|
|
sumQuantity = sumQuantity.add(quantity);
|
|
|
-
|
|
|
BigDecimal balancep = quantity.multiply(unitPrice);
|
|
|
sumPrice = sumPrice.add(balancep);
|
|
|
BigDecimal balanceUnitPrice = sumPrice.divide(sumQuantity, 2, BigDecimal.ROUND_HALF_UP);
|
|
|
inventory.setBalanceUnitPrice(balanceUnitPrice);
|
|
|
}
|
|
|
-
|
|
|
saveOrUpdateBatch(inventoryList);
|
|
|
}
|
|
|
}
|
|
@@ -138,7 +139,7 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
@Override
|
|
|
public void out(Long departmentId, Long warehouseId, List<? extends InOutFun> list) {
|
|
|
|
|
|
- //存储此一条数据出库的数量,可以改变
|
|
|
+ //存储一条数据出库的数量,可以改变
|
|
|
Map<Long, BigDecimal> map = list.stream()
|
|
|
.collect(Collectors.toMap(InOutFun::getBomSpecId, InOutFun::getQuantity));
|
|
|
|
|
@@ -167,8 +168,7 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
throw new ServiceException("出库失败,品名为:" + bomSpec.getName() + "的bom库存不足");
|
|
|
}
|
|
|
|
|
|
- List<InOutStorageDetails> outStorageDetails = inOutStorageDetailsMap.get(bomSpecId);
|
|
|
-
|
|
|
+ List<InOutStorageDetails> outStorageDetails = inOutStorageDetailsMap.getOrDefault(bomSpecId, Collections.emptyList());
|
|
|
// 循环次数
|
|
|
int j = 0;
|
|
|
|
|
@@ -176,27 +176,26 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
BigDecimal quantity = map.get(bomSpecId);
|
|
|
|
|
|
do {
|
|
|
-
|
|
|
if (outStorageDetails.size() == j) {
|
|
|
- throw new ServiceException("数据异常,无法计算库存单价");
|
|
|
+ throw new ServiceException("数据异常,无法计算库存单价!!");
|
|
|
}
|
|
|
-
|
|
|
// 获取这条记录剩余的数量
|
|
|
InOutStorageDetails inOutStorageDetails = outStorageDetails.get(j);
|
|
|
|
|
|
// 单条可出库数量
|
|
|
BigDecimal tempQuantity = inOutStorageDetails.getQuantity();
|
|
|
|
|
|
- if (tempQuantity.compareTo(quantity) >= 0) {
|
|
|
+ if (tempQuantity.compareTo(quantity) > 0) {
|
|
|
inOutStorageDetails.setQuantity(tempQuantity.subtract(quantity));
|
|
|
quantity = BigDecimal.ZERO;
|
|
|
+ } else if (tempQuantity.compareTo(quantity) < 0) {
|
|
|
+ inOutStorageDetails.setQuantity(BigDecimal.ZERO);
|
|
|
+ quantity = tempQuantity.subtract(quantity).abs();
|
|
|
} else {
|
|
|
inOutStorageDetails.setQuantity(BigDecimal.ZERO);
|
|
|
- quantity = tempQuantity.subtract(quantity);
|
|
|
+ quantity = BigDecimal.ZERO;
|
|
|
}
|
|
|
-
|
|
|
updateList.add(inOutStorageDetails);
|
|
|
-
|
|
|
j++;
|
|
|
|
|
|
} while (quantity.compareTo(BigDecimal.ZERO) > 0);
|
|
@@ -213,11 +212,16 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
.map(item -> item.getQuantity().multiply(item.getInUnitPrice()))
|
|
|
.reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
|
|
|
+ BigDecimal balanceUnitPrice;
|
|
|
// 结存单价
|
|
|
- BigDecimal balanceUnitPrice = totalInventoryAmount.divide(totalInventory, 2, RoundingMode.HALF_UP);
|
|
|
-
|
|
|
+ if (totalInventory.compareTo(BigDecimal.ZERO) == 0) {
|
|
|
+ balanceUnitPrice = BigDecimal.ZERO;
|
|
|
+ } else {
|
|
|
+ balanceUnitPrice = totalInventoryAmount.divide(totalInventory, 2, RoundingMode.HALF_UP);
|
|
|
+ }
|
|
|
// 赋值结存单价
|
|
|
inventory.setBalanceUnitPrice(balanceUnitPrice);
|
|
|
+ inventory.setQuantity(totalInventory);
|
|
|
}
|
|
|
|
|
|
// 修改计算结存单价的出入库明细表
|
|
@@ -225,32 +229,8 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
|
|
|
// 修改库存数量以及结存单价
|
|
|
updateBatchById(inventoryList);
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
- //将出库金额添加到map
|
|
|
- private void addOutPriceMap(Map<Long, BigDecimal> outPriceMap, Map<Long, BigDecimal> map, InOutStorageDetails details) {
|
|
|
-
|
|
|
- BigDecimal mapQuantity = map.get(details.getBomSpecId());
|
|
|
- BigDecimal multiply;
|
|
|
-
|
|
|
- //如果出库数量大于库存记录数量
|
|
|
- if (mapQuantity.compareTo(details.getQuantity()) > 0) {
|
|
|
- multiply = details.getQuantity().multiply(details.getInUnitPrice());
|
|
|
- } else {
|
|
|
- multiply = mapQuantity.multiply(details.getInUnitPrice());
|
|
|
- }
|
|
|
- if (outPriceMap.containsKey(details.getBomSpecId())) {
|
|
|
- BigDecimal naxtPrice = outPriceMap.get(details.getBomSpecId());
|
|
|
- BigDecimal addPrice = naxtPrice.add(multiply);
|
|
|
- outPriceMap.put(details.getBomSpecId(), addPrice);
|
|
|
- } else {
|
|
|
- outPriceMap.put(details.getBomSpecId(), multiply);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
private List<Inventory> getInventoryList(Long departmentId, Long warehouseId, List<Long> bomSpecIdList) {
|
|
|
return list(q -> q
|
|
|
.eq(Inventory::getDepartmentId, departmentId)
|
|
@@ -258,14 +238,12 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
.in(Inventory::getBomSpecId, bomSpecIdList)
|
|
|
);
|
|
|
}
|
|
|
-
|
|
|
private Map<Long, List<InOutStorageDetails>> getInOutStorageDetailsMap(Long warehouseId, List<Long> bomSpecIdList) {
|
|
|
List<InOutStorageDetails> list = inOutStorageDetailsService.list(q -> q
|
|
|
.eq(InOutStorageDetails::getWarehouseId, warehouseId)
|
|
|
.in(InOutStorageDetails::getBomSpecId, bomSpecIdList)
|
|
|
.gt(InOutStorageDetails::getQuantity, BigDecimal.ZERO)
|
|
|
);
|
|
|
-
|
|
|
return list.stream().collect(Collectors.groupingBy(InOutStorageDetails::getBomSpecId));
|
|
|
}
|
|
|
|