<?php
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, base2 from setup";
$result1=mysql_query($query1,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($result1)){
extract($rows);

}


set_time_limit(0);


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

	$query1=" Select Coupon_Id, Coupon_Reference, Coupon_Name, ".
	        " ifNull(convertcurfunc(Currency_Code, '".$PCurrCode."', Coupon_Date, 0, 0, '', '', ifNull(Coupon_Amount,0)),0) As Coupon_Amount, ".
			" ifNull(convertcurfunc(Currency_Code, '".$PCurrCode."', Coupon_Date, 0, 0, '', '', ifNull(Coupon_Amount,0)),0) -  ".
			" ifNull(convertcurfunc(Currency_Code, '".$PCurrCode."', Coupon_Date, 0, 0, '', '', ifNull(Coupon_AmountConsum,0)),0) As Coupon_RemAmount  ".
	        " From Coupon Where Coupon_Reference='".$_GET['lcode']."'";
	$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"];}
$str 	= $_GET['str'];if ($str == ""){$str = $_POST["str"];}
$PCurrCode   = $_GET['PCurrCode'];if ($PCurrCode == ""){$PCurrCode = $_POST["PCurrCode"];}
$PLedgerNo   = $_GET['PLedgerNo'];if ($PLedgerNo == ""){$PLedgerNo = $_POST["PLedgerNo"];}
$PNetTotal   = $_GET['PNetTotal'];if ($PNetTotal == ""){$PNetTotal = $_POST["PNetTotal"];}
$PCouponCode = $_POST["PCouponCode"]; if ($PCouponCode == ""){$PCouponCode = $_GET["PCouponCode"];}		
$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=iso-8859-1" />
<title>Select Coupon</title>
<script type="text/javascript" language="javascript">

function clearform()
{
  vFrm  	= document.form1.frm.value;
  vStr  	= document.form1.str.value;
  vCurr  	= document.form1.PCurrCode.value;    
  vLedger	= document.form1.PLedgerNo.value;
  vNetTotal = document.form1.PNetTotal.value;
	
  document.form1.PCouponCode.value='';

  document.form1.action='List_Coupon.php?PCouponCode=&PCurrCode='+vCurr+'&PLedgerNo='+vLedger+'&PNetTotal='+vNetTotal+'&frm='+vFrm+'&str='+vStr;   
  document.form1.submit();

}
function SetNextPage(startrange,perpage){
	vFrm      = document.form1.frm.value;
	vStr   	  = document.form1.str.value;
	vCurr  	  = document.form1.PCurrCode.value;   
	vLedger	  = document.form1.PLedgerNo.value;
	vNetTotal = document.form1.PNetTotal.value;	
	
	document.form1.action='List_Coupon.php?PCouponCode=&PCurrCode='+vCurr+'&PLedgerNo='+vLedger+'&PNetTotal='+vNetTotal+'&frm='+vFrm+'&str='+vStr+'&startrange='+startrange+'&perpage='+perpage;
	document.form1.submit();
}

function SetPrvPage(startrange,perpage){
	vFrm   	  = document.form1.frm.value;
	vStr   	  = document.form1.str.value;
	vCurr  	  = document.form1.PCurrCode.value;   
	vLedger	  = document.form1.PLedgerNo.value;
	vNetTotal = document.form1.PNetTotal.value;
	
	document.form1.action='List_Coupon.php?PCouponCode=&PCurrCode='+vCurr+'&PLedgerNo='+vLedger+'&PNetTotal='+vNetTotal+'&frm='+vFrm+'&str='+vStr+'&startrange='+startrange+'&perpage='+perpage;
	document.form1.submit();
}
function RefreshPage(){
	vFrm      = document.form1.frm.value;
	vStr      = document.form1.str.value;
	vPerPage  = document.form1.perpage.value;
	vCurr     = document.form1.PCurrCode.value;  
	vLedger   = document.form1.PLedgerNo.value;	
	vNetTotal = document.form1.PNetTotal.value;
	
	document.form1.action='List_Coupon.php?PCouponCode=&PCurrCode='+vCurr+'&PLedgerNo='+vLedger+'&PNetTotal='+vNetTotal+'&frm='+vFrm+'&str='+vStr+'&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;
	margin-left:0px;
	margin-top:0px;
}
.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 id='form1' name='form1' method='post' action=''>
  <table width='670' border='0' cellspacing='1' align='center' style='table-layout:fixed'>
	<col width=130>
	<col width=250>
	<col width=60>
	<col width=110>
	<col width=110>";

echo "
    <tr>
      <th colspan='5' bgcolor='#DDDDDD'><span class='style1'> COUPONS </span></th>
    </tr>
    <tr>
      <td bgcolor='#DDDDDD' > <span class='style2'>Search for Coupon  <br> Code / Name</span></td>
      <td colspan='4' bgcolor='#DDDDDD' >
	    <input type='text' name='PCouponCode' value='$PCouponCode' style='width:160px; ' />
		<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='btnCancel' value='Cancel' onclick='clearform();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
		<input type='hidden' name='id1' id='id1' value='$Coupon_Id'>
		<input type='hidden' name='code1' id='code1' value='$Coupon_Reference'>
		<input type='hidden' name='desc1' id='desc1' value='$Coupon_Name'>
		<input type='hidden' name='amount1' id='amount1' value='$Coupon_Amount'>
		<input type='hidden' name='amountrem1' id='amountrem1' value='$Coupon_RemAmount'>
		<input type='hidden' name='frm' value='$frm'>
		<input type='hidden' name='str' value='$str'>
		<input type='hidden' name='PCurrCode' value='$PCurrCode'>
		<input type='hidden' name='PLedgerNo' value='$PLedgerNo'>
		<input type='hidden' name='PNetTotal' value='$PNetTotal'>
			
		<span class='style2'>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 if($post=="1") { ?>
<script type="text/javascript" language="javascript">
var FormOption = document.form1.frm.value;
var vAmount    = document.form1.amount1.value;
var vRemAmount = document.form1.amountrem1.value;
var vNetTotal  = document.form1.PNetTotal.value;

vAmount     = parseFloat(vAmount);
vRemAmount  = parseFloat(vRemAmount);
vNetTotal   = parseFloat(vNetTotal);

if (FormOption == 1) {
	opener.document.retailpay.txtRetCouponId.value=document.form1.id1.value;
	opener.document.retailpay.txtRetCouponCode.value=document.form1.code1.value;
	opener.document.retailpay.txtRetCouponName.value=document.form1.desc1.value;
	
	opener.document.retailpay.txtRetCreditAmtB12.value = 0;
	opener.document.retailpay.txtRetCreditAmt.value	   = 0;
	opener.document.retailpay.txtRetCashAmtB12.value   = 0;
	opener.document.retailpay.txtRetCashAmt.value      = 0;
	
	if (vRemAmount >= vNetTotal) {
		opener.document.retailpay.txtRetCouponAmt.value = vNetTotal;
		opener.document.retailpay.txtRetCouponRemAmt.value = (vRemAmount - vNetTotal);
	}	
	else if (vRemAmount < vNetTotal) { 
		opener.document.retailpay.txtRetCouponAmt.value = vRemAmount;
		opener.document.retailpay.txtRetCouponRemAmt.value = 0;
	}
	
	
	opener.document.retailpay.txtRetCouponCode.focus();
	opener.document.retailpay.submit();
} 

self.close();



</script>
<?php } ?>

<?php

echo" <tr bgcolor='#E5E5E5'>
        <td align='left'><span class='style2'>Code </span></td>
        <td align='left'><span class='style2'>Name </span></td>
		<td align='center'><span class='style2'>Currency </span></td>
		<td align='center'><span class='style2'>Amount </span></td>
		<td align='center'><span class='style2'>Remain Amount </span></td>";
echo" </tr>";


$query = "";

$query = " Select Coupon.Coupon_Reference As Coupon_Code, Coupon.Coupon_Name As Coupon_Name, Currency_Code, ".
		 " ifNull(Coupon_Amount,0) As Coupon_Amount, (ifNull(Coupon_Amount,0) - ifNull(Coupon_AmountConsum,0)) As Coupon_RemAmount ".
		 " From Coupon ".
		 " Where (Coupon.Coupon_Reference like '%".$PCouponCode."%' or Coupon.Coupon_Name like '%".$PCouponCode."%' ) ".
		 " And ifNull(Coupon_AmountBase1,0) - ifNull(Coupon_AmountConsumBase1,0) > 0 ".
		 //" And Ledger_Number = '".$PLedgerNo."' ".
		 " Order By Coupon.Coupon_Reference ";
				

			
//echo $query;exit;	

if($post!="1") {
	
	$resultsearch=mysql_query($query,$connection) or die(mysql_error());
	$totalrows = mysql_num_rows($resultsearch);
	
	if ($startrange=='' || $PCouponCode != '')
		$startrange = 0;
	if ($perpage=='')
		$perpage = 20;
		
	$query = $query." limit ".$startrange.",".$perpage;
	//echo $query;exit;
	$result1=mysql_query($query,$connection) or die(mysql_error());
	
	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."'";	
		}
		
			
		echo "<tr  $Color > 
				<td align='left' ><span class='style4'><a href='List_Coupon.php?lcode=$Coupon_Code&frm=$frm&PCurrCode=$PCurrCode&PNetTotal=$PNetTotal'>$Coupon_Code</a></span></td>
 			    <td align='left'><span class='style4'>".$Coupon_Name."</span></td>
				<td align='center'><span class='style4'>".$Currency_Code."</span></td>
				<td align='right'><span class='style4'>".number_format($Coupon_Amount,2)."</span></td>
				<td align='right'><span class='style4'>".number_format($Coupon_RemAmount,2)."</span></td>
		     </tr>";


	}
	
	echo"<tr><td colspan='5'>&nbsp;</td></tr>
		 <tr><td colspan='5'><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>";
}


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