#!/usr/local/bin/php -q
<?
include (dirname(__FILE__)."/../includes/common.php");


function LocalCountCourse(){

	global $from_curr_row;
	global $to_curr_row;
	global $formula_type;
	global $formula_value;
	global $formula_fix;
	global $formula_perc;
	global $include_ps_comission;
	
	global $SETTINGS;
 	
	$exchange_rate = GetCourseByID($from_curr_row->currency_id,$to_curr_row->currency_id);
	$ps_comission = ($from_curr_row->who_pay_comission==1?$from_curr_row->payment_system_comission_perc:0) + ($to_curr_row->who_pay_comission==0?$to_curr_row->payment_system_comission_perc:0);
	
	if($formula_type=='value'){
		$course = $formula_value;
	}
	else{
		$course = $exchange_rate*(1+$formula_perc/100) + $formula_fix;
	}
	
	$course = $include_ps_comission==1 ? $course  / (1+$ps_comission) :$course;
	
	return round($course,$SETTINGS['course_precision']);
}

$table = "ex_exchanges";

$ex_res = mysql_query("select * from ex_exchanges where formula_type='formula'");
while($ex_row = mysql_fetch_object($ex_res)){
	$id = $ex_row->id;
	
	$row = mysql_fetch_object(mysql_query("select * from $table where id=$id"));
	
	$from=$row->ex_currency1_id;
	$to=$row->ex_currency2_id;
	$min_amount=floatval($row->min_amount);
	$max_amount=floatval($row->max_amount);
	$stat=$row->status;
	
	$init_value_form = $row->init_value_form;
	$init_value_in = (int)$row->init_value_in;
	$init_value_out = (int)$row->init_value_out;
	$formula_type = $row->formula_type;
	$formula_value = (int)$row->formula_value;
	$formula_perc = (float)$row->formula_perc*100;
	$formula_fix = (int)$row->formula_fix;
	$include_ps_comission = (int)$row->include_ps_comission;
	$current_course = $row->course;
	$current_course_date = strftime("%H:%M  %d %b %Y",$row->course_up_date);
	$current_course_method = stripslashes($row->course_up_method);
	
	
	
	/*
	form currency data
	*/	
	$from_curr_row = mysql_fetch_object(mysql_query("select ex_currencies.*, currencies.symbol as currency_symbol from ex_currencies inner join currencies on currencies.id = ex_currencies.currency_id where ex_currencies.id=$from"));
	$to_curr_row = mysql_fetch_object(mysql_query("select ex_currencies.*, currencies.symbol as currency_symbol from ex_currencies inner join currencies on currencies.id = ex_currencies.currency_id where ex_currencies.id=$to"));
	
	$course =  LocalCountCourse();
	mysql_query("update ex_exchanges set course = '$course', course_up_date=".time().", course_up_method='Автоматическое обновление CRON' where id=$id");	
}	
exit;
?>