Search in sources :

Example 21 with Unit

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;
}
Also used : StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) Unit(com.axelor.apps.base.db.Unit) BigDecimal(java.math.BigDecimal)

Example 22 with Unit

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;
}
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 23 with Unit

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);
    }
}
Also used : AxelorException(com.axelor.exception.AxelorException) StockMove(com.axelor.apps.stock.db.StockMove) InvoiceLine(com.axelor.apps.account.db.InvoiceLine) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) Unit(com.axelor.apps.base.db.Unit) BigDecimal(java.math.BigDecimal)

Example 24 with Unit

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);
}
Also used : StockLocationLine(com.axelor.apps.stock.db.StockLocationLine) Unit(com.axelor.apps.base.db.Unit) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) BigDecimal(java.math.BigDecimal)

Example 25 with Unit

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);
}
Also used : PurchaseOrderLine(com.axelor.apps.purchase.db.PurchaseOrderLine) AxelorException(com.axelor.exception.AxelorException) Company(com.axelor.apps.base.db.Company) StockLocation(com.axelor.apps.stock.db.StockLocation) Product(com.axelor.apps.base.db.Product) PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder) Unit(com.axelor.apps.base.db.Unit) LocalDate(java.time.LocalDate) Partner(com.axelor.apps.base.db.Partner) BigDecimal(java.math.BigDecimal) Transactional(com.google.inject.persist.Transactional)

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