Search in sources :

Example 1 with Unit

use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.

the class StockMoveLineServiceImpl method computeNewAveragePriceLocationLine.

protected void computeNewAveragePriceLocationLine(StockLocationLine stockLocationLine, StockMoveLine stockMoveLine) throws AxelorException {
    BigDecimal oldAvgPrice = stockLocationLine.getAvgPrice();
    // avgPrice in stock move line is a bigdecimal but is nullable.
    BigDecimal newQty = stockMoveLine.getRealQty();
    BigDecimal oldQty = stockLocationLine.getCurrentQty().subtract(newQty);
    BigDecimal newPrice = stockMoveLine.getWapPrice() != null ? stockMoveLine.getWapPrice() : stockMoveLine.getCompanyUnitPriceUntaxed();
    BigDecimal newAvgPrice;
    if (oldAvgPrice == null || oldQty == null || oldAvgPrice.compareTo(BigDecimal.ZERO) == 0 || oldQty.compareTo(BigDecimal.ZERO) == 0) {
        oldAvgPrice = BigDecimal.ZERO;
        oldQty = BigDecimal.ZERO;
    }
    Unit stockLocationLineUnit = stockLocationLine.getUnit();
    Unit stockMoveLineUnit = stockMoveLine.getUnit();
    if (stockLocationLineUnit != null && !stockLocationLineUnit.equals(stockMoveLineUnit)) {
        newQty = unitConversionService.convert(stockMoveLineUnit, stockLocationLineUnit, newQty, newQty.scale(), stockMoveLine.getProduct());
        newPrice = unitConversionService.convert(stockMoveLineUnit, stockLocationLineUnit, newPrice, newPrice.scale(), stockMoveLine.getProduct());
    }
    log.debug("Old price: {}, Old quantity: {}, New price: {}, New quantity: {}", oldAvgPrice, oldQty, newPrice, newQty);
    BigDecimal sum = oldAvgPrice.multiply(oldQty);
    sum = sum.add(newPrice.multiply(newQty));
    BigDecimal denominator = oldQty.add(newQty);
    if (denominator.compareTo(BigDecimal.ZERO) != 0) {
        int scale = appBaseService.getNbDecimalDigitForUnitPrice();
        newAvgPrice = sum.divide(denominator, scale, RoundingMode.HALF_UP);
    } else {
        newAvgPrice = oldAvgPrice;
    }
    stockLocationLineService.updateWap(stockLocationLine, newAvgPrice, stockMoveLine);
}
Also used : Unit(com.axelor.apps.base.db.Unit) BigDecimal(java.math.BigDecimal)

Example 2 with Unit

use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.

the class StockLocationLineServiceImpl method updateLocationFromProduct.

@Override
public StockLocationLine updateLocationFromProduct(StockLocationLine stockLocationLine, Product product) throws AxelorException {
    Unit productUnit = product.getUnit();
    Unit stockLocationUnit = stockLocationLine.getUnit();
    if (productUnit != null && !productUnit.equals(stockLocationUnit)) {
        int scale = appBaseService.getNbDecimalDigitForUnitPrice();
        int qtyScale = appBaseService.getNbDecimalDigitForQty();
        BigDecimal oldQty = stockLocationLine.getCurrentQty();
        BigDecimal oldAvgPrice = stockLocationLine.getAvgPrice();
        BigDecimal currentQty = unitConversionService.convert(stockLocationUnit, productUnit, stockLocationLine.getCurrentQty(), stockLocationLine.getCurrentQty().scale(), product);
        stockLocationLine.setCurrentQty(currentQty);
        stockLocationLine.setUnit(product.getUnit());
        stockLocationLine.setFutureQty(computeFutureQty(stockLocationLine));
        BigDecimal avgQty = BigDecimal.ZERO;
        if (currentQty.compareTo(BigDecimal.ZERO) != 0) {
            avgQty = oldQty.divide(currentQty, qtyScale, RoundingMode.HALF_UP);
        }
        BigDecimal newAvgPrice = oldAvgPrice.multiply(avgQty);
        updateWap(stockLocationLine, newAvgPrice.setScale(scale, RoundingMode.HALF_UP));
    }
    return stockLocationLine;
}
Also used : Unit(com.axelor.apps.base.db.Unit) BigDecimal(java.math.BigDecimal)

Example 3 with Unit

use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.

the class StockLocationLineServiceImpl method updateLocation.

@Override
@Transactional(rollbackOn = { Exception.class })
public void updateLocation(StockLocation stockLocation, Product product, Unit stockMoveLineUnit, BigDecimal qty, boolean current, boolean future, boolean isIncrement, LocalDate lastFutureStockMoveDate) throws AxelorException {
    StockLocationLine stockLocationLine = this.getOrCreateStockLocationLine(stockLocation, product);
    if (stockLocationLine == null) {
        return;
    }
    Unit stockLocationLineUnit = stockLocationLine.getUnit();
    if (stockLocationLineUnit == null) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.LOCATION_LINE_MISSING_UNIT), stockLocation.getName(), product.getFullName());
    }
    if (!stockLocationLineUnit.equals(stockMoveLineUnit)) {
        qty = unitConversionService.convert(stockMoveLineUnit, stockLocationLineUnit, qty, qty.scale(), product);
    }
    LOG.debug("Mise à jour du stock : Entrepot? {}, Produit? {}, Quantité? {}, Actuel? {}, Futur? {}, Incrément? {}, Date? {} ", stockLocation.getName(), product.getCode(), qty, current, future, isIncrement, lastFutureStockMoveDate);
    if (!isIncrement) {
        minStockRules(product, qty, stockLocationLine, current, future);
    } else {
        maxStockRules(product, qty, stockLocationLine, current, future);
    }
    stockLocationLine = this.updateLocation(stockLocationLine, stockMoveLineUnit, product, qty, current, future, isIncrement, lastFutureStockMoveDate);
    this.checkStockMin(stockLocationLine, false);
    stockLocationLineRepo.save(stockLocationLine);
}
Also used : AxelorException(com.axelor.exception.AxelorException) StockLocationLine(com.axelor.apps.stock.db.StockLocationLine) Unit(com.axelor.apps.base.db.Unit) Transactional(com.google.inject.persist.Transactional)

Example 4 with Unit

use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.

the class StockLocationLineServiceImpl method updateDetailLocation.

@Override
@Transactional(rollbackOn = { Exception.class })
public void updateDetailLocation(StockLocation stockLocation, Product product, Unit stockMoveLineUnit, BigDecimal qty, boolean current, boolean future, boolean isIncrement, LocalDate lastFutureStockMoveDate, TrackingNumber trackingNumber) throws AxelorException {
    StockLocationLine detailLocationLine = this.getOrCreateDetailLocationLine(stockLocation, product, trackingNumber);
    if (detailLocationLine == null) {
        return;
    }
    Unit stockLocationLineUnit = detailLocationLine.getUnit();
    if (stockLocationLineUnit == null) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.DETAIL_LOCATION_LINE_MISSING_UNIT), trackingNumber.getTrackingNumberSeq(), stockLocation.getName(), product.getFullName());
    }
    if (!stockLocationLineUnit.equals(stockMoveLineUnit)) {
        qty = unitConversionService.convert(stockMoveLineUnit, stockLocationLineUnit, qty, qty.scale(), product);
    }
    LOG.debug("Mise à jour du detail du stock : Entrepot? {}, Produit? {}, Quantité? {}, Actuel? {}, Futur? {}, Incrément? {}, Date? {}, Num de suivi? {} ", stockLocation.getName(), product.getCode(), qty, current, future, isIncrement, lastFutureStockMoveDate, trackingNumber);
    detailLocationLine = this.updateLocation(detailLocationLine, stockMoveLineUnit, product, qty, current, future, isIncrement, lastFutureStockMoveDate);
    this.checkStockMin(detailLocationLine, true);
    stockLocationLineRepo.save(detailLocationLine);
}
Also used : AxelorException(com.axelor.exception.AxelorException) StockLocationLine(com.axelor.apps.stock.db.StockLocationLine) Unit(com.axelor.apps.base.db.Unit) Transactional(com.google.inject.persist.Transactional)

Example 5 with Unit

use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.

the class InvoiceLineGenerator method unit.

/**
 * Récupérer la bonne unité.
 *
 * @param unit Unité de base.
 * @param unitDisplay Unité à afficher.
 * @return L'unité à utiliser.
 */
protected Unit unit(Unit unit, Unit displayUnit) {
    Unit resUnit = unit;
    if (displayUnit != null) {
        resUnit = displayUnit;
    }
    LOG.debug("Obtention de l'unité : Unité {}, Unité affichée {} : {}", new Object[] { unit, displayUnit, resUnit });
    return resUnit;
}
Also used : Unit(com.axelor.apps.base.db.Unit)

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