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
Comment #1
kbahey commentedPlease create a proper patch.
See http://drupal.org/patch for details.
Comment #2
tr commentedPatch attached for currency_api.module in branch 5.x-1.x-dev.
Comment #3
kbahey commentedCommitted.
Thank you.
Comment #4
Anonymous (not verified) commentedAutomatically closed -- issue fixed for two weeks with no activity.
Comment #5
steven jones commentedOh 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'] == 1because of the stupid code introduced in this patch. Patch coming in a bit...Comment #6
steven jones commentedHere'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!
Comment #7
kbahey commentedYour patch does not apply against the current 5.x.1.x-dev version (where all patches go).
Comment #8
steven jones commentedTry this one, I'm got some issues with my dev tools atm, so I'm manually editing patches.
Comment #9
kbahey commentedI 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.
Comment #10
steven jones commented12 hours! Pah, a cvs checkout confirms that it's working nicely. Thanks.
Comment #11
kbahey commentedCommitted to 6.x-1.x-dev.
1.3 should appear here shortly http://drupal.org/node/323725
Comment #12
Anonymous (not verified) commentedAutomatically closed -- issue fixed for two weeks with no activity.