Search in sources :

Example 1 with UnitConversionService

use of com.axelor.apps.base.service.UnitConversionService 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 2 with UnitConversionService

use of com.axelor.apps.base.service.UnitConversionService 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 3 with UnitConversionService

use of com.axelor.apps.base.service.UnitConversionService in project axelor-open-suite by axelor.

the class ManufOrderWorkflowService method createPurchaseOrderLineProduction.

private void createPurchaseOrderLineProduction(OperationOrder operationOrder, PurchaseOrder purchaseOrder) throws AxelorException {
    UnitConversionService unitConversionService = Beans.get(UnitConversionService.class);
    PurchaseOrderLineService purchaseOrderLineService = Beans.get(PurchaseOrderLineService.class);
    PurchaseOrderLine purchaseOrderLine;
    BigDecimal quantity = BigDecimal.ONE;
    Unit startUnit = Beans.get(UnitRepository.class).all().filter("self.name = 'Hour' AND self.unitTypeSelect = 3").fetchOne();
    for (ProdHumanResource humanResource : operationOrder.getProdHumanResourceList()) {
        Product product = humanResource.getProduct();
        Unit purchaseUnit = product.getPurchasesUnit();
        if (purchaseUnit != null) {
            quantity = unitConversionService.convert(startUnit, purchaseUnit, new BigDecimal(humanResource.getDuration() / 3600), 0, humanResource.getProduct());
        }
        purchaseOrderLine = purchaseOrderLineService.createPurchaseOrderLine(purchaseOrder, product, null, null, quantity, purchaseUnit);
        purchaseOrder.getPurchaseOrderLineList().add(purchaseOrderLine);
    }
}
Also used : PurchaseOrderLine(com.axelor.apps.purchase.db.PurchaseOrderLine) UnitConversionService(com.axelor.apps.base.service.UnitConversionService) ProdHumanResource(com.axelor.apps.production.db.ProdHumanResource) UnitRepository(com.axelor.apps.base.db.repo.UnitRepository) Product(com.axelor.apps.base.db.Product) PurchaseOrderLineService(com.axelor.apps.purchase.service.PurchaseOrderLineService) ChronoUnit(java.time.temporal.ChronoUnit) Unit(com.axelor.apps.base.db.Unit) BigDecimal(java.math.BigDecimal)

Example 4 with UnitConversionService

use of com.axelor.apps.base.service.UnitConversionService 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

Product (com.axelor.apps.base.db.Product)4 Unit (com.axelor.apps.base.db.Unit)4 UnitConversionService (com.axelor.apps.base.service.UnitConversionService)4 BigDecimal (java.math.BigDecimal)4 StockLocation (com.axelor.apps.stock.db.StockLocation)2 StockLocationLine (com.axelor.apps.stock.db.StockLocationLine)2 UnitRepository (com.axelor.apps.base.db.repo.UnitRepository)1 AppBaseService (com.axelor.apps.base.service.app.AppBaseService)1 ProdHumanResource (com.axelor.apps.production.db.ProdHumanResource)1 PurchaseOrderLine (com.axelor.apps.purchase.db.PurchaseOrderLine)1 PurchaseOrderLineService (com.axelor.apps.purchase.service.PurchaseOrderLineService)1 StockConfig (com.axelor.apps.stock.db.StockConfig)1 StockMoveLine (com.axelor.apps.stock.db.StockMoveLine)1 AxelorException (com.axelor.exception.AxelorException)1 ChronoUnit (java.time.temporal.ChronoUnit)1