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

session_start();

include("config.inc.php");
include("Functions_Sales.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");

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

//Group List
$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);
if ($groupexist == 0)
	$querygroup = "select group_code, group_name from grouping order by group_code";

$resultgroup = mysql_query($querygroup, $connection) or die(mysql_error());

//Read From Module Table
$querymodule = " Select ifNull(Module_Pack,0) As Module_Pack  From Module ";
$resultmodule = mysql_query($querymodule, $connection) or die(mysql_error());
while ($rowmodule = mysql_fetch_array($resultmodule)) {
	extract($rowmodule);
}
//Item SubGroups List
$querysubgroup = " select distinct subgroup_code, subgroup_name " .
	" from subgroup where subgroup_code in (select subgroup_code from " . $HTTP_SESSION_VARS['dbLabel'] . ".loginstk where user_account='" . $HTTP_SESSION_VARS['useraccount'] . "') " .
	" order by subgroup_code";
$resultsubgroup = mysql_query($querysubgroup, $connection) or die(mysql_error());
$subgroupexist   = mysql_num_rows($resultsubgroup);
if ($subgroupexist == 0)
	$querysubgroup = "select distinct subgroup_code,subgroup_name from subgroup order by subgroup_code";
$resultfromsubgroup = mysql_query($querysubgroup, $connection) or die(mysql_error());

//Read From CashRegister Table
$queryCashRegister = " Select  Warehouse_Code As StpWarehouseCode, ifNull(CashReg_ShowSell1,0) As CashRegShowSell1, ifNull(CashReg_ShowSell2,0) As CashRegShowSell2,  " .
	" ifNull(CashReg_ShowSell3,0) As CashRegShowSell3, ifNull(CashReg_ShowSell4,0) As CashRegShowSell4, ifNull(CashReg_ShowSell5,0) As CashRegShowSell5, " .
	" ifNull(CashReg_ShowSell6,0) As CashRegShowSell6, Currency_Code As StpCurrencyCode, ifNull(CashRegister_ItemsWithoutSerial,0) As CashRegister_ItemsWithoutSerial " .
	" ,Client_Account as Client_Account" .
	" From CashRegister " .
	" Where CashRegister_Name = '" . $_SESSION['cashregister'] . "' ";
//echo $queryCashRegister;
$resultCashRegister = mysql_query($queryCashRegister, $connection) or die(mysql_error());
while ($rowCashRegister = mysql_fetch_array($resultCashRegister)) {
	extract($rowCashRegister);
}


set_time_limit(0);

//$ShowSell=1;


if ($_GET['lcode'] != "") {

	if ($Client_Account != '' && $frm == 2)

		$query1 = "select Item_Code, Item_Description, Item_Barcode, if('" . $StpCurrencyCode . "'='" . $Base1Curr . "'," .
			"if($ShowSell =1,Item_SellingPrice1Base1,if($ShowSell =2,Item_SellingPrice2Base1,if($ShowSell =3,Item_SellingPrice3Base1,if($ShowSell =4,Item_SellingPrice4Base1,if($ShowSell =5,Item_SellingPrice5Base1,if($ShowSell =6,Item_SellingPrice6Base1,Item_SellingPrice1Base1)))))),
			  if($ShowSell =1,Item_SellingPrice1Base2,if($ShowSell =2,Item_SellingPrice2Base2,if($ShowSell =3,Item_SellingPrice3Base2,if($ShowSell =4,Item_SellingPrice4Base2,if($ShowSell =5,Item_SellingPrice5Base2,if($ShowSell =6,Item_SellingPrice6Base2,Item_SellingPrice1Base2))))))) As Item_SellPrice " .
			",Item_ModifySellingPrice " .
			"from items where item_code='" . $_GET['lcode'] . "' and showInPos = 1";
	else
		$query1 = "select Item_Code, Item_Description, Item_Barcode, if('" . $StpCurrencyCode . "'='" . $Base1Curr . "'," .
			"Item_SellingPrice1Base1,
			Item_SellingPrice1Base2) As Item_SellPrice " .
			"from items where item_code='" . $_GET['lcode'] . "' and showInPos = 1";   //echo $query1;exit;
	//echo  $query1;
	$result1 = mysql_query($query1, $connection) or die(mysql_error());
	while ($rows = mysql_fetch_array($result1)) {
		extract($rows);
	}

	$post = "1";
}



$frm	   = $_GET['frm'];
if ($frm == "") {
	$frm = $_POST["frm"];
}
$SCode 	   = $_GET['SCode'];
if ($SCode == "") {
	$SCode = $_POST["SCode"];
}
$ItemWh    = $_GET['ItemWh'];
if ($ItemWh == "") {
	$ItemWh = $_POST["ItemWh"];
}
$TransDate = $_GET['TransDate'];
if ($TransDate == "") {
	$TransDate = $_POST["TransDate"];
}
$ShowSell = $_GET['ShowSell'];
if ($ShowSell == "") {
	$ShowSell = $_POST["ShowSell"];
}

$ItemType    = $_POST["ItemType"];
if ($ItemType == "") {
	$ItemType = $_GET["ItemType"];
}
$ItemGroup   = $_POST["ItemGroup"];
if ($ItemGroup == "") {
	$ItemGroup = $_GET["ItemGroup"];
}
$ItemSubGroup   = $_POST["ItemSubGroup"];
if ($ItemSubGroup == "") {
	$ItemSubGroup = $_GET["ItemSubGroup"];
}
$PItem_Code  = $_POST["PItem_Code"];
if ($PItem_Code == "") {
	$PItem_Code = $_GET["PItem_Code"];
}
$perpage     = $_POST["perpage"];
if ($perpage == "") {
	$perpage = $_GET["perpage"];
}


?>
<!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() {
			vFrm = document.form1.frm.value;
			vTransDate = document.form1.TransDate.value;
			vItemWh = document.form1.ItemWh.value;
			vShowSell = document.form1.ShowSell.value;

			vItemWh = vItemWh.replace('&', '%26');

			document.form1.PItem_Code.value = '';
			document.form1.ItemGroup.value = '';
			document.form1.ItemSubGroup.value = '';
			document.form1.SCode.value = '';

			document.form1.action = 'List_ItemsBarcode.php?PItem_Code=&lcode=&frm=' + vFrm + '&TransDate=' + vTransDate + '&ItemWh=' + vItemWh + '&ShowSell=' + vShowSell;
			document.form1.submit();

		}

		function SetNextPage(startrange, perpage) {
			vFrm = document.form1.frm.value;
			vItemType = document.form1.ItemType.value;
			vItemGroup = document.form1.ItemGroup.value;
			vItemSubGroup = document.form1.ItemSubGroup.value;
			vTransDate = document.form1.TransDate.value;
			vItemWh = document.form1.ItemWh.value;
			vShowSell = document.form1.ShowSell.value;

			vItemWh = vItemWh.replace('&', '%26');

			document.form1.action = 'List_ItemsBarcode.php?PItem_Code=&lcode=&frm=' + vFrm + '&ItemType=' + vItemType + '&ItemGroup=' + vItemGroup + '&ItemSubGroup=' + vItemSubGroup + '&TransDate=' + vTransDate + '&ItemWh=' + vItemWh + '&ShowSell' + vShowSell + '&startrange=' + startrange + '&perpage=' + perpage;
			document.form1.submit();
		}

		function SetPrvPage(startrange, perpage) {
			vFrm = document.form1.frm.value;
			vItemType = document.form1.ItemType.value;
			vItemGroup = document.form1.ItemGroup.value;
			vItemSubGroup = document.form1.ItemSubGroup.value;
			vTransDate = document.form1.TransDate.value;
			vItemWh = document.form1.ItemWh.value;
			vShowSell = document.form1.ShowSell.value;

			vItemWh = vItemWh.replace('&', '%26');

			document.form1.action = 'List_ItemsBarcode.php?PItem_Code=&lcode=&frm=' + vFrm + '&ItemType=' + vItemType + '&ItemGroup=' + vItemGroup + '&ItemSubGroup=' + vItemSubGroup + '&TransDate=' + vTransDate + '&ItemWh=' + vItemWh + '&ShowSell' + vShowSell + '&startrange=' + startrange + '&perpage=' + perpage;
			document.form1.submit();
		}

		function RefreshPage() {
			vFrm = document.form1.frm.value;
			vItemType = document.form1.ItemType.value;
			vItemGroup = document.form1.ItemGroup.value;
			vItemSubGroup = document.form1.ItemSubGroup.value;
			vTransDate = document.form1.TransDate.value;
			vItemWh = document.form1.ItemWh.value;
			vPerPage = document.form1.perpage.value;
			vShowSell = document.form1.ShowSell.value;

			vItemWh = vItemWh.replace('&', '%26');

			document.form1.action = 'List_ItemsBarcode.php?PItem_Code=&lcode=&frm=' + vFrm + '&ItemType=' + vItemType + '&ItemGroup=' + vItemGroup + '&ItemSubGroup=' + vItemSubGroup + '&TransDate=' + vTransDate + '&ItemWh=' + vItemWh + '&ShowSell' + vShowSell + '&startrange=0&perpage=' + vPerPage;
			document.form1.submit();
		}
	</script>
	<style type="text/css">
		<!--
		body,
		td,
		th {
			font-family: Verdana, Arial, Helvetica, sans-serif;
			font-size: 14px;
		}

		.style1 {
			font-size: 16px;
			color: #333333;
			font-weight: bold;
			font-family: "Times New Roman", Times, serif;
		}

		.style2 {
			color: #999999;
			font-weight: bold;
			font-size: 11px;
		}

		.style3 {
			color: #999999
		}

		.style4 {
			font-family: Verdana, Arial, Helvetica, sans-serif;
			font-size: 11px;
		}
		-->
	</style>
</head>

<body>

	<?php


	echo "
<form name='form1' method='post' action=''>
<input type='hidden' name='TransDate' value='$TransDate' >
<input type='hidden' name='ItemWh' value='$ItemWh' >
<input type='hidden' name='frm' value='$frm' >
<input type='hidden' name='SCode' value='$SCode' >
<input type='hidden' name='ShowSell' value='$ShowSell' >
<input type='hidden' name='Item_ModifySellingPrice' value='$Item_ModifySellingPrice' >";


	if ($ItemType == "") $ItemType = "Trading";
	if (!empty($TransDate))  $TransDate = explodeword($TransDate);

	$transD = $TransDate;
	if (empty($TransDate))  $TransDate = 'null';
	else   $TransDate = "'" . $TransDate . "'";



	if (empty($ItemWh))  $ItemWh = $StpWarehouseCode;

	if (empty($ItemWh))   $ItemWh = 'null';
	else   $ItemWh = "'" . $ItemWh . "'";

	echo "<table width='800' border='0' cellspacing='1' align='center' style='table-layout:fixed'>
		<col width=120>
		<col width=250>
		<col width=110>
		<col width=90>
		<col width=90>
		<col width=90>
		<col width=90>
		<col width=90>
		<col width=90>

    <tr>
      <th colspan='9' bgcolor='#DDDDDD'><span class='style1'> ITEMS </span></th>
    </tr>
	<tr>
		<td valign='center' align='left' bgcolor='#DDDDDD'><span class='style2'>Item Group</span></td>
		<td colspan='2' bgcolor='#DDDDDD'><select name='ItemGroup' id='ItemGroup' style='font-size:11px; width:350px; height:22px;' onchange='RefreshPage();' >";
	echo "<option></option>";
	while ($groupf = mysql_fetch_row($resultgroup)) {
		echo "<option ";
		if ($ItemGroup == $groupf[0])
			echo " selected ";
		echo " value='$groupf[0]'>$groupf[0] - $groupf[1]</option>";
	}

	echo "</select></td>
		
		<td align='left' bgcolor='#DDDDDD'><span class='style2'>Item Type</span></td>
			<td colspan='5' bgcolor='#DDDDDD'><select name='ItemType' style='font-size:11px; width:180px; height:22px;' onchange='RefreshPage();'>";

	if ($ItemType == "Packed" && $Module_Pack == 1) {
		echo "<option selected value='Packed'>Packed</option>";
	} else if ($Module_Pack == 1) {
		echo "<option value='Packed'>Packed</option>";
	}
	if ($ItemType == "PromoPacked" && $Module_Pack == 1) {
		echo "<option selected value='PromoPacked'>Promo Packed</option>";
	} else if ($Module_Pack == 1) {
		echo "<option value='PromoPacked'>Promo Packed</option>";
	}
	if ($ItemType == "Trading") {
		echo "<option selected value='Trading'>Trading</option>";
	} else {
		echo "<option value='Trading'>Trading</option>";
	}
	if ($ItemType == "PromoTrading") {
		echo "<option selected value='PromoTrading'>Promo Trading</option>";
	} else {
		echo "<option value='PromoTrading'>Promo Trading</option>";
	}
	if ($ItemType == "Production") {
		echo "<option selected value='Production'>Production</option>";
	} else {
		echo "<option value='Production'>Production</option>";
	}
	if ($ItemType == "NonStock") {
		echo "<option selected value='NonStock'>Non-Stock</option>";
	} else {
		echo "<option value='NonStock'>Non-Stock</option>";
	}
	echo "</select></td>
	</tr>	
	<tr>";
	echo "<td align='left' bgcolor='#DDDDDD'><span class='style2'>Item Subgroup</span></td>";

	echo "   <td width='45%'  bgcolor='#F5F5F5' ><span class='style8'><select name='ItemSubGroup' id='ItemSubGroup' style='width:350px'  onchange='RefreshPage();' >";
	echo "<option></option>";
	while ($subgrpf = mysql_fetch_row($resultfromsubgroup)) {
		echo "<option ";
		if ($ItemSubGroup == $subgrpf[0])
			echo " selected ";
		echo " value='$subgrpf[0]'>$subgrpf[0] | $subgrpf[1]</option>";
	}
	echo "</select>";
	echo "</span></td>";


	echo "</tr>
    <tr>
      <td bgcolor='#DDDDDD' > <span class='style2'>Search for Item  <br> Code / Desc.</span></td>
      <td bgcolor='#DDDDDD' colspan='2'>
	        <input type='text' name='PItem_Code' value='$PItem_Code' style='width:180px; ' />
			<input type='hidden' name='code1' id='code1' value='$Item_Code'>
			<input type='hidden' name='barcode1' id='barcode1' value='$Item_Barcode'>
			<input type='hidden' name='price1' id='price1' value='$Item_SellPrice'>
			<input type='hidden' name='desc1' id='desc1' value='$Item_Description' />
	        <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;'/></td>
			
			
			<td bgcolor='#DDDDDD' colspan='3' align='right'><span class='style2'>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>
      
	    <td bgcolor='#DDDDDD' colspan='3'>&nbsp;</td>
    </tr>";
	?>
	<?php if ($post == "1") { ?>
		<script type="text/javascript" language="javascript">
			var FormOption = document.form1.frm.value;

			if (FormOption == 1) {
				opener.document.retaildt.txtItemCode.value = document.form1.code1.value;
				opener.document.retaildt.txtItemDesc.value = document.form1.desc1.value;
				opener.document.retaildt.txtItemBarcode.value = document.form1.barcode1.value;
				opener.document.retaildt.txtItemBarcode.focus();
				opener.document.retaildt.submit();
			} else if (FormOption == 2) {
				opener.document.retail.txtItemBarcode.value = document.form1.code1.value;
				opener.document.retail.txtItemPrice.value = document.form1.price1.value;
				opener.document.retail.txtItemModifySellingPrice.value = document.form1.Item_ModifySellingPrice.value;
				opener.document.retail.txtItemQty.focus();
				//opener.document.retail.submit();
				opener.document.retail.btnSubmit.click();
			} else if (FormOption == 3) {
				opener.document.form1.txtItemButtonValue.value = document.form1.code1.value;
				opener.document.form1.txtItemButtonLabel.value = document.form1.desc1.value;
				opener.document.form1.txtItemButtonValue.focus();
			} else if (FormOption == 4) {
				opener.document.form1.txtRelatedItemValue.value = document.form1.code1.value;
				opener.document.form1.txtRelatedItemValue.focus();
			}

			self.close();
		</script>
	<?php } ?>

	<?php

	echo " <tr bgcolor='#E5E5E5'>
        <td ><span class='style2'>Item Code </span></td>
        <td ><span class='style2'>Item Description </span></td>
		<td ><span class='style2'>Barcode </span></td>
		<td ><span class='style2'>Exist Before</span></td>
		<td ><span class='style2'>Exist After</span></td>
	    <td align='center'><span class='style2'> Price 1 </span></td>
	    <td align='center'><span class='style2'> Price 2 </span></td>
	    <td align='center'><span class='style2'> Price 3 </span></td>
		<td align='center'><span class='style2'> Price 4 </span></td>
		<td align='center'><span class='style2'> Price 5 </span></td>
		<td align='center'><span class='style2'> Price 6 </span></td>";
	echo " </tr>";



	$query = "";
	/*if ($ItemType == 'Packed' || $ItemType == 'PromoPacked') {
	$query =" select Item_Code, Item_Description, Item_Barcode, Group_code, ItemCurrCode, Item_SellingPrice1, Item_SellingPrice1Base1, Item_SellingPrice1Base2, ".
	        " Item_SellingPrice2, Item_SellingPrice2Base1, Item_SellingPrice2Base2, Item_SellingPrice3, Item_SellingPrice3Base1, Item_SellingPrice3Base2, ".
			" Item_SellingPrice4, Item_SellingPrice4Base1, Item_SellingPrice4Base2, Item_SellingPrice5, Item_SellingPrice5Base1, Item_SellingPrice5Base2, ".
			" Item_SellingPrice6, Item_SellingPrice6Base1, Item_SellingPrice6Base2, Warehouse_Code, Min(floor(QuantityExists)) As QuantityExists ".
			" from ( ";
}*/

	$query = $query . " select SQL_CALC_FOUND_ROWS Items.Item_Code, Items.Item_Description, Items.Item_Barcode, Items.Group_code, Items.Currency_code As ItemCurrCode, Item_SellingPrice1, Item_SellingPrice1Base1, Item_SellingPrice1Base2, " .
		" Item_SellingPrice2, Item_SellingPrice2Base1, Item_SellingPrice2Base2, Item_SellingPrice3, Item_SellingPrice3Base1, Item_SellingPrice3Base2, " .
		" Item_SellingPrice4, Item_SellingPrice4Base1, Item_SellingPrice4Base2, Item_SellingPrice5, Item_SellingPrice5Base1, Item_SellingPrice5Base2, " .
		" Item_SellingPrice6, Item_SellingPrice6Base1, Item_SellingPrice6Base2 " .
		" ,QtyExists(item_code,'',now(),0,'','','') as QTYEXISTS, QtyExistsPOS(item_code) as QTYEXISTSWPOS";

	//, StkTransaction.Warehouse_Code,				
	/*if ($ItemType == 'Packed' || $ItemType == 'PromoPacked') 				
	$query = $query." PackDt.Item_CodePackDt, PackDt_Quantity, cast(Sum((QuantityProduceIn - QuantityProduceOut + QuantityEntered - QuantityDelivered)/PackDt_Quantity) as decimal(20,2)) as QuantityExists ";
else 	
	$query = $query." cast(Sum(QuantityProduceIn - QuantityProduceOut + QuantityEntered - QuantityDelivered) as decimal(20,2)) as QuantityExists ";
*/

	$query = $query . " from Items ";
	/*
if ($ItemType == 'Packed' || $ItemType == 'PromoPacked') {
	$query = $query." Inner Join PackDt on Items.Item_Code = PackDt.Item_Code ".
					" Inner Join StkTransaction on StkTransaction.Item_Code = PackDt.Item_CodePackDt ";
} 
else					
	$query = $query." Left Join StkTransaction on StkTransaction.Item_Code = Items.Item_Code  ";
*/

	if ($frm == 2)
		$query = $query . " where (Items.Item_Code like '" . $PItem_Code . "%' or Items.Item_Description like '%" . $PItem_Code . "%' ) ";
	else
		$query = $query . " where (Items.Item_Code like '%" . $PItem_Code . "%' or Items.Item_Barcode like '%" . $PItem_Code . "%' or Items.Item_Barcode2 like '%" . $PItem_Code . "%' or Items.Item_Barcode3 like '%" . $PItem_Code . "%' or Items.Item_Description like '%" . $PItem_Code . "%' ) ";

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

	$query = $query .
		" and (('" . $ItemType . "' = 'Trading'      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_NonStock,0) = 0) 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 . "' = '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) ) " .
		" and ((ifNull(Items.item_active,0) = 1 and 1 = 1) Or (ifNull(Items.item_active,0) = 0 and if($TransDate is null,True,ifNull(Date_Format(Items.item_inactivedate,'%Y-%m-%d'), $TransDate) >= $TransDate)) )  " .
		" and (('" . $ItemGroup . "' != '' and Items.Group_code = '" . $ItemGroup . "') or ('" . $ItemGroup . "' = '' and 1 = 1)) " .
		" and (('" . $ItemSubGroup . "' != '' and Items.SubGroup_code = '" . $ItemSubGroup . "') or ('" . $ItemSubGroup . "' = '' and 1 = 1)) " .
		//" and if(StkTransaction.Item_Code is null, True, if($TransDate is null,True,Date_Format(Transaction_Date,'%Y-%m-%d') <= $TransDate)) ".
		" and ((('$CashRegister_ItemsWithoutSerial'=0 or ifNull(Items.Item_Pack,0) = 1) and 1=1) or ('$CashRegister_ItemsWithoutSerial'=1 and ifNull(Items.Item_WithSerialNo,0) = 0) ) ";

	/*if ($ItemType == 'PromoPacked') {		
	$query = $query." and (PackDt.PackDt_ToDate >= now() or PackDt.PackDt_ToDate is null ) ";
}*/

	/*if ($ItemType == 'Packed' || $ItemType == 'PromoPacked') {		
	$query = $query."   group by Items.Item_Code, Items.Item_Description, PackDt.Item_CodePackDt, PackDt_Quantity, StkTransaction.Warehouse_Code ".
                    " ) Q1 ".
                    " group by Item_Code, Item_Description, Warehouse_Code ".
                    " order by Item_Code, Item_Description, Warehouse_Code ";
}
else {
	$query = $query." group by Items.Item_Code, Items.Item_Description, StkTransaction.Warehouse_Code ".
					" order by Items.Item_Code, Items.Item_Description, StkTransaction.Warehouse_Code  ";		
}*/
	$query = $query . " and ifnull(Item_active,0) = 1 and showInPos = 1";
	$query = $query . " order by Items.Item_Code, Items.Item_Description  ";

	// echo $query;
	if ($post != "1") {

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

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

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

		$result1 = mysql_query($query, $connection) or die(mysql_error());



		$rowXX = mysql_fetch_row(mysql_query("SELECT FOUND_ROWS();", $connection));
		$totalrows = $rowXX[0];


		while ($rows = mysql_fetch_array($result1)) {
			extract($rows);

			$ct += 1;

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

				$Color = "style='color:#0000FF;background-color:" . $grid_color2 . "'";
			}


			if ($StpCurrencyCode != $ItemCurrCode) {
				if ($StpCurrencyCode == $Base1Curr) {
					$Item_SellingPrice1 = $Item_SellingPrice1Base1;
					$Item_SellingPrice2 = $Item_SellingPrice2Base1;
					$Item_SellingPrice3 = $Item_SellingPrice3Base1;
					$Item_SellingPrice4 = $Item_SellingPrice4Base1;
					$Item_SellingPrice5 = $Item_SellingPrice5Base1;
					$Item_SellingPrice6 = $Item_SellingPrice6Base1;
				} else if ($StpCurrencyCode == $Base2Curr) {
					$Item_SellingPrice1 = $Item_SellingPrice1Base2;
					$Item_SellingPrice2 = $Item_SellingPrice2Base2;
					$Item_SellingPrice3 = $Item_SellingPrice3Base2;
					$Item_SellingPrice4 = $Item_SellingPrice4Base2;
					$Item_SellingPrice5 = $Item_SellingPrice5Base2;
					$Item_SellingPrice6 = $Item_SellingPrice6Base2;
				} else {
					$Item_SellingPrice1 = ConvertCurrency($ItemCurrCode, $StpCurrencyCode, insertMySQLDate(displayToday()), $Item_SellingPrice1, $connection);
					$Item_SellingPrice2 = ConvertCurrency($ItemCurrCode, $StpCurrencyCode, insertMySQLDate(displayToday()), $Item_SellingPrice2, $connection);
					$Item_SellingPrice3 = ConvertCurrency($ItemCurrCode, $StpCurrencyCode, insertMySQLDate(displayToday()), $Item_SellingPrice3, $connection);
					$Item_SellingPrice4 = ConvertCurrency($ItemCurrCode, $StpCurrencyCode, insertMySQLDate(displayToday()), $Item_SellingPrice4, $connection);
					$Item_SellingPrice5 = ConvertCurrency($ItemCurrCode, $StpCurrencyCode, insertMySQLDate(displayToday()), $Item_SellingPrice5, $connection);
					$Item_SellingPrice6 = ConvertCurrency($ItemCurrCode, $StpCurrencyCode, insertMySQLDate(displayToday()), $Item_SellingPrice6, $connection);
				}
			} else {
				$Item_SellingPrice1 = $Item_SellingPrice1;
				$Item_SellingPrice2 = $Item_SellingPrice2;
				$Item_SellingPrice3 = $Item_SellingPrice3;
				$Item_SellingPrice4 = $Item_SellingPrice4;
				$Item_SellingPrice5 = $Item_SellingPrice5;
				$Item_SellingPrice6 = $Item_SellingPrice6;
			}


			if ($CashRegShowSell6 == 1)
				$CashRegShowSell = 6;
			else if ($CashRegShowSell5 == 1)
				$CashRegShowSell = 5;
			else if ($CashRegShowSell4 == 1)
				$CashRegShowSell = 4;
			else if ($CashRegShowSell3 == 1)
				$CashRegShowSell = 3;
			else if ($CashRegShowSell2 == 1)
				$CashRegShowSell = 2;
			else if ($CashRegShowSell1 == 1)
				$CashRegShowSell = 1;
			/*if ($totalrows == 1) { 

			header ('Location:List_ItemsBarcode.php?lcode='.htmlentities($Item_Code).'&frm='.$frm.'&ItemType='.$ItemType.'&ItemGroup='.$ItemGroup.'&TransDate='.$TransDate);
			//exit;
		}	*/


			echo "<tr  $Color > 
				<td align='left' ><span class='style4'><a href='List_ItemsBarcode.php?lcode=$Item_Code&frm=$frm&ItemType=$ItemType&ItemGroup=$ItemGroup&TransDate=$transD&ShowSell=$CashRegShowSell'>$Item_Code</a></span></td>
				<td align='left' ><span class='style4'>$Item_Description</span></td>
 			    <td align='left'><span class='style4'>" . $Item_Barcode . "</span></td>
 			    <td align='left'><span class='style4'>" . $QTYEXISTS . "</span></td> 
 			    <td align='left'><span class='style4'>" . $QTYEXISTSWPOS . "</span></td>";

			if ($CashRegShowSell1 == 1)
				echo "<td align='right'><span class='style4'>" . number_format($Item_SellingPrice1, $DecimalBase) . "</span></td>";
			else
				echo "<td align='right'>&nbsp;</td>";

			if ($CashRegShowSell2 == 1)
				echo "<td align='right'><span class='style4'>" . number_format($Item_SellingPrice2, $DecimalBase) . "</span></td>";
			else
				echo "<td align='right'>&nbsp;</td>";

			if ($CashRegShowSell3 == 1)
				echo "<td align='right'><span class='style4'>" . number_format($Item_SellingPrice3, $DecimalBase) . "</span></td>";
			else
				echo "<td align='right'>&nbsp;</td>";

			if ($CashRegShowSell4 == 1)
				echo "<td align='right'><span class='style4'>" . number_format($Item_SellingPrice4, $DecimalBase) . "</span></td>";
			else
				echo "<td align='right'>&nbsp;</td>";

			if ($CashRegShowSell5 == 1)
				echo "<td align='right'><span class='style4'>" . number_format($Item_SellingPrice5, $DecimalBase) . "</span></td>";
			else
				echo "<td align='right'>&nbsp;</td>";

			if ($CashRegShowSell6 == 1)
				echo "<td align='right'><span class='style4'>" . number_format($Item_SellingPrice6, $DecimalBase) . "</span></td>";
			else
				echo "<td align='right'>&nbsp;</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>";
		echo "<table>";

		echo "</td></tr>";
	}
	function explodeword($date)
	{

		$trim = explode('/', $date);
		$trim = "$trim[2]" . "-" . "$trim[1]" . "-" . "$trim[0]";
		return $trim;
	}

	function ConvertCurrency($FromCurr, $ToCurr, $FromDate, $Amount, $connection)
	{
		$query = "SELECT ifNull(convertcurfunc('" . $FromCurr . "', '" . $ToCurr . "', '" . $FromDate . "', 0, 0, '', '', ifNull($Amount,0)),0) From Dual ";
		$result = selectData($connection, $query);
		//echo $query;
		return	$result[0];
	}
	?>
	</table>
	</form>
</body>

</html>