use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.
the class StockMoveServiceImpl method computeMasses.
private void computeMasses(StockMove stockMove) throws AxelorException {
StockConfig stockConfig = stockMove.getCompany().getStockConfig();
Unit endUnit = stockConfig != null ? stockConfig.getCustomsMassUnit() : null;
boolean massesRequiredForStockMove = false;
List<StockMoveLine> stockMoveLineList = stockMove.getStockMoveLineList();
if (stockMoveLineList == null) {
return;
}
UnitConversionService unitConversionService = Beans.get(UnitConversionService.class);
for (StockMoveLine stockMoveLine : stockMoveLineList) {
Product product = stockMoveLine.getProduct();
boolean massesRequiredForStockMoveLine = stockMoveLineService.checkMassesRequired(stockMove, stockMoveLine);
if (product == null || !ProductRepository.PRODUCT_TYPE_STORABLE.equals(product.getProductTypeSelect())) {
continue;
}
BigDecimal netMass = stockMoveLine.getNetMass();
if (netMass.signum() == 0) {
Unit startUnit = product.getMassUnit();
if (startUnit != null && endUnit != null) {
netMass = unitConversionService.convert(startUnit, endUnit, product.getNetMass(), product.getNetMass().scale(), null);
stockMoveLine.setNetMass(netMass);
}
}
if (netMass.signum() != 0) {
BigDecimal totalNetMass = netMass.multiply(stockMoveLine.getRealQty());
stockMoveLine.setTotalNetMass(totalNetMass);
} else if (massesRequiredForStockMoveLine) {
throw new AxelorException(stockMove, TraceBackRepository.CATEGORY_NO_VALUE, I18n.get(IExceptionMessage.STOCK_MOVE_18));
}
if (!massesRequiredForStockMove && massesRequiredForStockMoveLine) {
massesRequiredForStockMove = true;
}
}
if (massesRequiredForStockMove && endUnit == null) {
throw new AxelorException(stockMove, TraceBackRepository.CATEGORY_NO_VALUE, I18n.get(IExceptionMessage.STOCK_MOVE_17));
}
}
use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.
the class ManufOrderServiceImpl method createDiffProdProductList.
@Override
public List<ProdProduct> createDiffProdProductList(List<ProdProduct> prodProductList, List<StockMoveLine> stockMoveLineList) throws AxelorException {
List<ProdProduct> diffConsumeList = new ArrayList<>();
for (ProdProduct prodProduct : prodProductList) {
Product product = prodProduct.getProduct();
Unit newUnit = prodProduct.getUnit();
List<StockMoveLine> stockMoveLineProductList = stockMoveLineList.stream().filter(stockMoveLine1 -> stockMoveLine1.getProduct() != null).filter(stockMoveLine1 -> stockMoveLine1.getProduct().equals(product)).collect(Collectors.toList());
if (stockMoveLineProductList.isEmpty()) {
StockMoveLine stockMoveLine = new StockMoveLine();
stockMoveLineProductList.add(stockMoveLine);
}
BigDecimal diffQty = computeDiffQty(prodProduct, stockMoveLineProductList, product);
BigDecimal plannedQty = prodProduct.getQty();
BigDecimal realQty = diffQty.add(plannedQty);
if (diffQty.compareTo(BigDecimal.ZERO) != 0) {
ProdProduct diffProdProduct = new ProdProduct();
diffProdProduct.setQty(diffQty);
diffProdProduct.setPlannedQty(plannedQty);
diffProdProduct.setRealQty(realQty);
diffProdProduct.setProduct(product);
diffProdProduct.setUnit(newUnit);
diffConsumeList.add(diffProdProduct);
}
}
// There are stock move lines with products that are not available in
// prod product list. It needs to appear in the prod product list
List<StockMoveLine> stockMoveLineMissingProductList = stockMoveLineList.stream().filter(stockMoveLine1 -> stockMoveLine1.getProduct() != null).filter(stockMoveLine1 -> !prodProductList.stream().map(ProdProduct::getProduct).collect(Collectors.toList()).contains(stockMoveLine1.getProduct())).collect(Collectors.toList());
for (StockMoveLine stockMoveLine : stockMoveLineMissingProductList) {
if (stockMoveLine.getQty().compareTo(BigDecimal.ZERO) != 0) {
ProdProduct diffProdProduct = new ProdProduct();
diffProdProduct.setQty(stockMoveLine.getQty());
diffProdProduct.setPlannedQty(BigDecimal.ZERO);
diffProdProduct.setRealQty(stockMoveLine.getQty());
diffProdProduct.setProduct(stockMoveLine.getProduct());
diffProdProduct.setUnit(stockMoveLine.getUnit());
diffConsumeList.add(diffProdProduct);
}
}
return diffConsumeList;
}
use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.
the class CostSheetServiceImpl method computeTotalProducedQty.
protected BigDecimal computeTotalProducedQty(Product producedProduct, List<StockMoveLine> producedStockMoveLineList, LocalDate calculationDate, LocalDate previousCostSheetDate, int calculationTypeSelect) throws AxelorException {
BigDecimal totalQty = BigDecimal.ZERO;
Map<List<Object>, BigDecimal> producedStockMoveLinePerProductAndUnit = getTotalQtyPerProductAndUnit(producedStockMoveLineList, calculationDate, previousCostSheetDate, calculationTypeSelect);
for (List<Object> keys : producedStockMoveLinePerProductAndUnit.keySet()) {
Iterator<Object> iterator = keys.iterator();
Product product = (Product) iterator.next();
Unit unit = (Unit) iterator.next();
BigDecimal realQty = producedStockMoveLinePerProductAndUnit.get(keys);
if (product == null || !product.equals(producedProduct)) {
continue;
}
totalQty = totalQty.add(unitConversionService.convert(unit, costSheet.getManufOrder().getUnit(), realQty, realQty.scale(), product));
}
return totalQty;
}
use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.
the class ReservedQtyServiceImpl method reallocateQty.
/**
* Reallocate quantity in stock location line after entry into storage.
*
* @param stockMoveLine
* @param stockLocation
* @param stockLocationLine
* @param product
* @param qty the quantity in stock move line unit.
* @throws AxelorException
*/
protected void reallocateQty(StockMoveLine stockMoveLine, StockLocation stockLocation, StockLocationLine stockLocationLine, Product product, BigDecimal qty) throws AxelorException {
Unit stockMoveLineUnit = stockMoveLine.getUnit();
Unit stockLocationLineUnit = stockLocationLine.getUnit();
BigDecimal stockLocationQty = convertUnitWithProduct(stockMoveLineUnit, stockLocationLineUnit, qty, product);
// the quantity that will be allocated in stock location line
BigDecimal realReservedQty;
// the quantity that will be allocated in stock move line
BigDecimal leftToAllocate = stockLocationLine.getRequestedReservedQty().subtract(stockLocationLine.getReservedQty());
realReservedQty = stockLocationQty.min(leftToAllocate);
allocateReservedQuantityInSaleOrderLines(realReservedQty, stockLocation, product, stockLocationLineUnit, Optional.of(stockMoveLine));
updateReservedQty(stockLocationLine);
}
use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.
the class ReservedQtyServiceImpl method allocateReservedQuantityInSaleOrderLines.
/**
* The new parameter allocated stock move line is used if we are allocating a stock move line.
* This method will reallocate the lines with the same stock move (and the same product) before
* other stock move lines.
*
* <p>We are using an optional because in the basic use of the method, the argument is empty.
*/
protected BigDecimal allocateReservedQuantityInSaleOrderLines(BigDecimal qtyToAllocate, StockLocation stockLocation, Product product, Unit stockLocationLineUnit, Optional<StockMoveLine> allocatedStockMoveLine) throws AxelorException {
List<StockMoveLine> stockMoveLineListToAllocate = stockMoveLineRepository.all().filter("self.stockMove.fromStockLocation.id = :stockLocationId " + "AND self.product.id = :productId " + "AND self.stockMove.statusSelect = :planned " + "AND self.reservationDateTime IS NOT NULL " + "AND self.reservedQty < self.requestedReservedQty").bind("stockLocationId", stockLocation.getId()).bind("productId", product.getId()).bind("planned", StockMoveRepository.STATUS_PLANNED).order("reservationDateTime").order("stockMove.estimatedDate").fetch();
// put stock move lines with the same stock move on the beginning of the list.
allocatedStockMoveLine.ifPresent(stockMoveLine -> stockMoveLineListToAllocate.sort(// Note: this comparator imposes orderings that are inconsistent with equals.
(sml1, sml2) -> {
if (sml1.getStockMove().equals(sml2.getStockMove())) {
return 0;
} else if (sml1.getStockMove().equals(stockMoveLine.getStockMove())) {
return -1;
} else if (sml2.getStockMove().equals(stockMoveLine.getStockMove())) {
return 1;
} else {
return 0;
}
}));
BigDecimal leftQtyToAllocate = qtyToAllocate;
for (StockMoveLine stockMoveLine : stockMoveLineListToAllocate) {
BigDecimal leftQtyToAllocateStockMove = convertUnitWithProduct(stockLocationLineUnit, stockMoveLine.getUnit(), leftQtyToAllocate, product);
BigDecimal neededQtyToAllocate = stockMoveLine.getRequestedReservedQty().subtract(stockMoveLine.getReservedQty());
BigDecimal allocatedStockMoveQty = leftQtyToAllocateStockMove.min(neededQtyToAllocate);
BigDecimal allocatedQty = convertUnitWithProduct(stockMoveLine.getUnit(), stockLocationLineUnit, allocatedStockMoveQty, product);
// update reserved qty in stock move line and sale order line
updateReservedQuantityFromStockMoveLine(stockMoveLine, product, allocatedStockMoveQty);
// update left qty to allocate
leftQtyToAllocate = leftQtyToAllocate.subtract(allocatedQty);
}
return qtyToAllocate.subtract(leftQtyToAllocate);
}
Aggregations