<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}


include("config.inc.php");
include("function.inc.php");
include("Functions_Trans.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);
}
$fromdate   =$_POST["fromdate"];
$todate     =$_POST["todate"];

$querydate="select search_startingdate from setup";
$resultdate=mysql_query($querydate,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdate)){
	extract($rows);
}	
$MainCompanyName=explode('_',$_SESSION['dbName']); 
$QueryDir = " Select Date_Format(StartFinancialDate,'%d/%m/%Y') 
			From ".$_SESSION['dbLabel'].".Directories ". 	
			" Where  Upper(CompanyName) = Upper('".$MainCompanyName[0]."') ".
			" and Upper(Diryear) = Upper('".$MainCompanyName[1]."') ";
//echo $QueryDir;			
$ResultDir=mysql_query($QueryDir,$connection) or die(mysql_error());   
$RowsDir=mysql_fetch_row($ResultDir);
$StartFinancialDate = $RowsDir[0];


if ($fromdate == '') {
if ($search_startingdate == '-1') {
	$fromdate = $StartFinancialDate;
}

else if($search_startingdate == '0') {
	$fromdate = date('01/m/Y');
}

else if($querydate > '0') {
	$added_timestamp = strtotime('-'.$search_startingdate.' month', time());

	$fromdate = date('01/m/Y', $added_timestamp); 
}
}

if ($todate == '')
	$todate =  date('d/m/Y');


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


$MainCompanyName=explode('_',$_SESSION['dbName']);
  

$orderby    = $_POST['orderby']; 
$perpage    = $_POST['perpage']; 
$startrange = $_POST['startrange']; 
$promo = $_GET['p'];

if (empty($orderby))  $orderby = 2;


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">
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>
<title>Selling Price History</title>
<script type="text/javascript" language="javascript">


function SetPagination(startrange,perpage){
	document.search.startrange.value= startrange;
	document.search.perpage.value= perpage;
	document.search.submit();
}

function SetPaginationLast(perpage,totalrows){
	var result=totalrows % perpage;
	if (result == 0) result = perpage;
	startrange=totalrows-result;
	document.search.startrange.value= startrange;
	document.search.perpage.value= perpage;
	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;
    }
    return true;
}

function RefreshPage()
{
	document.search.startrange.value= 0;
	document.search.submit();

}



function ShowHideDetails(idval) {
	vSlideDivId = '#displaydetails'+idval;
	$(vSlideDivId).slideToggle();
}


function clearform(){
	document.search.startrange.value= 0;
	document.search.fromdate.value = '';
	document.search.todate.value = '';
	document.search.perpage.value = '';
	document.search.action='Form_BrowseSellingPrice.php';
	document.search.submit();
}


</script>

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	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;
}
.style15 {color: #5151FF; }
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #000000 ;font-size: 10px;}

</style>
<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>
</head>
<body class='bodybackground'>
<form name='search' method='post' action=''>
<input type='hidden' name='startrange' value='$startrange'>

HERE;


			
$querysearch=" select sellingprice_id, sellingprice_ref, date_format(creationDate,'%d-%m-%Y') as historyDate, Group_Name, SubGroup_Name, Category_name,
				sellingprice_nbr, default_per, default_val, user_code, Date_Format(sellingprice_fromdate,'%d-%m-%Y') as pricefromdate, Date_Format(sellingprice_todate,'%d-%m-%Y') as pricetodate
				from sellingprice_adjust
				left join grouping on grouping.group_code = sellingprice_adjust.group_code
				left join subgroup on subgroup.subgroup_code = sellingprice_adjust.subgroup_code
				left join category on category.category_code = sellingprice_adjust.category_code 
				where 1=1 ";
if($promo == 1)
	$querysearch .= " and ifnull(sellingprice_promo,0) = 1 ";
if ($fromdate != '' && $todate !='' )
	$querysearch .= " And Date_Format(creationDate,'%Y-%m-%d') >= '".explodeword($fromdate)."' and Date_Format(creationDate,'%Y-%m-%d') <= '".explodeword($todate)."' ";
else {	
	$querysearch .= " and ifnull(sellingprice_promo,0) = 0 ";
	if ($fromdate != '' && $todate !='' )
		$querysearch .= " And Date_Format(creationDate,'%Y-%m-%d') between '".explodeword($fromdate)."' and '".explodeword($todate)."' ";
}
if ($orderby == 1){
	if($promo == 1) $querysearch .= " Order By sellingprice_fromdate, sellingprice_todate Desc ";
	else $querysearch .= " Order By creationDate Desc ";
}
else if ($orderby == 2)
	$querysearch .= " Order By sellingprice_ref desc ";	
	 

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

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

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

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:940px' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>List of Selling Prices
			</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'>
	<col width=200>
	  <col width=440>
	  <col width=320>

	  <tr class='Search-header-bg'>";
	  

echo" <td align='left' ><span class='style4'>Order By</span>	
		 <span class='style4'><select name='orderby' id='orderby' value='$orderby' onchange='RefreshPage();'>";
		
		if ($orderby==1)
			$s11 = 'selected';
		else if ($orderby==2)
			$s12 = 'selected';
			
		echo"<option ".$s11." value='1'>Date</option>
			 <option ".$s12." value='2'>Reference</option>
			 </select></span></td>";
echo "<td height = '24px'><span class='style4'>From&nbsp;</span><input type='text' name='fromdate' id='fromdate' value='$fromdate' maxlength='10' size='9'>
		<A HREF=\"#\" onClick=\"displayCalendar(document.search.fromdate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\"><image src='./img/cal.gif' border=0 align=center></image></A></span>
&nbsp; &nbsp;<span class='style4'>To&nbsp;</span><input type='text' name='todate' id='todate' value='$todate' maxlength='10' size='9'>
		<A HREF=\"#\" onClick=\"displayCalendar(document.search.todate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\"><image src='./img/cal.gif' border=0 align=center></image></A></span>
		</td>";			 
echo "	<td><button type='button' id='btnSubmit' name='btnSubmit' onclick='RefreshPage(); 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></td>";
echo" </tr>
	  ";	
				 

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

echo"<tr>
		<td  id='search-content1' style='height:34px;width:31px;'>&nbsp;</td>
		<td  id='search-content2' style='height:34px;width:1025px;'>
		<table width='100%' border=0>
		<tr>
			<td align='right' style='width:380px;'><span class='style4'>Per Page </span><span class='style4'><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></span></td>
			<td  style="width:74px;border-right:1px solid #000000;text-align:right;">';
			
			if($startrange > 0)
				echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetPagination('".$startrange."','".$perpage."')>";

			$diff= $startrange - $perpage;
				
			if (($diff) >= 0 ){
				$prvstartrange = $startrange - $perpage;
				echo"&nbsp;&nbsp;<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPagination('".$prvstartrange."','".$perpage."')>";
			}
			else
				echo "&nbsp;";
		
echo"		</td><td  align='left' style='width:74px;'>";
		
		
			if ($startrange <= $totalrows){
				$startrange1 = $startrange + $perpage;
				if  ($startrange1 < $totalrows){
					echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetPagination('".$startrange1."','".$perpage."')>";
					echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetPaginationLast('".$perpage."','".$totalrows."')>";	
				}
			} else
				echo "&nbsp;";
				
			
echo"		</td><td style='width:380px;'>&nbsp;</td>
		</tr>";
echo"	</table></td>
		<td id='search-content3' style='width:31px;'>&nbsp;</td>";
echo"</tr>";			

	echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		<td id='search-content2'>
			<div style='overflow:scroll;height:400px;width:1000px;'>
			<table border='0'  cellpadding='0' cellspacing='1' style='width:960px;'>
			  <col width=20>
			  <col width=90>
			  <col width=100>";
			if($promo == 1)  echo " <col width=100>";
	echo	 "<col width=130>
			  <col width=130>
			  <col width=130>
			  <col width=120>
			  <col width=120>
			  <col width=120>
			  <col width=120>
			  ";
	  
	  
	if($message!=''){
		echo"<tr class='Search-header-bg'>
				<td height='15' align='center' colspan='16' class='searchmessage'>".$message."</td>
			</tr>";
	}
	
	echo"<tr height='25' class='Search-Label-bg'>
			<td  height='25' align='center'><span class='style4'>&nbsp;</span></td>
			<td  height='25' align='center'><span class='style4'>REF.</span></td>";
	if($promo == 1)
	echo 	"	<td align='center'><span class='style4'>From Date</span></td><td  height='25' align='center'><span class='style4'>To Date</span></td>";
	else echo	"	<td align='center'><span class='style4'>Date</span></td>";
	echo 	"	<td align='center'><span class='style4'>Group Name</span></td>
				<td align='center'><span class='style4'>Subgroup Name</span></td>
				<td align='center'><span class='style4'>Category Name</span></td>
				<td align='center'><span class='style4'>Selling Price</span></td>
				<td align='center'><span class='style4'>Default %</span></td>
				<td align='center'><span class='style4'>Default Value</span></td>
				<td align='center'><span class='style4'>User Code</span></td>
			</tr>";

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

	if($ct%2==0)
		$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";	
			
	
	echo'<tr '.$Color.'>';

	echo'	<td align="center"><span class="style5"><img src="img/details.gif" onMouseOver="style.cursor=\'hand\'"  onclick="ShowHideDetails('.$sellingprice_id.');" title="Show Details" style="width:15px; height:15px; "></span></td>';

	echo'	<td align="left"><span class="style5">'.$sellingprice_ref.'</span></td>';
	
	if($promo == 1)
		echo '	<td align="center"><span class="style5">'.$pricefromdate.'</span></td><td align="center"><span class="style5">'.$pricetodate.'</span></td>';
	else	echo '	<td align="center"><span class="style5">'.$historyDate.'</span></td>';
	echo '	<td align="center"><span class="style5">'.$Group_Name.'</span></td>
			<td align="center"><span class="style5">'.$SubGroup_Name.'</span></td>
			<td align="center"><span class="style5">'.$Category_name.'</span></td>
			<td align="center"><span class="style5">Selling Price'.$sellingprice_nbr.'</span></td>
			<td align="center"><span class="style5">'.$default_per.'</span></td>
			<td align="center"><span class="style5">'.$default_val.'</span></td>
			<td align="center"><span class="style5">'.$user_code.'</span></td>
		</tr>';

		
	$sub_query = " select sellingpricedt_id, sellingprice_adjustdt.item_code as item_code, item_description,sellingprice_adjustdt.currency_code as currency_code,
					sellingpricedt_per, sellingpriceBefore, sellingprice".$sellingprice_nbr.", sellingprice".$sellingprice_nbr."b1, sellingprice".$sellingprice_nbr."b2, sellingpricedt_val
					from sellingprice_adjustdt
					inner join items on items.item_code = sellingprice_adjustdt.item_code
					where sellingprice_id = $sellingprice_id
					order by sellingprice_adjustdt.item_code ";

	$sub_result=mysql_query($sub_query,$connection) or die(mysql_error($connection));	
	$sub_totalrows = mysql_num_rows($sub_result);
		
	if ($sub_totalrows > 0) {
		$SlideDivId = 'displaydetails'.$sellingprice_id;
			
		echo "<tr ".$Color." >";
		echo "<td colspan='10' valign='top' align='left'><div id='$SlideDivId' class='div-search-details slideAll' style='display:none; margin-left:100px;'>";
		echo "<table border='0' style='table-layout:fixed; width:780px;' cellpadding='0' cellspacing='1'>";
		echo "<tr><td width='140px'><span class='style4'>Item Code</span></td>
				  <td width='240px'><span class='style4'>Description</span></td> 
				  <td width='100px'><span class='style4'>Currency</span></td> 
				  <td width='100px' align='center'><span class='style4'>Default %</span></td>
				  <td width='100px' align='center'><span class='style4'>Default Value</span></td>
				  <td width='100px' align='center'><span class='style4'>Previous Price</span></td>
				  <td width='100px' align='center'><span class='style4'>New Price</span></td>
			 </tr>";
		while($sub_rows=mysql_fetch_array($sub_result)){
			echo "<tr><td align='left' valign='top' class='style9'>".$sub_rows[1]."</td>
					  <td align='left' valign='top' class='style9'>".$sub_rows[2]."</td>
					  <td align='left' valign='top' class='style9'>".$sub_rows[3]."</td>
					  <td align='center' valign='top' class='style9'>".$sub_rows[4]."</td>
					  <td align='center' valign='top' class='style9'>".$sub_rows[9]."</td>
					  <td align='right' valign='top' class='style9'>".$sub_rows[5]."</td>
					  <td align='right' valign='top' class='style9'>".$sub_rows[6]."</td>
					</tr>";
		}
			
		echo "</table></div></td>";
		echo "</tr>";
	}	
}
							

			
			
echo"</table></div></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:920px;' valign='top'><table width='100%' border=0 height=28px>
		<tr><td style='width:280px;'>&nbsp;</td><td align='right' style='width:74px;border-right:1px solid #000000;'>";
		
			if($startrange > 0)
				echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetPagination('".$startrange."','".$perpage."')>";
				
		//$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			//$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPagination('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='150' align='left'>";
		
		
		if ($startrange <= $totalrows){
			//$startrange = $startrange + $perpage;
			if  ($startrange1 < $totalrows){
				echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetPagination('".$startrange1."','".$perpage."')>";
				echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetPaginationLast('".$perpage."','".$totalrows."')>";	
			}	
		} else
			echo "&nbsp;";
			
		echo"</td><td width='170'>&nbsp;</td></tr>";
		echo"</table></td><td id='search-bottom3' style='height:34px; width:31px;'>&nbsp;</td>";

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

print <<<HERE
</form>
</body>

HERE;
function explodeword($date)
{

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