1 (edited by notrinos 11/26/2017 12:40:51 pm)

Topic: Adapting to google's error when getting exchange rates.

I have found google does not convert following currencies STD,  ITL, IRR, FRF, DEM, VND, BYR to USD but it does opposite (USD to those currencies).

Should we change lines 213-225 of gl_db_rates.inc

elseif ($provider == 'GOOGLE')
    {
        $val = '';
        
        $regexp = "%([\d|.]+)\s+{$curr_a}\s+=\s+<span\sclass=(.*)>([\d|.]+)\s+{$curr_b}\s*</span>%s";
        if (preg_match($regexp, $contents, $matches)) 
        {
            $val = $matches[3];
            $val = str_replace(',', '', $val);
            if ($val != 0)
                $val = 1 / $val;
        }
    }

TO

    elseif ($provider == 'GOOGLE')
    {
        $val = '';
        
        $regexp = "%([\d|.]+)\s+{$curr_a}\s+=\s+<span\sclass=(.*)>([\d|.]+)\s+{$curr_b}\s*</span>%s";
        if (preg_match($regexp, $contents, $matches)) 
        {
            $val = $matches[3];
            $val = str_replace(',', '', $val);
            if ($val != 0)
                $val = 1 / $val;
        }
// Start of New Code lines
        if(!$val) {
                $filename = "/finance/converter?a=1&from={$curr_b}&to={$curr_a}";
                $regexp = "%([\d|.]+)\s+{$curr_b}\s+=\s+<span\sclass=(.*)>([\d|.]+)\s+{$curr_a}\s*</span>%s";
                $contents = url_get_contents("http://".$site.$filename);
                if (preg_match($regexp, $contents, $matches))  {
                    $val = $matches[3];
                    $val = str_replace(',', '', $val);
                }
        }
// End of New code lines
    }

to adapting to the google's issue ?

if this issue will not be solved FA will shows folowing red error comment:
number_format() expects parameter 2 to be integer, float given in file: C:\xampp\htdocs\fa243\includes\current_user.inc at line 318

Phuong

Re: Adapting to google's error when getting exchange rates.

The reason is that it is a very big numner for these conversions:
1 EUR = 24810.7356 STD
The inverse obtained is obviously too small:
1 STD = 0.000040305133073120169802623667474011 EUR
This is more than the 4 digits after the decimal place.
Hence Google needs to increase the number of decimal places for such evaluations.
Doing it in FA with the default values will be of no use and hence @notrinos solution is elegant.

If you search for "1 STD = ? EUR" you will get 0.000040 as the answer now.
Hence their evaluator for the search engine is okay.

Incidentally, we may have a Chart of Accounts for Sao Tome among the forum users - @odaio!

Alternatively, if all the reverse rates work, then we can swap the currencies and make just 1 single check. The reverse for IDR to OMR does not work.

USD to any currency works - hence only when a non USD is involved as the From and To currencies, we will need 2 conversions and then a division! Here we will have the second check only when the first fails.

Post's attachments

STD_xchg.png 34.2 kb, file has never been downloaded. 

You don't have the permssions to download the attachments of this post.

Re: Adapting to google's error when getting exchange rates.

@joe: want to commit this with a check for if (empty($val)) { ?

Re: Adapting to google's error when getting exchange rates.

Willdo later.

Joe

Re: Adapting to google's error when getting exchange rates.

Committed to stable Repo.

/Joe