|
@@ -1,14 +1,12 @@
|
|
|
package com.sd.business.service.inventory.impl;
|
|
|
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
-import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
|
import com.sd.business.entity.bom.po.Bom;
|
|
|
import com.sd.business.entity.bom.po.BomSpec;
|
|
|
import com.sd.business.entity.department.po.Department;
|
|
|
-import com.sd.business.entity.in.po.InOutStorageBalance;
|
|
|
import com.sd.business.entity.in.po.InOutStorageBom;
|
|
|
import com.sd.business.entity.inventory.bo.InOutFun;
|
|
|
import com.sd.business.entity.inventory.dto.InventorySelectDto;
|
|
@@ -19,13 +17,10 @@ import com.sd.business.entity.inventory.po.Inventory;
|
|
|
import com.sd.business.entity.inventory.vo.InventoryVo;
|
|
|
import com.sd.business.entity.inventory.vo.QuantityByDepartmentVo;
|
|
|
import com.sd.business.entity.inventory.vo.QuantityByWarehouseVo;
|
|
|
-import com.sd.business.entity.purchase.po.PurchaseBom;
|
|
|
import com.sd.business.entity.warehouse.po.Warehouse;
|
|
|
import com.sd.business.mapper.inventory.InventoryMapper;
|
|
|
import com.sd.business.service.bom.BomSpecService;
|
|
|
-import com.sd.business.service.in.InOutStorageBalanceService;
|
|
|
import com.sd.business.service.inventory.InventoryService;
|
|
|
-import com.sd.business.service.purchase.PurchaseBomService;
|
|
|
import com.sd.framework.util.excel.util.ExcelUtil;
|
|
|
import com.sd.framework.util.sql.Sql;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -33,8 +28,6 @@ import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.math.RoundingMode;
|
|
|
-import java.util.Collection;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
@@ -57,12 +50,6 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
private HttpServletResponse response;
|
|
|
|
|
|
@Autowired
|
|
|
- private PurchaseBomService purchaseBomService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private InOutStorageBalanceService inOutStorageBalanceService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
private BomSpecService bomSpecService;
|
|
|
|
|
|
@Override
|
|
@@ -74,7 +61,6 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
.selectAs(BomSpec::getCode, InventoryVo::getBomSpecCode)
|
|
|
.selectAs(BomSpec::getName, InventoryVo::getBomSpecName)
|
|
|
.selectAs(Department::getName, InventoryVo::getDepartmentName)
|
|
|
- .multiply(Inventory::getQuantity, Inventory::getBalanceUnitPrice, InventoryVo::getTotalPrice)
|
|
|
|
|
|
.from(Inventory.class)
|
|
|
.leftJoin(BomSpec.class, Inventory::getBomSpecId)
|
|
@@ -150,32 +136,13 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
public void purchaseInStorage(Long purchaseId, Long departmentId, Long warehouseId, List<InOutStorageBom> inOutStorageBomList) {
|
|
|
List<Long> bomSpecIdList = inOutStorageBomList.stream().map(InOutStorageBom::getBomSpecId).collect(Collectors.toList());
|
|
|
synchronized (this) {
|
|
|
+
|
|
|
// 通过事业部id和出库id获取bom规格库存
|
|
|
List<Inventory> inventoryList = getInventoryList(departmentId, warehouseId, bomSpecIdList);
|
|
|
|
|
|
// 通过bom规格分组
|
|
|
Map<Long, Inventory> map = inventoryList.stream().collect(Collectors.toMap(Inventory::getBomSpecId, Function.identity()));
|
|
|
|
|
|
- Map<Long, PurchaseBom> purchaseBomMap = purchaseBomService.mapKEntity(PurchaseBom::getBomSpecId,
|
|
|
- q -> q.eq(PurchaseBom::getPurchaseId, purchaseId).in(PurchaseBom::getBomSpecId, bomSpecIdList));
|
|
|
-
|
|
|
- // 保存结存单价批次
|
|
|
- List<InOutStorageBalance> inOutStorageDetailsList = inOutStorageBomList.stream().map(item -> {
|
|
|
- // 获取入库单价
|
|
|
- PurchaseBom purchaseBom = purchaseBomMap.get(item.getBomSpecId());
|
|
|
- BigDecimal inUnitPrice = purchaseBom == null ? BigDecimal.ZERO : purchaseBom.getUnitPrice();
|
|
|
- item.setUnitPrice(inUnitPrice);
|
|
|
-
|
|
|
- InOutStorageBalance inOutStorageBalance = new InOutStorageBalance();
|
|
|
- inOutStorageBalance.setDepartmentId(departmentId);
|
|
|
- inOutStorageBalance.setWarehouseId(warehouseId);
|
|
|
- inOutStorageBalance.setBomSpecId(item.getBomSpecId());
|
|
|
- inOutStorageBalance.setInUnitPrice(inUnitPrice);
|
|
|
- inOutStorageBalance.setQuantity(item.getQuantity());
|
|
|
- return inOutStorageBalance;
|
|
|
- }).collect(Collectors.toList());
|
|
|
- inOutStorageBalanceService.saveBatch(inOutStorageDetailsList);
|
|
|
-
|
|
|
for (InOutStorageBom inOutStorageBom : inOutStorageBomList) {
|
|
|
|
|
|
// 库存
|
|
@@ -185,26 +152,13 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
tempInventory.setDepartmentId(departmentId);
|
|
|
tempInventory.setBomSpecId(item);
|
|
|
tempInventory.setQuantity(BigDecimal.ZERO);
|
|
|
- tempInventory.setBalanceUnitPrice(BigDecimal.ZERO);
|
|
|
inventoryList.add(tempInventory);
|
|
|
return tempInventory;
|
|
|
});
|
|
|
|
|
|
- // 结存数量
|
|
|
+ // 数量
|
|
|
BigDecimal sumQuantity = inventory.getQuantity().add(inOutStorageBom.getQuantity());
|
|
|
-
|
|
|
- // 结存金额
|
|
|
- PurchaseBom purchaseBom = purchaseBomMap.get(inOutStorageBom.getBomSpecId());
|
|
|
- BigDecimal sumPrice = inventory.getQuantity().multiply(inventory.getBalanceUnitPrice())
|
|
|
- .add(inOutStorageBom.getQuantity().multiply(purchaseBom.getUnitPrice()));
|
|
|
-
|
|
|
- // 获取入库结存单价
|
|
|
- BigDecimal balanceUnitPrice = sumPrice.divide(sumQuantity, 3, RoundingMode.HALF_UP);
|
|
|
-
|
|
|
inventory.setQuantity(sumQuantity);
|
|
|
- inventory.setBalanceUnitPrice(balanceUnitPrice);
|
|
|
-
|
|
|
- inOutStorageBom.setBalanceInventoryQuantity(sumQuantity);
|
|
|
}
|
|
|
|
|
|
saveOrUpdateBatch(inventoryList);
|
|
@@ -226,28 +180,6 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
|
|
|
// 通过bom规格分组
|
|
|
Map<Long, Inventory> map = inventoryList.stream().collect(Collectors.toMap(Inventory::getBomSpecId, Function.identity()));
|
|
|
-
|
|
|
- // 保存结存单价批次
|
|
|
- List<InOutStorageBalance> inOutStorageBalanceList = list.stream().map(item -> {
|
|
|
-
|
|
|
- // todo 获取临时结存单价
|
|
|
- Inventory inventory = map.get(item.getBomSpecId());
|
|
|
- BigDecimal inUnitPrice = inventory == null ? BigDecimal.ZERO : inventory.getBalanceUnitPrice();
|
|
|
-
|
|
|
- InOutStorageBalance inOutStorageBalance = new InOutStorageBalance();
|
|
|
- inOutStorageBalance.setDepartmentId(departmentId);
|
|
|
- inOutStorageBalance.setWarehouseId(warehouseId);
|
|
|
- inOutStorageBalance.setBomSpecId(item.getBomSpecId());
|
|
|
- inOutStorageBalance.setInUnitPrice(inUnitPrice);
|
|
|
- inOutStorageBalance.setQuantity(item.getQuantity());
|
|
|
-
|
|
|
- return inOutStorageBalance;
|
|
|
- }).collect(Collectors.toList());
|
|
|
- inOutStorageBalanceService.saveBatch(inOutStorageBalanceList);
|
|
|
-
|
|
|
- // 通过出库id获取bom规格结存列表
|
|
|
- Map<Long, List<InOutStorageBalance>> inOutStorageBalanceMap = getInOutStorageBalanceMap(warehouseId, bomSpecIdList);
|
|
|
-
|
|
|
for (InOutFun inOutFun : list) {
|
|
|
|
|
|
// 库存
|
|
@@ -260,15 +192,7 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
inventoryList.add(tempInventory);
|
|
|
return tempInventory;
|
|
|
});
|
|
|
-
|
|
|
- // 获取入库结存单价
|
|
|
- BigDecimal balanceUnitPrice = getBalanceUnitPrice(inOutStorageBalanceMap, inOutFun);
|
|
|
-
|
|
|
inventory.setQuantity(inventory.getQuantity().add(inOutFun.getQuantity()));
|
|
|
- inventory.setBalanceUnitPrice(balanceUnitPrice);
|
|
|
-
|
|
|
- // 保存当前入库记录的结存库存
|
|
|
- inOutFun.setBalanceInventoryQuantity(inventory.getQuantity());
|
|
|
}
|
|
|
|
|
|
saveOrUpdateBatch(inventoryList);
|
|
@@ -285,9 +209,6 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
List<Inventory> inventoryList = getInventoryList(departmentId, warehouseId, bomSpecIdList);
|
|
|
Map<Long, Inventory> inventoryMap = inventoryList.stream().collect(Collectors.toMap(Inventory::getBomSpecId, Function.identity()));
|
|
|
|
|
|
- // 获取bom规格id和结存明细列表map
|
|
|
- Map<Long, List<InOutStorageBalance>> inOutStorageBalanceMap = getInOutStorageBalanceMap(warehouseId, bomSpecIdList);
|
|
|
-
|
|
|
for (InOutFun inOutFun : list) {
|
|
|
|
|
|
BigDecimal quantity = inOutFun.getQuantity();
|
|
@@ -305,39 +226,14 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
}
|
|
|
inventory.setQuantity(inventory.getQuantity().subtract(quantity));
|
|
|
|
|
|
- // 计算出库结存单价
|
|
|
- BigDecimal balanceUnitPrice = getOutBalanceUnitPrice(inOutStorageBalanceMap, inOutFun);
|
|
|
-
|
|
|
- // 赋值结存单价
|
|
|
- inventory.setBalanceUnitPrice(balanceUnitPrice);
|
|
|
-
|
|
|
- // 保存当前入库记录的结存库存
|
|
|
- inOutFun.setBalanceInventoryQuantity(inventory.getQuantity());
|
|
|
-
|
|
|
}
|
|
|
|
|
|
- // 修改计算结存单价的出入库明细表
|
|
|
- List<InOutStorageBalance> inOutStorageDetailsList = inOutStorageBalanceMap.values()
|
|
|
- .stream().flatMap(Collection::stream).collect(Collectors.toList());
|
|
|
- inOutStorageBalanceService.updateBatchById(inOutStorageDetailsList);
|
|
|
-
|
|
|
- // 修改库存数量以及结存单价
|
|
|
+ // 修改库存数量
|
|
|
updateBatchById(inventoryList);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 通过出库id获取bom规格结存列表
|
|
|
- */
|
|
|
- private Map<Long, List<InOutStorageBalance>> getInOutStorageBalanceMap(Long warehouseId, List<Long> bomSpecIdList) {
|
|
|
- return inOutStorageBalanceService.mapKGroup(InOutStorageBalance::getBomSpecId, q -> q
|
|
|
- .eq(InOutStorageBalance::getWarehouseId, warehouseId)
|
|
|
- .in(InOutStorageBalance::getBomSpecId, bomSpecIdList)
|
|
|
- .gt(InOutStorageBalance::getQuantity, BigDecimal.ZERO)
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
* 通过事业部id和出库id获取bom规格库存
|
|
|
*/
|
|
|
private List<Inventory> getInventoryList(Long departmentId, Long warehouseId, List<Long> bomSpecIdList) {
|
|
@@ -348,67 +244,4 @@ public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 获取结存单价
|
|
|
- */
|
|
|
- private BigDecimal getBalanceUnitPrice(Map<Long, List<InOutStorageBalance>> map, InOutFun inOutFun) {
|
|
|
-
|
|
|
- List<InOutStorageBalance> inOutStorageBalanceList = map.get(inOutFun.getBomSpecId());
|
|
|
-
|
|
|
- // 没有结存记录,结存单价为0
|
|
|
- if (ObjectUtil.isEmpty(inOutStorageBalanceList)) {
|
|
|
- return BigDecimal.ZERO;
|
|
|
- }
|
|
|
-
|
|
|
- // 获取结存数量
|
|
|
- BigDecimal sumQuantity = inOutStorageBalanceList.stream()
|
|
|
- .map(InOutStorageBalance::getQuantity)
|
|
|
- .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
-
|
|
|
- // 数量为0,结存单价为0
|
|
|
- if (sumQuantity.compareTo(BigDecimal.ZERO) == 0) {
|
|
|
- return BigDecimal.ZERO;
|
|
|
- }
|
|
|
-
|
|
|
- // 获取结存金额
|
|
|
- BigDecimal sumPrice = inOutStorageBalanceList.stream()
|
|
|
- .map(item -> item.getQuantity().multiply(item.getInUnitPrice()))
|
|
|
- .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
-
|
|
|
- // 计算新结存单价
|
|
|
- return sumPrice.divide(sumQuantity, 3, RoundingMode.HALF_UP);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取出库结存单价
|
|
|
- */
|
|
|
- private BigDecimal getOutBalanceUnitPrice(Map<Long, List<InOutStorageBalance>> map, InOutFun inOutFun) {
|
|
|
-
|
|
|
- // 出库数量
|
|
|
- BigDecimal outQuantity = inOutFun.getQuantity();
|
|
|
-
|
|
|
- // 结存列表
|
|
|
- List<InOutStorageBalance> inOutStorageBalanceList = map.get(inOutFun.getBomSpecId());
|
|
|
-
|
|
|
- // 没有结存记录,结存单价为0
|
|
|
- if (ObjectUtil.isEmpty(inOutStorageBalanceList)) {
|
|
|
- return BigDecimal.ZERO;
|
|
|
- }
|
|
|
-
|
|
|
- for (InOutStorageBalance inOutStorageBalance : inOutStorageBalanceList) {
|
|
|
- // 可出库数量
|
|
|
- BigDecimal quantity = inOutStorageBalance.getQuantity();
|
|
|
-
|
|
|
- if (outQuantity.compareTo(quantity) > 0) {
|
|
|
- inOutStorageBalance.setQuantity(BigDecimal.ZERO);
|
|
|
- outQuantity = outQuantity.subtract(quantity);
|
|
|
- } else {
|
|
|
- inOutStorageBalance.setQuantity(quantity.subtract(outQuantity));
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return getBalanceUnitPrice(map, inOutFun);
|
|
|
- }
|
|
|
-
|
|
|
}
|