Search in sources :

Example 41 with PurchaseOrder

use of com.axelor.apps.purchase.db.PurchaseOrder 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 42 with PurchaseOrder

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

the class MrpServiceImpl method generateProposals.

@Override
@Transactional(rollbackOn = { Exception.class })
public void generateProposals(Mrp mrp, boolean isProposalPerSupplier) throws AxelorException {
    Map<Pair<Partner, LocalDate>, PurchaseOrder> purchaseOrders = new HashMap<>();
    Map<Partner, PurchaseOrder> purchaseOrdersPerSupplier = new HashMap<>();
    List<MrpLine> mrpLineList = mrpLineRepository.all().filter("self.mrp.id = ?1 AND self.proposalToProcess = true", mrp.getId()).order("maturityDate").fetch();
    for (MrpLine mrpLine : mrpLineList) {
        if (!mrpLine.getProposalGenerated()) {
            mrpLineService.generateProposal(mrpLine, purchaseOrders, purchaseOrdersPerSupplier, isProposalPerSupplier);
            mrpLine.setProposalToProcess(false);
            mrpLineRepository.save(mrpLine);
        }
    }
}
Also used : HashMap(java.util.HashMap) PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder) MrpLine(com.axelor.apps.supplychain.db.MrpLine) Partner(com.axelor.apps.base.db.Partner) Pair(org.apache.commons.lang3.tuple.Pair) Transactional(com.google.inject.persist.Transactional)

Example 43 with PurchaseOrder

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

the class MrpServiceImpl method createPurchaseMrpLines.

@Transactional(rollbackOn = { Exception.class })
protected void createPurchaseMrpLines(Mrp mrp, PurchaseOrderLine purchaseOrderLine, MrpLineType purchaseOrderMrpLineType) throws AxelorException {
    PurchaseOrder purchaseOrder = purchaseOrderLine.getPurchaseOrder();
    LocalDate maturityDate = purchaseOrderLine.getEstimatedDelivDate();
    if (maturityDate == null) {
        maturityDate = purchaseOrder.getDeliveryDate();
    }
    if (maturityDate == null) {
        maturityDate = purchaseOrderLine.getDesiredDelivDate();
    }
    maturityDate = this.computeMaturityDate(maturityDate, purchaseOrderMrpLineType);
    if (this.isBeforeEndDate(maturityDate) || purchaseOrderMrpLineType.getIgnoreEndDate()) {
        Unit unit = purchaseOrderLine.getProduct().getUnit();
        BigDecimal qty = purchaseOrderLine.getQty().subtract(purchaseOrderLine.getReceivedQty());
        if (!unit.equals(purchaseOrderLine.getUnit())) {
            qty = Beans.get(UnitConversionService.class).convert(purchaseOrderLine.getUnit(), unit, qty, qty.scale(), purchaseOrderLine.getProduct());
        }
        MrpLine mrpLine = this.createMrpLine(mrp, purchaseOrderLine.getProduct(), purchaseOrderMrpLineType, qty, maturityDate, BigDecimal.ZERO, purchaseOrder.getStockLocation(), purchaseOrderLine);
        if (mrpLine != null) {
            mrpLine.setSupplierPartner(purchaseOrder.getSupplierPartner());
            mrpLineRepository.save(mrpLine);
        }
    }
}
Also used : PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder) MrpLine(com.axelor.apps.supplychain.db.MrpLine) Unit(com.axelor.apps.base.db.Unit) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) Transactional(com.google.inject.persist.Transactional)

Example 44 with PurchaseOrder

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

the class PurchaseOrderServiceSupplychainImpl method mergePurchaseOrders.

@Transactional(rollbackOn = { Exception.class })
@Override
public PurchaseOrder mergePurchaseOrders(List<PurchaseOrder> purchaseOrderList, Currency currency, Partner supplierPartner, Company company, StockLocation stockLocation, Partner contactPartner, PriceList priceList, TradingName tradingName) throws AxelorException {
    String numSeq = "";
    String externalRef = "";
    for (PurchaseOrder purchaseOrderLocal : purchaseOrderList) {
        if (!numSeq.isEmpty()) {
            numSeq += "-";
        }
        numSeq += purchaseOrderLocal.getPurchaseOrderSeq();
        if (!externalRef.isEmpty()) {
            externalRef += "|";
        }
        if (purchaseOrderLocal.getExternalReference() != null) {
            externalRef += purchaseOrderLocal.getExternalReference();
        }
    }
    PurchaseOrder purchaseOrderMerged = this.createPurchaseOrder(AuthUtils.getUser(), company, contactPartner, currency, null, numSeq, externalRef, stockLocation, appBaseService.getTodayDate(company), priceList, supplierPartner, tradingName);
    super.attachToNewPurchaseOrder(purchaseOrderList, purchaseOrderMerged);
    this.computePurchaseOrder(purchaseOrderMerged);
    purchaseOrderRepo.save(purchaseOrderMerged);
    super.removeOldPurchaseOrders(purchaseOrderList);
    return purchaseOrderMerged;
}
Also used : PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder) Transactional(com.google.inject.persist.Transactional)

Example 45 with PurchaseOrder

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

the class PurchaseOrderServiceSupplychainImpl method createPurchaseOrder.

@Override
public PurchaseOrder createPurchaseOrder(User buyerUser, Company company, Partner contactPartner, Currency currency, LocalDate deliveryDate, String internalReference, String externalReference, StockLocation stockLocation, LocalDate orderDate, PriceList priceList, Partner supplierPartner, TradingName tradingName) throws AxelorException {
    LOG.debug("Création d'une commande fournisseur : Société = {},  Reference externe = {}, Fournisseur = {}", company.getName(), externalReference, supplierPartner.getFullName());
    PurchaseOrder purchaseOrder = super.createPurchaseOrder(buyerUser, company, contactPartner, currency, deliveryDate, internalReference, externalReference, orderDate, priceList, supplierPartner, tradingName);
    purchaseOrder.setStockLocation(stockLocation);
    purchaseOrder.setPaymentMode(supplierPartner.getOutPaymentMode());
    purchaseOrder.setPaymentCondition(supplierPartner.getPaymentCondition());
    if (purchaseOrder.getPaymentMode() == null) {
        purchaseOrder.setPaymentMode(this.accountConfigService.getAccountConfig(company).getOutPaymentMode());
    }
    if (purchaseOrder.getPaymentCondition() == null) {
        purchaseOrder.setPaymentCondition(this.accountConfigService.getAccountConfig(company).getDefPaymentCondition());
    }
    purchaseOrder.setTradingName(tradingName);
    return purchaseOrder;
}
Also used : PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder)

Aggregations

PurchaseOrder (com.axelor.apps.purchase.db.PurchaseOrder)84 PurchaseOrderLine (com.axelor.apps.purchase.db.PurchaseOrderLine)26 AxelorException (com.axelor.exception.AxelorException)26 Transactional (com.google.inject.persist.Transactional)16 BigDecimal (java.math.BigDecimal)12 Company (com.axelor.apps.base.db.Company)11 Context (com.axelor.rpc.Context)10 Partner (com.axelor.apps.base.db.Partner)9 Product (com.axelor.apps.base.db.Product)9 ArrayList (java.util.ArrayList)9 Invoice (com.axelor.apps.account.db.Invoice)7 List (java.util.List)7 PurchaseOrderRepository (com.axelor.apps.purchase.db.repo.PurchaseOrderRepository)6 SaleOrder (com.axelor.apps.sale.db.SaleOrder)6 PurchaseOrderSupplychainService (com.axelor.apps.supplychain.service.PurchaseOrderSupplychainService)6 LocalDate (java.time.LocalDate)6 PurchaseOrderService (com.axelor.apps.purchase.service.PurchaseOrderService)5 StockMove (com.axelor.apps.stock.db.StockMove)5 Unit (com.axelor.apps.base.db.Unit)4 PurchaseOrderLineService (com.axelor.apps.purchase.service.PurchaseOrderLineService)4