Search in sources :

Example 46 with PurchaseOrder

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

the class PurchaseOrderController method mergePurchaseOrder.

// Generate single purchase order from several
@SuppressWarnings({ "rawtypes", "unchecked" })
public void mergePurchaseOrder(ActionRequest request, ActionResponse response) {
    List<PurchaseOrder> purchaseOrderList = new ArrayList<>();
    List<Long> purchaseOrderIdList = new ArrayList<>();
    boolean fromPopup = false;
    if (request.getContext().get("purchaseOrderToMerge") != null) {
        if (request.getContext().get("purchaseOrderToMerge") instanceof List) {
            // No confirmation popup, purchase orders are content in a parameter list
            List<Map> purchaseOrderMap = (List<Map>) request.getContext().get("purchaseOrderToMerge");
            for (Map map : purchaseOrderMap) {
                purchaseOrderIdList.add(new Long((Integer) map.get("id")));
            }
        } else {
            // After confirmation popup, purchase order's id are in a string separated by
            // ","
            String purchaseOrderIdListStr = (String) request.getContext().get("purchaseOrderToMerge");
            for (String purchaseOrderId : purchaseOrderIdListStr.split(",")) {
                purchaseOrderIdList.add(new Long(purchaseOrderId));
            }
            fromPopup = true;
        }
    }
    // Check if currency, supplierPartner and company are the same for all selected
    // purchase orders
    Currency commonCurrency = null;
    Partner commonSupplierPartner = null;
    Company commonCompany = null;
    Partner commonContactPartner = null;
    TradingName commonTradingName = null;
    // Useful to determine if a difference exists between contact partners of all
    // purchase orders
    boolean existContactPartnerDiff = false;
    PriceList commonPriceList = null;
    // Useful to determine if a difference exists between price lists of all
    // purchase orders
    boolean existPriceListDiff = false;
    StockLocation commonLocation = null;
    // Useful to determine if a difference exists between stock locations of all
    // purchase orders
    boolean existLocationDiff = false;
    boolean allTradingNamesAreNull = true;
    PurchaseOrder purchaseOrderTemp;
    int count = 1;
    for (Long purchaseOrderId : purchaseOrderIdList) {
        purchaseOrderTemp = JPA.em().find(PurchaseOrder.class, purchaseOrderId);
        purchaseOrderList.add(purchaseOrderTemp);
        if (count == 1) {
            commonCurrency = purchaseOrderTemp.getCurrency();
            commonSupplierPartner = purchaseOrderTemp.getSupplierPartner();
            commonCompany = purchaseOrderTemp.getCompany();
            commonContactPartner = purchaseOrderTemp.getContactPartner();
            commonPriceList = purchaseOrderTemp.getPriceList();
            commonLocation = purchaseOrderTemp.getStockLocation();
            commonTradingName = purchaseOrderTemp.getTradingName();
            allTradingNamesAreNull = commonTradingName == null;
        } else {
            if (commonCurrency != null && !commonCurrency.equals(purchaseOrderTemp.getCurrency())) {
                commonCurrency = null;
            }
            if (commonSupplierPartner != null && !commonSupplierPartner.equals(purchaseOrderTemp.getSupplierPartner())) {
                commonSupplierPartner = null;
            }
            if (commonCompany != null && !commonCompany.equals(purchaseOrderTemp.getCompany())) {
                commonCompany = null;
            }
            if (!Objects.equals(commonTradingName, purchaseOrderTemp.getTradingName())) {
                commonTradingName = null;
                allTradingNamesAreNull = false;
            }
            if (commonContactPartner != null && !commonContactPartner.equals(purchaseOrderTemp.getContactPartner())) {
                commonContactPartner = null;
                existContactPartnerDiff = true;
            }
            if (commonPriceList != null && !commonPriceList.equals(purchaseOrderTemp.getPriceList())) {
                commonPriceList = null;
                existPriceListDiff = true;
            }
            if (commonLocation != null && !commonLocation.equals(purchaseOrderTemp.getStockLocation())) {
                commonLocation = null;
                existLocationDiff = true;
            }
        }
        count++;
    }
    StringBuilder fieldErrors = new StringBuilder();
    if (commonCurrency == null) {
        fieldErrors.append(I18n.get(com.axelor.apps.purchase.exception.IExceptionMessage.PURCHASE_ORDER_MERGE_ERROR_CURRENCY));
    }
    if (commonSupplierPartner == null) {
        if (fieldErrors.length() > 0) {
            fieldErrors.append("<br/>");
        }
        fieldErrors.append(I18n.get(com.axelor.apps.purchase.exception.IExceptionMessage.PURCHASE_ORDER_MERGE_ERROR_SUPPLIER_PARTNER));
    }
    if (commonCompany == null) {
        if (fieldErrors.length() > 0) {
            fieldErrors.append("<br/>");
        }
        fieldErrors.append(I18n.get(com.axelor.apps.purchase.exception.IExceptionMessage.PURCHASE_ORDER_MERGE_ERROR_COMPANY));
    }
    if (commonTradingName == null && !allTradingNamesAreNull) {
        fieldErrors.append(I18n.get(com.axelor.apps.purchase.exception.IExceptionMessage.PURCHASE_ORDER_MERGE_ERROR_TRADING_NAME));
    }
    if (fieldErrors.length() > 0) {
        response.setFlash(fieldErrors.toString());
        return;
    }
    // parameters
    if (request.getContext().get("priceList") != null) {
        commonPriceList = JPA.em().find(PriceList.class, new Long((Integer) ((Map) request.getContext().get("priceList")).get("id")));
    }
    if (request.getContext().get("contactPartner") != null) {
        commonContactPartner = JPA.em().find(Partner.class, new Long((Integer) ((Map) request.getContext().get("contactPartner")).get("id")));
    }
    if (request.getContext().get("stockLocation") != null) {
        commonLocation = JPA.em().find(StockLocation.class, new Long((Integer) ((Map) request.getContext().get("stockLocation")).get("id")));
    }
    if (!fromPopup && (existContactPartnerDiff || existPriceListDiff || existLocationDiff)) {
        // Need to display intermediate screen to select some values
        ActionViewBuilder confirmView = ActionView.define("Confirm merge purchase order").model(Wizard.class.getName()).add("form", "purchase-order-merge-confirm-form").param("popup", "true").param("show-toolbar", "false").param("show-confirm", "false").param("popup-save", "false").param("forceEdit", "true");
        if (existPriceListDiff) {
            confirmView.context("contextPriceListToCheck", "true");
        }
        if (existContactPartnerDiff) {
            confirmView.context("contextContactPartnerToCheck", "true");
            confirmView.context("contextPartnerId", commonSupplierPartner.getId().toString());
        }
        if (existLocationDiff) {
            confirmView.context("contextLocationToCheck", "true");
        }
        confirmView.context("purchaseOrderToMerge", Joiner.on(",").join(purchaseOrderIdList));
        response.setView(confirmView.map());
        return;
    }
    try {
        PurchaseOrder purchaseOrder = Beans.get(PurchaseOrderSupplychainService.class).mergePurchaseOrders(purchaseOrderList, commonCurrency, commonSupplierPartner, commonCompany, commonLocation, commonContactPartner, commonPriceList, commonTradingName);
        if (purchaseOrder != null) {
            // Open the generated purchase order in a new tab
            response.setView(ActionView.define("Purchase order").model(PurchaseOrder.class.getName()).add("grid", "purchase-order-grid").add("form", "purchase-order-form").param("search-filters", "purchase-order-filters").param("forceEdit", "true").context("_showRecord", String.valueOf(purchaseOrder.getId())).map());
            response.setCanClose(true);
        }
    } catch (Exception e) {
        response.setFlash(e.getLocalizedMessage());
    }
}
Also used : Company(com.axelor.apps.base.db.Company) StockLocation(com.axelor.apps.stock.db.StockLocation) ArrayList(java.util.ArrayList) PurchaseOrderSupplychainService(com.axelor.apps.supplychain.service.PurchaseOrderSupplychainService) ActionViewBuilder(com.axelor.meta.schema.actions.ActionView.ActionViewBuilder) AxelorException(com.axelor.exception.AxelorException) Currency(com.axelor.apps.base.db.Currency) PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder) ArrayList(java.util.ArrayList) List(java.util.List) PriceList(com.axelor.apps.base.db.PriceList) TradingName(com.axelor.apps.base.db.TradingName) Map(java.util.Map) Partner(com.axelor.apps.base.db.Partner) PriceList(com.axelor.apps.base.db.PriceList) Wizard(com.axelor.apps.base.db.Wizard)

Example 47 with PurchaseOrder

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

the class PurchaseOrderController method updateAmountToBeSpreadOverTheTimetable.

public void updateAmountToBeSpreadOverTheTimetable(ActionRequest request, ActionResponse response) {
    try {
        PurchaseOrder purchaseOrder = request.getContext().asType(PurchaseOrder.class);
        Beans.get(PurchaseOrderSupplychainService.class).updateAmountToBeSpreadOverTheTimetable(purchaseOrder);
        response.setValue("amountToBeSpreadOverTheTimetable", purchaseOrder.getAmountToBeSpreadOverTheTimetable());
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder) PurchaseOrderSupplychainService(com.axelor.apps.supplychain.service.PurchaseOrderSupplychainService) AxelorException(com.axelor.exception.AxelorException)

Example 48 with PurchaseOrder

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

the class PurchaseOrderController method applyToAllBudgetDistribution.

public void applyToAllBudgetDistribution(ActionRequest request, ActionResponse response) {
    try {
        PurchaseOrderSupplychainService purchaseOrderSupplychainService = Beans.get(PurchaseOrderSupplychainService.class);
        PurchaseOrder purchaseOrder = request.getContext().asType(PurchaseOrder.class);
        purchaseOrder = Beans.get(PurchaseOrderRepository.class).find(purchaseOrder.getId());
        AppBudget appBudget = Beans.get(AppBudgetRepository.class).all().fetchOne();
        if (appBudget.getManageMultiBudget()) {
            purchaseOrderSupplychainService.applyToallBudgetDistribution(purchaseOrder);
        } else {
            purchaseOrderSupplychainService.setPurchaseOrderLineBudget(purchaseOrder);
            response.setValue("purchaseOrderLineList", purchaseOrder.getPurchaseOrderLineList());
        }
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder) PurchaseOrderSupplychainService(com.axelor.apps.supplychain.service.PurchaseOrderSupplychainService) AppBudget(com.axelor.apps.base.db.AppBudget) AxelorException(com.axelor.exception.AxelorException)

Example 49 with PurchaseOrder

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

the class PurchaseOrderController method createShipmentCostLine.

public void createShipmentCostLine(ActionRequest request, ActionResponse response) {
    try {
        PurchaseOrder purchaseOrder = request.getContext().asType(PurchaseOrder.class);
        String message = Beans.get(PurchaseOrderSupplychainService.class).createShipmentCostLine(purchaseOrder);
        if (message != null) {
            response.setFlash(message);
        }
        response.setValues(purchaseOrder);
    } catch (Exception e) {
        TraceBackService.trace(response, e);
    }
}
Also used : PurchaseOrder(com.axelor.apps.purchase.db.PurchaseOrder) PurchaseOrderSupplychainService(com.axelor.apps.supplychain.service.PurchaseOrderSupplychainService) AxelorException(com.axelor.exception.AxelorException)

Example 50 with PurchaseOrder

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

the class PurchaseOrderController method updateEstimatedDelivDate.

public void updateEstimatedDelivDate(ActionRequest request, ActionResponse response) {
    PurchaseOrder purchaseOrder = request.getContext().asType(PurchaseOrder.class);
    List<PurchaseOrderLine> purchaseOrderLineList = purchaseOrder.getPurchaseOrderLineList();
    if (purchaseOrderLineList != null) {
        for (PurchaseOrderLine purchaseOrderLine : purchaseOrderLineList) {
            Integer receiptState = purchaseOrderLine.getReceiptState();
            if (receiptState != null && !receiptState.equals(PurchaseOrderLineRepository.RECEIPT_STATE_RECEIVED) && !receiptState.equals(PurchaseOrderLineRepository.RECEIPT_STATE_PARTIALLY_RECEIVED)) {
                purchaseOrderLine.setEstimatedDelivDate(purchaseOrder.getDeliveryDate());
            }
        }
    }
    response.setValue("purchaseOrderLineList", purchaseOrderLineList);
}
Also used : PurchaseOrderLine(com.axelor.apps.purchase.db.PurchaseOrderLine) 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