Exemplo n.º 1
0
 /**
  * Update the store configs with latest currency conversion data
  * @param   int Config id (optional, if none given, all will be taken)
  */
 public function convertCurrencies($intId = 0)
 {
     $arrColumns = array(Config::getTable() . '.currencyAutomator=?');
     $arrValues = array('1');
     if ($intId > 0) {
         $arrColumns[] = Config::getTable() . '.id=?';
         $arrValues[] = $intId;
     }
     $objConfigs = Config::findBy($arrColumns, $arrValues);
     if (null === $objConfigs) {
         return;
     }
     while ($objConfigs->next()) {
         switch ($objConfigs->currencyProvider) {
             case 'ecb.int':
                 $fltCourse = $objConfigs->currency == 'EUR' ? 1 : 0;
                 $fltCourseOrigin = $objConfigs->currencyOrigin == 'EUR' ? 1 : 0;
                 $objRequest = new \Request();
                 $objRequest->send('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
                 if ($objRequest->hasError()) {
                     \System::log('Error retrieving data from European Central Bank (ecb.int): ' . $objRequest->error . ' (Code ' . $objRequest->code . ')', __METHOD__, TL_ERROR);
                     return;
                 }
                 $objXml = new \SimpleXMLElement($objRequest->response);
                 foreach ($objXml->Cube->Cube->Cube as $currency) {
                     if (!$fltCourse && strtolower($currency['currency']) == strtolower($objConfigs->currency)) {
                         $fltCourse = (double) $currency['rate'];
                     }
                     if (!$fltCourseOrigin && strtolower($currency['currency']) == strtolower($objConfigs->currencyOrigin)) {
                         $fltCourseOrigin = (double) $currency['rate'];
                     }
                 }
                 // Log if one of the currencies is not available
                 if (!$fltCourse || !$fltCourseOrigin) {
                     \System::log('Could not find currency to convert in European Central Bank (ecb.int).', __METHOD__, TL_ERROR);
                     return;
                 }
                 $objConfigs->priceCalculateFactor = $fltCourse / $fltCourseOrigin;
                 $objConfigs->save();
                 break;
             case 'admin.ch':
                 $fltCourse = $objConfigs->currency == 'CHF' ? 1 : 0;
                 $fltCourseOrigin = $objConfigs->currencyOrigin == 'CHF' ? 1 : 0;
                 $objRequest = new \Request();
                 $objRequest->send('http://www.afd.admin.ch/publicdb/newdb/mwst_kurse/wechselkurse.php');
                 if ($objRequest->hasError()) {
                     \System::log('Error retrieving data from Swiss Federal Department of Finance (admin.ch): ' . $objRequest->error . ' (Code ' . $objRequest->code . ')', __METHOD__, TL_ERROR);
                     return;
                 }
                 $objXml = new \SimpleXMLElement($objRequest->response);
                 foreach ($objXml->devise as $currency) {
                     if (!$fltCourse && $currency['code'] == strtolower($objConfigs->currency)) {
                         $fltCourse = (double) $currency->kurs;
                     }
                     if (!$fltCourseOrigin && $currency['code'] == strtolower($objConfigs->currencyOrigin)) {
                         $fltCourseOrigin = (double) $currency->kurs;
                     }
                 }
                 // Log if one of the currencies is not available
                 if (!$fltCourse || !$fltCourseOrigin) {
                     \System::log('Could not find currency to convert in Swiss Federal Department of Finance (admin.ch).', __METHOD__, TL_ERROR);
                     return;
                 }
                 $objConfigs->priceCalculateFactor = $fltCourse / $fltCourseOrigin;
                 $objConfigs->save();
                 break;
             default:
                 // !HOOK: other currency providers
                 if (isset($GLOBALS['ISO_HOOKS']['convertCurrency']) && is_array($GLOBALS['ISO_HOOKS']['convertCurrency'])) {
                     foreach ($GLOBALS['ISO_HOOKS']['convertCurrency'] as $callback) {
                         $objCallback = \System::importStatic($callback[0]);
                         $objCallback->{$callback}[1]($objConfigs->current());
                     }
                 }
         }
     }
 }