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);
}
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;
}
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);
}
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);
}
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;
}
Aggregations