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

include("config.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");

                $fromdate = $_GET['from_date'];
                $todate = $_GET['to_date'];
				$reportoption = $_GET['reportoption'];
                $physicaldamage = $_GET['physicaldamage'];
				$rmastatus = $_GET['rmastatus'];
                $complete = $_GET['complete'];
				$groupby = $_GET['groupby'];
				$orderby = $_GET['orderby'];
				$charge = $_GET['charge'];
				$warranty = $_GET['warranty'];
				$rmaresult = $_GET['rmaresult'];
				$serialnumber = $_GET['serialnumber'];
				$group = $_GET['group'];
				$problemcategory = $_GET['problemcategory'];
				$fromclient = $_GET['fromclient'];
				$toclient = $_GET['toclient'];
				$status = $_GET['status'];
				$combo = $_GET['combo'];
				$costcenterchoose = $_GET['costcenterchoose'];
				$projectchoose = $_GET['projectchoose'];
				$costcentercheck = $_GET['costcentercheck'];
				$projectcheck = $_GET['projectcheck'];
				$exp = $_GET['exp'];
				if($exp == 1)
				{
header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
header("Content-type:   application/x-msexcel; charset=utf-8");
header("Content-Disposition: attachment; filename=RMA_Report.xls"); 
				}
				
   $fromdate = explode("/", $fromdate);
   $fromdate = $fromdate[2]."-".$fromdate[1]."-".$fromdate[0];
   
   $todate = explode("/", $todate);
   $todate = $todate[2]."-".$todate[1]."-".$todate[0];
   
   $query_setup  = "select company_name, base1, base2 from setup";
   $result_setup = mysql_query($query_setup, $connection);
   $row_setup    = mysql_fetch_array($result_setup);
   extract($row_setup);
   
   $query_module  = "select module_Costcenter, module_project from module";
   $result_module = mysql_query($query_module, $connection); 
   $row_module    = mysql_fetch_array($result_module);
   extract($row_module);
   
   $query_rmacharge  = "select rmacharge_amount from rma";
   $result_rmacharge = mysql_query($query_rmacharge, $connection); 
   $row_rmacharge    = mysql_fetch_array($result_rmacharge);
   extract($row_rmacharge);
   
echo"
<html>
<head>
<title></title>
<table>
<tr>
<td style='padding-left:500px; font-size:20px; text-align: center'><b><u> $company_name </u></b></td>
</tr>
<tr>
<td style='padding-left:500px; font-size:18px; text-align: center'><b> $reportoption</b></td>
</tr>";
//<td style='padding-left:320px'> <b><u>Printed Date:</u></b> $fromdate </td>
/*<table width='2000px'>
<table>
<tr>
<td><b> Date: </b></td>
<td style='padding-left:20px'><b> From </b> </td>
<td style='padding-left:5px'> $fromdate</td>
<td style='padding-left:200px'><b> Client:</b> </td>
<td style='padding-left:5px'> From</td> 
<td style='padding-left:5px'> $fromclient</td>
<td style='padding-left:200px'><b> Status:</b> $rmastatus</td>
</tr>

<table>
<tr>
<td style='width:20px;padding-left:58px'><b> To </b> </td>
<td style='width:100px;padding-left:22px'> $todate</td>
<td style='padding-left:225px'>To</td>
<td style='padding-left:25px'> $toclient</td>
<td style='padding-left:200px'><b> Warranty:</b> $warranty</td>
</tr>
*/
echo"
<table>
<br>
<tr>
<td><u><b>Date:</b></u></td>
<td style='padding-left:50px'><b>From:</b></td>
<td style='padding-left:10px'>$fromdate</td>
<td style='padding-left:50px'><b>To:</b></td>
<td style='padding-left:10px'>$todate</td>
<td style='padding-left:300px'><b><u>Time:</b></u> ".date("H:i:s")."</td>
</tr>
</table>
<table>
<br>
<tr>
<td><u><b>Serial No</b></u></td>
<td style='padding-left:100px'><u><b>Date</b></u></td>
<td style='padding-left:150px'><u><b>RMA Ref.</b></u></td>
<td style='padding-left:70px'><u><b>Client</b></u></td>
</tr>";
/*
<tr><td><b> Item:</b></td></tr>
<tr><td><b> Problem:</b></td></tr>
<tr><td><b> Service Center:</b></td></tr>
<tr><td><b> Solution:</b></td></tr>
<tr><td><b> Type:</b></td></tr>
";
*/
 $query_rma = $query_rma."select Serial_Number,concat(items.item_code,items.item_description) as item_code,rmadt.rma_date,rma_reference,concat(rmadt.client_code,' - ',clients.ledger_name) as client_code,rmadt_problem,".
    " rmadt_servicecenter,rmadt_solution,rma.rma_type ".
    " from rmadt,rma,clients,items ".
    " where rma.rma_id=rmadt.rma_id ".
    " and clients.ledger_number=rma.client_code ".
    " and items.item_code=rmadt.item_Code ".
    " and rmadt.rma_date between '$fromdate' and '$todate 23:59:59'";
	
    if ($serialnumber !="") //Serial no
	
      $query_rma = $query_rma." and Serial_Number='$serialnumber'";
   
      $query_rma = $query_rma." order by Serial_Number,rmadt.rma_date,items.item_code";
	
	
	//echo $query_rma;
	//exit;
    $result_rma = mysql_query($query_rma, $connection);
	
    while($row_rma = mysql_fetch_array($result_rma)){
	
	extract($row_rma);
	echo "<tr>
<td style='padding-left:0px'>$serialnumber</td>
<td style='padding-left:50px'>$rma_date</td>
<td style='padding-left:0px; text-align:right '>$rma_reference</td>
<td style='padding-left:50px; text-align:right '>$client_code</td>
</tr>
<tr><td>Item:</td><td>$item_code</b></td></tr>
<tr><td>Problem:</td><td>$rmadt_problem</b></td></tr>
<tr><td>Service Center:</td><td>$rmadt_servicecenter</b></td></tr>
<tr><td>Solution:</td><td>$rmadt_solution</b></td></tr>
<tr><td>Type:</td><td>$rma_type</b></td></tr>
<tr><td><hr></td></tr>";
}

echo"
<script>
//window.print();
</script>
</table>
</body>
</html>";
?>