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