Search in sources :

Example 6 with Unit

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));
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) UnitConversionService(com.axelor.apps.base.service.UnitConversionService) StockConfig(com.axelor.apps.stock.db.StockConfig) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) Product(com.axelor.apps.base.db.Product) Unit(com.axelor.apps.base.db.Unit) BigDecimal(java.math.BigDecimal)

Example 7 with Unit

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;
}
Also used : StockLocationRepository(com.axelor.apps.stock.db.repo.StockLocationRepository) IExceptionMessage(com.axelor.apps.production.exceptions.IExceptionMessage) ProductionConfig(com.axelor.apps.production.db.ProductionConfig) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) ProductCompanyService(com.axelor.apps.base.service.ProductCompanyService) Transactional(com.google.inject.persist.Transactional) BigDecimal(java.math.BigDecimal) Pair(org.apache.commons.lang3.tuple.Pair) BillOfMaterial(com.axelor.apps.production.db.BillOfMaterial) ProductVariantService(com.axelor.apps.base.service.ProductVariantService) SaleOrder(com.axelor.apps.sale.db.SaleOrder) StockMoveLineService(com.axelor.apps.stock.service.StockMoveLineService) ProductionConfigService(com.axelor.apps.production.service.config.ProductionConfigService) StockLocationService(com.axelor.apps.stock.service.StockLocationService) RoundingMode(java.math.RoundingMode) ProdProcessLine(com.axelor.apps.production.db.ProdProcessLine) StockConfig(com.axelor.apps.stock.db.StockConfig) StockMove(com.axelor.apps.stock.db.StockMove) StockMoveRepository(com.axelor.apps.stock.db.repo.StockMoveRepository) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) Set(java.util.Set) AppBaseService(com.axelor.apps.base.service.app.AppBaseService) StringUtils(com.axelor.common.StringUtils) Collectors(java.util.stream.Collectors) SequenceService(com.axelor.apps.base.service.administration.SequenceService) List(java.util.List) Product(com.axelor.apps.base.db.Product) ManufOrder(com.axelor.apps.production.db.ManufOrder) ProdResidualProduct(com.axelor.apps.production.db.ProdResidualProduct) Optional(java.util.Optional) ManufOrderRepository(com.axelor.apps.production.db.repo.ManufOrderRepository) Joiner(com.google.common.base.Joiner) Company(com.axelor.apps.base.db.Company) AppProductionService(com.axelor.apps.production.service.app.AppProductionService) ProductRepository(com.axelor.apps.base.db.repo.ProductRepository) ProdProductRepository(com.axelor.apps.production.db.repo.ProdProductRepository) LocalDateTime(java.time.LocalDateTime) StockMoveService(com.axelor.apps.stock.service.StockMoveService) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AxelorException(com.axelor.exception.AxelorException) UnitConversionService(com.axelor.apps.base.service.UnitConversionService) StockLocation(com.axelor.apps.stock.db.StockLocation) I18n(com.axelor.i18n.I18n) ProdProcess(com.axelor.apps.production.db.ProdProcess) ProdProduct(com.axelor.apps.production.db.ProdProduct) ProductionOrder(com.axelor.apps.production.db.ProductionOrder) StringTool(com.axelor.apps.tool.StringTool) Sequence(com.axelor.apps.base.db.Sequence) Logger(org.slf4j.Logger) TraceBackRepository(com.axelor.exception.db.repo.TraceBackRepository) MoreObjects(com.google.common.base.MoreObjects) OperationOrder(com.axelor.apps.production.db.OperationOrder) StockConfigProductionService(com.axelor.apps.production.service.config.StockConfigProductionService) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) Beans(com.axelor.inject.Beans) Unit(com.axelor.apps.base.db.Unit) OperationOrderService(com.axelor.apps.production.service.operationorder.OperationOrderService) Comparator(java.util.Comparator) OperationOrderStockMoveService(com.axelor.apps.production.service.operationorder.OperationOrderStockMoveService) Collections(java.util.Collections) ArrayList(java.util.ArrayList) Product(com.axelor.apps.base.db.Product) ProdResidualProduct(com.axelor.apps.production.db.ProdResidualProduct) ProdProduct(com.axelor.apps.production.db.ProdProduct) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) ProdProduct(com.axelor.apps.production.db.ProdProduct) Unit(com.axelor.apps.base.db.Unit) BigDecimal(java.math.BigDecimal)

Example 8 with Unit

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;
}
Also used : ProdProduct(com.axelor.apps.production.db.ProdProduct) Product(com.axelor.apps.base.db.Product) ProdResidualProduct(com.axelor.apps.production.db.ProdResidualProduct) ArrayList(java.util.ArrayList) List(java.util.List) Unit(com.axelor.apps.base.db.Unit) BigDecimal(java.math.BigDecimal)

Example 9 with Unit

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);
}
Also used : Unit(com.axelor.apps.base.db.Unit) BigDecimal(java.math.BigDecimal)

Example 10 with Unit

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);
}
Also used : Company(com.axelor.apps.base.db.Company) StockLocationRepository(com.axelor.apps.stock.db.repo.StockLocationRepository) StockMoveLineRepository(com.axelor.apps.stock.db.repo.StockMoveLineRepository) IExceptionMessage(com.axelor.apps.supplychain.exception.IExceptionMessage) Inject(com.google.inject.Inject) Transactional(com.google.inject.persist.Transactional) BigDecimal(java.math.BigDecimal) AxelorException(com.axelor.exception.AxelorException) UnitConversionService(com.axelor.apps.base.service.UnitConversionService) StockLocation(com.axelor.apps.stock.db.StockLocation) CancelReason(com.axelor.apps.base.db.CancelReason) I18n(com.axelor.i18n.I18n) StockLocationLineService(com.axelor.apps.stock.service.StockLocationLineService) TraceBackRepository(com.axelor.exception.db.repo.TraceBackRepository) StockMove(com.axelor.apps.stock.db.StockMove) StockMoveRepository(com.axelor.apps.stock.db.repo.StockMoveRepository) AppBaseService(com.axelor.apps.base.service.app.AppBaseService) Collectors(java.util.stream.Collectors) SupplyChainConfig(com.axelor.apps.supplychain.db.SupplyChainConfig) Objects(java.util.Objects) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) List(java.util.List) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) StockLocationLine(com.axelor.apps.stock.db.StockLocationLine) Beans(com.axelor.inject.Beans) Product(com.axelor.apps.base.db.Product) Unit(com.axelor.apps.base.db.Unit) Optional(java.util.Optional) AppSupplychainService(com.axelor.apps.supplychain.service.app.AppSupplychainService) Comparator(java.util.Comparator) SupplyChainConfigService(com.axelor.apps.supplychain.service.config.SupplyChainConfigService) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) BigDecimal(java.math.BigDecimal)

Aggregations

Unit (com.axelor.apps.base.db.Unit)45 BigDecimal (java.math.BigDecimal)38 Product (com.axelor.apps.base.db.Product)19 AxelorException (com.axelor.exception.AxelorException)13 Transactional (com.google.inject.persist.Transactional)12 StockMoveLine (com.axelor.apps.stock.db.StockMoveLine)11 UnitConversionService (com.axelor.apps.base.service.UnitConversionService)9 Company (com.axelor.apps.base.db.Company)8 PurchaseOrderLine (com.axelor.apps.purchase.db.PurchaseOrderLine)8 StockLocationLine (com.axelor.apps.stock.db.StockLocationLine)8 AppBaseService (com.axelor.apps.base.service.app.AppBaseService)6 SaleOrderLine (com.axelor.apps.sale.db.SaleOrderLine)6 StockLocation (com.axelor.apps.stock.db.StockLocation)6 StockMove (com.axelor.apps.stock.db.StockMove)6 LocalDate (java.time.LocalDate)6 List (java.util.List)6 ProdProduct (com.axelor.apps.production.db.ProdProduct)5 ProdResidualProduct (com.axelor.apps.production.db.ProdResidualProduct)5 ArrayList (java.util.ArrayList)5 BillOfMaterial (com.axelor.apps.production.db.BillOfMaterial)4