<?php
include_once("../includes/config.php");
include_once("../includes/functions.php");
include_once("../classes/cls_utils.php");
$currency_list_array = Utils::GetCountryList();
//Check currency codes already updated to table , if no value exists then save data to table
$currencyObj    =   Utils::getCurrencyCodeCount();
if($currencyObj->status==SUCCESS)
        {
            $currencyObjVal =   $currencyObj->data;
            $currencyCount  =   $currencyObjVal->currency_count;
            if($currencyCount==0)
                {
            // Save currency data to table // get Json data from global array and insert to table
                
                if(!empty($currency_list_array))
                    {
                        foreach($currency_list_array->results as $key=>$objVals)
                            {
                                $objCurrencyVo->vcurrency_name      = $objVals->currencyName;
                                $objCurrencyVo->vcurrency_code      = $objVals->currencyId;
                                $objCurrencyVo->vcurrency_symbol    = $objVals->currencySymbol;
                                $objCurrencyVo->nexchange_price     = '1';
                                $objCurrencyVo->vactive             = "Y";
                                $currencyInfo                       =  Utils::saveAllCurrencyCodes($objCurrencyVo);
                            }
                    }

            }
        }
        
      
   $currencyListCount         =  Utils:: getActiveCurrencyListCount();
  
   $count  = 0;
   if($currencyListCount>0){
        $sqlCurrencySetting = "SELECT * FROM ".$tableprefix."settings "	;
        $rSCurrency = mysql_query($sqlCurrencySetting);
         if(mysql_num_rows($rSCurrency)!= 0) {

                $row = mysql_fetch_array($rSCurrency);

                $admin_currency             = $row['admin_currency'];
                $currency_settings_mode     = $row['currency_settings'];
                $currency_api_key           = trim($row['currency_api_key']);
          }
       
       if($currency_settings_mode=="FREE"){
           $max_currencies     = 8;
       }else{
           $max_currencies     = 2;
       }
      
     for($c=0;$c<$currencyListCount;$c++) {
        
        
         if($currencyListCount<$count){
             break;
         }
         $currencylistObj                   =   Utils::getActiveCurrencyListWithLimit($count,$max_currencies);
         
    
      if($currencylistObj->status==SUCCESS)
        {
            $currencyArr = array();
            $currencyUsdArr = array();
            $currencylistObjVals    =  $currencylistObj->data;
            foreach($currencylistObjVals as $currencyData)
                {
                    $currencyArr[]      =  $admin_currency."_".$currencyData->vcurrency_code;
                    $currencyUsdArr[]   =  $currencyData->vcurrency_code."_USD";
                }
            $currencyCodeSting          =   implode(",",$currencyArr);
            $currencyUsdSting           =   implode(",",$currencyUsdArr);
        }

   
        if(!empty($currencyCodeSting))
        {
               if($currency_settings_mode=='FREE')
                {
                    $api_end_point      =  'http://free.currencyconverterapi.com/api/v3/convert?';
                    $endPointUrl        =  $api_end_point."q=".$currencyCodeSting;
                    $endPointUrl_Usd    =   $api_end_point."q=".$currencyUsdSting;
                }else{
                    $api_end_point      =  'http://www.currencyconverterapi.com/api/v3/convert?apiKey='.$currency_api_key;
                    $endPointUrl        =   $api_end_point."&q=".$currencyCodeSting;
                    $endPointUrl_Usd    =   $api_end_point."&q=".$currencyUsdSting;
                }
               //echo $endPointUrl.'<br />';
             
                
                $result = file_get_contents($endPointUrl);
                $currencyObj   =   json_decode($result);
               
//Update exchange rate to table with respect to admin selected currency
                 if(!empty($currencyObj))
                {
                       foreach($currencyObj->results as $valObj)
                       {
                           $objCurrencyVo->vcurrency_code       =   $valObj->to;
                           $objCurrencyVo->nexchange_price      =   round($valObj->val,2);
                           $currencyUpdateObj   = Utils::updateCurrencyRates($objCurrencyVo);
                       }
                }


                //Admin currency conversion rate for USD
                //echo $endPointUrl_Usd;
               $result_usd = file_get_contents($endPointUrl_Usd);
                $currencyUsdObj   =   json_decode($result_usd);

                  if(!empty($currencyUsdObj))
                {
                       foreach($currencyUsdObj->results as $valObj)
                       {
                           $objUsdCurrencyVo->vcurrency_code       =   $valObj->fr;
                           $objUsdCurrencyVo->usdollar_rate        =   round($valObj->val,2);
                           $currencyUpdateObj   = Utils::updateUsdollarRates($objUsdCurrencyVo);
                       }
                }
          }
           
         
           $count =  $count+$max_currencies;
        }
    }

            echo "Currency Updated Successfully."
?>
