Search in sources :

Example 26 with Currency

use of com.axelor.apps.base.db.Currency in project axelor-open-suite by axelor.

the class SaleOrderController method mergeSaleOrder.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void mergeSaleOrder(ActionRequest request, ActionResponse response) {
    List<SaleOrder> saleOrderList = new ArrayList<SaleOrder>();
    List<Long> saleOrderIdList = new ArrayList<Long>();
    boolean fromPopup = false;
    String lineToMerge;
    if (request.getContext().get("saleQuotationToMerge") != null) {
        lineToMerge = "saleQuotationToMerge";
    } else {
        lineToMerge = "saleOrderToMerge";
    }
    if (request.getContext().get(lineToMerge) != null) {
        if (request.getContext().get(lineToMerge) instanceof List) {
            // No confirmation popup, sale orders are content in a parameter list
            List<Map> saleOrderMap = (List<Map>) request.getContext().get(lineToMerge);
            for (Map map : saleOrderMap) {
                saleOrderIdList.add(new Long((Integer) map.get("id")));
            }
        } else {
            // After confirmation popup, sale order's id are in a string separated by ","
            String saleOrderIdListStr = (String) request.getContext().get(lineToMerge);
            for (String saleOrderId : saleOrderIdListStr.split(",")) {
                saleOrderIdList.add(new Long(saleOrderId));
            }
            fromPopup = true;
        }
    }
    // Check if currency, clientPartner and company are the same for all selected sale orders
    Currency commonCurrency = null;
    Partner commonClientPartner = null;
    Company commonCompany = null;
    Partner commonContactPartner = null;
    Team commonTeam = null;
    // Useful to determine if a difference exists between teams of all sale orders
    boolean existTeamDiff = false;
    // Useful to determine if a difference exists between contact partners of all sale orders
    boolean existContactPartnerDiff = false;
    PriceList commonPriceList = null;
    // Useful to determine if a difference exists between price lists of all sale orders
    boolean existPriceListDiff = false;
    SaleOrder saleOrderTemp;
    int count = 1;
    for (Long saleOrderId : saleOrderIdList) {
        saleOrderTemp = JPA.em().find(SaleOrder.class, saleOrderId);
        saleOrderList.add(saleOrderTemp);
        if (count == 1) {
            commonCurrency = saleOrderTemp.getCurrency();
            commonClientPartner = saleOrderTemp.getClientPartner();
            commonCompany = saleOrderTemp.getCompany();
            commonContactPartner = saleOrderTemp.getContactPartner();
            commonTeam = saleOrderTemp.getTeam();
            commonPriceList = saleOrderTemp.getPriceList();
        } else {
            if (commonCurrency != null && !commonCurrency.equals(saleOrderTemp.getCurrency())) {
                commonCurrency = null;
            }
            if (commonClientPartner != null && !commonClientPartner.equals(saleOrderTemp.getClientPartner())) {
                commonClientPartner = null;
            }
            if (commonCompany != null && !commonCompany.equals(saleOrderTemp.getCompany())) {
                commonCompany = null;
            }
            if (commonContactPartner != null && !commonContactPartner.equals(saleOrderTemp.getContactPartner())) {
                commonContactPartner = null;
                existContactPartnerDiff = true;
            }
            if (commonTeam != null && !commonTeam.equals(saleOrderTemp.getTeam())) {
                commonTeam = null;
                existTeamDiff = true;
            }
            if (commonPriceList != null && !commonPriceList.equals(saleOrderTemp.getPriceList())) {
                commonPriceList = null;
                existPriceListDiff = true;
            }
        }
        count++;
    }
    StringBuilder fieldErrors = new StringBuilder();
    if (commonCurrency == null) {
        fieldErrors.append(I18n.get(IExceptionMessage.SALE_ORDER_MERGE_ERROR_CURRENCY));
    }
    if (commonClientPartner == null) {
        if (fieldErrors.length() > 0) {
            fieldErrors.append("<br/>");
        }
        fieldErrors.append(I18n.get(IExceptionMessage.SALE_ORDER_MERGE_ERROR_CLIENT_PARTNER));
    }
    if (commonCompany == null) {
        if (fieldErrors.length() > 0) {
            fieldErrors.append("<br/>");
        }
        fieldErrors.append(I18n.get(IExceptionMessage.SALE_ORDER_MERGE_ERROR_COMPANY));
    }
    if (fieldErrors.length() > 0) {
        response.setFlash(fieldErrors.toString());
        return;
    }
    // Check if priceList or contactPartner are content in 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("team") != null) {
        commonTeam = JPA.em().find(Team.class, new Long((Integer) ((Map) request.getContext().get("team")).get("id")));
    }
    if (!fromPopup && (existContactPartnerDiff || existPriceListDiff || existTeamDiff)) {
        // Need to display intermediate screen to select some values
        ActionViewBuilder confirmView = ActionView.define("Confirm merge sale order").model(Wizard.class.getName()).add("form", "sale-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", commonClientPartner.getId().toString());
        }
        if (existTeamDiff) {
            confirmView.context("contextTeamToCheck", "true");
        }
        confirmView.context(lineToMerge, Joiner.on(",").join(saleOrderIdList));
        response.setView(confirmView.map());
        return;
    }
    try {
        SaleOrder saleOrder = Beans.get(SaleOrderCreateService.class).mergeSaleOrders(saleOrderList, commonCurrency, commonClientPartner, commonCompany, commonContactPartner, commonPriceList, commonTeam);
        if (saleOrder != null) {
            // Open the generated sale order in a new tab
            response.setView(ActionView.define("Sale order").model(SaleOrder.class.getName()).add("grid", "sale-order-grid").add("form", "sale-order-form").param("search-filters", "sale-order-filters").param("forceEdit", "true").context("_showRecord", String.valueOf(saleOrder.getId())).map());
            response.setCanClose(true);
        }
    } catch (Exception e) {
        response.setFlash(e.getLocalizedMessage());
    }
}
Also used : SaleOrderCreateService(com.axelor.apps.sale.service.saleorder.SaleOrderCreateService) Company(com.axelor.apps.base.db.Company) ArrayList(java.util.ArrayList) SaleOrder(com.axelor.apps.sale.db.SaleOrder) ActionViewBuilder(com.axelor.meta.schema.actions.ActionView.ActionViewBuilder) BirtException(org.eclipse.birt.core.exception.BirtException) AxelorException(com.axelor.exception.AxelorException) IOException(java.io.IOException) Currency(com.axelor.apps.base.db.Currency) List(java.util.List) PriceList(com.axelor.apps.base.db.PriceList) ArrayList(java.util.ArrayList) Team(com.axelor.team.db.Team) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Partner(com.axelor.apps.base.db.Partner) PriceList(com.axelor.apps.base.db.PriceList) Wizard(com.axelor.apps.base.db.Wizard)

Example 27 with Currency

use of com.axelor.apps.base.db.Currency in project axelor-open-suite by axelor.

the class OpportunitySaleOrderServiceImpl method createSaleOrderFromOpportunity.

@Override
@Transactional(rollbackOn = { Exception.class })
public SaleOrder createSaleOrderFromOpportunity(Opportunity opportunity) throws AxelorException {
    Currency currency = null;
    Company company = opportunity.getCompany();
    if (opportunity.getCurrency() != null) {
        currency = opportunity.getCurrency();
    } else if (opportunity.getPartner() != null && opportunity.getPartner().getCurrency() != null) {
        currency = opportunity.getPartner().getCurrency();
    } else if (company != null) {
        currency = company.getCurrency();
    }
    SaleOrder saleOrder = createSaleOrder(opportunity, currency);
    opportunity.addSaleOrderListItem(saleOrder);
    if (opportunity.getSalesStageSelect() < OpportunityRepository.SALES_STAGE_PROPOSITION) {
        opportunity.setSalesStageSelect(OpportunityRepository.SALES_STAGE_PROPOSITION);
    }
    saleOrder.setTradingName(opportunity.getTradingName());
    saleOrderRepo.save(saleOrder);
    return saleOrder;
}
Also used : Company(com.axelor.apps.base.db.Company) Currency(com.axelor.apps.base.db.Currency) SaleOrder(com.axelor.apps.sale.db.SaleOrder) Transactional(com.google.inject.persist.Transactional)

Example 28 with Currency

use of com.axelor.apps.base.db.Currency 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 29 with Currency

use of com.axelor.apps.base.db.Currency in project axelor-open-suite by axelor.

the class SupplierCatalogServiceImpl method updateInfoFromCatalog.

@SuppressWarnings("unchecked")
@Override
public Map<String, Object> updateInfoFromCatalog(Product product, BigDecimal qty, Partner partner, Currency currency, LocalDate date, Company company) throws AxelorException {
    Map<String, Object> info = null;
    List<SupplierCatalog> supplierCatalogList = null;
    if (appPurchaseService.getAppPurchase().getManageSupplierCatalog()) {
        supplierCatalogList = (List<SupplierCatalog>) productCompanyService.get(product, "supplierCatalogList", company);
    }
    if (supplierCatalogList != null && !supplierCatalogList.isEmpty()) {
        SupplierCatalog supplierCatalog = Beans.get(SupplierCatalogRepository.class).all().filter("self.product = ?1 AND self.minQty <= ?2 AND self.supplierPartner = ?3 ORDER BY self.minQty DESC", product, qty, partner).fetchOne();
        if (supplierCatalog != null) {
            info = new HashMap<>();
            info.put("price", currencyService.getAmountCurrencyConvertedAtDate(supplierCatalog.getSupplierPartner().getCurrency(), currency, supplierCatalog.getPrice(), date).setScale(appBaseService.getNbDecimalDigitForUnitPrice(), RoundingMode.HALF_UP));
            info.put("productName", supplierCatalog.getProductSupplierName());
            info.put("productCode", supplierCatalog.getProductSupplierCode());
        } else if (this.getSupplierCatalog(product, partner, company) != null) {
            info = new HashMap<>();
            info.put("price", currencyService.getAmountCurrencyConvertedAtDate((Currency) productCompanyService.get(product, "purchaseCurrency", company), currency, (BigDecimal) productCompanyService.get(product, "purchasePrice", company), date).setScale(appBaseService.getNbDecimalDigitForUnitPrice(), RoundingMode.HALF_UP));
            info.put("productName", null);
            info.put("productCode", null);
        }
    }
    return info;
}
Also used : SupplierCatalogRepository(com.axelor.apps.purchase.db.repo.SupplierCatalogRepository) HashMap(java.util.HashMap) Currency(com.axelor.apps.base.db.Currency) SupplierCatalog(com.axelor.apps.purchase.db.SupplierCatalog) BigDecimal(java.math.BigDecimal)

Example 30 with Currency

use of com.axelor.apps.base.db.Currency in project axelor-open-suite by axelor.

the class InvoiceServiceSupplychainImpl method getDefaultAdvancePaymentInvoice.

@Override
public Set<Invoice> getDefaultAdvancePaymentInvoice(Invoice invoice) throws AxelorException {
    if (!Beans.get(AppSupplychainService.class).isApp("supplychain")) {
        return super.getDefaultAdvancePaymentInvoice(invoice);
    }
    SaleOrder saleOrder = invoice.getSaleOrder();
    Company company = invoice.getCompany();
    Currency currency = invoice.getCurrency();
    if (company == null || saleOrder == null) {
        return super.getDefaultAdvancePaymentInvoice(invoice);
    }
    boolean generateMoveForInvoicePayment = Beans.get(AccountConfigService.class).getAccountConfig(company).getGenerateMoveForInvoicePayment();
    String filter = writeGeneralFilterForAdvancePayment();
    filter += " AND self.saleOrder = :_saleOrder";
    if (!generateMoveForInvoicePayment) {
        filter += " AND self.currency = :_currency";
    }
    Query<Invoice> query = Beans.get(InvoiceRepository.class).all().filter(filter).bind("_status", InvoiceRepository.STATUS_VALIDATED).bind("_operationSubType", InvoiceRepository.OPERATION_SUB_TYPE_ADVANCE).bind("_saleOrder", saleOrder);
    if (!generateMoveForInvoicePayment) {
        if (currency == null) {
            return new HashSet<>();
        }
        query.bind("_currency", currency);
    }
    Set<Invoice> advancePaymentInvoices = new HashSet<>(query.fetch());
    filterAdvancePaymentInvoice(invoice, advancePaymentInvoices);
    return advancePaymentInvoices;
}
Also used : Company(com.axelor.apps.base.db.Company) Invoice(com.axelor.apps.account.db.Invoice) Currency(com.axelor.apps.base.db.Currency) InvoiceRepository(com.axelor.apps.account.db.repo.InvoiceRepository) SaleOrder(com.axelor.apps.sale.db.SaleOrder) HashSet(java.util.HashSet)

Aggregations

Currency (com.axelor.apps.base.db.Currency)41 Company (com.axelor.apps.base.db.Company)19 BigDecimal (java.math.BigDecimal)19 AxelorException (com.axelor.exception.AxelorException)17 Partner (com.axelor.apps.base.db.Partner)14 ArrayList (java.util.ArrayList)12 List (java.util.List)10 Invoice (com.axelor.apps.account.db.Invoice)9 PaymentMode (com.axelor.apps.account.db.PaymentMode)9 PriceList (com.axelor.apps.base.db.PriceList)9 Map (java.util.Map)9 SaleOrder (com.axelor.apps.sale.db.SaleOrder)8 LocalDate (java.time.LocalDate)8 BankDetails (com.axelor.apps.base.db.BankDetails)7 Wizard (com.axelor.apps.base.db.Wizard)7 ActionViewBuilder (com.axelor.meta.schema.actions.ActionView.ActionViewBuilder)7 Product (com.axelor.apps.base.db.Product)6 Transactional (com.google.inject.persist.Transactional)6 PaymentCondition (com.axelor.apps.account.db.PaymentCondition)4 BankOrder (com.axelor.apps.bankpayment.db.BankOrder)4