Page 1 of 1

BLC 3.1.1 with EUR default currency cannot do checkout

Posted: Tue Apr 22, 2014 6:24 am
by Theo Schumacher
Hi,

I have switched in BLC_LOCALE and BLC_CURRENCY tables to 'EUR' as default currency with fr_FR as default locale.
When creating a cart, all products, shipment etc are shown in EUR.
When doing checkout on delivery, I get a message:

Code: Select all

[artifact:mvn] [ERROR] 11:57:29 DefaultErrorHandler - An error occurred during the workflow
[artifact:mvn] java.lang.UnsupportedOperationException: No currency conversion service is registered, cannot add different currency types together (USD EUR)
[artifact:mvn]    at org.broadleafcommerce.common.money.Money.add(Money.java:176)
[artifact:mvn]    at org.broadleafcommerce.core.pricing.service.workflow.AdjustOrderPaymentsActivity.execute(AdjustOrderPaymentsActivity.java:87)


Checking in the source code I found out that in

Code: Select all

public class AdjustOrderPaymentsActivity extends BaseActivity<ProcessContext<Order>> {

    @Override
    public ProcessContext<Order> execute(ProcessContext<Order> context) throws Exception {
        Order order = context.getSeedData();

        OrderPayment unconfirmedThirdParty = null;
        Money appliedPaymentsWithoutThirdParty = Money.ZERO;
.... more code


Money.ZERO generates an amount with currency 'USD' even though the 'order' has a currency set as 'EUR'. This seams to generate the currency mismatch.

I guess this is a bug. Should I register that on Github ?

Thank you

Re: BLC 3.1.1 with EUR default currency cannot do checkout

Posted: Tue Apr 22, 2014 12:29 pm
by phillipuniverse
Looking at Money.ZERO, it uses getDefaultCurrency(), which attempts to use the currency attached to the BroadleafRequestContext:

Code: Select all

if (brc != null && brc.getBroadleafCurrency() != null) {
    assert brc.getBroadleafCurrency().getCurrencyCode()!=null;
    return Currency.getInstance(brc.getBroadleafCurrency().getCurrencyCode());
}


This is put on the BroadleafRequestContext via the BroadleafCurrencyResolver (BroadleafCurrencyResolverImpl). Once it finds the default currency it actually puts it in session:

Code: Select all

@Override
public BroadleafCurrency resolveCurrency(WebRequest request) {
    BroadleafCurrency currency = null;

    // 1) Check request for currency
    currency = (BroadleafCurrency) request.getAttribute(CURRENCY_VAR, WebRequest.SCOPE_REQUEST);

    // 2) Check for a request parameter
    if (currency == null && BLCRequestUtils.getURLorHeaderParameter(request, CURRENCY_CODE_PARAM) != null) {
        String currencyCode = BLCRequestUtils.getURLorHeaderParameter(request, CURRENCY_CODE_PARAM);
        currency = broadleafCurrencyService.findCurrencyByCode(currencyCode);
        if (LOG.isTraceEnabled()) {
            LOG.trace("Attempt to find currency by param " + currencyCode + " resulted in " + currency);
        }
    }

    // 3) Check session for currency
    if (currency == null && BLCRequestUtils.isOKtoUseSession(request)) {
        currency = (BroadleafCurrency) request.getAttribute(CURRENCY_VAR, WebRequest.SCOPE_GLOBAL_SESSION);
    }

    // 4) Check locale for currency
    if (currency == null) {
        Locale locale = (Locale) request.getAttribute(BroadleafLocaleResolverImpl.LOCALE_VAR, WebRequest.SCOPE_REQUEST);
        if (locale != null) {
            currency = locale.getDefaultCurrency();
        }
    }

    // 5) Check default currency from DB
    if (currency == null) {
        currency = broadleafCurrencyService.findDefaultBroadleafCurrency();
    }

    if (BLCRequestUtils.isOKtoUseSession(request)) {
        request.setAttribute(CURRENCY_VAR, currency, WebRequest.SCOPE_GLOBAL_SESSION);
    }
    return currency;
}


So it's possible that your session still has USD in it?

Re: BLC 3.1.1 with EUR default currency cannot do checkout

Posted: Wed Apr 23, 2014 3:07 am
by Theo Schumacher
//1 and //2 section always returns NULL for currency

//3 assigns "EUR"

Every time AdjustOrderPaymentsActivity is called,

Code: Select all

        Money appliedPaymentsWithoutThirdParty = Money.ZERO;

assigns currency "USD" to appliedPaymentsWithoutThirdParty

therefore, during checkout, the line 88 generates the crash:

Code: Select all

                    appliedPaymentsWithoutThirdParty = appliedPaymentsWithoutThirdParty.add(payment.getAmount());


Money.ZERO is declared as

Code: Select all

public static final Money ZERO = new Money(BigDecimal.ZERO);


maybe this is the reason why it always returns a USD.

Is it enough to set DEFAULT_FLAG in the database or is there also an XML declaration for the default currency used before the DB-Access ?

Re: BLC 3.1.1 with EUR default currency cannot do checkout

Posted: Tue Apr 29, 2014 9:40 am
by Theo Schumacher
Hi,

I have discovered what is causing the problem. I'am running on a fedora 20 box which starts by default a jvm with a language code 'en' and environment 'US'. This causes the currecny set to be USD at the startup of jetty server. Later, broadleaf initializes new instances of Money class with this default currency, as done in Money.ZERO.

I have changed the build xml file of demo site like this:

Code: Select all

    <target name="jetty-demo" depends="start-db">
        <delete dir="war/WEB-INF/lib"/>
        <artifact:mvn mavenHome="${maven.home}" fork="true">
            <jvmarg value="-XX:MaxPermSize=256M" />
            <jvmarg value="-Xmx512M" />
            <jvmarg value="-Xdebug" />
            <jvmarg value="-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n" />
            <jvmarg value="-javaagent:${spring.instrument.path}" />
            <jvmarg value="-Druntime.environment=${runtime.environment}" /> 
           <jvmarg value="-Duser.language=fr" />
           <jvmarg value="-Duser.region=FR" />
            <arg value="compile"/>
            <arg value="war:exploded"/>
            <arg value="jetty:run"/>
        </artifact:mvn>
    </target>


The two addiitonal lines

Code: Select all

           <jvmarg value="-Duser.language=fr" />
           <jvmarg value="-Duser.region=FR" />

initializes the jvm with currency EUR and then, the checkout in broadleaf will work as well.

I have read in the broadleaf documentation that the community edition can only handle one currency, which is fione for me. But the way it crashes depending on the initial value in the jvm , i would still consider this as a bug. Maybe it's only a workaround but my solution is doing well.