Просмотр файла www/admin/exchange/add.php

Размер файла: 9.98Kb
<?
define("ADMIN_AUTHORIZATION",1);
include("../../../includes/common.php");

$ex_currs1 = array(array("id" => 0,
						"name" => "Все",
						"selected" => ($ex1_id==0)?"selected=\"selected\"":"",
						));
$ex_currs2 = array(array("id" => 0,
						"name" => "Все",
						"selected" => ($ex2_id==0)?"selected=\"selected\"":"",
						));

$ex_curr_res = mysql_query("select ex_currencies.*, currencies.code as real_curr_code from ex_currencies inner join currencies on currencies.id = ex_currencies.currency_id order by ex_currencies.name");
while ($ex_curr_row = mysql_fetch_object($ex_curr_res))
{
	$ex_currs1[] = array("id" => $ex_curr_row->id,
						"name" => $ex_curr_row->name,
						"selected" => ($ex1_id==$ex_curr_row->id)?"selected=\"selected\"":"",
						);
	
	$ex_currs2[] = array("id" => $ex_curr_row->id,
						"name" => $ex_curr_row->name,
						"selected" => ($ex2_id==$ex_curr_row->id)?"selected=\"selected\"":"",
						);
}
$smarty->assign("ex_currs1", $ex_currs1);
$smarty->assign("ex_currs2", $ex_currs2);

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";
$action = "update";
$id = (int)(trim($_GET['id']=="")?trim($_POST['id']):trim($_GET['id'])+0);

$row = mysql_fetch_object(mysql_query("select * from $table where id=$id"));

if($row->id==""&&$id>0){
	$_SESSION['ERROR_MSG'] = "Ошибка при работе с данными, направление ID $id не обнаружено.";
	header("Location: ./index.php?l_pager");
	exit;
}
elseif($id==0){
	$action = "add";
}

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



if($action=="update"){
/*
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"));
}
elseif($action=="add"){
	$res = mysql_query("select ex_currencies.*, currencies.code as real_curr_code from ex_currencies inner join currencies on currencies.id = ex_currencies.currency_id order by ex_currencies.name");

	$i=1;
	while($row=mysql_fetch_object($res)){
		$ex_currs_ids[$i]=$row->id;
		$ex_currs_names[$i]=stripslashes($row->name)."($row->code)";	
		$i++;
	}
	$smarty->assign('ex_currs_ids',$ex_currs_ids);
	$smarty->assign('ex_currs_names',$ex_currs_names);
}

if(isset($_POST['subm'])&&$_POST['id']>0){
	reset ($_POST);
	while (list($key, $value) = each ($_POST)) {
		$$key = $value;
	}

	if($_POST['action']=="add"&&is_numeric($_POST['from'])&&is_numeric($_POST['to'])&&$_POST['from']>0&&$_POST['to']>0){
		$row = mysql_fetch_object(mysql_query("select * from ex_exchanges where ex_currency1_id =".$_POST['from']." and ex_currency2_id =".$_POST['to']));
		if($row->id!=""){
			$error .="Направление обмена уже существует.<br>\n";
		}
		else{
			$row = mysql_fetch_object(mysql_query("select * from ex_currencies where id=".$_POST['from']));
			if($row->id==""){
				$error .="Укажите, откуда идет обмен.<br>\n";
			}
			$row = mysql_fetch_object(mysql_query("select * from ex_currencies where id=".$_POST['to']));
			if($row->id==""){
				$error .="Укажите, куда идет обмен.<br>\n";
			}

			if($_POST['from']==$_POST['to']){
				$error .="Валюты должны быть разными.<br>\n";
			}
		}
	}
	elseif($_POST['action']=="add"){
		$error .="Укажите направление обмена, пожалуйста.<br>\n";
	}

	if(!(is_numeric($_POST['min_amount'])&&$_POST['min_amount']>=0)){
		$error .= "Укажите минимальную сумму обмена.<br>\n";
	}
	if(!(is_numeric($_POST['max_amount'])&&$_POST['max_amount']>=0)){
		$error .= "Укажите максимальную сумму обмена.<br>\n";
	}
	
	if(!($_POST['status']==0||$_POST['status']==1)){
		$error .="Укажите статус, пожалуйста.<br>\n";
	}
	
	
	if($error==""){
		$arr = Array(
			"min_amount" =>floatval($min_amount),   
			"max_amount" =>floatval($max_amount),   
			"init_value_form" => $init_value_form,   
			"init_value_in" =>floatval($init_value_in),   
			"init_value_out" =>floatval($init_value_out),   
			"formula_type" =>$formula_type,   
			"formula_value" =>floatval($formula_value),   
			"formula_perc" =>floatval($formula_perc)/100,   
			"formula_fix" =>floatval($formula_fix),   
			"include_ps_comission" => isset($_POST['include_ps_comission'])?1:0,
			"course" => LocalCountCourse(),   
			"course_up_date" => time(),   
			"course_up_method" => "Администратор ".$ADMIN_DATA['name']."[ID $ADMIN_ID]",   
			"status"=>$stat
		);
		
		if ($id<=0)
		{
			$arr['ex_currency1_id']=$from;
			$arr['ex_currency2_id']=$to;
		}
		
		if(($id<0?AddRecord($table,$arr,TRUE):UpdateRecord($table,$arr,$id)) ){
			$_SESSION['STATUS_MSG'] = "Информация успешно сохранена!";
			header("Location: ./index.php");
			exit;
		}
		else{
			$_SESSION['ERROR_MSG'] = "Ошибка при работе с базой данных.";
			header("Location: ./index.php");
			exit;
		}
	}
}	
elseif(isset($_POST['subm'])&&$_POST['action']=="add"){
	reset ($_POST);
	while (list($key, $value) = each ($_POST)) {
		$$key = $value;
	}

	if(!(is_numeric($_POST['curr_from_id'])&&$_POST['curr_from_id']>=0)){
		$error .= "Укажите, с какой валюты будет идти обмен.<br>\n";
	}
	if(!(is_numeric($_POST['curr_to_id'])&&$_POST['curr_to_id']>=0)){
		$error .= "Укажите, на какую валюту будет идти обмен.<br>\n";
	}
	
	if(trim($error)==""){
		$res = mysql_query("select * from $table where ex_currency1_id=".$_POST['curr_from_id']." and ex_currency2_id=".$_POST['curr_to_id']."");
		if(mysql_num_rows($res)>0){
			$error .= "Это направление уже существует.";
		}
	}	
	
	if($error==""){
		$arr = Array(
			"ex_currency1_id" =>floatval($curr_from_id),   
			"ex_currency2_id" =>intval($curr_to_id),   
			"status"=>0
		);
		
		if(($id=AddRecord($table,$arr,TRUE))){
			header("Location: ./add.php?id=".$id);
			exit;
		}
		else{
			$_SESSION['ERROR_MSG'] = "Ошибка при работе с базой данных.";
			header("Location: ./index.php");
			exit;
		}
	}
}



/* smarty */
$smarty->assign('id',$id);
$smarty->assign('currency_from_name',stripslashes($from_curr_row->name));
$smarty->assign('currency_from_symbol',stripslashes($from_curr_row->currency_symbol));
$smarty->assign('currency_to_symbol',stripslashes($to_curr_row->currency_symbol));
$smarty->assign('currency_to_name',stripslashes($to_curr_row->name));
$smarty->assign('currency_from_id',stripslashes($from_curr_row->id));
$smarty->assign('currency_to_id',stripslashes($to_curr_row->id));

$smarty->assign('min_amount',$min_amount);
$smarty->assign('max_amount',$max_amount);
$smarty->assign('init_value_form',$init_value_form);
$smarty->assign('init_value_in',$init_value_in);
$smarty->assign('init_value_out',$init_value_out);

$smarty->assign('current_course',$current_course);
$smarty->assign('current_course_date',$current_course_date);
$smarty->assign('current_course_method',$current_course_method);

$smarty->assign('stat',$stat);
$smarty->assign('formula_type',$formula_type);
$smarty->assign('formula_value',$formula_value);
$smarty->assign('formula_perc',$formula_perc);
$smarty->assign('formula_fix',$formula_fix);
$smarty->assign('include_ps_comission',$include_ps_comission);

$smarty->assign('exchange_rate',GetCourseByID($from_curr_row->currency_id,$to_curr_row->currency_id));
$smarty->assign('course_prec',pow(10,$SETTINGS['course_precision']));

$smarty->assign('from_ps_comission',(float)$from_curr_row->payment_system_comission_perc);
$smarty->assign('from_ps_whopay',(int)$from_curr_row->who_pay_comission);
$smarty->assign('to_ps_comission',(float)$to_curr_row->payment_system_comission_perc);
$smarty->assign('to_ps_whopay',(int)$to_curr_row->who_pay_comission);
$smarty->assign('include_ps_comission_value',($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));

$page_title = $id>0?"Редактирование направления обмена":"Добавление нового направления обмена";
$smarty->assign('index_page','./index.php?l_pager');
$smarty->assign('page_title',$page_title);
$smarty->assign('top_menu_tpl',"admin_exchange_menu.tpl");

$smarty->assign('action',$action);

$smarty->assign('error',$error);
$smarty->assign('status',$status);

/* table template */
$smarty->assign('fields',$fields);
$smarty->assign('table_width',"95%");
//$smarty->assign('main_tpl',"admin_add_form.tpl");
$smarty->assign('main_tpl',"admin_exchange_add.tpl");
/* display content */
$smarty->display('admin_index.tpl');

?>