Search in sources :

Example 36 with PurchaseOrderLine

use of com.axelor.apps.purchase.db.PurchaseOrderLine 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)

Example 37 with PurchaseOrderLine

use of com.axelor.apps.purchase.db.PurchaseOrderLine in project axelor-open-suite by axelor.

the class MrpServiceImpl method createPurchaseMrpLines.

protected void createPurchaseMrpLines() throws AxelorException {
    MrpLineType purchaseOrderMrpLineType = this.getMrpLineType(MrpLineTypeRepository.ELEMENT_PURCHASE_ORDER);
    if (purchaseOrderMrpLineType == null) {
        return;
    }
    String statusSelect = purchaseOrderMrpLineType.getStatusSelect();
    List<Integer> statusList = StringTool.getIntegerList(statusSelect);
    if (statusList.isEmpty()) {
        statusList.add(PurchaseOrderRepository.STATUS_VALIDATED);
    }
    // TODO : Manage the case where order is partially delivered
    List<PurchaseOrderLine> purchaseOrderLineList = purchaseOrderLineRepository.all().filter("self.product.id in (?1) AND self.purchaseOrder.stockLocation in (?2) AND self.receiptState != ?3 " + "AND self.purchaseOrder.statusSelect IN (?4)", this.productMap.keySet(), this.stockLocationList, PurchaseOrderRepository.STATE_RECEIVED, statusList).fetch();
    for (PurchaseOrderLine purchaseOrderLine : purchaseOrderLineList) {
        this.createPurchaseMrpLines(mrpRepository.find(mrp.getId()), purchaseOrderLineRepository.find(purchaseOrderLine.getId()), mrpLineTypeRepository.find(purchaseOrderMrpLineType.getId()));
        JPA.clear();
    }
}
Also used : PurchaseOrderLine(com.axelor.apps.purchase.db.PurchaseOrderLine) MrpLineType(com.axelor.apps.supplychain.db.MrpLineType)

Example 38 with PurchaseOrderLine

use of com.axelor.apps.purchase.db.PurchaseOrderLine in project axelor-open-suite by axelor.

the class PurchaseOrderInvoiceServiceImpl method generateInvoiceFromLines.

public Invoice generateInvoiceFromLines(PurchaseOrder purchaseOrder, BigDecimal percentSum) throws AxelorException {
    if (percentSum.equals(BigDecimal.ZERO)) {
        throw new AxelorException(purchaseOrder, TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.SO_INVOICE_NO_LINES_SELECTED));
    }
    Map<Long, BigDecimal> qtyToInvoiceMap = new HashMap<>();
    for (PurchaseOrderLine purchaseOrderLine : purchaseOrder.getPurchaseOrderLineList()) {
        BigDecimal realQty = purchaseOrderLine.getQty().multiply(percentSum).divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
        qtyToInvoiceMap.put(purchaseOrderLine.getId(), realQty);
        if (qtyToInvoiceMap.get(purchaseOrderLine.getId()).compareTo(purchaseOrderLine.getQty()) > 0) {
            throw new AxelorException(purchaseOrderLine, TraceBackRepository.CATEGORY_INCONSISTENCY, I18n.get(IExceptionMessage.PO_INVOICE_QTY_MAX));
        }
    }
    return this.generateInvoice(purchaseOrder, purchaseOrder.getPurchaseOrderLineList(), qtyToInvoiceMap);
}
Also used : PurchaseOrderLine(com.axelor.apps.purchase.db.PurchaseOrderLine) AxelorException(com.axelor.exception.AxelorException) HashMap(java.util.HashMap) BigDecimal(java.math.BigDecimal)

Example 39 with PurchaseOrderLine

use of com.axelor.apps.purchase.db.PurchaseOrderLine in project axelor-open-suite by axelor.

the class PurchaseOrderServiceSupplychainImpl method computeExTaxTotalWithoutShippingLines.

@Override
public BigDecimal computeExTaxTotalWithoutShippingLines(PurchaseOrder purchaseOrder) {
    List<PurchaseOrderLine> purchaseOrderLines = purchaseOrder.getPurchaseOrderLineList();
    if (purchaseOrderLines == null) {
        return BigDecimal.ZERO;
    }
    BigDecimal exTaxTotal = BigDecimal.ZERO;
    for (PurchaseOrderLine purchaseOrderLine : purchaseOrderLines) {
        if (!purchaseOrderLine.getProduct().getIsShippingCostsProduct()) {
            exTaxTotal = exTaxTotal.add(purchaseOrderLine.getExTaxTotal());
        }
    }
    return exTaxTotal;
}
Also used : PurchaseOrderLine(com.axelor.apps.purchase.db.PurchaseOrderLine) BigDecimal(java.math.BigDecimal)

Example 40 with PurchaseOrderLine

use of com.axelor.apps.purchase.db.PurchaseOrderLine in project axelor-open-suite by axelor.

the class PurchaseOrderServiceSupplychainImpl method generateBudgetDistribution.

@Transactional
@Override
public void generateBudgetDistribution(PurchaseOrder purchaseOrder) {
    if (purchaseOrder.getPurchaseOrderLineList() != null) {
        for (PurchaseOrderLine purchaseOrderLine : purchaseOrder.getPurchaseOrderLineList()) {
            if (purchaseOrderLine.getBudget() != null && (purchaseOrderLine.getBudgetDistributionList() == null || purchaseOrderLine.getBudgetDistributionList().isEmpty())) {
                BudgetDistribution budgetDistribution = new BudgetDistribution();
                budgetDistribution.setBudget(purchaseOrderLine.getBudget());
                budgetDistribution.setAmount(purchaseOrderLine.getCompanyExTaxTotal());
                purchaseOrderLine.addBudgetDistributionListItem(budgetDistribution);
            }
        }
    // purchaseOrderRepo.save(purchaseOrder);
    }
}
Also used : PurchaseOrderLine(com.axelor.apps.purchase.db.PurchaseOrderLine) BudgetDistribution(com.axelor.apps.account.db.BudgetDistribution) Transactional(com.google.inject.persist.Transactional)

Aggregations

PurchaseOrderLine (com.axelor.apps.purchase.db.PurchaseOrderLine)76 BigDecimal (java.math.BigDecimal)28 PurchaseOrder (com.axelor.apps.purchase.db.PurchaseOrder)26 Transactional (com.google.inject.persist.Transactional)18 Product (com.axelor.apps.base.db.Product)14 ArrayList (java.util.ArrayList)13 AxelorException (com.axelor.exception.AxelorException)11 Context (com.axelor.rpc.Context)10 Company (com.axelor.apps.base.db.Company)9 Unit (com.axelor.apps.base.db.Unit)9 SaleOrderLine (com.axelor.apps.sale.db.SaleOrderLine)9 PurchaseOrderLineService (com.axelor.apps.purchase.service.PurchaseOrderLineService)8 LocalDate (java.time.LocalDate)8 Partner (com.axelor.apps.base.db.Partner)7 StockMoveLine (com.axelor.apps.stock.db.StockMoveLine)7 HashMap (java.util.HashMap)7 BudgetDistribution (com.axelor.apps.account.db.BudgetDistribution)5 InvoiceLine (com.axelor.apps.account.db.InvoiceLine)5 TaxLine (com.axelor.apps.account.db.TaxLine)5 List (java.util.List)5