<?php
header("Content-type: text/html; charset=windows-1256");

session_start();
if($_SESSION['dbName']==""){
header("location:../index.php");
}


include("config.inc.php");
include("function.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");


$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2 from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

//Read From CashRegister Table
$queryCashRegister=" Select Upper(CashRegister_Name) As CashRegister_Name, CashRegisterTTC ".
				   " From CashRegister ".
				   " Where CashRegister_Name = '".$_SESSION['cashregister']."' ";
$resultCashRegister=mysql_query($queryCashRegister,$connection) or die(mysql_error());
while($rowCashRegister=mysql_fetch_array($resultCashRegister)){
	extract($rowCashRegister);
}


$keyword    = $_POST['keyword']; if ($keyword == ""){$keyword = $_GET["keyword"];}
$$keywordID    = $_POST['$keywordID']; if ($$keywordID == ""){$$keywordID = $_GET["$keywordID"];}
$searchby   = $_POST['searchby']; if ($searchby == ""){$searchby = $_GET["searchby"];}
$orderby    = $_POST['orderby']; if ($orderby == ""){$orderby = $_GET["orderby"];}
$perpage    = $_POST['perpage']; if ($perpage == ""){$perpage = $_GET["perpage"];}
$startrange = $_POST['startrange']; if ($startrange == ""){$startrange = $_GET["startrange"];}


if (empty($orderby)) $orderby = 1;
if (empty($searchby)) $searchby = 1;

if ($startrange=='') $startrange = 0;
if ($perpage=='')	 $perpage = 20;

print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<link rel="stylesheet" type="text/css" media="all" href="css/style.css">
<title>Items Info</title>
<script type="text/javascript" language="javascript">

function ClearForm()
{
  document.search.keyword.value='';

  document.search.action='Form_RetailItemsInfo.php';
  document.search.submit();

}

function LoadForm() 
{
	x=document.search;
	x.keyword.focus();
	
}

function SetNextPage(startrange,perpage){ 

	document.search.action='Form_RetailItemsInfo.php?keyword=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function SetPrvPage(startrange,perpage){

	document.search.action='Form_RetailItemsInfo.php?keyword=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}


function RefreshPage()
{	

  //document.search.keyword.value='';
  document.search.action='Form_RetailItemsInfo.php?startrange=0';
  document.search.submit();

}

function SubmitForm()
{	

  //document.search.keyword.value='';
  document.search.action='Form_RetailItemsInfo.php?startrange=0';
  document.search.submit();

}


function kd(e)
{
	x=document.search;
    var intKey = (window.Event) ? e.which : e.keyCode;
	
    if (intKey == 13) { //enter key
        x.btnSubmit.click();
		return false;
    }
	
	else if (intKey == 27) { //ESC key
		self.close();
		return false;
	}
	
    return true;
}

function SetFirstPage(startrange,perpage){
	document.search.action='Form_RetailItemsInfo.php?startrange=0&perpage='+perpage;
	document.search.submit();
}

function SetLastPage(perpage,totalrows){
	var result=totalrows % perpage;
	if (result == 0) result = perpage;
	startrange=totalrows-result;
	document.search.action='Form_RetailItemsInfo.php?startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function OpenKeyboard(field){
	VirtualKeyboard.attachInput(field);
	VirtualKeyboard.open(field,'td');
}

function printItem(){
	if(!document.getElementById('itemcode0')){
		alert('There is No Item to Print!');
		return 0;
	} else {
		window.open('Rep_itemInfo.php?ic='+document.getElementById('itemcode0').value);
		
	}
	
}

</script>

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 0px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}

.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}

.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}

.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #000000 ;font-size: 10px;}

 button {
  padding: 0 0 0 0;
  margin: 0 3px 0 3px;
  border: none;
  cursor: pointer;
  text-decoration: none;
  outline: none;
  white-space: nowrap;
  overflow: visible;
  height: 22px; 
  font-family: 11px Arial, sans-serif;
  font-size: 13px;
  color: #000000;
  background: none;
  font-weight: bold;

}
</style>

</head>
<body onLoad='LoadForm();' class='bodybackground'>
<form name='search' method='post' action="">
HERE;



$PFind = GetSQLValueString($keyword, 'text');

	

echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed'>";
echo"<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-top2' style='height:34px; width:600px' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>Item Info</span></td></tr>
			</table>
		</td>
		<td id='search-top3' style='height:34px; width:31px;'>&nbsp;</td>
	</tr>";

echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		 <td id='search-content2' >";
			 
echo"<table border='0' cellspacing='1' style='table-layout:fixed; width:600px;'>
	  <col width=120>
	  <col width=480>";

echo" <tr class='Search-header-bg'>";	  
echo"   <td align='left'><span class='style4'>Search</span></td>
		<td align='left'><span class='style4'><input type='text' name='keyword' id='keyword' value='$keyword' style='width:250px;' onkeydown='return kd(event)'  /></span>
			<button type='button' id='btnSubmit' name='btnSubmit' onclick='SubmitForm(); return false;' title='Find'><span><span>Find</span></span></button>
			<button type='button' id='btnCancel' name='btnCancel' onclick='ClearForm();return false;' title='Cancel'><span><span>Cancel</span></span></button>
			<button type='button' id='btnPrint' name='btnPrint' onclick='printItem();return false;' title='Print'><span><span>Print</span></span></button>
		</td>";


echo"</tr>";
echo"	</table></td>
		<td id='search-content3' style='width:31px;'>&nbsp;</td>";
echo"</tr>";	



if ($keyword != '' || $keywordID != '') {
	if($keywordID != '' ){
		$queryItmCode = "select ifnull(item_code ,'') from temp_retaildt where saledt_id = $keywordID";
		// echo $queryItmCode;echo"<BR>";
		$res = mysql_query($queryItmCode,$connection);
		$rows = mysql_fetch_array($res);
		if($rows[0] != '' )
			  $keyword = $rows[0];
	}
	$querysearch = " Select items.Item_Code, Item_Description, Item_Barcode, Item_Barcode2, Item_Barcode3, items.Group_Code, items.SubGroup_Code, ".
				   " Item_SellingPrice1Base1, Item_SellingPrice2Base1, Item_SellingPrice3Base1, Item_SellingPrice4Base1, Item_SellingPrice5Base1, ".
				   " Item_SellingPrice6Base1, Item_SellingPrice1Base2 , Item_Picture,item_location.description1 as dsc1,".
				   " item_location.description2 as dsc2,item_location.description3 as dsc3 , category_name, subgroup_name".
				   " From Items ".
				   " left join item_location on items.item_code = item_location.item_code".
 				   " left join category on items.Category_code = category.Category_Code".
 				   " left join subgroup  on items.SubGroup_Code = subgroup.subgroup_Code".
 				   " Where (upper(items.Item_Code) = upper('".$keyword."') or ".
				   "	    upper(Item_Barcode)  =  upper('".$keyword."') or ".
				   "	    upper(Item_Barcode2) = upper('".$keyword."') or ".
				   "	    upper(Item_Barcode3) = upper('".$keyword."') ) ".
				   " Order By Item_Code ";
		
				 
	// echo $querysearch;exit;		 

	$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());
	$totalrows = mysql_num_rows($resultsearch);
		

			
	$querysearch = $querysearch." limit ".$startrange.",".$perpage;

	$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());	
	
	echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
			<td id='search-content2'>";
	echo"<table border='0'  cellpadding='0' cellspacing='1' style='width:600px;'>
		  <col width=100>
		  <col width=250> 
		  <col width=250>";
			

	$i = 0;
	while($rows=mysql_fetch_array($resultsearch)){
			
		extract($rows);
		$ct+=1;

		//if($ct%2==0)
			$Color="style='background-color:".$grid_color1."'";
		//else			
			//$Color="style='background-color:".$grid_color2."'";	
		
		echo'<tr height="25" '.$Color.'>';
		echo"	<td align='left'><span class='style4'>Item Code</span></td>";
		echo"	<td align='left'><span class='style5'>".$Item_Code."</span><input type='hidden' name='itemcode$i' id='itemcode$i' value='$Item_Code'></td>";
		echo"	<td align='left' rowspan=9><img src='uploads/$Item_Picture' alt='N/A'/></td>";
		echo"</tr>";
		
		echo'<tr height="25" '.$Color.'>';
		echo"	<td align='left'><span class='style4'>Item Name</span></td>";
		echo"	<td align='left'><span class='style5'>".$Item_Description."</span></td>";
		echo"</tr>";
		
		echo'<tr height="25" '.$Color.'>';
		echo"	<td align='left'><span class='style4'>Group</span></td>";
		echo"	<td align='left'><span class='style5'>".$Group_Code."</span></td>";
		echo"</tr>";
		
		echo'<tr height="25" '.$Color.'>';
		echo"	<td align='left'><span class='style4'>SubGroup</span></td>";
		echo"	<td align='left'><span class='style5'>".$subgroup_name."</span></td>";
		echo"</tr>";
		
		echo'<tr height="25" '.$Color.'>';
		echo"	<td align='left'><span class='style4'>Category </span></td>";
		echo"	<td align='left'><span class='style5'>".$category_name."</span></td>";
		echo"</tr>";
		
		echo'<tr height="25" '.$Color.'>';
		echo"	<td align='left'><span class='style4'>Barcode</span></td>";
		echo"	<td align='left'><span class='style5'>".$Item_Barcode."</span></td>";
		echo"</tr>";
		
		echo'<tr height="25" '.$Color.'>';
		echo"	<td align='left'><span class='style4'>Barcode2</span></td>";
		echo"	<td align='left'><span class='style5'>".$Item_Barcode2."</span></td>";
		echo"</tr>";
		
		echo'<tr height="25" '.$Color.'>';
		echo"	<td align='left'><span class='style4'>Barcode3</span></td>";
		echo"	<td align='left'><span class='style5'>".$Item_Barcode3."</span></td>";
		echo"</tr>";
		
		echo'<tr height="25" '.$Color.'>';
		echo"	<td align='left'><span class='style4'>Price1 $base1</span></td>";
		echo"	<td align='left'><span class='style5'>".number_format($Item_SellingPrice1Base1, $DecimalBase1)."</span></td>";
		echo"</tr>";
		
		echo'<tr height="25" '.$Color.'>';
		echo"	<td align='left'><span class='style4'>Price1 $base2</span></td>";
		echo"	<td align='left'><span class='style5'>".number_format($Item_SellingPrice1Base2, $DecimalBase1)."</span></td>";
		echo"	<td align='left'><span class='style5'> Location : ".$dsc1." - ".$dsc2." - ".$dsc3."</span></td>"; 

		echo"</tr>";
		// Adding Quantity Exists
				$QqtyExists = "Select  Sum(QuantityProduceIn - QuantityProduceOut + QuantityEntered - QuantityDelivered )    From StkTransaction where item_code = '".$Item_Code."' ";
				$resExists = mysql_query($QqtyExists, $connection);
				$rowExists =  mysql_fetch_array($resExists);
				$qtyExists = $rowExists[0];
				
				$QqtyExistsWPOS = " select QtyExistsPOS('".$Item_Code."') ";
				$resExistsWPOS = mysql_query($QqtyExistsWPOS, $connection);
				$rowExistsWPOS =  mysql_fetch_array($resExistsWPOS);
				$qtyExistsWPOS = $rowExistsWPOS[0];
				
		echo'<tr height="25" '.$Color.'>';
		echo"	<td align='left'><span class='style4'>Existing before</span></td>";
		echo"	<td align='left'><span class='style5'>".number_format($qtyExists, $DecimalBase1)."</span></td>";
		echo"	<td align='left' colspan=2><span class='style4'>Existing after</span><span class='style5'>&nbsp;&nbsp;".number_format($qtyExistsWPOS, $DecimalBase1)."</span></td>";

		echo"</tr>";
		
	}
								
	echo"</table></td><td id='search-content3' style='width:31px;'>&nbsp;</td></tr>";	
	
}        

echo"<tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; width:600px;' valign='top'></td>
		<td id='search-bottom3' style='height:34px; width:31px;' valign='top'></td>";
echo"</tr>";
echo"</table>";	  

print <<<HERE

</form>
</body>

HERE;

?>