<?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); 
			$array = getGlobalSettings(); foreach ($array as $settings){extract($settings);}
		
		?>
			<div class="align-center" style="margin:25%"><img src="../img/loading.gif" alt="@" /><br />
			<?php echo $lbl_thank_u_for_waiting ?></div>
		<?php //foreach($_POST as $key => $value) $_POST[$key] = urldecode(trim($value));
		$barcode_id = mysqli_real_escape_string($mysqli, $_POST['barcode_id']);
		$customer_id = mysqli_real_escape_string($mysqli, $_POST['customer_id']);
		$input = str_replace(',', '.', $_POST['input']);
		$description = $_POST['description'];
		$total_amount = mysqli_real_escape_string($mysqli, $_POST['sumbox']);
		$errors = array();

		if (strlen(implode($input)) == 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, credits, 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[]= "Δε βρέθηκε Πελάτης!";
		$str = implode("\n", $errors);
		die("Σφάλμα Αναζήτησης:\n\n" . $str);
		}else {	
		$cus = mysqli_fetch_array($result, MYSQLI_ASSOC);
		$cust_to_update = $cus['id'];
		$cust_credits = $cus['credits'];
		$cust_category_id = $cus['group_id'];
		$random_string = rand_string(20);
		
				foreach ($input as $key => $val){
				if(!empty($val)){
					$cred = "SELECT id, credits, description  FROM cls_categories WHERE id='$description[$key]'";
					$result_cred = mysqli_query($mysqli, $cred) or die("Query failed : " . mysqli_error($mysqli));
					$line = mysqli_fetch_array($result_cred, MYSQLI_ASSOC);
						$cat_id[$key] = $line['id'];
						$description[$key] = $line['description'];
						$credits[$key] = round(($line['credits'] *  $val), 0); 	
						$multiplier[$key] = $line['credits'];##Update Transactions Table				
					mysqli_query($mysqli, "SET NAMES 'utf8' " ); //Gia Ellinika
					//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, category_id, branch_id) 
					VALUES (NULL , '$customer_id', '$barcode_id', '$cust_category_id', 'Αγορές', '$description[$key]', '$val',  '$credits[$key]', NOW(), '$user', '$random_string', '$total_amount', '$multiplier[$key]', '$cat_id[$key]', '$user_branch')") or die(mysqli_error($mysqli));
					}
				}
				//calculate total asum of array credits
				$sum_credits = array_sum($credits);
				//update customer credits and amount
				mysqli_query ($mysqli, "UPDATE cls_customers set turnover=turnover + '$total_amount', credits= credits +'$sum_credits',
							 score = score + '$sum_credits', last_transaction_date = NOW() WHERE id='$customer_id'") or die("Failed to update cust balance ". mysqli_error($mysqli));
				//update transactions log (user's last transaction)
				mysqli_query ($mysqli, "UPDATE users set params='$random_string' WHERE username='$user'") or die("Failed to update transaction log ". mysqli_error($mysqli));

				?>
				<script>window.location = "idle.php"</script>
				<?php //Print Receipt if enabled by the user - if enabled on system parameters
				$mode='live';
				$total_credits = $cust_credits + $sum_credits;
				$template = 1;
					if ($print_receipt == 1){
						echo printTestReceipt($template, $sum_credits, $total_credits, $random_string, $barcode_id, $mode);
					}
				}
				mysqli_close($mysqli); 

if (sizeof($errors) > 0) {
  $str = implode("\n", $errors);
  die("Σφάλμα Αναζήτησης:\n\n" . $str);
}
die('OK'); // send success indicator
?>
</body>
</html>
<?php
} else {
    header('Location: login.php');
}
?>