Search in sources :

Example 26 with StockLocationLine

use of com.axelor.apps.stock.db.StockLocationLine in project axelor-open-suite by axelor.

the class StockLocationLineServiceSupplychainImpl method getAvailableQty.

@Override
public BigDecimal getAvailableQty(StockLocation stockLocation, Product product) {
    if (!appSupplychainService.isApp("supplychain")) {
        return super.getAvailableQty(stockLocation, product);
    }
    StockLocationLine stockLocationLine = getStockLocationLine(stockLocation, product);
    BigDecimal availableQty = BigDecimal.ZERO;
    if (stockLocationLine != null) {
        availableQty = stockLocationLine.getCurrentQty().subtract(stockLocationLine.getReservedQty());
    }
    return availableQty;
}
Also used : StockLocationLine(com.axelor.apps.stock.db.StockLocationLine) BigDecimal(java.math.BigDecimal)

Example 27 with StockLocationLine

use of com.axelor.apps.stock.db.StockLocationLine in project axelor-open-suite by axelor.

the class StockLocationServiceSupplychainImpl method getReservedQty.

@Override
public BigDecimal getReservedQty(Long productId, Long locationId, Long companyId) throws AxelorException {
    if (productId != null) {
        Product product = productRepo.find(productId);
        Unit productUnit = product.getUnit();
        UnitConversionService unitConversionService = Beans.get(UnitConversionService.class);
        if (locationId == null || locationId == 0L) {
            List<StockLocation> stockLocations = getNonVirtualStockLocations(companyId);
            if (!stockLocations.isEmpty()) {
                BigDecimal reservedQty = BigDecimal.ZERO;
                for (StockLocation stockLocation : stockLocations) {
                    StockLocationLine stockLocationLine = stockLocationLineService.getOrCreateStockLocationLine(stockLocationRepo.find(stockLocation.getId()), productRepo.find(productId));
                    if (stockLocationLine != null) {
                        Unit stockLocationLineUnit = stockLocationLine.getUnit();
                        reservedQty = reservedQty.add(stockLocationLine.getReservedQty());
                        if (productUnit != null && !productUnit.equals(stockLocationLineUnit)) {
                            reservedQty = unitConversionService.convert(stockLocationLineUnit, productUnit, reservedQty, reservedQty.scale(), product);
                        }
                    }
                }
                return reservedQty;
            }
        } else {
            StockLocationLine stockLocationLine = stockLocationLineService.getOrCreateStockLocationLine(stockLocationRepo.find(locationId), productRepo.find(productId));
            if (stockLocationLine != null) {
                Unit stockLocationLineUnit = stockLocationLine.getUnit();
                if (productUnit != null && !productUnit.equals(stockLocationLineUnit)) {
                    return unitConversionService.convert(stockLocationLineUnit, productUnit, stockLocationLine.getReservedQty(), stockLocationLine.getReservedQty().scale(), product);
                }
                return stockLocationLine.getReservedQty();
            }
        }
    }
    return BigDecimal.ZERO;
}
Also used : UnitConversionService(com.axelor.apps.base.service.UnitConversionService) StockLocation(com.axelor.apps.stock.db.StockLocation) Product(com.axelor.apps.base.db.Product) StockLocationLine(com.axelor.apps.stock.db.StockLocationLine) Unit(com.axelor.apps.base.db.Unit) BigDecimal(java.math.BigDecimal)

Example 28 with StockLocationLine

use of com.axelor.apps.stock.db.StockLocationLine in project axelor-open-suite by axelor.

the class ReservedQtyServiceImpl method updateRequestedReservedQty.

@Override
@Transactional(rollbackOn = { Exception.class })
public void updateRequestedReservedQty(SaleOrderLine saleOrderLine, BigDecimal newReservedQty) throws AxelorException {
    if (saleOrderLine.getProduct() == null || !saleOrderLine.getProduct().getStockManaged()) {
        return;
    }
    StockMoveLine stockMoveLine = getPlannedStockMoveLine(saleOrderLine);
    if (stockMoveLine == null) {
        // only change requested quantity in sale order line
        saleOrderLine.setRequestedReservedQty(newReservedQty);
        return;
    }
    checkBeforeUpdatingQties(stockMoveLine, newReservedQty);
    if (Beans.get(AppSupplychainService.class).getAppSupplychain().getBlockDeallocationOnAvailabilityRequest()) {
        checkAvailabilityRequest(stockMoveLine, newReservedQty, true);
    }
    BigDecimal diffReservedQuantity = newReservedQty.subtract(saleOrderLine.getRequestedReservedQty());
    // update in stock move line and sale order line
    BigDecimal newAllocatedQty = updateRequestedReservedQuantityInStockMoveLines(saleOrderLine, stockMoveLine.getProduct(), newReservedQty);
    StockLocationLine stockLocationLine = stockLocationLineService.getOrCreateStockLocationLine(stockMoveLine.getStockMove().getFromStockLocation(), stockMoveLine.getProduct());
    Product product = stockMoveLine.getProduct();
    // update in stock location line
    BigDecimal diffReservedQuantityLocation = convertUnitWithProduct(stockMoveLine.getUnit(), stockLocationLine.getUnit(), diffReservedQuantity, product);
    stockLocationLine.setRequestedReservedQty(stockLocationLine.getRequestedReservedQty().add(diffReservedQuantityLocation));
    // update reserved qty
    if (newAllocatedQty.compareTo(saleOrderLine.getReservedQty()) < 0) {
        updateReservedQty(saleOrderLine, newAllocatedQty);
    }
}
Also used : StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) Product(com.axelor.apps.base.db.Product) StockLocationLine(com.axelor.apps.stock.db.StockLocationLine) BigDecimal(java.math.BigDecimal) Transactional(com.google.inject.persist.Transactional)

Example 29 with StockLocationLine

use of com.axelor.apps.stock.db.StockLocationLine in project axelor-open-suite by axelor.

the class ReservedQtyServiceImpl method updateReservedQty.

@Override
@Transactional(rollbackOn = { Exception.class })
public void updateReservedQty(StockMoveLine stockMoveLine, BigDecimal newReservedQty) throws AxelorException {
    StockLocationLine stockLocationLine = stockLocationLineService.getOrCreateStockLocationLine(stockMoveLine.getStockMove().getFromStockLocation(), stockMoveLine.getProduct());
    updateReservedQty(stockLocationLine, stockMoveLine, newReservedQty);
}
Also used : StockLocationLine(com.axelor.apps.stock.db.StockLocationLine) Transactional(com.google.inject.persist.Transactional)

Example 30 with StockLocationLine

use of com.axelor.apps.stock.db.StockLocationLine in project axelor-open-suite by axelor.

the class ReservedQtyServiceImpl method updateRequestedQuantityInFromStockLocation.

@Override
public void updateRequestedQuantityInFromStockLocation(StockMoveLine stockMoveLine, StockLocation stockLocation, Product product, int toStatus, BigDecimal requestedReservedQty) throws AxelorException {
    if (product == null || !product.getStockManaged()) {
        return;
    }
    Unit stockMoveLineUnit = stockMoveLine.getUnit();
    StockLocationLine stockLocationLine = stockLocationLineService.getStockLocationLine(stockLocation, product);
    if (stockLocationLine == null) {
        return;
    }
    Unit stockLocationLineUnit = stockLocationLine.getUnit();
    // the quantity that will be allocated in stock location line
    BigDecimal realReservedQty;
    // the quantity that will be allocated in stock move line
    BigDecimal realReservedStockMoveQty;
    // if we cancel, subtract the quantity using the previously allocated quantity.
    if (toStatus == StockMoveRepository.STATUS_CANCELED || toStatus == StockMoveRepository.STATUS_REALIZED) {
        realReservedStockMoveQty = stockMoveLine.getReservedQty();
        // convert the quantity for stock location line
        realReservedQty = convertUnitWithProduct(stockMoveLineUnit, stockLocationLineUnit, realReservedStockMoveQty, stockMoveLine.getProduct());
        // reallocate quantity in other stock move lines
        if (isReallocatingQtyOnCancel(stockMoveLine)) {
            reallocateQty(stockMoveLine, stockLocation, stockLocationLine, product, realReservedQty);
        }
        // no more reserved qty in stock move and sale order lines
        updateReservedQuantityFromStockMoveLine(stockMoveLine, product, stockMoveLine.getReservedQty().negate());
        // update requested quantity in sale order line
        SaleOrderLine saleOrderLine = stockMoveLine.getSaleOrderLine();
        if (saleOrderLine != null) {
            // requested quantity should never be below delivered quantity.
            if (toStatus == StockMoveRepository.STATUS_REALIZED) {
                saleOrderLine.setRequestedReservedQty(saleOrderLine.getRequestedReservedQty().max(saleOrderLine.getDeliveredQty()));
            } else if (!saleOrderLine.getIsQtyRequested()) {
                // if we cancel and do not want to request quantity, the requested quantity become the new
                // delivered quantity.
                saleOrderLine.setRequestedReservedQty(saleOrderLine.getDeliveredQty());
            }
        }
    } else {
        BigDecimal requestedReservedQtyInLocation = convertUnitWithProduct(stockMoveLineUnit, stockLocationLine.getUnit(), requestedReservedQty, product);
        realReservedQty = computeRealReservedQty(stockLocationLine, requestedReservedQtyInLocation);
        // convert back the quantity for the stock move line
        realReservedStockMoveQty = convertUnitWithProduct(stockLocationLineUnit, stockMoveLineUnit, realReservedQty, stockMoveLine.getProduct());
        updateReservedQuantityFromStockMoveLine(stockMoveLine, product, realReservedStockMoveQty);
        // reallocate quantity in other stock move lines
        if (supplychainConfigService.getSupplyChainConfig(stockLocation.getCompany()).getAutoAllocateOnAllocation()) {
            BigDecimal availableQuantityInLocation = stockLocationLine.getCurrentQty().subtract(stockLocationLine.getReservedQty());
            availableQuantityInLocation = convertUnitWithProduct(stockLocationLineUnit, stockMoveLineUnit, availableQuantityInLocation, product);
            BigDecimal qtyRemainingToAllocate = availableQuantityInLocation.subtract(realReservedStockMoveQty);
            reallocateQty(stockMoveLine, stockLocation, stockLocationLine, product, qtyRemainingToAllocate);
        }
    }
    updateReservedQty(stockLocationLine);
    updateRequestedReservedQty(stockLocationLine);
    checkReservedQtyStocks(stockLocationLine, stockMoveLine, toStatus);
}
Also used : StockLocationLine(com.axelor.apps.stock.db.StockLocationLine) Unit(com.axelor.apps.base.db.Unit) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) BigDecimal(java.math.BigDecimal)

Aggregations

StockLocationLine (com.axelor.apps.stock.db.StockLocationLine)42 BigDecimal (java.math.BigDecimal)25 Product (com.axelor.apps.base.db.Product)13 Transactional (com.google.inject.persist.Transactional)10 StockLocation (com.axelor.apps.stock.db.StockLocation)8 AxelorException (com.axelor.exception.AxelorException)8 Unit (com.axelor.apps.base.db.Unit)7 StockMoveLine (com.axelor.apps.stock.db.StockMoveLine)7 TrackingNumber (com.axelor.apps.stock.db.TrackingNumber)6 InventoryLine (com.axelor.apps.stock.db.InventoryLine)4 StockLocationLineRepository (com.axelor.apps.stock.db.repo.StockLocationLineRepository)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Company (com.axelor.apps.base.db.Company)3 StockMove (com.axelor.apps.stock.db.StockMove)3 Beans (com.axelor.inject.Beans)3 UnitConversionService (com.axelor.apps.base.service.UnitConversionService)2 AppBaseService (com.axelor.apps.base.service.app.AppBaseService)2 SaleOrderLine (com.axelor.apps.sale.db.SaleOrderLine)2 Inventory (com.axelor.apps.stock.db.Inventory)2