use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.
the class ProductionProductStockLocationServiceImpl method getBuildingQty.
protected BigDecimal getBuildingQty(Product product, Company company, StockLocation stockLocation) throws AxelorException {
if (product == null || product.getUnit() == null) {
return BigDecimal.ZERO;
}
Long companyId = 0L;
Long stockLocationId = 0L;
if (company != null) {
companyId = company.getId();
if (stockLocation != null) {
stockLocationId = stockLocation.getId();
}
}
String query = manufOrderService.getBuildingQtyForAProduct(product.getId(), companyId, stockLocationId);
List<StockMoveLine> stockMoveLineList = stockMoveLineRepository.all().filter(query).fetch();
BigDecimal sumBuildingQty = BigDecimal.ZERO;
if (!stockMoveLineList.isEmpty()) {
Unit unitConversion = product.getUnit();
for (StockMoveLine stockMoveLine : stockMoveLineList) {
BigDecimal productBuildingQty = stockMoveLine.getRealQty();
unitConversionService.convert(stockMoveLine.getUnit(), unitConversion, productBuildingQty, productBuildingQty.scale(), product);
sumBuildingQty = sumBuildingQty.add(productBuildingQty);
}
}
return sumBuildingQty;
}
use of com.axelor.apps.base.db.Unit 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.db.Unit in project axelor-open-suite by axelor.
the class WorkflowVentilationServiceSupplychainImpl method stockMoveProcess.
private void stockMoveProcess(Invoice invoice) throws AxelorException {
// update qty invoiced in stock move line
for (InvoiceLine invoiceLine : invoice.getInvoiceLineList()) {
StockMoveLine stockMoveLine = invoiceLine.getStockMoveLine();
if (stockMoveLine == null) {
continue;
}
if (isStockMoveInvoicingPartiallyActivated(invoice, stockMoveLine)) {
BigDecimal qty = stockMoveLine.getQtyInvoiced();
StockMove stockMove = stockMoveLine.getStockMove();
if (stockMoveInvoiceService.isInvoiceRefundingStockMove(stockMove, invoice)) {
qty = qty.subtract(invoiceLine.getQty());
} else {
qty = qty.add(invoiceLine.getQty());
}
Unit movUnit = stockMoveLine.getUnit(), invUnit = invoiceLine.getUnit();
try {
qty = unitConversionService.convert(invUnit, movUnit, qty, appBaseService.getNbDecimalDigitForQty(), null);
} catch (AxelorException e) {
throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.STOCK_MOVE_INVOICE_QTY_INVONVERTIBLE_UNIT) + "\n" + e.getMessage());
}
if (stockMoveLine.getRealQty().compareTo(qty) >= 0) {
stockMoveLine.setQtyInvoiced(qty);
} else {
throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.STOCK_MOVE_INVOICE_QTY_MAX));
}
} else {
// set qty invoiced to the maximum (or emptying it if refund) for all stock move lines
boolean invoiceIsRefund = stockMoveInvoiceService.isInvoiceRefundingStockMove(stockMoveLine.getStockMove(), invoice);
stockMoveLine.setQtyInvoiced(invoiceIsRefund ? BigDecimal.ZERO : stockMoveLine.getRealQty());
// search in sale/purchase order lines to set split stock move lines to invoiced.
if (stockMoveLine.getSaleOrderLine() != null) {
stockMoveLineRepository.all().filter("self.saleOrderLine.id = :saleOrderLineId AND self.stockMove.id = :stockMoveId").bind("saleOrderLineId", stockMoveLine.getSaleOrderLine().getId()).bind("stockMoveId", stockMoveLine.getStockMove().getId()).fetch().forEach(stockMvLine -> stockMvLine.setQtyInvoiced(invoiceIsRefund ? BigDecimal.ZERO : stockMvLine.getRealQty()));
}
if (stockMoveLine.getPurchaseOrderLine() != null) {
stockMoveLineRepository.all().filter("self.purchaseOrderLine.id = :purchaseOrderLineId AND self.stockMove.id = :stockMoveId").bind("purchaseOrderLineId", stockMoveLine.getPurchaseOrderLine().getId()).bind("stockMoveId", stockMoveLine.getStockMove().getId()).fetch().forEach(stockMvLine -> stockMvLine.setQtyInvoiced(invoiceIsRefund ? BigDecimal.ZERO : stockMvLine.getRealQty()));
}
}
}
// update stock moves invoicing status
for (StockMove stockMove : invoice.getStockMoveSet()) {
stockMoveInvoiceService.computeStockMoveInvoicingStatus(stockMove);
}
}
use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.
the class ReservedQtyServiceImpl method updateRequestedQuantityInFromStockLocation.
@Override
public void updateRequestedQuantityInFromStockLocation(StockMoveLine stockMoveLine, StockLocation stockLocation, Product product, int toStatus, BigDecimal requestedReservedQty) throws AxelorException {
if (product == null || !product.getStockManaged()) {
return;
}
Unit stockMoveLineUnit = stockMoveLine.getUnit();
StockLocationLine stockLocationLine = stockLocationLineService.getStockLocationLine(stockLocation, product);
if (stockLocationLine == null) {
return;
}
Unit stockLocationLineUnit = stockLocationLine.getUnit();
// the quantity that will be allocated in stock location line
BigDecimal realReservedQty;
// the quantity that will be allocated in stock move line
BigDecimal realReservedStockMoveQty;
// if we cancel, subtract the quantity using the previously allocated quantity.
if (toStatus == StockMoveRepository.STATUS_CANCELED || toStatus == StockMoveRepository.STATUS_REALIZED) {
realReservedStockMoveQty = stockMoveLine.getReservedQty();
// convert the quantity for stock location line
realReservedQty = convertUnitWithProduct(stockMoveLineUnit, stockLocationLineUnit, realReservedStockMoveQty, stockMoveLine.getProduct());
// reallocate quantity in other stock move lines
if (isReallocatingQtyOnCancel(stockMoveLine)) {
reallocateQty(stockMoveLine, stockLocation, stockLocationLine, product, realReservedQty);
}
// no more reserved qty in stock move and sale order lines
updateReservedQuantityFromStockMoveLine(stockMoveLine, product, stockMoveLine.getReservedQty().negate());
// update requested quantity in sale order line
SaleOrderLine saleOrderLine = stockMoveLine.getSaleOrderLine();
if (saleOrderLine != null) {
// requested quantity should never be below delivered quantity.
if (toStatus == StockMoveRepository.STATUS_REALIZED) {
saleOrderLine.setRequestedReservedQty(saleOrderLine.getRequestedReservedQty().max(saleOrderLine.getDeliveredQty()));
} else if (!saleOrderLine.getIsQtyRequested()) {
// if we cancel and do not want to request quantity, the requested quantity become the new
// delivered quantity.
saleOrderLine.setRequestedReservedQty(saleOrderLine.getDeliveredQty());
}
}
} else {
BigDecimal requestedReservedQtyInLocation = convertUnitWithProduct(stockMoveLineUnit, stockLocationLine.getUnit(), requestedReservedQty, product);
realReservedQty = computeRealReservedQty(stockLocationLine, requestedReservedQtyInLocation);
// convert back the quantity for the stock move line
realReservedStockMoveQty = convertUnitWithProduct(stockLocationLineUnit, stockMoveLineUnit, realReservedQty, stockMoveLine.getProduct());
updateReservedQuantityFromStockMoveLine(stockMoveLine, product, realReservedStockMoveQty);
// reallocate quantity in other stock move lines
if (supplychainConfigService.getSupplyChainConfig(stockLocation.getCompany()).getAutoAllocateOnAllocation()) {
BigDecimal availableQuantityInLocation = stockLocationLine.getCurrentQty().subtract(stockLocationLine.getReservedQty());
availableQuantityInLocation = convertUnitWithProduct(stockLocationLineUnit, stockMoveLineUnit, availableQuantityInLocation, product);
BigDecimal qtyRemainingToAllocate = availableQuantityInLocation.subtract(realReservedStockMoveQty);
reallocateQty(stockMoveLine, stockLocation, stockLocationLine, product, qtyRemainingToAllocate);
}
}
updateReservedQty(stockLocationLine);
updateRequestedReservedQty(stockLocationLine);
checkReservedQtyStocks(stockLocationLine, stockMoveLine, toStatus);
}
use of com.axelor.apps.base.db.Unit in project axelor-open-suite by axelor.
the class MrpLineServiceImpl method generatePurchaseProposal.
@Transactional(rollbackOn = { Exception.class })
protected void generatePurchaseProposal(MrpLine mrpLine, Map<Pair<Partner, LocalDate>, PurchaseOrder> purchaseOrders, Map<Partner, PurchaseOrder> purchaseOrdersPerSupplier, boolean isProposalsPerSupplier) throws AxelorException {
Product product = mrpLine.getProduct();
StockLocation stockLocation = mrpLine.getStockLocation();
LocalDate maturityDate = mrpLine.getMaturityDate();
Partner supplierPartner = mrpLine.getSupplierPartner();
if (supplierPartner == null) {
supplierPartner = product.getDefaultSupplierPartner();
if (supplierPartner == null) {
throw new AxelorException(mrpLine, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.MRP_LINE_1), product.getFullName());
}
}
Company company = stockLocation.getCompany();
Pair<Partner, LocalDate> key = null;
PurchaseOrder purchaseOrder = null;
if (isProposalsPerSupplier) {
if (purchaseOrdersPerSupplier != null) {
purchaseOrder = purchaseOrdersPerSupplier.get(supplierPartner);
}
} else {
if (purchaseOrders != null) {
key = Pair.of(supplierPartner, maturityDate);
purchaseOrder = purchaseOrders.get(key);
}
}
if (purchaseOrder == null) {
purchaseOrder = purchaseOrderRepo.save(purchaseOrderSupplychainService.createPurchaseOrder(AuthUtils.getUser(), company, null, supplierPartner.getCurrency(), maturityDate, this.getPurchaseOrderOrigin(mrpLine), null, stockLocation, appBaseService.getTodayDate(company), Beans.get(PartnerPriceListService.class).getDefaultPriceList(supplierPartner, PriceListRepository.TYPE_PURCHASE), supplierPartner, null));
if (isProposalsPerSupplier) {
if (purchaseOrdersPerSupplier != null) {
purchaseOrdersPerSupplier.put(supplierPartner, purchaseOrder);
}
} else {
if (purchaseOrders != null) {
purchaseOrders.put(key, purchaseOrder);
}
}
if (mrpLine.getMrpLineOriginList().size() == 1) {
if (mrpLine.getMrpLineOriginList().get(0).getRelatedToSelect().equals(MrpLineOriginRepository.RELATED_TO_SALE_ORDER_LINE)) {
purchaseOrder.setGeneratedSaleOrderId(saleOrderLineRepo.find(mrpLine.getMrpLineOriginList().get(0).getRelatedToSelectId()).getSaleOrder().getId());
}
}
}
Unit unit = product.getPurchasesUnit();
BigDecimal qty = mrpLine.getQty();
if (unit == null) {
unit = product.getUnit();
} else {
qty = Beans.get(UnitConversionService.class).convert(product.getUnit(), unit, qty, qty.scale(), product);
}
PurchaseOrderLine poLine = purchaseOrderLineService.createPurchaseOrderLine(purchaseOrder, product, null, null, qty, unit);
poLine.setDesiredDelivDate(maturityDate);
purchaseOrder.addPurchaseOrderLineListItem(poLine);
purchaseOrderService.computePurchaseOrder(purchaseOrder);
linkToOrder(mrpLine, purchaseOrder);
}
Aggregations