Search in sources :

Example 56 with StockLocation

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

the class StockLocationServiceImpl method getQty.

@Override
public BigDecimal getQty(Long productId, Long locationId, Long companyId, String qtyType) throws AxelorException {
    if (productId != null) {
        Product product = productRepo.find(productId);
        Unit productUnit = product.getUnit();
        UnitConversionService unitConversionService = Beans.get(UnitConversionService.class);
        int scale = Beans.get(AppBaseService.class).getNbDecimalDigitForQty();
        if (locationId == null || locationId == 0L) {
            List<StockLocation> stockLocations = getNonVirtualStockLocations(companyId);
            if (!stockLocations.isEmpty()) {
                BigDecimal qty = BigDecimal.ZERO;
                for (StockLocation stockLocation : stockLocations) {
                    StockLocationLine stockLocationLine = stockLocationLineService.getStockLocationLine(stockLocationRepo.find(stockLocation.getId()), productRepo.find(productId));
                    if (stockLocationLine != null) {
                        Unit stockLocationLineUnit = stockLocationLine.getUnit();
                        qty = qty.add(qtyType.equals("real") ? stockLocationLine.getCurrentQty() : stockLocationLine.getFutureQty());
                        if (productUnit != null && !productUnit.equals(stockLocationLineUnit)) {
                            qty = unitConversionService.convert(stockLocationLineUnit, productUnit, qty, qty.scale(), product);
                        }
                    }
                }
                return qty.setScale(scale, RoundingMode.HALF_UP);
            }
        } else {
            StockLocationLine stockLocationLine = stockLocationLineService.getStockLocationLine(stockLocationRepo.find(locationId), productRepo.find(productId));
            if (stockLocationLine != null) {
                Unit stockLocationLineUnit = stockLocationLine.getUnit();
                BigDecimal qty = BigDecimal.ZERO;
                qty = qtyType.equals("real") ? stockLocationLine.getCurrentQty() : stockLocationLine.getFutureQty();
                if (productUnit != null && !productUnit.equals(stockLocationLineUnit)) {
                    qty = unitConversionService.convert(stockLocationLineUnit, productUnit, qty, qty.scale(), product);
                }
                return qty.setScale(scale, RoundingMode.HALF_UP);
            }
        }
    }
    return BigDecimal.ZERO;
}
Also used : UnitConversionService(com.axelor.apps.base.service.UnitConversionService) AppBaseService(com.axelor.apps.base.service.app.AppBaseService) 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)

Aggregations

StockLocation (com.axelor.apps.stock.db.StockLocation)56 Company (com.axelor.apps.base.db.Company)19 BigDecimal (java.math.BigDecimal)18 Product (com.axelor.apps.base.db.Product)15 AxelorException (com.axelor.exception.AxelorException)13 Transactional (com.google.inject.persist.Transactional)13 StockMove (com.axelor.apps.stock.db.StockMove)12 StockLocationRepository (com.axelor.apps.stock.db.repo.StockLocationRepository)12 ArrayList (java.util.ArrayList)12 StockConfig (com.axelor.apps.stock.db.StockConfig)10 StockConfigProductionService (com.axelor.apps.production.service.config.StockConfigProductionService)9 StockMoveLine (com.axelor.apps.stock.db.StockMoveLine)8 StockLocationService (com.axelor.apps.stock.service.StockLocationService)8 List (java.util.List)8 Partner (com.axelor.apps.base.db.Partner)6 Unit (com.axelor.apps.base.db.Unit)6 StockLocationLine (com.axelor.apps.stock.db.StockLocationLine)6 LocalDate (java.time.LocalDate)6 UnitConversionService (com.axelor.apps.base.service.UnitConversionService)5 AppBaseService (com.axelor.apps.base.service.app.AppBaseService)5