<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Programmer<programmer@armia.com>        		              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com � 2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
?>
<?php
include "../includes/config.php";
include "../includes/session.php";
include "../includes/functions.php";

if(isset( $_SESSION["sess_adminname"]) and $_SESSION["sess_adminname"]!= ""){
	$adminname = $_SESSION["sess_adminname"];
}else{
	header("Location:login.php");
	exit;
}



if(isset($_GET["txtSearch"]) and $_GET["txtSearch"]!="")
{
	$txtSearch = $_GET["txtSearch"];
	$cmbSearchType = $_GET["cmbSearchType"];
}
else if(isset($_POST["txtSearch"]) and $_POST["txtSearch"]!="")
{
	$txtSearch = $_POST["txtSearch"];
	$cmbSearchType = $_POST["cmbSearchType"];
}

if(isset($_POST['gotoPage']))
{

	header("Location:".$_SERVER['PHP_SELF']."?page=".$_POST['gotoPage']);
	
		
}


$qryopt="";

if(isset($_POST['cmbSearchType']))
{

	$qryopt = " AND coupon.ngift_product_id = ".$_POST['cmbSearchType'];


}

$sql = " SELECT pr.product_name,usr.first_name,usr.last_name,coupon.ngift_coupon_amount,
				LEFT(coupon.ngift_coupon_products,LENGTH(coupon.ngift_coupon_products)-1) AS gift_purchase
		  FROM ".$tableprefix."products pr,".$tableprefix."giftcoupon_master coupon,".$tableprefix."users usr,
		  		".$tableprefix."order_details odet,".$tableprefix."orders ods
		   WHERE pr.product_id = coupon.ngift_product_id
		    AND  coupon.ngift_product_id = odet.product_id
			AND  odet.order_id = ods.order_id
		    AND ods.user_id = usr.user_id " . $qryopt .
			" ORDER BY coupon.ngift_coupon_amount DESC";
			
	if(!(isset($pagenum)))
	{
		$pagenum = 1;
	} 
	
	$limit = getSettingsValue('npagination_limit');
	
	$page = isset($_GET['page'])? trim($_GET['page']):1;

	
	if(empty($page))
	{
		$page = 1;
	}
	
	$result=mysql_query($sql) or die(mysql_error());						
	
	$totalrows = mysql_num_rows($result);
	
	$limitvalue = ($page - 1) * $limit;
	
	$query_string = " LIMIT $limitvalue, $limit";
	
	$sql = $sql.$query_string;
	   
	$rs = mysql_query($sql) or die(mysql_error());

	$numofpages = ceil($totalrows / $limit);


include "../includes/htmltop.php";
?>
<script language="JavaScript" type="text/JavaScript">
function clickSearch()
{

  document.frmProductViewed.submit();
     
}

</script>

<body>
<center>
		
		<div align="left" id="layout">
		
		
				
				
				<!--header area start -->
<?php

	include_once("../includes/topheader_admin.php")

?>
				
				<!--header area end -->
				
				
				
				
				<div align="left" class="main_body">
				<!--main body start -->
				
				
				
<?php
		include_once("../includes/admintopmenu.php")
?>		
																					
								
					<div align="left" class="content_area">
					
					<!--content area start -->
							
					<div align="left" class="page_title">Gift Coupon Report</div>
					<div align="center" class="text_information grey_tr"><?php echo $message?></div>
            		<br>
											
					<div align="left">
					
					<form action="" method="post" name="frmGiftCoupon">
					
					<table width="100%" border="0" cellpadding="0" cellspacing="0">
					
					
					<tr valign="top">
					
						<td width="100%" align=right valign="baseline" class=''>Product ::
									<select name="cmbSearchType" class="textbox">
								<?php
								
									$select_product = "SELECT pr.product_id,pr.product_name 
													   FROM ".$tableprefix."products pr,
														".$tableprefix."giftcoupon_master gift_coupon 
													   WHERE pr.product_id = gift_coupon.ngift_product_id ";
									$result_product = mysql_query($select_product) or die(mysql_error());
									
									if(mysql_num_rows($result_product) != 0)
									{
									
										while($product_row = mysql_fetch_array($result_product))
										{
										
											if($product_row['product_id'] == $_POST['cmbSearchType'])
											{
											
												$product_checked = "selected";
											
											}
											else
											{
											
												$product_checked = "";
											
											}
										
								?>		
									
									<option value="<?php echo $product_row['product_id']?>" <?php echo $product_checked?>><?php echo $product_row['product_name']?></option>
								
								
								<?php	
								
										}
								
									}
								
								?>		
									</select>
										&nbsp;
										<a onClick="return clickSearch();" class="search_button"><?php echo BTN_SEARCH; ?></a>
									</td>
								</tr>
							</table>
						
					</form>
										
					</div>
										
					<div align="left">					
										
					<form action="" method="post" name="frmGiftCoupon2">
					<table width="100%" border="0" cellpadding="0" cellspacing="0" class="manage_table">
					
						<tr>
							<th width="3%">#</th>
							<th width="21%" class="boxalternatecolor2">Gift Coupon Name</th>
							<th width="29%">Customer Purchased</th>
							<th width="29%">Products Purchased</th>
							<th width="47%">Balance Amount</th>
						</tr>
<?php
$i = 1;

if(mysql_num_rows($result) > 0 )
{
	while($row = mysql_fetch_array($rs))
	{
																													
?>
								<tr>
									<td><?php echo $i?></td>
									<td align="left" class=boxalternatecolor2><?php echo htmlentities($row["product_name"])?></td>
									<td align="left"><?php echo $row["first_name"]."&nbsp;".$row["last_name"]?></td>
									<td align="left">
										<table cellpadding="0" cellspacing="0" width="100%" border="0">
										
										
								<?php
								
									if($row['gift_purchase'] == '')
									{
									
										$product_purchased = -1;
									
									}
									else
									{
									
										$product_purchased = $row['gift_purchase'];
									
									}
									
									
								
									$select_product_name = "SELECT product_name FROM ".$tableprefix."products 
															WHERE product_id IN(".$product_purchased.")";	
									
									$result_product_name = mysql_query($select_product_name) or mysql_error();
									
									if(mysql_num_rows($result_product_name) > 0)
									{
									
										while($product_name_row = mysql_fetch_array($result_product_name))
										{
									
																	
								?>		
										
										<tr>
											<td colspan="4" align="left"><?php echo $product_name_row['product_name']?></td>
										</tr>
										
								<?php
								
										}
								
									}
								
								?>		
										
									</table>	
									</td>
									<td colspan="5" align="left"><?php echo $row["ngift_coupon_amount"]?></td>
								</tr>
<?php
	$i++;
	}
}
else
{
										
?>										

								<tr>
									<td align="center" valign="middle" colspan="7" class="text_information">No Records Found !!</td>
								</tr>
										
										
										
										
<?php										
}


									if($page > 1)
									{
										$pageprev = $page-1;
										
										$previous_page = "<a href=".$_SERVER['PHP_SELF']."?page=$pageprev alt='Previous' border='0' class='small_gray_button'>".TEXT_PREV."</a>";
										
									}
																													
																						
									if($page < $numofpages)
									{
										$pagenext = ($page + 1);
										
										$next = "<a href=".$_SERVER['PHP_SELF']."?page=$pagenext alt='Next' border='0' class='small_gray_button'>".TEXT_NEXT."</a>";
										
									}	
	
																				
?>
										<tr>
											<td class="page_nos" colspan="5" align="center">
												<table width="100%" border="0" cellspacing="10" cellpadding="0">
													<tr align="left" valign="middle">
														<td width="21%" align="right"><?php echo $previous_page?></td>
														<td width="23%" align="right">
															Page(s) <?php echo $page ?>/<?php echo $numofpages?>&nbsp;&nbsp;Goto
														</td>
														<td width="8%" align="right">
															<select name="gotoPage" style="padding:4px 5px; margin-bottom:1px; ">
																<?php
																for($i=1;$i<=$numofpages;$i++)
																{		
																if($i == $_GET['page'])
																{
																$page_selected = "selected";
																}
																else
																{
																$page_selected = "";
																}
																?>									
																<option value="<?php echo $i?>" <?php echo $page_selected?>><?php echo $i?></option>
																<?php
																}
																?>									
															</select>
														</td>
														<td width="7%">
															<a onClick="Javascript:document.frmGiftCoupon2.submit();" class="go_button_2"><?php echo GO; ?></a>
														</td>
														<td width="41%" align="center"><?php echo $next?></td>
													</tr>
												</table>
											</td>
										</tr>
										</table>
										
					  </form>											

						</div>
						
						
				<!--content area end-->
								
				</div>
				
								
					<div align="left" class="clear_float"><img src="<?php echo $imagefolder;?>/clear.gif" alt="" width="1" height="1" /></div>
				<!--main body end -->
				</div>
				
				
<?php
	include "../includes/footer.php";
?>
