<?php session_start();
        include ('../scripts/classes.php');
		if (isset($_SESSION['logged']) && $_SESSION['logged'] == 'yes') {
		if ($_SESSION['lang']=="EN"){include ('../lang/en.php');}elseif ($_SESSION['lang']=="GR"){include ('../lang/gr.php');}else {include ('../lang/en.php');}
			$mode=$lbl_transaction_entry;
			$page_title =$lbl_cls_transaction_entry ;
			$array = getGlobalSettings(); foreach ($array as $settings){extract($settings);}
			$calibration = getScreenSettings(2); foreach ($calibration as $screen_settings){extract($screen_settings);}
	
	if ($_SERVER['REQUEST_METHOD'] != 'GET') exit;
	$barcode = mysqli_real_escape_string($mysqli, $_GET['barcode']);				
	//Customer Match Query
	$match = "SELECT a.id as cust, a.barcode_id, a.last_name, a.first_name, a.credits, a.birth_date, a.is_active, a.last_transaction_date, b.name AS name 
				FROM cls_customers a
				INNER JOIN cls_customer_groups b ON a.group_id=b.id 
				WHERE barcode_id='$barcode'";
                
	$result = mysqli_query($mysqli, $match) or die("Query failed : " . mysqli_error($mysqli));
	if (mysqli_num_rows($result) == 0){
		echo "System Error";
		$str = implode("\n", $errors);
		die("Search error:\n\n" . $str);
	//exit();
	}else {
		while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
		$barcode_id = $row['barcode_id']; 
		$customer_id = $row['cust']; 
		$last_name = $row['last_name'];  
		$first_name = $row['first_name']; 
		$group_id = $row['name'];
		$birth_date = $row['birth_date'];
		$active = $row['is_active'];
		$credits = $row['credits'];
		$last_transaction_date = $row['last_transaction_date'];
	}
}
?>
<!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" />
<script type="text/javascript" src="../scripts/autoSum.js"></script>
<script type="text/javascript" src="../scripts/menu.js" ></script>
<script>
function checkSubmit(e)
{
   if(e && e.keyCode == 13)
   {
      if (getsum()) {document.forms['transfrm'].submit()};
   } else if(e && e.keyCode == 112)
   {
   	window.location = "idle.php";
}
}
</script>
<script>
function validate(evt) {
  var theEvent = evt || window.event;
  var key = theEvent.keyCode || theEvent.which;
  key = String.fromCharCode( key );
	  var regex = /[0-9 \s \b]|\./;
  if( !regex.test(key) ) {
    theEvent.returnValue = false;
    if(theEvent.preventDefault) theEvent.preventDefault();
  }
}
</script>
<script>
function getsum() {
var fields = document.transfrm.elements["input[]"];
var total= new Number; 

for (i=0;i<fields.length;i++)
	{
 	  total = total - fields[i].value;
      var max_value = fields[i].getAttribute("data-max_attr"); 
      if (fields[i].value > 0) {
        if (Math.abs(fields[i].value) > max_value){
            alert("<?php echo _THE_VALUE ?>" + " " + fields[i].value + " <?php echo _EXCEEDS_MAX_ALLOWED_VALUE ?> (" + max_value + ")");
            return false;
        }  
      }
	}
	if (Math.abs(total)>0 && Math.abs(total)!= 'Nan'){
	   return confirm("<?php echo $lbl_total_sales_amount; ?>" + Math.abs(total));
	}else {
	   alert ('<?php echo $lbl_check_your_input; ?>');
	}
}
</script>
<?php //script to show numpad ?>
	<script>
		function Myfocus(pad, records_count) {
		var mypad = pad.id;
		var records = records_count;
			for (x=0;x<records;x++)
				{
				 document.getElementById('pad'+[x]).style.visibility = 'hidden';	
				}	
		//alert(records);
			document.getElementById(mypad).style.visibility = 'visible';
	}
	</script>
<?php //script to hide numpad ?>
	<script>
		function Myblur(pad) {
		var mypad = pad.id;
		document.getElementById(mypad).style.visibility = 'hidden';
	}
</script>
<script>	
		function insertThisInThere(thisChar, thereId) {
		function theCursorPosition(ofThisInput) {
		// set a fallback cursor location
		var theCursorLocation = 0;	 
		// find the cursor location via IE method...
		if (document.selection) {
		ofThisInput.focus();
		var theSelectionRange = document.selection.createRange();
		theSelectionRange.moveStart('character', -ofThisInput.value.length);
		theCursorLocation = theSelectionRange.text.length;
		} else if (ofThisInput.selectionStart || ofThisInput.selectionStart == '0') {
		// or the FF way
		theCursorLocation = ofThisInput.selectionStart;
		}
		return theCursorLocation;
		}
		// now get ready to place our new character(s)...
		var theIdElement = document.getElementById(thereId);
		var currentPos = theCursorPosition(theIdElement);
		var origValue = theIdElement.value;
		var newValue = origValue.substr(0, currentPos) + thisChar + origValue.substr(currentPos);
		theIdElement.value = newValue;	 
		}
</script>

</head>
<body onload="document.getElementById('input0').focus()">
<script>
top.document.title=self.document.title;
</script>
		<div onkeypress="return checkSubmit(event)">
		<fieldset style="min-height:200px">
		<legend><?php echo  $mode ?></legend>
		<br />
		<div  class="success">
		<ul>
			<li>
				<label class="bar_label_ready"><?php echo $lbl_found_match ?></label>
				<label style="font-size:20px;color:#999999;"> <?php echo $barcode_id; ?></label>
				<label style="font-size:20px;color:#43759A;"> <?php echo "(".$last_name; ?> </label>
				<label style="font-size:20px;color:#43759A;"> <?php echo $first_name; ?> </label>
				<label style="font-size:20px;color:#43759A"> <?php echo date("d.m.Y",strtotime( $birth_date));?> </label>
				<label style="font-size:20px;color:#43759A;"> <?php echo $group_id ; ?> </label>
				<label style="font-size:20px;color:#43759A;"> <?php echo "-".$lbl_balance." ".$credits.")"; ?> </label>
			</li>
		</ul>
		</div>
        <?php 
			if ($active==0){
				echo "<div class='critical'><ul><li><label style='font-size:20px;color:#FFF;'>$lbl_customer_is_inactive ($lbl_last_transaction_date:".date("d.m.Y",strtotime($last_transaction_date)).")</label></li></ul></div>";
				echo "<div style=\"float:right; padding:5px\" align=\"right\"><input type=\"button\" id=\"go_idle\" value=\"$lbl_idle\" class=\"big_idle\" onclick=\"window.location.href='idle.php'\"/></div>";
			exit();
			}
		?>
		<div style="float:right; padding:5px" class="align-right">
			<input type="button" id="go_idle" value="<?php echo $lbl_idle ?>" class="big_idle" onclick="window.location.href='idle.php'" /><br />
			<input type="button" id="go_reset" value="Reset" class="big_reset" onclick="document.forms['transfrm'].reset(); document.getElementById('input0').focus();" /><br />
			<input type="button" id="go_submit" value="OK" class="big_ok"  onclick="if (getsum()) {document.forms['transfrm'].submit()};"/>
		</div>
		<div style="width:82%; padding:15px;">
		<?php

			//Customer Match Query
			$cats = "SELECT id, description, image, active, credits, max_value FROM cls_categories WHERE active='1' ORDER BY ordering ASC";
			$result = mysqli_query($mysqli, $cats) or die("Query failed : " . mysqli_error($mysqli));
			$num_rows = mysqli_num_rows($result);
			//echo $num_rows;
			if (mysqli_num_rows($result) == 0){
				echo $lbl_no_cls_categories;
				}
			else {
			$records_count = mysqli_num_rows($result);
			//echo $records_count;
			echo "<form name='transfrm' id='transfrm' action='create_transaction.php' method='post'>";
			echo "<div style='width:100%; padding:15px; background-color:#CCC' align='center'><label style='font-size:".$header_fonts."px'>".
			$lbl_total_sales_amount." ".$currency."</label> 
			<input type='text' class='transbox' name='sumbox' readonly='readonly' style='height:".$detail_fields_height."px; width:".$detail_fields_width."px; font-size:".$detail_fonts."px'></div>";
			echo "<input type='hidden' name='barcode_id' id='barcode_id' value='$barcode' />";
			echo "<input type='hidden' name='customer_id' id='customer_id' value='$customer_id' />";
			echo "<br />";
			$input = array();
				for($i=0;$i<$num_rows;$i++){
				$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
				$id = $row['id']; 
				$description = $row['description']; 
                $image = $row['image'];  
				if ($image =='') $image = 'no_image.jpg';
				echo "<div>";
				?>
				<?php 
				//~~~~~~~~~~~~~~~~~~~~~~~~~~~~check if POS is activated~~~~~~~~~~~~~~~~~~~~~~~~~~
				if (isset($_SESSION['POS']) && $_SESSION['POS'] == 'ON'){
				include ('modules/pos/numeric_keypad.php');
				}
				//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
				?>
				<?php
				echo "<label style='font-size:".$detail_fonts."px'>$currency&nbsp;</label>
				<input type='text' name='input[]' id='input$i' ".(($row['max_value'] == '' || $row['max_value'] == '0') ? "data-max_attr='9999999999'" : "data-max_attr='".$row['max_value']."'")."
				style='height:".$detail_fields_height."px; width:".$detail_fields_width."px; font-size:".$detail_fonts."px'
				onFocus='startCalc(); Myfocus(pad$i, $records_count);' onBlur='startCalc();' class='transbox' autocomplete='off' onkeypress='validate(event);'>
				<img src='img/$image' style='height:".$detail_images."px; width:".$detail_images."px;' /><label style='font-size:".$detail_fonts."px'>$description</label>
				<input type='hidden' name='description[]' value='$id'></div><br />";
				}
			echo "</form>";	
			}
			?>
		</div>	
		</fieldset>
        </div>
</body>
</html>
<?php
} else {
    header('Location: login.php');
}
?>

