<?php


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

// var_dump($_POST);
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");

$querylog=" select ifNull(FC_CashRegisterDel,0) As FC_CashRegisterDel, ifNull(FC_CashRegisterMod,0) As FC_CashRegisterMod, ".
          " ifNull(FC_CashRegScrollPrint,0) As FC_CashRegScrollPrint, ifNull(login_belongstogrp,0) As FI_BelongsToGrp, ".
		  " ifNull(FC_CashRegisterCredit,0) As FC_CashRegisterCredit, ifNull(FC_SearchByCashRegister,0) As FC_SearchByCashRegister, ".
		  " ifNull(FC_CashRegisterVoid,0) As FC_CashRegisterVoid, ifNull(FC_CashRegisterWaste,0) As FC_CashRegisterWaste ".
          " 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);
}


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



$keyword    = $_POST['keyword']; if ($keyword == ""){$keyword = $_GET["keyword"];}
$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"];}
$expiredPromos = $_POST['expiredPromos']; if ($expiredPromos == ""){$expiredPromos = $_GET["expiredPromos"];}
 
$PMode = $_POST['PMode']; if ($PMode == ""){$PMode = $_GET["PMode"];}
$SelectedRec = $_POST['SelectedRec']; if ($SelectedRec == ""){$SelectedRec = $_GET["SelectedRec"];}
$SelectedRecColor = $_POST['SelectedRecColor']; if ($SelectedRecColor == ""){$SelectedRecColor = $_GET["SelectedRecColor"];}
$Sale_Id = $_POST['Sale_Id']; if ($Sale_Id == ""){$Sale_Id = $_GET["Sale_Id"];}
$index = $_POST['index']; if ($index == ""){$index = $_GET["index"];}


if (empty($orderby))  $orderby = 1;
if (empty($searchby)) $searchby = 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" />
HERE;

if($cashreg_colortheme=='red'){
	echo'<link rel="stylesheet" type="text/css" media="all" href="css-v2/style-red.css">';
	echo'<link rel="stylesheet" type="text/css" href="js/menu-v2/menu-red.css" />';
}	
else if($cashreg_colortheme=='' || $cashreg_colortheme=='default')	{
	echo'<link rel="stylesheet" type="text/css" media="all" href="css-v2/style-default.css">';
	echo'<link rel="stylesheet" type="text/css" href="js/menu-v2/menu-default.css" />';
}

print <<<HERE
<title>Search Promotions</title>
<script type="text/javascript" language="javascript">


function clearform()
{
   document.search.fromdate.value='';
  document.search.todate.value='';
  document.search.item_code.value='';
  document.search.group_code.value='';
  document.search.subgroup_code.value='';

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

}

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

function SetPrvPage(startrange,perpage){

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


function deleteItem(RetId){

	if (RetId != '') {
		if (confirm ( "Are you sure you want to Delete this record ?") )
		{
			document.search.action="Form_Retail_Actions.php?mainmode=deletePromoItem&PromoId="+RetId;
			document.search.submit();
		}
	}
	
}

function editItem(RetId){
	
	opener.document.form1.promotionId.value=RetId;
	opener.document.form1.actionmode.value='';
	opener.document.form1.submit();
	self.close();
}



function SubmitForm()
{	

  document.search.action='Form_RetailEditPromo.php?startrange=0';
  document.search.submit();

}

function SetFirstPage(startrange,perpage){
	document.search.action='Form_RetailEditPromo.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_RetailEditPromo.php?startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function RefreshPage()
{
 document.search.action='Form_RetailEditPromo.php?startrange=0';	
  document.search.submit();

}


function showlistitem(str){
	window.open('select_item.php?item='+str,'select_item','width=900,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showlistsubgroups(str){
	window.open('List_SubGroup.php?str='+str,'List_SubGroup','width=900,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showlistgroups(str){
	window.open('List_Grouping.php?str='+str,'List_SubGroup','width=900,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

</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   class='bodybackground'>
<form name='search' method='post' action="">
HERE;


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

if (empty($ViewPeriodDate))
   $ViewPeriodDate = 'Null';
if (empty($ViewNumberOfDays))   
   $ViewNumberOfDays = 1;
			
$querysearch= "select promotion_id, date_format(promotion_fromdate,'%Y-%m-%d %r') as promoFromdate, date_format(promotion_todate,'%Y-%m-%d %r') as promoTodate, promotion_itemCode, item_description,
				ifnull(promotion_discount,0) as promotion_discount
				from item_promotion 
				inner join items on items.item_code = promotion_itemCode
				where if( '$group_code'='', 1=1, Group_code = '$group_code')
				and  if( '$subgroup_code'='', 1=1, subgroup_code = '$subgroup_code') and  if( '$item_code'='', 1=1, item_code = '$item_code')";
				if($expiredPromos == 'on') $querysearch .=  "and promotion_todate  < now()	";
				else $querysearch .=  " and promotion_todate >= now() ";
if($fromdate != '' && $todate != '') {
	$ndate = explode('/', $fromdate);
	$fromdate = $ndate[2]."-".$ndate[1]."-".$ndate[0];
	
	$ndate = explode('/', $todate);
	$todate = $ndate[2]."-".$ndate[1]."-".$ndate[0];
	$querysearch .=  "	and (date_format(promotion_fromdate,'%Y-%m-%d') >= '$fromdate' and 	date_format(promotion_todate,'%Y-%m-%d') <= '$todate' )";			
}
$querysearch .= "	order by  promotion_fromdate,promotion_todate desc ";
			 
			  

			 
// echo $querysearch;			 

$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"<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:850px' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>List of Promotions
			</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:950px;'>
	  <col width=120>
	  <col width=150>
	  <col width=120>
	  <col width=220>
	  <col width=120>
	  <col width=220>";

		     
	
echo" <tr class='Search-header-bg'>
		<td><span class='style4'>From Date</span></td><td><input type='text' name='fromdate' id='fromdate' value='$fromdate' style='width:120px;'>
					<a href='#' onclick=\"displayCalendar(document.form1.fromdate,'dd/mm/yyyy',this); return false;\" name='anchor1' id='anchor1'><img src='./img/cal.gif' border=0 ></a>
		</td>
		<td><span class='style4'>Group </span></td><td><input type='text' name='group_code' id='group_code' value='$group_code' style='width:150px;'>
			<img src='img/btnopen.jpg' width='15' height=20' onclick=\"showlistgroups('16');\" onmouseover=\"style.cursor='pointer'\" title='Select Group'>
		</td>
		<td><span class='style4'>Sub Group</span></td><td><input type='text' name='subgroup_code' id='subgroup_code' value='$subgroup_code' style='width:150px;'>
			<img src='img/btnopen.jpg' width='15' height=20' onclick=\"showlistsubgroups('11');\" onmouseover=\"style.cursor='pointer'\" title='Select SubGroup'>
		</td>
	</tr>";	  

echo" <tr class='Search-header-bg'>		
		<td><span class='style4'>To Date</span></td><td><input type='text' name='todate' id='todate' value='$todate' style='width:120px;'>
			<a href='#' onclick=\"displayCalendar(document.form1.todate,'dd/mm/yyyy',this); return false;\" name='anchor1' id='anchor1'><img src='./img/cal.gif' border=0 ></a>
		</td>
		<td><span class='style4'>Item</span></td><td><input type='text' name='item_code' id='item_code' value='$item_code' style='width:150px;'>
			<img src='img/btnopen.jpg' width='15' height=20' onclick=\"showlistitem('21');\" onmouseover=\"style.cursor='pointer'\" title='Select Item'>
		</td>
		<td colspan='2'><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></td>
	</tr>";	  
	if($expiredPromos == 'on') $expiredChecked = 'checked'; else $expiredChecked = '';
echo" 
	<tr class='Search-header-bg'>
	<td height='15' align='center' colspan='6'><input type='checkbox' id='expiredPromos' name='expiredPromos' $expiredChecked><span class='style4'>Expired Promotions</span></td>";

echo" 	</tr>";
echo" 
	  <tr class='Search-header-bg'>
		<td height='15' align='center' colspan='6'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></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:1040px;'>
		<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:0px solid #000000;text-align:right;">';
			
			if($startrange > 0)
				echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetFirstPage('".$startrange."','".$perpage."')>";

			$diff= $startrange - $perpage;
				
			if (($diff) >= 0 ){
				$prvstartrange = $startrange - $perpage;
				echo"<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$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=SetNextPage('".$startrange1."','".$perpage."')>";
					echo"<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$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'>";
	echo"<div style='overflow:scroll;height:400px;width:950px;'>";
	echo"<table border='0'  cellpadding='0' cellspacing='1' >
		  <col width=30>
		  <col width=30>
		  <col width=140>
		  <col width=140>
		  <col width=180>
		  <col width=280>
		  <col width=150>
		  ";

echo" <tr class='Search-Label-bg'>
		
		<td  height='25' align='left'><span class='style4'> </span></td>
		<td  height='25' align='left'><span class='style4'> </span></td>
		<td  height='25' align='center'><span class='style4'>From Date/Time </span></td>
		<td  height='25' align='center'><span class='style4'>To Date/Time </span></td>
		<td  height='25' align='center'><span class='style4'>Item Code </span></td>
		<td  height='25' align='center'><span class='style4'>Description</span></td>
		<td  height='25' align='center'><span class='style4'>Discount %</span></td>
	</tr>";
	
		
	

$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  ".$Color.">";
	
	echo'	<td align="center"><span class="style5"><img src="img/delete.png" style="cursor:pointer;" title="Delete Record" onclick="deleteItem(\''.$promotion_id.'\');"></span></td>
			<td align="center"><span class="style5"><img src="img/edit.png" style="cursor:pointer;" title="Edit Record" onclick="editItem(\''.$promotion_id.'\');"></span></td>
			<td align="center"><span class="style5">'.$promoFromdate.'</span></td>
			<td align="center"><span class="style5">'.$promoTodate.'</span></td>
			<td align="left"><span class="style5">'.$promotion_itemCode.'</span></td>
			<td align="left"><span class="style5">'.$item_description.'</span></td>
			<td align="center"><span class="style5">'.$promotion_discount.'</span></td>
		</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:1040px;' valign='top'><table width='100%' border=0 height=28px>
		<tr><td style='width:380px;'>&nbsp;</td><td align='right' style='width:74px;border-right:0px solid #000000; ' valign='top'>";
		
			if($startrange > 0)
				echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetFirstPage('".$startrange."','".$perpage."')>";
				
		//$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			//$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='74px' align='left' valign='top'>";
		
		
		if ($startrange <= $totalrows){
			//$startrange = $startrange + $perpage;
			if  ($startrange1 < $totalrows){
				echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange1."','".$perpage."')>";
				echo"<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."')>";	
			}	
		} else
			echo "&nbsp;";
			
		echo"</td><td width='380px'>&nbsp;";
		
		//include("toolbar.php");	 
		
		echo"</td></tr>";
		echo"</table></td><td id='search-bottom3' style='height:34px; width:31px;'>&nbsp;</td></tr>";

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


          

		  

print <<<HERE

</form>
</body>

HERE;

?>