<?php
session_start();

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");

$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);

}

set_time_limit(0);

//Read From Login Table
$querylog=" select ifNull(FI_ShowCostPrice,0) As FI_ShowCostPrice, ifNull(Login_negativeQty,0) As Login_negativeQty ".
		  " from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}


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

   
$frm	   	= $_GET['frm']; if ($frm == ""){$frm = $_POST["frm"];}
$index		= $_GET['index']; if ($index == "") $index = $_POST['index'];
$perpage   	= $_GET['perpage']; if ($perpage == ""){$perpage = $_POST["perpage"];}
$startrange = $_GET['startrange']; if ($startrange == ""){$startrange = $_POST["startrange"];}
$TransDate  = $_GET['TransDate']; if ($TransDate == ""){$TransDate = $_POST["TransDate"];}
$ItemType   = $_GET['ItemType']; if ($ItemType == ""){$ItemType = $_POST["ItemType"];}
$ItemSort   = $_GET['ItemSort']; if ($ItemSort == ""){$ItemSort = $_POST["ItemSort	"];}
$ItemGroup  = $_GET['ItemGroup']; if ($ItemGroup == ""){$ItemGroup = $_POST["ItemGroup"];}
$ItemWh  	= $_GET['ItemWh']; if ($ItemWh == ""){$ItemWh = $_POST["ItemWh"];}
$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"];}

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

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

    $_GET['post']="1";
	
}
//Gilbert 19-11-2013
$query_login=" select ifNull(FI_Items,0) as FI_Items, ifNull(FI_ItemsApp,0) as FI_ItemsApp, ".
			 " FI_SellingPriceInItems as FI_SellingPriceInItems, FI_CostPriceInItems as FI_CostPriceInItems   ".
			 " 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);
}



?>
<!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>Item History</title>
<link rel="stylesheet" type="text/css" media="all" href="js/grid/UI_DataGrid_V1.css">
<script type="text/javascript" src="js/grid/UI_DataGrid.js"></script>
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>

<script type="text/javascript" language="javascript">

function clearform()
{
	document.form1.PItem_Description.value='';
	document.form1.PItem_Code.value='';
	document.form1.SCode.value='';
	document.form1.action='Form_ItemHistory.php';
	document.form1.submit();
}
function SetNextPage(startrange,perpage){
	vFrm       = document.form1.frm.value;
	vItemType  = document.form1.ItemType.value;
	vItemSort  = document.form1.ItemSort.value;
	vItemGroup = document.form1.ItemGroup.value;
	vItemWh    = document.form1.ItemWh.value;
	vTransDate = document.form1.TransDate.value;
	
	document.form1.action='Form_ItemHistory.php?PItem_Code=&lcode=&frm='+vFrm+'&ItemType='+vItemType+'&ItemSort='+vItemSort+'&ItemGroup='+vItemGroup+'&ItemWh='+vItemWh+'&TransDate='+vTransDate+'&startrange='+startrange+'&perpage='+perpage;
	document.form1.submit();
}

function SetPrvPage(startrange,perpage){
	vFrm       = document.form1.frm.value;
	vItemType  = document.form1.ItemType.value;
	vItemSort  = document.form1.ItemSort.value;
	vItemGroup = document.form1.ItemGroup.value;
	vItemWh    = document.form1.ItemWh.value;
	vTransDate = document.form1.TransDate.value;
	
	document.form1.action='Form_ItemHistory.php?PItem_Code=&lcode=&frm='+vFrm+'&ItemType='+vItemType+'&ItemSort='+vItemSort+'&ItemGroup='+vItemGroup+'&ItemWh='+vItemWh+'&TransDate='+vTransDate+'&startrange='+startrange+'&perpage='+perpage;
	document.form1.submit();
}
function RefreshPage(){
	vFrm       = document.form1.frm.value;
	vItemType  = document.form1.ItemType.value;
	vItemSort  = document.form1.ItemSort.value;
	vItemGroup = document.form1.ItemGroup.value;
	vItemWh    = document.form1.ItemWh.value;
	vTransDate = document.form1.TransDate.value;
	vPerPage   = document.form1.perpage.value;
	
	document.form1.action='Form_ItemHistory.php?PItem_Code=&lcode=&frm='+vFrm+'&ItemType='+vItemType+'&ItemSort='+vItemSort+'&ItemGroup='+vItemGroup+'&ItemWh='+vItemWh+'&TransDate='+vTransDate+'&startrange=0&perpage='+vPerPage;
	document.form1.submit();
}

function ShowSlidingDiv(PIndex) {
	var Div = "#slidingDiv"+PIndex;
	$(Div).slideToggle();
	
}

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=20,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: 11px;
}
.style3 {color: #999999}
.style4{
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
-->
</style></head>

<body>

<?php

echo "
<form id='form1' name='form1' method='post' action=''>
  <table width='760' border='0' cellspacing='1' align='center' style='table-layout:fixed'>
	<col width=25>
	<col width=100>
	<col width=100>
	<col width=100>
	<col width=200>
	<col width=100>";
	
   

echo "
    <tr>
      <th colspan='6' bgcolor='#DDDDDD'><span class='style1'> Item History </span></th>
    </tr>
    <tr>
      <td bgcolor='#DDDDDD' colspan='2'> <span style='font-size: 11px; color: #999999; font-weight: bold;'></span></td>
      <td bgcolor='#DDDDDD' colspan='4' align=right>
	        <input type='hidden' name='PItem_Code' value='$PItem_Code'  size=12/>
            <input type='hidden' name='code1' id='code1'  value='$Item_Code'>
			<input type='hidden' name='ItemVat' id='ItemVat'  value='$ItemVat'>
	        <input type='hidden' name='PItem_Description'  value='$PItem_Description' size=15/>
	        <input type='hidden' name='Submit' value='Find' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
			<input type='hidden' name='cancelbtn' value='Cancel' onclick='clearform();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>";
			

				
			echo"<input name='desc1' type='hidden' id='desc1' value='$Item_Description' />
			<input type='hidden' name='TransDate' value='$TransDate' />
			<input type='hidden' name='ItemType' value='$ItemType' />
			<input type='hidden' name='ItemSort' value='$ItemSort' />
			<input type='hidden' name='ItemGroup' value='$ItemGroup' />
			<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='index' value='$index'>
			
			<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>

    </tr>";
?>	
<?php

echo" <tr bgcolor='#E5E5E5'>
        <td ><span class='style2'>&nbsp</span></td>
		<td ><span class='style2'>Purchase Date </span></td>
        <td ><span class='style2'>Reference</span></td>
		<td align='center'><span class='style2'>Supplier Code</span></td>
		<td align='center'><span class='style2'>Supplier Name</span></td>
		<td align='center'><span class='style2'>Price</span></td>
		";

echo" </tr>";

 



$query = " select date_format(purchases.Purchase_Date,'%d/%m/%Y') purchase_date,Purchase_Reference purchase_reference
			,suppliers.ledger_number ledger_number,suppliers.ledger_name ledger_name ,PurchaseDt_Price
			from purchases,purchasesdt 
			left join suppliers on suppliers.ledger_number = purchasesdt.supplier_code
			where purchasesdt.purchase_id = purchases.purchase_id
			and purchasesdt.item_Code = '".$PItem_Code."'
			order by Purchase_Date
		";


	//echo $query;exit;	

	
	$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;
	//echo $query;exit;
	$result1=mysql_query($query,$connection) or die(mysql_error());
	
	$ct = 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."'";	
		}

		$vItemCode = rawurlencode($Item_Code);


	
		echo "<tr  $Color > 
				<td align='center'><label><span></span></label></td>
				
				<td align='left' ><span class='style4'>$purchase_date</span></td>
				<td align='center'><span class='style4'>".$purchase_reference."</span></td>
				<td align='left'><span class='style4'>".$ledger_number."</span></td>
				<td align='left'><span class='style4'>".$ledger_name."</span></td>
				<td align='right'><span class='style4'>".number_format($PurchaseDt_Price, $DecimalBase)."</span></td>";

 	
				
		echo "</tr>";


	}
	
	echo"<tr><td colspan='6'>&nbsp;</td></tr>
		 <tr><td colspan='6'><table width='100%' border=0><tr><td width='270'>&nbsp;</td><td width='110' 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='110' 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='270'>&nbsp;</td></tr>";
	echo"</table>";

	echo"</td></tr>";



?>
  </table>
</form>
</body>
</html>
