<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Cronjob extends CI_Controller {

	public function currency($key = NULL)
	{
		set_time_limit(0);
		
		if($key == 'uc8r1n37zk2s') {
			$ch = curl_init(CURRENCY_API);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);		
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                       	
			curl_setopt($ch, CURLOPT_HTTPHEADER, 
				array(
					'charset=UTF-8',
					'Content-Type: application/x-www-form-urlencoded'
				)
			);   	
			$response = curl_exec($ch);
			curl_close($ch);
			
			$arr = json_decode($response, TRUE);
			$usd = (isset($arr['data']['USD']) ? $arr['data']['USD'] : 0);
			$myr = (isset($arr['data']['MYR']) ? $arr['data']['MYR'] : 0);
			$idr = (isset($arr['data']['IDR']) ? $arr['data']['IDR'] : 0);
			$usdt = (isset($arr['data']['USD']) ? $arr['data']['USD'] : 0);
			$thb = (isset($arr['data']['THB']) ? $arr['data']['THB'] : 0);
			$vnd = (isset($arr['data']['VND']) ? $arr['data']['VND'] : 0);
			$sgd = (isset($arr['data']['SGD']) ? $arr['data']['SGD'] : 0);
			
			$query = $this->db->query("SELECT * FROM sw_setting LIMIT 1")->row_array();
			$in_rate = ($query['topup_rate'] + $query['currency_in_rate']);
			$out_rate = ($query['withdrawal_rate'] + $query['currency_out_rate']);
			
			if($usd > 0) {
				$usd_in_margin = (($usd * $in_rate) / 100);
				$usd_in_amount = ($usd + $usd_in_margin);
				$usd_out_margin = (($usd * $out_rate) / 100);
				$usd_out_amount = ($usd - $usd_out_margin);
				
				$this->db->query("UPDATE sw_currencies SET t_currency_rate = ?, w_currency_rate = ? WHERE currency_id = ? LIMIT 1", array($usd_in_amount, $usd_out_amount, 1));
			}
			
			if($myr > 0) {
				$myr_in_margin = (($myr * $in_rate) / 100);
				$myr_in_amount = ($myr + $myr_in_margin);
				$myr_out_margin = (($myr * $out_rate) / 100);
				$myr_out_amount = ($myr - $myr_out_margin);
				
				$this->db->query("UPDATE sw_currencies SET t_currency_rate = ?, w_currency_rate = ? WHERE currency_id = ? LIMIT 1", array($myr_in_amount, $myr_out_amount, 3));
			}
			
			if($idr > 0) {
				$idr_in_margin = (($idr * $in_rate) / 100);
				$idr_in_amount = ($idr + $idr_in_margin);
				$idr_out_margin = (($idr * $out_rate) / 100);
				$idr_out_amount = ($idr - $idr_out_margin);
				
				$this->db->query("UPDATE sw_currencies SET t_currency_rate = ?, w_currency_rate = ? WHERE currency_id = ? LIMIT 1", array($idr_in_amount, $idr_out_amount, 4));
			}
			
			if($usdt > 0) {
				$usdt_in_margin = (($usdt * $in_rate) / 100);
				$usdt_in_amount = ($usdt + $usdt_in_margin);
				$usdt_out_margin = (($usdt * $out_rate) / 100);
				$usdt_out_amount = ($usdt - $usdt_out_margin);
				
				$this->db->query("UPDATE sw_currencies SET t_currency_rate = ?, w_currency_rate = ? WHERE currency_id = ? LIMIT 1", array($usdt_in_amount, $usdt_out_amount, 5));
			}
			
			if($thb > 0) {
				$thb_in_margin = (($thb * $in_rate) / 100);
				$thb_in_amount = ($thb + $thb_in_margin);
				$thb_out_margin = (($thb * $out_rate) / 100);
				$thb_out_amount = ($thb - $thb_out_margin);
				
				$this->db->query("UPDATE sw_currencies SET t_currency_rate = ?, w_currency_rate = ? WHERE currency_id = ? LIMIT 1", array($thb_in_amount, $thb_out_amount, 6));
			}
			
			if($vnd > 0) {
				$vnd_in_margin = (($vnd * $in_rate) / 100);
				$vnd_in_amount = ($vnd + $vnd_in_margin);
				$vnd_out_margin = (($vnd * $out_rate) / 100);
				$vnd_out_amount = ($vnd - $vnd_out_margin);
				
				$this->db->query("UPDATE sw_currencies SET t_currency_rate = ?, w_currency_rate = ? WHERE currency_id = ? LIMIT 1", array($vnd_in_amount, $vnd_out_amount, 7));
			}
			
			if($sgd > 0) {
				$sgd_in_margin = (($sgd * $in_rate) / 100);
				$sgd_in_amount = ($sgd + $sgd_in_margin);
				$sgd_out_margin = (($sgd * $out_rate) / 100);
				$sgd_out_amount = ($sgd - $sgd_out_margin);
				
				$this->db->query("UPDATE sw_currencies SET t_currency_rate = ?, w_currency_rate = ? WHERE currency_id = ? LIMIT 1", array($sgd_in_amount, $sgd_out_amount, 8));
			}
		}	
	}
}

/* End of file cronjob.php */
/* Location: ./application/controllers/cronjob.php */