<?
define("ADMIN_AUTHORIZATION",1);
include("../../../includes/common.php");

$id = $_GET['id']==""?$_POST['id']:$_GET['id'];
$action = $_GET['action']==""?$_POST['action']:$_GET['action'];
$page = $_GET['page']==""?$_POST['page']:$_GET['page'];
$sort_by = $_GET['sort_by']==""?$_POST['sort_by']:$_GET['sort_by'];
$sort_ord = $_GET['sort_ord']==""?$_POST['sort_ord']:$_GET['sort_ord'];
$sort = "page=$page&sort_by=$sort_by&sort_ord=$sort_ord";

$pay_flag = false;

if (!is_numeric($id)){
	$_SESSION['ERROR_MSG'] = "Ошибка при передаче данных!";
	header("Location: $SITE_URL/$ADMIN_DIR/orders/index.php?l_sort");
	exit;
}
else{
	/* tranfering money */
	$order_row = @mysql_fetch_object(@mysql_query("select orders.*, ex_currencies.code, ex_currencies.precision from orders left outer join ex_currencies on orders.ex_currency_from_id = ex_currencies.id where orders.id=$id and orders.status=1 and orders.to_status=1 and orders.from_status=1"));

	if($order_row->code == "WMR"||$order_row->code=="WMZ"||$order_row->code == "WME"||$order_row->code == "WMU"){
		$row = mysql_fetch_object(mysql_query("select * from wm_orders where id=$id"));
	}
	elseif($order_row->code == "EPD"){
		if(( $res_arr=EPortCheckInv($order_row->from_order_id,round($order_row->amount_from-$order_row->ex_currency_from_comission,$order_row->precision),"info") )){

			@mysql_query("update eport_orders set status = 0, eport_order_id = '".$res_arr['eport_order_id']."', sum_eye = '".$res_arr['sum_eye']."', sum_rur = '".$res_arr['sum_rur']."' where id=$order_row->from_order_id");

			@mysql_query("update orders set from_status = 0, batch_from ='".$res_arr['eport_order_id']."' where id = $order_row->id");

			/* changing balance */
			$comments = "Зачисление средств из EPort в систему. Операция обмена # $order_row->id.";
			ChangeBalance($order_row->ex_currency_from_id,round($order_row->amount_from-$order_row->ex_currency_from_comission,$order_row->precision),$comments);

			CompleteExchange($order_row->id);

			$pay_flag = true;
		}
	}
}

if($pay_flag){
	$_SESSION['STATUS_MSG'] = "Заказ был оплачен, деньги получателю переведены!";
	header("Location: $SITE_URL/$ADMIN_DIR/orders/index.php?l_sort");
}
else{
	$_SESSION['ERROR_MSG'] = "Заказ еще не оплачен!";
	header("Location: $SITE_URL/$ADMIN_DIR/orders/index.php?l_sort");
}
exit;
?>