When using the Currency API to convert from a currency to the same currency, the result is not what is expected. I consider this a bug because a valid function call, e.g. $factor = currency_api_convert('USD', 'USD', 1.0);, will produce invalid results. In this case, the result of the call has $factor['value']=0, but $factor['value']=1 is expected.

Fixing this requires a small modification to currency_api.module, in the function currency_api_convert():

Just after:

  $from = strtoupper($currency_from);
  $to   = strtoupper($currency_to);

insert:

  if ($from == $to) {
    return array(
      'status'  => TRUE,
      'message' => 'success',
      'value'   => 1.0,
      'date'    => date('n/j/Y'),
      'time'    => date('g:ia'),
    );
  }

This patch will check to see if the $from and $to currencies are identical, and if so will skip the DB lookup and the Yahoo lookup and just return an identity array.

Comments

kbahey’s picture

Version: master » 5.x-1.x-dev
Status: Active » Needs work

Please create a proper patch.

See http://drupal.org/patch for details.

tr’s picture

Status: Needs work » Needs review
StatusFileSize
new555 bytes

Patch attached for currency_api.module in branch 5.x-1.x-dev.

kbahey’s picture

Status: Needs review » Fixed

Committed.

Thank you.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.

steven jones’s picture

Version: 5.x-1.x-dev » 5.x-1.2
Priority: Normal » Critical
Status: Closed (fixed) » Active

Oh dear. This code has actually introduced a serious bug.

Right, some example calls:

$result = currency_api_convert('GBP', 'USD', 20);

should give:

$result['value'] == 34.90200 (at time of writing)

But,

$result = currency_api_convert('GBP', 'GBP', 20);

will give $result['value'] == 1 because of the stupid code introduced in this patch. Patch coming in a bit...

steven jones’s picture

StatusFileSize
new2.12 KB

Here's the patch (against 5.x-1.x), I'd suggest rolling 5.x-1.3 with this fix in ASAP, this is a serious bug!

kbahey’s picture

Version: 5.x-1.2 » 5.x-1.x-dev

Your patch does not apply against the current 5.x.1.x-dev version (where all patches go).

$ patch -p0 --dry-run < x.patch
patching file currency_api.module
Hunk #1 FAILED at 84.
Hunk #5 FAILED at 589.
2 out of 6 hunks FAILED -- saving rejects to file currency_api.module.rej
steven jones’s picture

StatusFileSize
new1.44 KB

Try this one, I'm got some issues with my dev tools atm, so I'm manually editing patches.

kbahey’s picture

Status: Active » Patch (to be ported)

I committed it, so wait 12 hours then download the -dev version and report back if it works OK.

If it does, I will port the patch to 6.x.

steven jones’s picture

Status: Patch (to be ported) » Active

12 hours! Pah, a cvs checkout confirms that it's working nicely. Thanks.

kbahey’s picture

Status: Active » Fixed

Committed to 6.x-1.x-dev.

1.3 should appear here shortly http://drupal.org/node/323725

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.