Search in sources :

Example 1 with PurchaseOrderService

use of com.axelor.apps.purchase.service.PurchaseOrderService in project axelor-open-suite by axelor.

the class IntercoServiceImpl method generateIntercoPurchaseFromSale.

@Override
@Transactional(rollbackOn = { Exception.class })
public PurchaseOrder generateIntercoPurchaseFromSale(SaleOrder saleOrder) throws AxelorException {
    PurchaseOrderService purchaseOrderService = Beans.get(PurchaseOrderService.class);
    Company intercoCompany = findIntercoCompany(saleOrder.getClientPartner());
    // create purchase order
    PurchaseOrder purchaseOrder = new PurchaseOrder();
    purchaseOrder.setCompany(intercoCompany);
    purchaseOrder.setContactPartner(saleOrder.getContactPartner());
    purchaseOrder.setCurrency(saleOrder.getCurrency());
    purchaseOrder.setDeliveryDate(saleOrder.getDeliveryDate());
    purchaseOrder.setOrderDate(saleOrder.getCreationDate());
    purchaseOrder.setPriceList(saleOrder.getPriceList());
    purchaseOrder.setTradingName(saleOrder.getTradingName());
    purchaseOrder.setPurchaseOrderLineList(new ArrayList<>());
    purchaseOrder.setPrintingSettings(Beans.get(TradingNameService.class).getDefaultPrintingSettings(null, intercoCompany));
    purchaseOrder.setStatusSelect(PurchaseOrderRepository.STATUS_DRAFT);
    purchaseOrder.setSupplierPartner(saleOrder.getCompany().getPartner());
    purchaseOrder.setTradingName(saleOrder.getTradingName());
    // in ati
    purchaseOrder.setInAti(saleOrder.getInAti());
    // copy payments
    PaymentMode intercoPaymentMode = Beans.get(PaymentModeService.class).reverseInOut(saleOrder.getPaymentMode());
    purchaseOrder.setPaymentMode(intercoPaymentMode);
    purchaseOrder.setPaymentCondition(saleOrder.getPaymentCondition());
    // copy delivery info
    purchaseOrder.setDeliveryDate(saleOrder.getDeliveryDate());
    purchaseOrder.setStockLocation(Beans.get(StockLocationService.class).getDefaultReceiptStockLocation(intercoCompany));
    purchaseOrder.setShipmentMode(saleOrder.getShipmentMode());
    purchaseOrder.setFreightCarrierMode(saleOrder.getFreightCarrierMode());
    // copy timetable info
    purchaseOrder.setExpectedRealisationDate(saleOrder.getExpectedRealisationDate());
    purchaseOrder.setAmountToBeSpreadOverTheTimetable(saleOrder.getAmountToBeSpreadOverTheTimetable());
    // create lines
    List<SaleOrderLine> saleOrderLineList = saleOrder.getSaleOrderLineList();
    if (saleOrderLineList != null) {
        for (SaleOrderLine saleOrderLine : saleOrderLineList) {
            this.createIntercoPurchaseLineFromSaleLine(saleOrderLine, purchaseOrder);
        }
    }
    purchaseOrder.setPrintingSettings(intercoCompany.getPrintingSettings());
    // compute the purchase order
    purchaseOrderService.computePurchaseOrder(purchaseOrder);
    purchaseOrder.setCreatedByInterco(true);
    Beans.get(PurchaseOrderRepository.class).save(purchaseOrder);
    if (Beans.get(AppSupplychainService.class).getAppSupplychain().getIntercoPurchaseOrderCreateRequested()) {
        Beans.get(PurchaseOrderService.class).requestPurchaseOrder(purchaseOrder);
    }
    saleOrder.setExternalReference(purchaseOrder.getPurchaseOrderSeq());
    purchaseOrder.setExternalReference(saleOrder.getSaleOrderSeq());
    return purchaseOrder;
}
Also used : Company(com.axelor.apps.base.db.Company) PaymentModeService(com.axelor.apps.account.service.payment.PaymentModeService) PurchaseOrderService(com.axelor.apps.purchase.service.PurchaseOrderService) PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder) SaleOrderLine(com.axelor.apps.sale.db.SaleOrderLine) PurchaseOrderRepository(com.axelor.apps.purchase.db.repo.PurchaseOrderRepository) PaymentMode(com.axelor.apps.account.db.PaymentMode) Transactional(com.google.inject.persist.Transactional)

Aggregations

PaymentMode (com.axelor.apps.account.db.PaymentMode)1 PaymentModeService (com.axelor.apps.account.service.payment.PaymentModeService)1 Company (com.axelor.apps.base.db.Company)1 PurchaseOrder (com.axelor.apps.purchase.db.PurchaseOrder)1 PurchaseOrderRepository (com.axelor.apps.purchase.db.repo.PurchaseOrderRepository)1 PurchaseOrderService (com.axelor.apps.purchase.service.PurchaseOrderService)1 SaleOrderLine (com.axelor.apps.sale.db.SaleOrderLine)1 Transactional (com.google.inject.persist.Transactional)1