<?php session_start();
		if (isset($_SESSION['logged']) && $_SESSION['logged'] == 'yes') {
            $user = $_SESSION['user'];
			include ('../scripts/classes.php'); if ($_SESSION['lang']=="EN"){include ('../lang/en.php');}elseif ($_SESSION['lang']=="GR"){include ('../lang/gr.php');}else {include ('../lang/en.php');}
		if ($_SERVER['REQUEST_METHOD'] != 'POST') exit;
        $user_branch = $_SESSION['branch'];
        if ($user_branch == '') exit (_USER_DOES_NOT_BELONG_TO_A_BRANCH); 
		?>
			<div class="align-center" style="margin:25%"><img src="../img/loading.gif" alt="@" /><br />
			<?php echo $lbl_thank_u_for_waiting ?></div>
		<?php 
		$barcode_id = mysqli_real_escape_string($mysqli, $_POST['barcode_id']);
		$customer_id = mysqli_real_escape_string($mysqli, $_POST['customer_id']);
		$amount = mysqli_real_escape_string($mysqli, $_POST['inputRedeem']);
		$redeem_credits = mysqli_real_escape_string($mysqli, $_POST['sumbox']);
		$multiplier = mysqli_real_escape_string($mysqli, $_POST['multiplier']);
		$errors = array();

		if ($redeem_credits == 0){
			$errors[]= "No transaction";
			$str = implode("\n", $errors);
			die($str);
			}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $page_title ?></title>
<link rel="shortcut icon" href="../img/favicon.png" />
<link href="../cms1.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php		
	//Customer Match Query
	$match = "SELECT id, group_id FROM cls_customers WHERE barcode_id='$barcode_id'";
	$result = mysqli_query($mysqli, $match) or die("Query failed : " . mysqli_error($mysqli));
		if (mysqli_num_rows($result) == 0){
			$errors[]= "No customer found!";
			$str = implode("\n", $errors);
			   die("Search error:\n\n" . $str);
			}else {	
			$cus = mysqli_fetch_array($result, MYSQLI_ASSOC);
			$cust_to_update = $cus['id'];
			$cust_category_id = $cus['group_id'];
			$random_string = rand_string(20);						
			mysqli_query($mysqli, "SET NAMES 'utf8'") or die("Query failed : " . mysqli_error($mysqli));
			//check for field values
			mysqli_query ($mysqli, "INSERT INTO cls_transactions 
			(id, customer_id, barcode_id, cust_category_id, transaction_type, category, amount, credits, created_on, created_by, transaction_id, trans_total_amount, multiplier, branch_id) 
			VALUES (NULL , '$customer_id', '$barcode_id', '$cust_category_id', 'Εξαργύρωση', 'Έκπτωση', '$amount',  '-$redeem_credits', NOW(), '$user', '$random_string', '$amount', '$multiplier', '$user_branch')") or die(mysqli_error($mysqli));					
		}
			//update customer credits and amount
			mysqli_query ($mysqli, "UPDATE cls_customers SET credits = credits -'$redeem_credits', redeem_amount = redeem_amount + '$amount', last_transaction_date = NOW()
						WHERE id='$customer_id'") or die("Failed to update cust balance ". mysqli_error($mysqli));
							//update_transactions_log
			mysqli_query ($mysqli, "UPDATE users SET params='$random_string' WHERE username='$user'") or die("Failed to update transaction history ". mysqli_error($mysqli));
			mysqli_close($mysqli); 
			?>
			<script type="text/javascript">window.location = "idle.php"</script>
    <?php if (sizeof($errors) > 0) {
  $str = implode("\n", $errors);
  die("Search error:\n\n" . $str);
}
die('OK'); // send success indicator
} else {
    header('Location: login.php');
}
?>