Search in sources :

Example 36 with StockMoveLine

use of com.axelor.apps.stock.db.StockMoveLine in project axelor-open-suite by axelor.

the class ReservedQtyServiceImpl method requestQty.

@Override
@Transactional(rollbackOn = { Exception.class })
public void requestQty(SaleOrderLine saleOrderLine) throws AxelorException {
    if (saleOrderLine.getProduct() == null || !saleOrderLine.getProduct().getStockManaged()) {
        return;
    }
    saleOrderLine.setIsQtyRequested(true);
    if (saleOrderLine.getQty().signum() < 0) {
        throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.SALE_ORDER_LINE_REQUEST_QTY_NEGATIVE));
    }
    StockMoveLine stockMoveLine = getPlannedStockMoveLine(saleOrderLine);
    if (stockMoveLine != null) {
        stockMoveLine.setIsQtyRequested(true);
    }
    this.updateRequestedReservedQty(saleOrderLine, saleOrderLine.getQty());
}
Also used : AxelorException(com.axelor.exception.AxelorException) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) Transactional(com.google.inject.persist.Transactional)

Example 37 with StockMoveLine

use of com.axelor.apps.stock.db.StockMoveLine in project axelor-open-suite by axelor.

the class ReservedQtyServiceImpl method updateReservedQty.

@Override
public void updateReservedQty(SaleOrderLine saleOrderLine) throws AxelorException {
    // compute from stock move lines
    List<StockMoveLine> stockMoveLineList = stockMoveLineRepository.all().filter("self.saleOrderLine.id = :saleOrderLineId " + "AND self.stockMove.statusSelect = :planned").bind("saleOrderLineId", saleOrderLine.getId()).bind("planned", StockMoveRepository.STATUS_PLANNED).fetch();
    BigDecimal reservedQty = BigDecimal.ZERO;
    for (StockMoveLine stockMoveLine : stockMoveLineList) {
        reservedQty = reservedQty.add(convertUnitWithProduct(stockMoveLine.getUnit(), saleOrderLine.getUnit(), stockMoveLine.getReservedQty(), saleOrderLine.getProduct()));
    }
    saleOrderLine.setReservedQty(reservedQty);
}
Also used : StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) BigDecimal(java.math.BigDecimal)

Example 38 with StockMoveLine

use of com.axelor.apps.stock.db.StockMoveLine in project axelor-open-suite by axelor.

the class ReservedQtyServiceImpl method updateRequestedReservedQuantityInStockMoveLines.

@Override
public BigDecimal updateRequestedReservedQuantityInStockMoveLines(SaleOrderLine saleOrderLine, Product product, BigDecimal newReservedQty) throws AxelorException {
    if (product == null || !product.getStockManaged()) {
        return BigDecimal.ZERO;
    }
    List<StockMoveLine> stockMoveLineList = getPlannedStockMoveLines(saleOrderLine);
    BigDecimal deliveredQty = saleOrderLine.getDeliveredQty();
    BigDecimal allocatedRequestedQty = newReservedQty.subtract(deliveredQty);
    if (allocatedRequestedQty.signum() < 0) {
        throw new AxelorException(TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.SALE_ORDER_LINE_REQUESTED_QTY_TOO_LOW));
    }
    for (StockMoveLine stockMoveLine : stockMoveLineList) {
        BigDecimal stockMoveRequestedQty = convertUnitWithProduct(saleOrderLine.getUnit(), stockMoveLine.getUnit(), allocatedRequestedQty, product);
        BigDecimal requestedQtyInStockMoveLine = stockMoveLine.getQty().min(stockMoveRequestedQty);
        stockMoveLine.setRequestedReservedQty(requestedQtyInStockMoveLine);
        BigDecimal saleOrderRequestedQtyInStockMoveLine = convertUnitWithProduct(stockMoveLine.getUnit(), saleOrderLine.getUnit(), requestedQtyInStockMoveLine, product);
        allocatedRequestedQty = allocatedRequestedQty.subtract(saleOrderRequestedQtyInStockMoveLine);
    }
    saleOrderLine.setRequestedReservedQty(newReservedQty.subtract(allocatedRequestedQty));
    return saleOrderLine.getRequestedReservedQty().subtract(deliveredQty);
}
Also used : AxelorException(com.axelor.exception.AxelorException) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) BigDecimal(java.math.BigDecimal)

Example 39 with StockMoveLine

use of com.axelor.apps.stock.db.StockMoveLine in project axelor-open-suite by axelor.

the class AccountingCutOffServiceImpl method getStockMoveLines.

public List<Long> getStockMoveLines(Batch batch) {
    int offset = 0;
    Boolean includeNotStockManagedProduct = batch.getSupplychainBatch().getIncludeNotStockManagedProduct();
    List<StockMoveLine> stockMoveLineList;
    List<Long> stockMoveLineIdList = new ArrayList<>();
    Query<StockMove> stockMoveQuery = stockMoverepository.all().filter(":batch MEMBER OF self.batchSet").bind("batch", batch);
    List<Long> stockMoveIdList = stockMoveQuery.select("id").fetch(0, 0).stream().map(m -> (Long) m.get("id")).collect(Collectors.toList());
    if (stockMoveIdList.isEmpty()) {
        stockMoveLineIdList.add(0L);
    } else {
        Query<StockMoveLine> stockMoveLineQuery = stockMoveLineRepository.all().filter("self.stockMove.id IN :stockMoveIdList").bind("stockMoveIdList", stockMoveIdList).order("id");
        while (!(stockMoveLineList = stockMoveLineQuery.fetch(FETCH_LIMIT, offset)).isEmpty()) {
            offset += stockMoveLineList.size();
            for (StockMoveLine stockMoveLine : stockMoveLineList) {
                Product product = stockMoveLine.getProduct();
                if (!checkStockMoveLine(stockMoveLine, product, includeNotStockManagedProduct)) {
                    stockMoveLineIdList.add(stockMoveLine.getId());
                }
            }
            JPA.clear();
        }
    }
    return stockMoveLineIdList;
}
Also used : TaxAccountService(com.axelor.apps.account.service.TaxAccountService) AppAccountService(com.axelor.apps.account.service.app.AppAccountService) Move(com.axelor.apps.account.db.Move) Inject(com.google.inject.Inject) Transactional(com.google.inject.persist.Transactional) BigDecimal(java.math.BigDecimal) SaleOrderRepository(com.axelor.apps.sale.db.repo.SaleOrderRepository) TaxLine(com.axelor.apps.account.db.TaxLine) MoveCreateService(com.axelor.apps.account.service.move.MoveCreateService) MoveToolService(com.axelor.apps.account.service.move.MoveToolService) MoveValidateService(com.axelor.apps.account.service.move.MoveValidateService) SaleOrder(com.axelor.apps.sale.db.SaleOrder) RoundingMode(java.math.RoundingMode) AnalyticMoveLineRepository(com.axelor.apps.account.db.repo.AnalyticMoveLineRepository) AccountManagementAccountService(com.axelor.apps.account.service.AccountManagementAccountService) InvoiceLineManagement(com.axelor.apps.account.service.invoice.generator.line.InvoiceLineManagement) FETCH_LIMIT(com.axelor.apps.base.service.administration.AbstractBatch.FETCH_LIMIT) StockMove(com.axelor.apps.stock.db.StockMove) StockMoveRepository(com.axelor.apps.stock.db.repo.StockMoveRepository) Collectors(java.util.stream.Collectors) Currency(com.axelor.apps.base.db.Currency) List(java.util.List) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) Product(com.axelor.apps.base.db.Product) PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder) AccountConfigSupplychainService(com.axelor.apps.supplychain.service.config.AccountConfigSupplychainService) LocalDate(java.time.LocalDate) Partner(com.axelor.apps.base.db.Partner) Company(com.axelor.apps.base.db.Company) Query(com.axelor.db.Query) PurchaseOrderLine(com.axelor.apps.purchase.db.PurchaseOrderLine) StockMoveLineRepository(com.axelor.apps.stock.db.repo.StockMoveLineRepository) AccountConfig(com.axelor.apps.account.db.AccountConfig) PurchaseOrderRepository(com.axelor.apps.purchase.db.repo.PurchaseOrderRepository) AnalyticMoveLineService(com.axelor.apps.account.service.AnalyticMoveLineService) ArrayList(java.util.ArrayList) AxelorException(com.axelor.exception.AxelorException) MoveLine(com.axelor.apps.account.db.MoveLine) UnitConversionService(com.axelor.apps.base.service.UnitConversionService) Batch(com.axelor.apps.base.db.Batch) AppAccountRepository(com.axelor.apps.base.db.repo.AppAccountRepository) MoveLineService(com.axelor.apps.account.service.move.MoveLineService) JPA(com.axelor.db.JPA) Iterator(java.util.Iterator) SupplychainBatchRepository(com.axelor.apps.supplychain.db.repo.SupplychainBatchRepository) ReconcileService(com.axelor.apps.account.service.ReconcileService) AnalyticDistributionTemplate(com.axelor.apps.account.db.AnalyticDistributionTemplate) Account(com.axelor.apps.account.db.Account) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) Comparator(java.util.Comparator) Collections(java.util.Collections) MoveRepository(com.axelor.apps.account.db.repo.MoveRepository) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine) Tax(com.axelor.apps.account.db.Tax) StockMove(com.axelor.apps.stock.db.StockMove) ArrayList(java.util.ArrayList) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) Product(com.axelor.apps.base.db.Product)

Example 40 with StockMoveLine

use of com.axelor.apps.stock.db.StockMoveLine in project axelor-open-suite by axelor.

the class AccountingCutOffServiceImpl method generateProductMoveLine.

protected MoveLine generateProductMoveLine(Move move, StockMoveLine stockMoveLine, String origin, boolean isPurchase, boolean recoveredTax, boolean ati, String moveDescription, boolean isReverse, LocalDate originDate) throws AxelorException {
    SaleOrderLine saleOrderLine = stockMoveLine.getSaleOrderLine();
    PurchaseOrderLine purchaseOrderLine = stockMoveLine.getPurchaseOrderLine();
    Company company = move.getCompany();
    LocalDate moveDate = move.getDate();
    Partner partner = move.getPartner();
    boolean isFixedAssets = false;
    BigDecimal amountInCurrency = null;
    BigDecimal totalQty = null;
    BigDecimal notInvoicedQty = null;
    if (isPurchase && purchaseOrderLine != null) {
        totalQty = purchaseOrderLine.getQty();
        notInvoicedQty = unitConversionService.convert(stockMoveLine.getUnit(), purchaseOrderLine.getUnit(), stockMoveLine.getRealQty().subtract(stockMoveLine.getQtyInvoiced()), stockMoveLine.getRealQty().scale(), purchaseOrderLine.getProduct());
        isFixedAssets = purchaseOrderLine.getFixedAssets();
        if (ati && !recoveredTax) {
            amountInCurrency = purchaseOrderLine.getInTaxTotal();
        } else {
            amountInCurrency = purchaseOrderLine.getExTaxTotal();
        }
    }
    if (!isPurchase && saleOrderLine != null) {
        totalQty = saleOrderLine.getQty();
        notInvoicedQty = unitConversionService.convert(stockMoveLine.getUnit(), saleOrderLine.getUnit(), stockMoveLine.getRealQty().subtract(stockMoveLine.getQtyInvoiced()), stockMoveLine.getRealQty().scale(), saleOrderLine.getProduct());
        if (ati) {
            amountInCurrency = saleOrderLine.getInTaxTotal();
        } else {
            amountInCurrency = saleOrderLine.getExTaxTotal();
        }
    }
    if (totalQty == null || BigDecimal.ZERO.compareTo(totalQty) == 0) {
        return null;
    }
    BigDecimal qtyRate = notInvoicedQty.divide(totalQty, 10, RoundingMode.HALF_UP);
    amountInCurrency = amountInCurrency.multiply(qtyRate).setScale(2, RoundingMode.HALF_UP);
    if (amountInCurrency == null || amountInCurrency.compareTo(BigDecimal.ZERO) == 0) {
        return null;
    }
    Product product = stockMoveLine.getProduct();
    Account account = accountManagementAccountService.getProductAccount(product, company, partner.getFiscalPosition(), isPurchase, isFixedAssets);
    boolean isDebit = false;
    if ((isPurchase && amountInCurrency.compareTo(BigDecimal.ZERO) == 1) || !isPurchase && amountInCurrency.compareTo(BigDecimal.ZERO) == -1) {
        isDebit = true;
    }
    if (isReverse) {
        isDebit = !isDebit;
    }
    MoveLine moveLine = moveLineService.createMoveLine(move, partner, account, amountInCurrency, isDebit, originDate, ++counter, origin, moveDescription);
    moveLine.setDate(moveDate);
    moveLine.setDueDate(moveDate);
    getAndComputeAnalyticDistribution(product, move, moveLine);
    move.addMoveLineListItem(moveLine);
    if (recoveredTax) {
        TaxLine taxLine = accountManagementAccountService.getTaxLine(originDate, product, company, partner.getFiscalPosition(), isPurchase);
        if (taxLine != null) {
            moveLine.setTaxLine(taxLine);
            moveLine.setTaxRate(taxLine.getValue());
            moveLine.setTaxCode(taxLine.getTax().getCode());
            if (taxLine.getValue().compareTo(BigDecimal.ZERO) != 0) {
                generateTaxMoveLine(move, moveLine, origin, isPurchase, isFixedAssets, moveDescription);
            }
        }
    }
    return moveLine;
}
Also used : PurchaseOrderLine(com.axelor.apps.purchase.db.PurchaseOrderLine) Account(com.axelor.apps.account.db.Account) Company(com.axelor.apps.base.db.Company) MoveLine(com.axelor.apps.account.db.MoveLine) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) AnalyticMoveLine(com.axelor.apps.account.db.AnalyticMoveLine) Product(com.axelor.apps.base.db.Product) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) LocalDate(java.time.LocalDate) Partner(com.axelor.apps.base.db.Partner) BigDecimal(java.math.BigDecimal) TaxLine(com.axelor.apps.account.db.TaxLine)

Aggregations

StockMoveLine (com.axelor.apps.stock.db.StockMoveLine)121 BigDecimal (java.math.BigDecimal)59 StockMove (com.axelor.apps.stock.db.StockMove)44 AxelorException (com.axelor.exception.AxelorException)41 Product (com.axelor.apps.base.db.Product)33 Transactional (com.google.inject.persist.Transactional)31 ArrayList (java.util.ArrayList)28 List (java.util.List)18 Company (com.axelor.apps.base.db.Company)16 StockMoveRepository (com.axelor.apps.stock.db.repo.StockMoveRepository)16 StockLocation (com.axelor.apps.stock.db.StockLocation)14 StockMoveLineService (com.axelor.apps.stock.service.StockMoveLineService)14 Unit (com.axelor.apps.base.db.Unit)13 Beans (com.axelor.inject.Beans)13 ProdProduct (com.axelor.apps.production.db.ProdProduct)12 StockMoveService (com.axelor.apps.stock.service.StockMoveService)12 I18n (com.axelor.i18n.I18n)12 Inject (com.google.inject.Inject)12 AppBaseService (com.axelor.apps.base.service.app.AppBaseService)10 SaleOrderLine (com.axelor.apps.sale.db.SaleOrderLine)10