<?php
session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
include("config.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");

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

$query1="select grid_color1, grid_color2, DecimalBase, DecimalBase1, DecimalBase2, base1, base2 from setup";
$result1=mysql_query($query1,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($result1)){
extract($rows);

}

//Read From Module Table
$querymodule=" Select ifNull(Module_Grn,0) As Module_Grn, ifNull(Module_SerialNo,0) As ModuleSerialNo From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

//Gilbert 20-11-2013
$query_login=" select ifNull(FI_Items,0) as FI_Items, ifNull(FI_ItemsApp,0) as FI_ItemsApp , ifNull(FI_ShowCostPrice,0) as FI_ShowCostPrice  ".
			 " from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
$resultlogin=mysql_query($query_login,$connection) or die(mysql_error());

while($rows=mysql_fetch_array($resultlogin)){
	extract($rows);
}

$querygroup=" select distinct group_code,group_name ".
			" from grouping where group_code in (select group_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."') ".
			" order by group_code";
$resultgroup = mysql_query($querygroup,$connection) or die(mysql_error());
$groupexist   = mysql_num_rows($resultgroup);

$item	   	= $_GET['item']; if ($item == ""){$item = $_POST["item"];}
$index		= $_GET['index']; if ($index == "") $index = $_POST['index'];
$SCode		= $_GET['SCode']; if ($SCode == "") $SCode = $_POST['SCode'];
$perpage   	= $_GET['perpage']; if ($perpage == ""){$perpage = $_POST["perpage"];}
$startrange = $_GET['startrange']; if ($startrange == ""){$startrange = $_POST["startrange"];}
$TransDate  = $_GET['TransDate']; if ($TransDate == ""){$TransDate = $_POST["TransDate"];}
$TransID  	= $_GET['TransID']; if ($TransID == ""){$TransID = $_POST["TransID"];}
$ItemType   = $_GET['ItemType']; if ($ItemType == ""){$ItemType = $_POST["ItemType"];}
$ItemSort   = $_GET['ItemSort']; if ($ItemSort == ""){$ItemSort = $_POST["ItemSort	"];}
$ItemGroup  = $_GET['ItemGroup']; if ($ItemGroup == ""){$ItemGroup = $_POST["ItemGroup"];}
$PItem_Code = $_POST["PItem_Code"]; if ($PItem_Code == ""){$PItem_Code = $_GET["PItem_Code"];}
$PItem_Description  = $_POST["PItem_Description"]; if ($PItem_Description == ""){$PItem_Description = $_GET["PItem_Description"];}

$vtext1 = $_GET['vtext1']; if ($vtext1 == ""){$vtext1 = $_POST["vtext1"];}
$vtext2 = $_GET['vtext2']; if ($vtext2 == ""){$vtext2 = $_POST["vtext2"];}
$vtext3 = $_GET['vtext3']; if ($vtext3 == ""){$vtext3 = $_POST["vtext3"];}
$vtext4 = $_GET['vtext4']; if ($vtext4 == ""){$vtext4 = $_POST["vtext4"];}
$vtext5 = $_GET['vtext5']; if ($vtext5 == ""){$vtext5 = $_POST["vtext5"];}
$vtext6 = $_GET['vtext6']; if ($vtext6 == ""){$vtext6 = $_POST["vtext6"];}
$text1value   = $_GET['text1value']; if ($text1value == ""){$text1value = $_POST["text1value"];} 
$i_unitcoef   = $_GET['i_unitcoef']; if ($i_unitcoef == ""){$i_unitcoef = $_POST["i_unitcoef"];} 
$txtItemUnit1 = $_GET['txtItemUnit1']; if ($txtItemUnit1 == ""){$txtItemUnit1 = $_POST["txtItemUnit1"];} 
$txtItemUnit2 = $_GET['txtItemUnit2']; if ($txtItemUnit2 == ""){$txtItemUnit2 = $_POST["txtItemUnit2"];} 
   
if($_GET['lcode']!=""){

	$query1=" select Item_Code, Item_Description, if(Items.Item_Vat !='', 1, 0) As ItemVat, ifNull(item_withserialno,0) As ItemWithSerial, ".
	        " ifNull(Items.Item_Production,0) As ItemProduction, ifNull(Items.Item_SemiFinished,0) As ItemSemiFinished ".
            " from items where item_code='".$_GET['lcode']."'";
	$result1=mysql_query($query1,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($result1)){
    	extract($rows);
	}

   $_GET['post']="1";
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Select Items</title>
<script type="text/javascript" language="javascript">

function clearform()
{
	document.form1.PItem_Description.value='';
	document.form1.PItem_Code.value='';
	document.form1.SCode.value='';
	
	vItem      = document.form1.item.value;
	vItemType  = document.form1.ItemType.value;
	vItemGroup = document.form1.ItemGroup.value;
	vTransDate = document.form1.TransDate.value;
	vtransferrequest_date = document.form1.transferrequest_date.value;
	vfromwh = document.form1.fromwh.value;
	
	document.form1.action='List_PurItems.php?PItem_Code=&lcode=&item='+vItem+'&ItemType='+vItemType+'&ItemGroup='+vItemGroup+'&fromwh='+fromwh+'&transferrequest_date='+vtransferrequest_date+'&TransDate='+vTransDate;
	document.form1.submit();
}

function RefreshPage(){
	document.form1.PItem_Code.value='';
	document.form1.PItem_Description.value='';
	
	vItem      = document.form1.item.value;
	vItemType  = document.form1.ItemType.value;
	vItemGroup = document.form1.ItemGroup.value;
	vTransDate = document.form1.TransDate.value;
	vtransferrequest_date = document.form1.transferrequest_date.value;
	vfromwh = document.form1.fromwh.value;
	
	document.form1.action='List_PurItems.php?PItem_Code=&lcode=&item='+vItem+'&ItemType='+vItemType+'&ItemGroup='+vItemGroup+'&fromwh='+fromwh+'&transferrequest_date='+vtransferrequest_date+'&TransDate='+vTransDate;
	document.form1.submit();
	
}

function SetNextPage(startrange,perpage){
	vItem      = document.form1.item.value;
	vItemType  = document.form1.ItemType.value;
	vItemGroup = document.form1.ItemGroup.value;
	vTransDate = document.form1.TransDate.value;
	vtransferrequest_date = document.form1.transferrequest_date.value;
	vfromwh = document.form1.fromwh.value;
	
	document.form1.action='List_PurItems.php?PItem_Code=&lcode=&item='+vItem+'&ItemType='+vItemType+'&ItemGroup='+vItemGroup+'&fromwh='+fromwh+'&transferrequest_date='+vtransferrequest_date+'&TransDate='+vTransDate+'&startrange='+startrange+'&perpage='+perpage;
	document.form1.submit();
}

function SetPrvPage(startrange,perpage){
	vItem      = document.form1.item.value;
	vItemType  = document.form1.ItemType.value;
	vItemGroup = document.form1.ItemGroup.value;
	vTransDate = document.form1.TransDate.value;
	vtransferrequest_date = document.form1.transferrequest_date.value;
	vfromwh = document.form1.fromwh.value;
	
	document.form1.action='List_PurItems.php?PItem_Code=&lcode=&item='+vItem+'&ItemType='+vItemType+'&ItemGroup='+vItemGroup+'&fromwh='+fromwh+'&transferrequest_date='+vtransferrequest_date+'&TransDate='+vTransDate+'&startrange='+startrange+'&perpage='+perpage;
	document.form1.submit();
}

function openitemform(){

 win = window.open('./Form_Items.php?mainmode=insert', 'Form_Items', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=1200,height=750,top=100,left=100');		
	win.creator = self;
	win.focus();
}

</script>
<style type="text/css">
<!--
body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 14px;
}
.style1 {
	color: #333333;
	font-weight: bold;
}
.style2 {
	color: #999999;
	font-weight: bold;
	font-size: 12px;
}
.style3 {color: #999999}
.style4{
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
-->
</style></head>

<body>
<form id="form1" name="form1" method="post" action="">
  <table width="800" border="0" cellspacing="1" align="center" style='table-layout:fixed'>
	<col width=10>
	<col width=80>
	<col width=120>
	<col width=100>
	<col width=50>
	<col width=50>
	<col width=80>
	<col width=60>
	<col width=60>

    <tr>
      <th  <?php if($fromwh != ''){ ?> colspan="10" <?php } else{ ?> colspan="9" <?php } ?> bgcolor="#DDDDDD"><span class="style1"> ITEMS </span></th>
    </tr>
    <tr>
      <td bgcolor="#DDDDDD" colspan="2"> <span style="font-size: 11px; color: #999999; font-weight: bold;">Search for Item  <br> Code / Desc. / Remark</span></td>
      <td bgcolor="#DDDDDD" >
	        <input type="text" name="PItem_Code" value="<?php echo $PItem_Code; ?>" placeholder='Code'  style='width: 150px;'/>
            <input type="hidden" name="code1" id="code1" value="<?php echo $Item_Code; ?>" >
			<input type="hidden" name="ItemVat" id="ItemVat"  value="<?php echo $ItemVat; ?>">
			<input type="hidden" name="ItemType" id="ItemType"  value="<?php echo $ItemType; ?>">
			<input type="hidden" name="ItemGroup" id="ItemGroup"  value="<?php echo $ItemGroup; ?>">
			<input type="hidden" name="ModuleSerialNo" id="ModuleSerialNo"  value="<?php echo $ModuleSerialNo; ?>">
			<input type="hidden" name="ItemWithSerial" id="ItemWithSerial"  value="<?php echo $ItemWithSerial; ?>">
			<input type="hidden" name="ItemProduction" id="ItemProduction"  value="<?php echo $ItemProduction; ?>">
			<input type="hidden" name="ItemSemiFinished" id="ItemSemiFinished"  value="<?php echo $ItemSemiFinished; ?>"></td>
			<input name="vtext1" type="hidden" id="vtext1" value="<?php echo $vtext1; ?>" />
			<input name="vtext2" type="hidden" id="vtext2" value="<?php echo $vtext2; ?>" />
			<input name="vtext3" type="hidden" id="vtext3" value="<?php echo $vtext3; ?>" />
			<input name="vtext4" type="hidden" id="vtext4" value="<?php echo $vtext4; ?>" />
			<input name="vtext5" type="hidden" id="vtext5" value="<?php echo $vtext5; ?>" />
			<input name="vtext6" type="hidden" id="vtext6" value="<?php echo $vtext6; ?>" />
			<input name="vtext7" type="hidden" id="vtext7" value="<?php echo $vtext7; ?>" />
			<input name="txtItemUnit1" type="hidden" id="txtItemUnit1" value="<?php echo $txtItemUnit1; ?>" />
			<input name="txtItemUnit2" type="hidden" id="txtItemUnit2" value="<?php echo $txtItemUnit2; ?>" />
			<input name="ItemType" type="hidden" id="ItemType" value="<?php echo $ItemType; ?>" />
			<input name="ItemGroup" type="hidden" id="ItemGroup" value="<?php echo $ItemGroup; ?>" />
			<input name="i_unitcoef" type="hidden" id="i_unitcoef" value="<?php echo $i_unitcoef; ?>" />
			<input name="qtyexist" type="hidden" id="qtyexist"  value="<?php echo $qtyexist; ?>">
			<input name="transferrequest_date" type="hidden" id="transferrequest_date"  value="<?php echo $transferrequest_date; ?>">
			<input name="fromwh" type="hidden" id="fromwh"  value="<?php echo $fromwh; ?>">
			
      <td bgcolor="#DDDDDD" <?php if($fromwh != ''){ ?> colspan="5" <?php } else{ ?> colspan="4" <?php } ?> >
	        <input type="text" name="PItem_Description"  value="<?php echo $PItem_Description; ?>" placeholder='Desc. / Remark' style='width: 150px;' />
	        <input type="submit" name="Submit" value="Find" style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
			<input type="button" name="cancelbtn" value="Cancel" onclick='clearform();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
			
			<?php
				$query = "select max(ifnull(PrequestApproved_Level,1)) from ".$_SESSION['dbLabel'].".login where Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')";
				$result=mysql_query($query,$connection) or die(mysql_error());
				$rows=mysql_fetch_array($result);
				$max = $rows[0];
				
				$query = "select ifnull(PrequestApproved_Level,1) from ".$_SESSION['dbLabel'].".login where user_account = '".$_SESSION['useraccount']."'  and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')";
				$result=mysql_query($query,$connection) or die(mysql_error());
				$rows=mysql_fetch_array($result);
				$current_level = $rows[0];
				
			//Gilbert 20-11-2013
			if($FI_ItemsApp==1 && $FI_Items==1 && ($current_level <= $max) ){
				echo"&nbsp;<input type='button' name='addbtn' value='Add Item' onclick='openitemform();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>";
			}	
			?>
			<input name="desc1" type="hidden" id="desc1" value="<?php echo $Item_Description; ?>" /></td>
      <td bgcolor="#DDDDDD" colspan="2">
	        <input name="TransDate" type="hidden" id="TransDate" value="<?php echo $TransDate; ?>" />
			<input name="TransID" type="hidden" id="TransID" value="<?php echo $TransID; ?>" />
			<input name="SCode" type="hidden" id="SCode" value="<?php echo $SCode; ?>" />
			<input name="item" type="hidden" id="item" value="<?php echo $item; ?>" />

<?php
	echo"<span style='font-size: 11px; color: #999999; font-weight: bold;'>Result per page </span><select name='perpage' id='perpage' value='$perpage' onchange='RefreshPage();' >";

    if ($perpage==20)
		$s20 = 'selected';
	else if ($perpage==30)
		$s30 = 'selected';
	else if ($perpage==40)
		$s40 = 'selected';
	else if ($perpage==50)
		$s50 = 'selected';

			
				
    echo"<option ".$s20." value='20'>20</option>
		 <option ".$s30." value='30'>30</option>
		 <option ".$s40." value='40'>40</option>
		 <option ".$s50." value='50'>50</option>
		 </select></td>";
			 
    echo"</tr>";
?>	
<?php if($_GET['post']=="1") { ?>
<script type="text/javascript" language="javascript">

x=document.form1;
item1=x.item.value;

if(item1=="1"){
	opener.document.purchasedt.txtItemCode.value=document.form1.code1.value;
	opener.document.purchasedt.txtItemDesc.value=document.form1.desc1.value;
	opener.document.purchasedt.txtPurDtVat.value=document.form1.ItemVat.value;
	//opener.document.purchasedt.txtItemCodePacked.value='';
	//opener.document.purchasedt.txtItemDescPacked.value='';

	//Check if item with serial no true, put the qty by default zero and disable it.
	if (document.form1.ItemWithSerial.value == 1 && document.form1.ModuleSerialNo.value == 1 &&
	    document.form1.ItemProduction.value == 0 && document.form1.ItemSemiFinished.value == 0) {
		opener.document.purchasedt.txtPurDtQty.value=0;
		opener.document.purchasedt.txtPurDtQty.readOnly = true;
	}
	else {
		opener.document.purchasedt.txtPurDtQty.value='';
		opener.document.purchasedt.txtPurDtQty.readOnly = false;
	}

	opener.document.purchasedt.submit();
	opener.document.purchasedt.txtItemCode.focus();
	self.close();
}else if(item1=="2"){
	opener.document.purchasedt.txtItemCodePacked.value=document.form1.code1.value;
	opener.document.purchasedt.txtItemDescPacked.value=document.form1.desc1.value;
	opener.document.purchasedt.txtPurDtVat.value=document.form1.ItemVat.value;
	opener.document.purchasedt.txtItemCode.value='';
	opener.document.purchasedt.txtItemDesc.value='';

	//Check if item with serial no true, put the qty by default zero and disable it.
	if (document.form1.ItemWithSerial.value == 1 && document.form1.ModuleSerialNo.value == 1) {
		opener.document.purchasedt.txtPurDtQty.value=0;
		opener.document.purchasedt.txtPurDtQty.readOnly = true;
	}
	else {
		opener.document.purchasedt.txtPurDtQty.value='';
		opener.document.purchasedt.txtPurDtQty.readOnly = false;
	}
	opener.document.purchasedt.txtItemCodePacked.focus();
	self.close();
}
else if(item1=="3"){
	opener.document.purchasedt.txtItemCode.value=document.form1.code1.value;
	opener.document.purchasedt.txtItemDesc.value=document.form1.desc1.value;
	opener.document.purchasedt.txtPorDtVat.value=document.form1.ItemVat.value;
	//opener.document.purchasedt.txtItemCodePacked.value='';
	//opener.document.purchasedt.txtItemDescPacked.value='';

	opener.document.purchasedt.submit();
	opener.document.purchasedt.txtItemCode.focus();
	self.close();
}
else if(item1=="4"){
	
	var vtext1 = document.form1.vtext1.value;
	var vtext2 = document.form1.vtext2.value;

	var vtext3 = document.form1.vtext3.value;
	var vtext4 = document.form1.vtext4.value;
	var vtext5 = document.form1.vtext5.value;
	var vtext6 = document.form1.vtext6.value;

	var parentText = window.opener.document.getElementById(vtext1);
	parentText.value =  document.form1.code1.value;
	

	if (vtext2!='')	{
		var parentText1 = window.opener.document.getElementById(vtext2);
		parentText1.value = document.form1.desc1.value;
	}
					
	if (vtext3!='')	{
		var parentText2 = window.opener.document.getElementById(vtext3);
		parentText2.value = document.form1.ItemVat.value;
	}
					
	if (vtext4!='')	{ 
		var parentText3 = window.opener.document.getElementById(vtext4);
		parentText3.value = document.form1.txtItemUnit1.value;
	}
					
	if (vtext5!='')	{
		var parentText4 = window.opener.document.getElementById(vtext5);
		parentText4.value = document.form1.txtItemUnit2.value;
	}	
			
	if (vtext6!='')	{
		var parentText5 = window.opener.document.getElementById(vtext6);
		parentText5.value = document.form1.i_unitcoef.value;
	}	
					
	self.close();
}
else if(item1=="5"){ 
	var vtext1 = document.form1.vtext1.value;
	var vtext2 = document.form1.vtext2.value;

	var parentText = window.opener.document.getElementById(vtext1);
	parentText.value =  document.form1.code1.value;
	//window.opener.document.search.txtitem1.value = item1;

	if (vtext2!='')	{
		var parentText1 = window.opener.document.getElementById(vtext2);
		parentText1.value = document.form1.desc1.value;
	}
			
					
	self.close();
}
else if(item1=="6"){
	
	var vtext1 = document.form1.vtext1.value;
	var vtext2 = document.form1.vtext2.value;
	var vtext3 = document.form1.vtext3.value;
	var vtext4 = document.form1.vtext4.value;
	var vtext5 = document.form1.vtext5.value;
	var vtext7 = document.form1.vtext7.value;
	
	var parentText = window.opener.document.getElementById(vtext1);
	parentText.value =  document.form1.code1.value;
	

	if (vtext2!='')	{
		var parentText1 = window.opener.document.getElementById(vtext2);
		parentText1.value = document.form1.desc1.value;
	}
					
	if (vtext3!='')	{
		var parentText5 = window.opener.document.getElementById(vtext3);
		parentText5.value = document.form1.i_unitcoef.value;
	}	
	
	/* if (vtext4!='')	{ 
		var parentText3 = window.opener.document.getElementById(vtext4);
		parentText3.value = document.form1.txtItemUnit1.value;
	} */
					
	/* if (vtext5!='')	{
		var parentText4 = window.opener.document.getElementById(vtext5);
		parentText4.value = document.form1.txtItemUnit2.value;
	} */
	/* if (vtext7!='')	{
		var parentText7 = window.opener.document.getElementById(vtext7);
		parentText7.value = document.form1.qtyexist.value;
	} */
	parentText.onchange();				
	self.close();
}else if (item1 == 7) {
	var vtext1 = document.form1.vtext1.value;
	var vtext2 = document.form1.vtext2.value;
	var vtext3 = document.form1.vtext3.value;
	var vtext4 = document.form1.vtext4.value;
	var vtext5 = document.form1.vtext5.value;
	var vtext6 = document.form1.vtext6.value;
	
	var parentText = window.opener.document.getElementById(vtext1);
	parentText.value =  document.form1.code1.value;
	

	if (vtext2!='')	{
		var parentText1 = window.opener.document.getElementById(vtext2);
		parentText1.value = document.form1.desc1.value;
	}
					
	if (vtext3!='')	{
		var parentText5 = window.opener.document.getElementById(vtext3);
		parentText5.value = document.form1.i_unitcoef.value;
	}	
				
	if (vtext4!='')	{ 
		var parentText3 = window.opener.document.getElementById(vtext4);
		parentText3.value = document.form1.txtItemUnit1.value;
	}
					
	if (vtext5!='')	{
		var parentText4 = window.opener.document.getElementById(vtext5);
		parentText4.value = document.form1.txtItemUnit2.value;
	}
					
	self.close();	 
	
}
</script>
<?php } ?>
    <tr bgcolor="#E5E5E5">
      <td colspan="2"><span class="style2">Item Code </span></td>
      <td colspan="2"><span class="style2">Item Description </span></td>
      <td align="center"><span class="style2"> Unit</span></td>
	  <?php if($fromwh != ''){ ?>  <td><span class='style2'>Qty Exists</span></td>  <?php } ?>
	<?php if($FI_ShowCostPrice != 0) {  ?>
	  <td align="center"><span class="style2"> Currency</span></td>
	  <td><span class="style2"> Last Cost Price</span></td>
	  <td align="center"><span class="style2"> <?php echo $base1; ?></span></td>
	  <td align="center"><span class="style2"> <?php echo $base2; ?></span></td>
	 <?php } ?> 
    </tr>
<?php


if (!empty($TransDate))
   $TransDate = explodeword($TransDate);

if (empty($TransID))   
   $TransID = '';
   

if (empty($TransDate))   
   $TransDate = '';
else
   $TransDate = "'".$TransDate."'"; 
 
if ($ItemType=='')
	$ItemType = 'All';
$query= " select Item_Code, Item_Description, Group_code, Item_UnitSalesDesc, Currency_code, item_lastcostprice, item_lastcostpricebase1, item_lastcostpricebase2,if(Items.Item_Vat !='', 1, 0) As ItemVat 
		, Item_UnitPurchaseDesc, ifNull(Item_Unit,1) as Item_Unit, items.Item_Pack 
			from items ".
        " where Item_Code like '%".$PItem_Code."%' ".
		" and ( Item_Description like '%".$PItem_Description."%' or Item_Remark like '%".$PItem_Description."%' ) ".
		//" and (($item = 1 and 1 = 1) or ($item = 2 and ifNull(Item_Pack,0) = 1)) ".
		//" and ifNull(Item_Production,0) = 0 ".
		" and (('".$ItemType."' = 'Trading'      and ifNull(Items.Item_Pack,0) = 0 and ifNull(Items.Item_Promotion,0) = 0 and ifNull(Items.Item_Visible,0) = 1) or ".
		"      ('".$ItemType."' = 'Packed'       and ifNull(Items.Item_Pack,0) = 1 and ifNull(Items.Item_Promotion,0) = 0 and ifNull(Items.Item_Production,0) = 0 and ifNull(Items.Item_SemiFinished,0) = 0 and ifNull(Items.Item_NonStock,0) = 0) or ".
		"      ('".$ItemType."' = 'PromoPacked'  and ifNull(Items.Item_Pack,0) = 1 and ifNull(Items.Item_Promotion,0) = 1 and ifNull(Items.Item_Production,0) = 0 and ifNull(Items.Item_SemiFinished,0) = 0 and ifNull(Items.Item_NonStock,0) = 0) or ".
		"      ('".$ItemType."' = 'PromoTrading' and ifNull(Items.Item_Pack,0) = 0 and ifNull(Items.Item_Promotion,0) = 1 and ifNull(Items.Item_Production,0) = 0 and ifNull(Items.Item_SemiFinished,0) = 0 and ifNull(Items.Item_NonStock,0) = 0) or ".
		"      ('".$ItemType."' = 'RawMaterial'  and ifNull(Items.Item_Pack,0) = 0 and ifNull(Items.Item_Promotion,0) = 0 and ifNull(Items.Item_Production,0) = 0 and ifNull(Items.Item_SemiFinished,0) = 0 and ifNull(Items.Item_Visible,0) = 0) or ".
		"      (('".$ItemType."' = 'FinishGoodSingle' or '".$ItemType."' = 'FinishGoodPack')  and ifNull(Items.Item_Production,0) = 1 and ifNull(Items.Item_SemiFinished,0) = 0  ) or  ".
		"      (('".$ItemType."' = 'SemiFinishSingle' or '".$ItemType."' = 'SemiFinishPack')  and ifNull(Items.Item_Production,0) = 0 and ifNull(Items.Item_SemiFinished,0) = 1  ) or  ".
		"      ('".$ItemType."' = 'Production'   and ifNull(Items.Item_Pack,0) = 0 and (ifNull(Items.Item_Production,0) = 1 or ifNull(Items.Item_SemiFinished,0) = 1 or ifNull(Items.Item_ReverseProduction,0) = 1) and ifNull(Items.Item_NonStock,0) = 0) or  ".
		"      ('".$ItemType."' = 'NonStock'     and ifNull(Items.Item_Pack,0) = 0 and ifNull(Items.Item_Production,0) = 0 and ifNull(Items.Item_SemiFinished,0) = 0  and ifNull(Items.Item_NonStock,0) = 1) or ".
		"      ('".$ItemType."' = 'All' and 1 = 1) ) ".
		" and (('".$ItemGroup."' != '' and Items.Group_code = '".$ItemGroup."') or ('".$ItemGroup."' = '' and 1 = 1)) ".
		" and ((ifNull(item_active,0) = 1 and 1 = 1) Or (ifNull(item_active,0) = 0 and ifNull(item_inactivedate, '$TransDate') >= '$TransDate') )  ";

		
if ($groupexist > 0)
	$query= $query." and Items.group_code In (select distinct group_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."')  ";  

//$query= $query." order by Items.Item_Code ";	

if ($PItem_Description != '') {
	$query = $query." order by Items.Item_Description  ";
} else {
	$query = $query." order by Items.Item_Code  ";
}
//echo $query;
$resultsearch=mysql_query($query,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($resultsearch);
	
if ($startrange=='' || $PItem_Code != '')	$startrange = 0;
if ($perpage=='')	$perpage = 20;
		
$query = $query." limit ".$startrange.",".$perpage;
	
$result=mysql_query($query,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($result)){
extract($rows);

	$i_unit1  	  = $Item_UnitSalesDesc;
	$i_unit2 	  = $Item_UnitPurchaseDesc; 
	$i_unitcoef	  = $Item_Unit;
	
	$txtItemUnit1 = $i_unit1;
	$txtItemUnit2 = $i_unit2;

$ct+=1;

if($ct%2==0)
{
	$Color="style='color:#0000FF;background-color:".$grid_color1."'";
	
	}else{
		
	$Color="style='color:#0000FF;background-color:".$grid_color2."'";	
}

$vItemCode = rawurlencode($Item_Code);
$lnk = "ItemVat=$ItemVat&vtype=$vtype&vtext1=$vtext1&vtext2=$vtext2&vtext3=$vtext3&text1value=$text1value&vtext4=$vtext4&vtext5=$vtext5&vtext6=$vtext6&vtext7=$vtext7&txtItemUnit1=$txtItemUnit1&txtItemUnit2=$txtItemUnit2&i_unitcoef=$i_unitcoef";

if (!empty($transferrequest_date))
   $transferrequest_date1 = explodeword($transferrequest_date);

if (empty($transferrequest_date1))   
   $transferrequest_date1 = '';
else
   $transferrequest_date1 = "'".$transferrequest_date1."'"; 
 
 
if ($Item_Pack==1)
		{
			
				
				$min_sorder_qty = 0;
				$min_available_qty = 9999999999;
				$min_exists_qty = 9999999999;
				$available_qty = 0;
				$exist_qty = 0;
			
			$queryPack = "SELECT Item_CodePackDt, PackDt_Quantity FROM packdt WHERE Item_Code = '".$Item_Code."' And ifNull(PackDt_CostPercentage,0) != 0 ";
			$resultPack = mysql_query($queryPack, $connection);
			
			while($cur = mysql_fetch_row($resultPack)){
			
			$Qry_QtyExists = " SELECT ifnull(QtyExists('".$cur[0]."', '".$fromwh."', ".$transferrequest_date1.", 0, 'TR', '', ''),0) As QtyExists ";
			
			//echo $Qry_QtyExists."<br><br>";							
			
			$resultex = mysql_query($Qry_QtyExists, $connection);
			$sub_result3 = mysql_fetch_row($resultex);
		
			$exist_qty = ($sub_result3[0] / $cur[1]); 
			if ($exist_qty < $min_exists_qty && $exist_qty != 0){
				$min_exists_qty = $exist_qty;
			}
			
			}
			$min_exists_qty = floor($min_exists_qty);
		}
	  else
		{
			$Qry_QtyExists = " SELECT ifnull(QtyExists('".$Item_Code."', '".$fromwh."', ".$transferrequest_date1.", 0, 'TR', '', ''),0) As QtyExists ";
			
		//echo $Qry_QtyExists."<br><br>";				  
		$resultex = mysql_query($Qry_QtyExists, $connection);
		$rex = mysql_fetch_row($resultex);		
		$min_exists_qty = $rex[0];
		
		}


?>
    <tr <?php echo $Color; ?>>
	  <td align="left"><?php if($fromwh == ''){ ?><a href="List_PurItems.php?<?php echo $lnk; ?>&lcode=&SCode=<?php echo $vItemCode; ?>&item=<?php echo $item; ?>&ItemType=<?php echo $ItemType; ?>&ItemGroup=<?php echo $ItemGroup; ?>&TransDate=<?php echo $TransDate; ?>&TransID=<?php echo $TransID; ?>&PItem_Code=<?php echo $PItem_Code;?>" title=' Show Existing Quantities '><image src="./img/arrow-red.gif" border=0 style="width: 10px; height: 10px; " align="center"></image></a><?php } ?></td>
      <td align="left"><span class="style4"><a href="List_PurItems.php?<?php echo $lnk; ?>&lcode=<?php echo $vItemCode; ?>&item=<?php echo $item; ?>&qtyexist=<?php echo $min_exists_qty; ?>&fromwh=<?php echo $fromwh; ?>&transferrequest_date=<?php echo $transferrequest_date; ?>&TransDate=<?php echo $TransDate; ?>&TransID=<?php echo $TransID; ?>"><?php echo $Item_Code; ?></a></span></td>
      <td align="left" colspan="2"><span class="style4"><a href="List_PurItems.php?<?php echo $lnk; ?>&lcode=<?php echo $vItemCode; ?>&item=<?php echo $item; ?>&qtyexist=<?php echo $min_exists_qty; ?>&fromwh=<?php echo $fromwh; ?>&transferrequest_date=<?php echo $transferrequest_date; ?>&TransDate=<?php echo $TransDate; ?>&TransID=<?php echo $TransID; ?>"><?php echo $Item_Description; ?></a></span></td>
      <td align="center"><span class="style4"><?php echo $Item_UnitSalesDesc; ?></span></td>
	  <?php if($fromwh != ''){ ?>  <td><span class='style4'><?php echo number_format($min_exists_qty,2)?></span></td>  <?php } ?>
	  <?php if($FI_ShowCostPrice != 0) {  ?>
	  <td align="center"><span class="style4"><?php echo $Currency_code; ?></span></td>
	  
	  <td align="right"><span class="style4"><?php echo number_format($item_lastcostprice, $DecimalBase); ?></span></td>
	  <td align="right"><span class="style4"><?php echo number_format($item_lastcostpricebase1, $DecimalBase); ?></span></td>
	  <td align="right"><span class="style4"><?php echo number_format($item_lastcostpricebase2, $DecimalBase); ?></span></td>
	  <?php } ?>
    </tr>
<?php

if ($SCode == $Item_Code) {
	$queryitemsperwh="  Select Warehouse_Code, ".
					 "  cast(Sum(QuantityProduceIn - QuantityProduceOut + QuantityEntered - QuantityDelivered) as decimal(20,2)) as QuantityExists ".
					 "  From StkTransaction ".
					 "  where if('$TransDate' ='',True,Transaction_Date <= '$TransDate')  ".
					 "  And Item_Code = '".$Item_Code."'  ".
					 "  And Warehouse_Code like '%' ".
					 "  And ((Transaction_ID <> if('$TransID'='',0,'$TransID') And Transaction_Type = 'PI') or (Transaction_Type <> 'PI')) ".
					 "  And if($Module_Grn=1,Transaction_Type<>'PI',True) ".
					 "  Group by Warehouse_Code ".
					 //"  Having QuantityExists <> 0 ".
					 "  Order by Warehouse_Code "; //echo $queryitemsperwh;
	$resultitemsperwh=mysql_query($queryitemsperwh,$connection) or die(mysql_error());

	while($rows=mysql_fetch_array($resultitemsperwh)){
		extract($rows);
		
		echo "<tr $Color >";
		echo "  <td align='center' colspan='2'><span class='style4'>&nbsp;</span></td>";
		echo "  <td align='center'><span class='style4'>$Warehouse_Code </span></td>";
		echo "	<td align='right'><span class='style4'>$QuantityExists</span></td>";
		echo "  <td align='center' colspan='1'><span class='style4'>&nbsp;</span></td>";
		if($FI_ShowCostPrice != 0) {
		echo "  <td align='center' colspan='3'><span class='style4'>&nbsp;</span></td>";
		}
		echo "</tr>";
	}
}
	

}

echo"<tr><td colspan='9'>&nbsp;</td></tr>
		 <tr><td colspan=9><table width='100%' border=0><tr><td width='260'>&nbsp;</td><td width='150' align='center'>";
		
	$diff= $startrange - $perpage;
		
	if (($diff) >= 0 ){
		$prvstartrange = $startrange - $perpage;
		echo"<img src='img/arrowleft.jpg' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
	}
	else
		echo "&nbsp;";
		
	echo"</td><td width='150' align='center'>";
		
		
	if ($startrange <= $totalrows){
		$startrange = $startrange + $perpage;
		if  ($startrange <= $totalrows)
			echo"<img src='img/arrowright.jpg' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange."','".$perpage."')>";
	} else
		echo "&nbsp;";
			
	echo"</td><td width='170'>&nbsp;</td></tr>";

function explodeword($date)
{

$trim = explode('/',$date);
$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";
return $trim;
}
?>
  </table>
</form>
</body>
</html>
