Search in sources :

Example 91 with StockMoveLine

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

the class StockMoveInvoiceServiceImpl method getConsolidatedStockMoveLineList.

/**
 * Get a list of stock move lines consolidated by parent line (sale or purchase order).
 *
 * @param stockMoveLineList
 * @return
 * @throws AxelorException
 */
private List<StockMoveLine> getConsolidatedStockMoveLineList(List<StockMoveLine> stockMoveLineList) throws AxelorException {
    Map<SaleOrderLine, List<StockMoveLine>> stockMoveLineSaleMap = new LinkedHashMap<>();
    Map<PurchaseOrderLine, List<StockMoveLine>> stockMoveLinePurchaseMap = new LinkedHashMap<>();
    List<StockMoveLine> resultList = new ArrayList<>();
    List<StockMoveLine> list;
    for (StockMoveLine stockMoveLine : stockMoveLineList) {
        if (stockMoveLine.getSaleOrderLine() != null) {
            list = stockMoveLineSaleMap.get(stockMoveLine.getSaleOrderLine());
            if (list == null) {
                list = new ArrayList<>();
                stockMoveLineSaleMap.put(stockMoveLine.getSaleOrderLine(), list);
            }
            list.add(stockMoveLine);
        } else if (stockMoveLine.getPurchaseOrderLine() != null) {
            list = stockMoveLinePurchaseMap.get(stockMoveLine.getPurchaseOrderLine());
            if (list == null) {
                list = new ArrayList<>();
                stockMoveLinePurchaseMap.put(stockMoveLine.getPurchaseOrderLine(), list);
            }
            list.add(stockMoveLine);
        } else {
            // if the stock move line does not have a parent line (sale or purchase order line)
            resultList.add(stockMoveLine);
        }
    }
    for (List<StockMoveLine> stockMoveLines : stockMoveLineSaleMap.values()) {
        resultList.add(stockMoveLineServiceSupplychain.getMergedStockMoveLine(stockMoveLines));
    }
    for (List<StockMoveLine> stockMoveLines : stockMoveLinePurchaseMap.values()) {
        resultList.add(stockMoveLineServiceSupplychain.getMergedStockMoveLine(stockMoveLines));
    }
    return resultList;
}
Also used : PurchaseOrderLine(com.axelor.apps.purchase.db.PurchaseOrderLine) ArrayList(java.util.ArrayList) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) ArrayList(java.util.ArrayList) List(java.util.List) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) LinkedHashMap(java.util.LinkedHashMap)

Example 92 with StockMoveLine

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

the class StockMoveInvoiceServiceImpl method createInvoiceLines.

@Override
public List<InvoiceLine> createInvoiceLines(Invoice invoice, StockMove stockMove, List<StockMoveLine> stockMoveLineList, Map<Long, BigDecimal> qtyToInvoiceMap) throws AxelorException {
    List<InvoiceLine> invoiceLineList = new ArrayList<>();
    List<StockMoveLine> stockMoveLineToInvoiceList;
    if ((StockMoveRepository.ORIGIN_PURCHASE_ORDER.equals(stockMove.getOriginTypeSelect()) && supplyChainConfigService.getSupplyChainConfig(invoice.getCompany()).getActivateIncStockMovePartialInvoicing()) || (StockMoveRepository.ORIGIN_SALE_ORDER.equals(stockMove.getOriginTypeSelect()) && supplyChainConfigService.getSupplyChainConfig(invoice.getCompany()).getActivateOutStockMovePartialInvoicing())) {
        // we do not consolidate because the invoicing is partial
        stockMoveLineToInvoiceList = stockMoveLineList;
    } else {
        stockMoveLineToInvoiceList = getConsolidatedStockMoveLineList(stockMoveLineList);
    }
    for (StockMoveLine stockMoveLine : stockMoveLineToInvoiceList) {
        InvoiceLine invoiceLineCreated;
        Long id = stockMoveLine.getId();
        if (qtyToInvoiceMap != null) {
            invoiceLineCreated = this.createInvoiceLine(invoice, stockMoveLine, qtyToInvoiceMap.get(id));
        } else {
            invoiceLineCreated = this.createInvoiceLine(invoice, stockMoveLine, stockMoveLine.getRealQty().subtract(computeNonCanceledInvoiceQty(stockMoveLine)));
        }
        if (invoiceLineCreated != null) {
            invoiceLineList.add(invoiceLineCreated);
        }
    }
    return invoiceLineList;
}
Also used : InvoiceLine(com.axelor.apps.account.db.InvoiceLine) ArrayList(java.util.ArrayList) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine)

Example 93 with StockMoveLine

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

the class StockMoveInvoiceServiceImpl method createInvoiceLine.

@Override
public InvoiceLine createInvoiceLine(Invoice invoice, StockMoveLine stockMoveLine, BigDecimal qty) throws AxelorException {
    Product product = stockMoveLine.getProduct();
    boolean isTitleLine = false;
    int sequence = InvoiceLineGenerator.DEFAULT_SEQUENCE;
    SaleOrderLine saleOrderLine = stockMoveLine.getSaleOrderLine();
    PurchaseOrderLine purchaseOrderLine = stockMoveLine.getPurchaseOrderLine();
    if (saleOrderLine != null) {
        sequence = saleOrderLine.getSequence();
    } else if (purchaseOrderLine != null) {
        if (purchaseOrderLine.getIsTitleLine()) {
            isTitleLine = true;
        }
        sequence = purchaseOrderLine.getSequence();
    }
    // do not create lines with no qties
    if ((qty == null || qty.signum() == 0 || stockMoveLine.getRealQty().signum() == 0) && !isTitleLine) {
        return null;
    }
    if (product == null && !isTitleLine) {
        throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.STOCK_MOVE_INVOICE_1), stockMoveLine.getStockMove().getStockMoveSeq());
    }
    InvoiceLineGenerator invoiceLineGenerator = new InvoiceLineGeneratorSupplyChain(invoice, product, stockMoveLine.getProductName(), stockMoveLine.getDescription(), qty, stockMoveLine.getUnit(), sequence, false, stockMoveLine.getSaleOrderLine(), stockMoveLine.getPurchaseOrderLine(), stockMoveLine) {

        @Override
        public List<InvoiceLine> creates() throws AxelorException {
            InvoiceLine invoiceLine = this.createInvoiceLine();
            List<InvoiceLine> invoiceLines = new ArrayList<>();
            invoiceLines.add(invoiceLine);
            return invoiceLines;
        }
    };
    List<InvoiceLine> invoiceLines = invoiceLineGenerator.creates();
    InvoiceLine invoiceLine = null;
    if (invoiceLines != null && !invoiceLines.isEmpty()) {
        invoiceLine = invoiceLines.get(0);
        if (!stockMoveLine.getIsMergedStockMoveLine()) {
            // not a consolidated line so we can set the reference.
            invoiceLine.setStockMoveLine(stockMoveLine);
        } else {
            // set the reference to a correct stock move line by following either the sale order line or
            // purchase order line. We cannot have a consolidated line without purchase order line or
            // sale order line reference
            StockMoveLine nonConsolidatedStockMoveLine = null;
            StockMove stockMove = stockMoveLine.getStockMove();
            if (saleOrderLine != null) {
                nonConsolidatedStockMoveLine = stockMoveLineRepository.all().filter("self.saleOrderLine.id = :saleOrderLineId " + "AND self.stockMove.id = :stockMoveId " + "AND self.id != :stockMoveLineId").bind("saleOrderLineId", saleOrderLine.getId()).bind("stockMoveId", stockMove.getId()).bind("stockMoveLineId", stockMoveLine.getId()).order("id").fetchOne();
            } else if (purchaseOrderLine != null) {
                nonConsolidatedStockMoveLine = stockMoveLineRepository.all().filter("self.purchaseOrderLine.id = :purchaseOrderLineId " + "AND self.stockMove.id = :stockMoveId " + "AND self.id != :stockMoveLineId").bind("purchaseOrderLineId", purchaseOrderLine.getId()).bind("stockMoveId", stockMove.getId()).bind("stockMoveLineId", stockMoveLine.getId()).order("id").fetchOne();
            }
            invoiceLine.setStockMoveLine(nonConsolidatedStockMoveLine);
            deleteConsolidatedStockMoveLine(stockMoveLine);
        }
    }
    return invoiceLine;
}
Also used : AxelorException(com.axelor.exception.AxelorException) StockMove(com.axelor.apps.stock.db.StockMove) InvoiceLine(com.axelor.apps.account.db.InvoiceLine) ArrayList(java.util.ArrayList) Product(com.axelor.apps.base.db.Product) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) InvoiceLineGenerator(com.axelor.apps.account.service.invoice.generator.InvoiceLineGenerator) PurchaseOrderLine(com.axelor.apps.purchase.db.PurchaseOrderLine) InvoiceLineGeneratorSupplyChain(com.axelor.apps.supplychain.service.invoice.generator.InvoiceLineGeneratorSupplyChain) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine)

Example 94 with StockMoveLine

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

the class StockMoveInvoiceServiceImpl method createInvoice.

@Override
@Transactional(rollbackOn = { Exception.class })
public Invoice createInvoice(StockMove stockMove, Integer operationSelect, List<Map<String, Object>> stockMoveLineListContext) throws AxelorException {
    Invoice invoice;
    Map<Long, BigDecimal> qtyToInvoiceMap = new HashMap<>();
    if (operationSelect == StockMoveRepository.INVOICE_PARTIALLY) {
        for (Map<String, Object> map : stockMoveLineListContext) {
            if (map.get("qtyToInvoice") != null) {
                BigDecimal qtyToInvoiceItem = new BigDecimal(map.get("qtyToInvoice").toString());
                BigDecimal remainingQty = new BigDecimal(map.get("remainingQty").toString());
                if (qtyToInvoiceItem.compareTo(BigDecimal.ZERO) != 0) {
                    if (qtyToInvoiceItem.compareTo(remainingQty) > 0) {
                        qtyToInvoiceItem = remainingQty;
                    }
                    Long stockMoveLineId = Long.parseLong(map.get("stockMoveLineId").toString());
                    StockMoveLine stockMoveLine = stockMoveLineRepository.find(stockMoveLineId);
                    qtyToInvoiceMap.put(stockMoveLine.getId(), qtyToInvoiceItem);
                }
            }
        }
    } else {
        qtyToInvoiceMap = null;
    }
    Long origin = stockMove.getOriginId();
    if (StockMoveRepository.ORIGIN_SALE_ORDER.equals(stockMove.getOriginTypeSelect())) {
        invoice = createInvoiceFromSaleOrder(stockMove, saleOrderRepo.find(origin), qtyToInvoiceMap);
    } else if (StockMoveRepository.ORIGIN_PURCHASE_ORDER.equals(stockMove.getOriginTypeSelect())) {
        invoice = createInvoiceFromPurchaseOrder(stockMove, purchaseOrderRepo.find(origin), qtyToInvoiceMap);
    } else {
        invoice = createInvoiceFromOrderlessStockMove(stockMove, qtyToInvoiceMap);
    }
    return invoice;
}
Also used : Invoice(com.axelor.apps.account.db.Invoice) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) BigDecimal(java.math.BigDecimal) Transactional(com.google.inject.persist.Transactional)

Example 95 with StockMoveLine

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

the class ManufOrderServiceImpl method updateProducedStockMoveFromManufOrder.

@Override
@Transactional(rollbackOn = { Exception.class })
public void updateProducedStockMoveFromManufOrder(ManufOrder manufOrder) throws AxelorException {
    List<StockMoveLine> producedStockMoveLineList = manufOrder.getProducedStockMoveLineList();
    ManufOrderStockMoveService manufOrderStockMoveService = Beans.get(ManufOrderStockMoveService.class);
    Optional<StockMove> stockMoveOpt = manufOrderStockMoveService.getPlannedStockMove(manufOrder.getOutStockMoveList());
    StockMove stockMove;
    if (stockMoveOpt.isPresent()) {
        stockMove = stockMoveOpt.get();
    } else {
        stockMove = manufOrderStockMoveService._createToProduceStockMove(manufOrder, manufOrder.getCompany());
        manufOrder.addOutStockMoveListItem(stockMove);
        Beans.get(StockMoveService.class).plan(stockMove);
    }
    updateStockMoveFromManufOrder(producedStockMoveLineList, stockMove);
}
Also used : StockMove(com.axelor.apps.stock.db.StockMove) StockMoveService(com.axelor.apps.stock.service.StockMoveService) OperationOrderStockMoveService(com.axelor.apps.production.service.operationorder.OperationOrderStockMoveService) StockMoveLine(com.axelor.apps.stock.db.StockMoveLine) Transactional(com.google.inject.persist.Transactional)

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