Changeset 690

Show
Ignore:
Timestamp:
04/29/08 13:50:42 (7 months ago)
Author:
stavros
Message:

port Yahoo precision workaround to branches/1.3

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.3/A2BCustomer_UI/lib/Misc.php

    r457 r690  
    538538 
    539539 
     540// Update currency exchange rate list from finance.yahoo.com. 
     541// To work around yahoo truncating to 4 decimal places before 
     542// doing a division, leading to >10% errors with weak base_currency, 
     543// we always request in a strong currency and convert ourselves. 
     544// We use ounces of silver,  as if silver ever devalues significantly 
     545// we'll all be pretty much boned anyway,  wouldn't you say? 
     546function currencies_update_yahoo ($DBHandle, $instance_table) { 
     547        global $FG_DEBUG; 
     548        $strong_currency = 'XAG'; 
     549        $url = "http://download.finance.yahoo.com/d/quotes.csv?s="; 
     550        $return = ""; 
     551 
     552        $QUERY =  "SELECT id,currency,basecurrency FROM cc_currencies ORDER BY id"; 
     553        $old_currencies = $instance_table -> SQLExec ($DBHandle, $QUERY); 
     554 
     555        // we will retrieve a .CSV file e.g. USD to EUR and USD to CAD with a URL like: 
     556        // http://download.finance.yahoo.com/d/quotes.csv?s=USDEUR=X+USDCAD=X&f=l1 
     557        if (is_array($old_currencies)) { 
     558                $num_cur = count($old_currencies); 
     559                if ($FG_DEBUG >= 1) $return .= basename(__FILE__).' line:'.__LINE__."[CURRENCIES TO UPDATE = $num_cur]\n"; 
     560                for ($i = 0; $i < $num_cur; $i++) { 
     561                        if ($FG_DEBUG >= 1) $return .= $old_currencies[$i][0].' - '.$old_currencies[$i][1].' - '.$old_currencies[$i][2]."\n"; 
     562                        // Finish and add termination ? 
     563                        if ($i+1 == $num_cur) { 
     564                                $url .= $strong_currency.$old_currencies[$i][1]."=X&f=l1"; 
     565                        } else { 
     566                                $url .= $strong_currency.$old_currencies[$i][1]."=X+"; 
     567                        } 
     568 
     569                        // Save the index of base_currency when we find it 
     570                        if (strcasecmp(BASE_CURRENCY, $old_currencies[$i][1]) == 0) { 
     571                                $index_base_currency = $i; 
     572                        } 
     573                } 
     574 
     575                // Check we found the index of base_currency 
     576                if (!isset($index_base_currency)) { 
     577                        return gettext("Can't find our base_currency in cc_currencies.").' '.gettext('Currency update ABORTED.'); 
     578                } 
     579 
     580                // Call wget to download the URL to the .CVS file 
     581                exec("wget '".$url."' -O /tmp/currencies.cvs  2>&1", $output); 
     582                if ($FG_DEBUG >= 1) $return .= "wget '".$url."' -O /tmp/currencies.cvs\n".$output; 
     583 
     584                // get the file with the currencies to update the database 
     585                $currencies = file("/tmp/currencies.cvs"); 
     586 
     587                // trim off any leading/trailing comments/headers that may have been added 
     588                $i = 0; 
     589                while (!is_numeric(trim($currencies[$i]))) { 
     590                        $i++; 
     591                } 
     592                $currencies = array_slice($currencies,$i,$num_cur); 
     593 
     594                // do some simple checks to try to verify we've received exactly one 
     595                // valid response for each currency we requested 
     596                $num_res = count($currencies); 
     597                if ($num_res < $num_cur) { 
     598                        return gettext("The CSV file doesn't contain all the currencies we requested.").' '.gettext('Currency update ABORTED.'); 
     599                } 
     600                for ($i = 0; $i < $num_cur; $i++) { 
     601                        if (!is_numeric(trim($currencies[$i]))) { 
     602                                return gettext("At least one of the entries in the CSV file isn't a number.").' '.gettext('Currency update ABORTED.'); 
     603                        } 
     604                } 
     605 
     606                // Find base_currency's value in $strong_currency to help avoid Yahoo's 
     607                // early truncation,  and therefore win back a lot of accuracy 
     608                $base_value = $currencies[$index_base_currency]; 
     609 
     610                // Check our base_currency will still fund our addiction to tea and biscuits 
     611                if (round($base_value,5) < 0.00001) { 
     612                        return gettext('Our base_currency seems to be worthless.').' '.gettext('Currency update ABORTED.'); 
     613                } 
     614 
     615                // update each row we originally retrieved from cc_currencies 
     616                $i = 0; 
     617                foreach ($currencies as $currency){ 
     618 
     619                        $currency = trim($currency); 
     620 
     621                        if ($currency != 0) { 
     622                                $currency = $base_value / $currency; 
     623                        } 
     624 
     625                        //  extremely weak currencies are assigned the smallest value the schema permits 
     626                        if (round($currency, 5) < 0.00001) { 
     627                                $currency = '0.00001'; 
     628                        } 
     629 
     630                        // if the currency is base_currency then set to exactly 1.00000 
     631                        if ($i == $index_base_currency) $currency = 1; 
     632 
     633                        $QUERY = "UPDATE cc_currencies SET value='$currency'"; 
     634 
     635                        // if we've changed base_currency,  update each SQL row to reflect this 
     636                        if (BASE_CURRENCY != $old_currencies[$i][2]) { 
     637                                $QUERY .= ", basecurrency='".BASE_CURRENCY."'"; 
     638                        } 
     639 
     640                        $QUERY .= " , lastupdate = CURRENT_TIMESTAMP WHERE id ='".$old_currencies[$i][0]."'"; 
     641                        $result = $instance_table -> SQLExec ($DBHandle, $QUERY, 0); 
     642                        if ($FG_DEBUG >= 1) $return .= "$QUERY -> [$result]\n"; 
     643 
     644                        $i++; 
     645                } 
     646                $return .= gettext('Success! All currencies are now updated.'); 
     647        } 
     648        return $return; 
     649} 
     650 
     651 
    540652?> 
  • branches/1.3/A2Billing_AGI/libs_a2billing/crontjob/currencies_update_yahoo.php

    r505 r690  
    4444define ("BASE_CURRENCY", strtoupper($A2B->config["global"]['base_currency'])); 
    4545 
    46 // get in a csv file USD to EUR and USD to CAD 
    47 // http://download.finance.yahoo.com/d/quotes.csv?s=USDEUR=X+USDCAD=X&f=l1 
    4846 
    4947 
     
    6260$instance_table = new Table(); 
    6361$A2B -> set_instance_table ($instance_table); 
    64  
    65 $QUERY =  "SELECT id,currency,basecurrency FROM cc_currencies ORDER BY id"; 
    66 $result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY); 
    67          
    68 $url = "http://download.finance.yahoo.com/d/quotes.csv?s="; 
    69  
    70 /* result[index_result][field] */ 
    71  
    72 $index_base_currency = 0; 
    73  
    74 if (is_array($result)){ 
    75         $num_cur = count($result); 
    76         write_log(LOGFILE_CRONT_CURRENCY_UPDATE, basename(__FILE__).' line:'.__LINE__."[CURRENCIES TO UPDATE = $num_cur]", 0); 
    77         for ($i=0;$i<$num_cur;$i++){ 
    78                 if ($FG_DEBUG >= 1) echo $result[$i][0].' - '.$result[$i][1].' - '.$result[$i][2]."\n"; 
    79                 // Finish and add termination ?  
    80                 if ($i+1 == $num_cur) $url .= BASE_CURRENCY.$result[$i][1]."=X&f=l1"; 
    81                 else $url .= BASE_CURRENCY.$result[$i][1]."=X+"; 
    82                  
    83                 // Check what is the index of BASE_CURRENCY to save it  
    84                 if (strcasecmp(BASE_CURRENCY, $result[$i][1]) == 0) { 
    85                         $index_base_currency = $result[$i][0]; 
    86                 } 
    87         } 
    88          
    89         // Create the script to get the currencies 
    90         exec("wget '".$url."' -O /tmp/currencies.cvs  2>&1", $output); 
    91         if ($FG_DEBUG >= 1) echo "wget '".$url."' -O /tmp/currencies.cvs"; 
    92          
    93         // get the file with the currencies to update the database 
    94         $currencies = file("/tmp/currencies.cvs"); 
    95          
    96         // update database 
    97         foreach ($currencies as $currency){ 
    98                  
    99                 $currency = trim($currency); 
    100                  
    101                 if (!is_numeric($currency)){  
    102                         continue;  
    103                 } 
    104                 $id++; 
    105                 // if the currency is BASE_CURRENCY the set to 1 
    106                 if ($id == $index_base_currency) $currency = 1; 
    107                  
    108                 if ($currency!=0) $currency=1/$currency; 
    109                 $QUERY="UPDATE cc_currencies SET value=".$currency; 
    110                  
    111                 if (BASE_CURRENCY != $result[$i][2]){ 
    112                         $QUERY .= ", basecurrency='".BASE_CURRENCY."'"; 
    113                 } 
    114                 $QUERY .= " , lastupdate = CURRENT_TIMESTAMP WHERE id =".$id; 
    115                  
    116                 $result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY, 0); 
    117                 if ($FG_DEBUG >= 1) echo "$QUERY \n";  
    118                 //if ($id == 5) exit; 
    119         } 
    120         write_log(LOGFILE_CRONT_CURRENCY_UPDATE, basename(__FILE__).' line:'.__LINE__."[CURRENCIES UPDATED !!!]", 0); 
    121 
     62$return = currencies_update_yahoo($A2B -> DBHandle, $A2B -> instance_table); 
     63write_log(LOGFILE_CRONT_CURRENCY_UPDATE, basename(__FILE__).' line:'.__LINE__.$return, 0); 
    12264 
    12365?> 
  • branches/1.3/A2Billing_AGI/libs_a2billing/Misc.php

    r457 r690  
    538538 
    539539 
     540// Update currency exchange rate list from finance.yahoo.com. 
     541// To work around yahoo truncating to 4 decimal places before 
     542// doing a division, leading to >10% errors with weak base_currency, 
     543// we always request in a strong currency and convert ourselves. 
     544// We use ounces of silver,  as if silver ever devalues significantly 
     545// we'll all be pretty much boned anyway,  wouldn't you say? 
     546function currencies_update_yahoo ($DBHandle, $instance_table) { 
     547        global $FG_DEBUG; 
     548        $strong_currency = 'XAG'; 
     549        $url = "http://download.finance.yahoo.com/d/quotes.csv?s="; 
     550        $return = ""; 
     551 
     552        $QUERY =  "SELECT id,currency,basecurrency FROM cc_currencies ORDER BY id"; 
     553        $old_currencies = $instance_table -> SQLExec ($DBHandle, $QUERY); 
     554 
     555        // we will retrieve a .CSV file e.g. USD to EUR and USD to CAD with a URL like: 
     556        // http://download.finance.yahoo.com/d/quotes.csv?s=USDEUR=X+USDCAD=X&f=l1 
     557        if (is_array($old_currencies)) { 
     558                $num_cur = count($old_currencies); 
     559                if ($FG_DEBUG >= 1) $return .= basename(__FILE__).' line:'.__LINE__."[CURRENCIES TO UPDATE = $num_cur]\n"; 
     560                for ($i = 0; $i < $num_cur; $i++) { 
     561                        if ($FG_DEBUG >= 1) $return .= $old_currencies[$i][0].' - '.$old_currencies[$i][1].' - '.$old_currencies[$i][2]."\n"; 
     562                        // Finish and add termination ? 
     563                        if ($i+1 == $num_cur) { 
     564                                $url .= $strong_currency.$old_currencies[$i][1]."=X&f=l1"; 
     565                        } else { 
     566                                $url .= $strong_currency.$old_currencies[$i][1]."=X+"; 
     567                        } 
     568 
     569                        // Save the index of base_currency when we find it 
     570                        if (strcasecmp(BASE_CURRENCY, $old_currencies[$i][1]) == 0) { 
     571                                $index_base_currency = $i; 
     572                        } 
     573                } 
     574 
     575                // Check we found the index of base_currency 
     576                if (!isset($index_base_currency)) { 
     577                        return gettext("Can't find our base_currency in cc_currencies.").' '.gettext('Currency update ABORTED.'); 
     578                } 
     579 
     580                // Call wget to download the URL to the .CVS file 
     581                exec("wget '".$url."' -O /tmp/currencies.cvs  2>&1", $output); 
     582                if ($FG_DEBUG >= 1) $return .= "wget '".$url."' -O /tmp/currencies.cvs\n".$output; 
     583 
     584                // get the file with the currencies to update the database 
     585                $currencies = file("/tmp/currencies.cvs"); 
     586 
     587                // trim off any leading/trailing comments/headers that may have been added 
     588                $i = 0; 
     589                while (!is_numeric(trim($currencies[$i]))) { 
     590                        $i++; 
     591                } 
     592                $currencies = array_slice($currencies,$i,$num_cur); 
     593 
     594                // do some simple checks to try to verify we've received exactly one 
     595                // valid response for each currency we requested 
     596                $num_res = count($currencies); 
     597                if ($num_res < $num_cur) { 
     598                        return gettext("The CSV file doesn't contain all the currencies we requested.").' '.gettext('Currency update ABORTED.'); 
     599                } 
     600                for ($i = 0; $i < $num_cur; $i++) { 
     601                        if (!is_numeric(trim($currencies[$i]))) { 
     602                                return gettext("At least one of the entries in the CSV file isn't a number.").' '.gettext('Currency update ABORTED.'); 
     603                        } 
     604                } 
     605 
     606                // Find base_currency's value in $strong_currency to help avoid Yahoo's 
     607                // early truncation,  and therefore win back a lot of accuracy 
     608                $base_value = $currencies[$index_base_currency]; 
     609 
     610                // Check our base_currency will still fund our addiction to tea and biscuits 
     611                if (round($base_value,5) < 0.00001) { 
     612                        return gettext('Our base_currency seems to be worthless.').' '.gettext('Currency update ABORTED.'); 
     613                } 
     614 
     615                // update each row we originally retrieved from cc_currencies 
     616                $i = 0; 
     617                foreach ($currencies as $currency){ 
     618 
     619                        $currency = trim($currency); 
     620 
     621                        if ($currency != 0) { 
     622                                $currency = $base_value / $currency; 
     623                        } 
     624 
     625                        //  extremely weak currencies are assigned the smallest value the schema permits 
     626                        if (round($currency, 5) < 0.00001) { 
     627                                $currency = '0.00001'; 
     628                        } 
     629 
     630                        // if the currency is base_currency then set to exactly 1.00000 
     631                        if ($i == $index_base_currency) $currency = 1; 
     632 
     633                        $QUERY = "UPDATE cc_currencies SET value='$currency'"; 
     634 
     635                        // if we've changed base_currency,  update each SQL row to reflect this 
     636                        if (BASE_CURRENCY != $old_currencies[$i][2]) { 
     637                                $QUERY .= ", basecurrency='".BASE_CURRENCY."'"; 
     638                        } 
     639 
     640                        $QUERY .= " , lastupdate = CURRENT_TIMESTAMP WHERE id ='".$old_currencies[$i][0]."'"; 
     641                        $result = $instance_table -> SQLExec ($DBHandle, $QUERY, 0); 
     642                        if ($FG_DEBUG >= 1) $return .= "$QUERY -> [$result]\n"; 
     643 
     644                        $i++; 
     645                } 
     646                $return .= gettext('Success! All currencies are now updated.'); 
     647        } 
     648        return $return; 
     649} 
     650 
     651 
    540652?> 
  • branches/1.3/A2Billing_UI/lib/Misc.php

    r457 r690  
    538538 
    539539 
     540// Update currency exchange rate list from finance.yahoo.com. 
     541// To work around yahoo truncating to 4 decimal places before 
     542// doing a division, leading to >10% errors with weak base_currency, 
     543// we always request in a strong currency and convert ourselves. 
     544// We use ounces of silver,  as if silver ever devalues significantly 
     545// we'll all be pretty much boned anyway,  wouldn't you say? 
     546function currencies_update_yahoo ($DBHandle, $instance_table) { 
     547        global $FG_DEBUG; 
     548        $strong_currency = 'XAG'; 
     549        $url = "http://download.finance.yahoo.com/d/quotes.csv?s="; 
     550        $return = ""; 
     551 
     552        $QUERY =  "SELECT id,currency,basecurrency FROM cc_currencies ORDER BY id"; 
     553        $old_currencies = $instance_table -> SQLExec ($DBHandle, $QUERY); 
     554 
     555        // we will retrieve a .CSV file e.g. USD to EUR and USD to CAD with a URL like: 
     556        // http://download.finance.yahoo.com/d/quotes.csv?s=USDEUR=X+USDCAD=X&f=l1 
     557        if (is_array($old_currencies)) { 
     558                $num_cur = count($old_currencies); 
     559                if ($FG_DEBUG >= 1) $return .= basename(__FILE__).' line:'.__LINE__."[CURRENCIES TO UPDATE = $num_cur]\n"; 
     560                for ($i = 0; $i < $num_cur; $i++) { 
     561                        if ($FG_DEBUG >= 1) $return .= $old_currencies[$i][0].' - '.$old_currencies[$i][1].' - '.$old_currencies[$i][2]."\n"; 
     562                        // Finish and add termination ? 
     563                        if ($i+1 == $num_cur) { 
     564                                $url .= $strong_currency.$old_currencies[$i][1]."=X&f=l1"; 
     565                        } else { 
     566                                $url .= $strong_currency.$old_currencies[$i][1]."=X+"; 
     567                        } 
     568 
     569                        // Save the index of base_currency when we find it 
     570                        if (strcasecmp(BASE_CURRENCY, $old_currencies[$i][1]) == 0) { 
     571                                $index_base_currency = $i; 
     572                        } 
     573                } 
     574 
     575                // Check we found the index of base_currency 
     576                if (!isset($index_base_currency)) { 
     577                        return gettext("Can't find our base_currency in cc_currencies.").' '.gettext('Currency update ABORTED.'); 
     578                } 
     579 
     580                // Call wget to download the URL to the .CVS file 
     581                exec("wget '".$url."' -O /tmp/currencies.cvs  2>&1", $output); 
     582                if ($FG_DEBUG >= 1) $return .= "wget '".$url."' -O /tmp/currencies.cvs\n".$output; 
     583 
     584                // get the file with the currencies to update the database 
     585                $currencies = file("/tmp/currencies.cvs"); 
     586 
     587                // trim off any leading/trailing comments/headers that may have been added 
     588                $i = 0; 
     589                while (!is_numeric(trim($currencies[$i]))) { 
     590                        $i++; 
     591                } 
     592                $currencies = array_slice($currencies,$i,$num_cur); 
     593 
     594                // do some simple checks to try to verify we've received exactly one 
     595                // valid response for each currency we requested 
     596                $num_res = count($currencies); 
     597                if ($num_res < $num_cur) { 
     598                        return gettext("The CSV file doesn't contain all the currencies we requested.").' '.gettext('Currency update ABORTED.'); 
     599                } 
     600                for ($i = 0; $i < $num_cur; $i++) { 
     601                        if (!is_numeric(trim($currencies[$i]))) { 
     602                                return gettext("At least one of the entries in the CSV file isn't a number.").' '.gettext('Currency update ABORTED.'); 
     603                        } 
     604                } 
     605 
     606                // Find base_currency's value in $strong_currency to help avoid Yahoo's 
     607                // early truncation,  and therefore win back a lot of accuracy 
     608                $base_value = $currencies[$index_base_currency]; 
     609 
     610                // Check our base_currency will still fund our addiction to tea and biscuits 
     611                if (round($base_value,5) < 0.00001) { 
     612                        return gettext('Our base_currency seems to be worthless.').' '.gettext('Currency update ABORTED.'); 
     613                } 
     614 
     615                // update each row we originally retrieved from cc_currencies 
     616                $i = 0; 
     617                foreach ($currencies as $currency){ 
     618 
     619                        $currency = trim($currency); 
     620 
     621                        if ($currency != 0) { 
     622                                $currency = $base_value / $currency; 
     623                        } 
     624 
     625                        //  extremely weak currencies are assigned the smallest value the schema permits 
     626                        if (round($currency, 5) < 0.00001) { 
     627                                $currency = '0.00001'; 
     628                        } 
     629 
     630                        // if the currency is base_currency then set to exactly 1.00000 
     631                        if ($i == $index_base_currency) $currency = 1; 
     632 
     633                        $QUERY = "UPDATE cc_currencies SET value='$currency'"; 
     634 
     635                        // if we've changed base_currency,  update each SQL row to reflect this 
     636                        if (BASE_CURRENCY != $old_currencies[$i][2]) { 
     637                                $QUERY .= ", basecurrency='".BASE_CURRENCY."'"; 
     638                        } 
     639 
     640                        $QUERY .= " , lastupdate = CURRENT_TIMESTAMP WHERE id ='".$old_currencies[$i][0]."'"; 
     641                        $result = $instance_table -> SQLExec ($DBHandle, $QUERY, 0); 
     642                        if ($FG_DEBUG >= 1) $return .= "$QUERY -> [$result]\n"; 
     643 
     644                        $i++; 
     645                } 
     646                $return .= gettext('Success! All currencies are now updated.'); 
     647        } 
     648        return $return; 
     649} 
     650 
     651 
    540652?> 
  • branches/1.3/A2Billing_UI/Public/A2B_currencies.php

    r505 r690  
    2424        $instance_table = new Table(); 
    2525        $A2B -> set_instance_table ($instance_table); 
    26          
    27         $QUERY =  "SELECT id,currency,basecurrency FROM cc_currencies ORDER BY id"; 
    28         $result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY); 
    29                  
    30         $url = "http://download.finance.yahoo.com/d/quotes.csv?s="; 
    31          
    32         /* result[index_result][field] */ 
    33          
    34         $index_base_currency = 0; 
    35  
    36         if (is_array($result)){ 
    37                 $num_cur = count($result); 
    38                 for ($i=0;$i<$num_cur;$i++){ 
    39                          
    40                         // Finish and add termination ?  
    41                         if ($i+1 == $num_cur) $url .= BASE_CURRENCY.$result[$i][1]."=X&f=l1"; 
    42                         else $url .= BASE_CURRENCY.$result[$i][1]."=X+"; 
    43                          
    44                         // Check what is the index of BASE_CURRENCY to save it 
    45                         if (strcasecmp(BASE_CURRENCY, $result[$i][1]) == 0) { 
    46                                 $index_base_currency = $result[$i][0]; 
    47                         } 
    48                 } 
    49                  
    50                 // Create the script to get the currencies 
    51                 exec("wget '".$url."' -O /tmp/currencies.cvs  2>&1", $output); 
    52                  
    53                 // get the file with the currencies to update the database 
    54                 $currencies = file("/tmp/currencies.cvs"); 
    55                  
    56                 // update database 
    57                 foreach ($currencies as $currency){ 
    58                          
    59                         $currency = trim($currency); 
    60                          
    61                         if (!is_numeric($currency)){  
    62                                 continue;  
    63                         } 
    64                         $id++; 
    65                         // if the currency is BASE_CURRENCY the set to 1 
    66                         if ($id == $index_base_currency) $currency = 1; 
    67                          
    68                         if ($currency!=0) $currency=1/$currency; 
    69                         $QUERY="UPDATE cc_currencies set value=".$currency; 
    70                          
    71                         if (BASE_CURRENCY != $result[$i][2]){ 
    72                                 $QUERY .= ",basecurrency='".BASE_CURRENCY."'"; 
    73                         } 
    74                         $QUERY .= " , lastupdate = CURRENT_TIMESTAMP WHERE id =".$id; 
    75                          
    76                         $result = $A2B -> instance_table -> SQLExec ($A2B->DBHandle, $QUERY, 0); 
    77                 } 
    78         } 
    79         $update_msg = '<center><font color="green"><b>'.gettext('Success! All currencies are now updated.').'</b></font></center>'; 
     26        $return = currencies_update_yahoo($A2B -> DBHandle, $A2B -> instance_table); 
     27        $update_msg = '<center><font color="green"><b>'.$return.'</b></font></center>'; 
    8028} 
    8129/***********************************************************************************/ 


Google