<?php
include_once("../includes/config.php");
include_once("../includes/arrays.inc.php");
include_once("../includes/functions.php");
include_once(BASE_URL.'/classes/cls_utils.php');
//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);
                            }
                    }

            }
        }

 $currencylistObj                   =   Utils::getActiveCurrencyList();
 $admin_currency                    =   getSettingsValue('admin_currency');
 $currency_settings_mode            =   getSettingsValue('currency_settings');
 $currency_api_key                  =   getSettingsValue('currency_api_key');

  if($currencylistObj->status==SUCCESS)
        {
            $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://www.freecurrencyconverterapi.com/api/v3/convert?';
                    $endPointUrl        =  $api_end_point."q=".$currencyCodes;
                    $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=".$currencyCodes;
                    $endPointUrl_Usd    =   $api_end_point."&q=".$currencyUsdSting;
                }

                $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);
                       }
                }
            }
?>
