<?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'];
				
   $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:50px; font-size:20px; text-align: center'><b> $company_name </b></td>
<td style='padding-left:320px'> <b><u>Printed Date:</u></b> $fromdate </td>
</tr>
<table>
<tr>
<td style='padding-left:470px'><b><u>Time:</b></u> ".date("H:i:s")."</td>
</tr>
</table>
</head>
<body>";
/*<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'><u><b>Ref.</b></u></td>
<td style='padding-left:50px'><u><b>Client</b></u></td>
<td style='padding-left:10px'><u><b>Promised Date</b></u></td>
<td style='padding-left:70px'><u><b>Curr.</b></u></td>
<td style='padding-left:70px'><u><b>Amount</b></u></td>
</tr>
";

$query_rma = "SELECT distinct(RMA.RMA_REFERENCE),date_format(RMADT.RMA_DATE,'%d/%m/%Y') RMA_DATE1 ,RMA.CLIENT_CODE , CLIENTS.LEDGER_NAME"
." ,RMA.CLIENT_CODE ,clients.ledger_name  , RMA_PromisedDate , rma.rma_id";

$query_rma = $query_rma . ",CLIENTS.Client_Telephone1, Rma.Rma_Remark , date_format(RMA_PromisedDate, '%d/%m/%Y %H:%i:%s') as RMA_PromisedDate "; //, rma.Currency_code

$query_rma = $query_rma . " ,'$charge' as currency_code";

if($charge == $base1)
{
	$query_rma = $query_rma .", rma.Rmacharge_amountbase1 as Rmacharge";
}
else
	if($charge == $base2)
	{
		$query_rma = $query_rma .", rma.Rmacharge_amountbase2 as Rmacharge";
	}
	
	$query_rma = $query_rma ." FROM RMADT";
    $query_rma = $query_rma ." INNER JOIN RMA ON RMA.RMA_ID = RMADT.RMA_ID";


  if($module_Costcenter == '1'){
	  if($costcentercheck == 1)
     $query_rma = $query_rma ." and rma.costcent_code like '$costcenterchoose%'";
  else
      $query_rma = $query_rma ."and rma.costcent_code = '$costcenterchoose'";
  }
  
  if ($module_project == '1')
  {
  if ($projectcheck == '1')
      $query_rma = $query_rma ." and rma.project_code like '$projectchoose%'";
  else
      $query_rma = $query_rma ." and rma.project_code = '$projectchoose'";
  }
  
  if ($rmastatus == "Pending")
  {
    $query_rma = $query_rma ." AND (RMADT.RMA_TYPE = 'MI'  and not exists(select 1 from rmadt as rmadt1 where rmadt1.rma_type = 'MR'";
    $query_rma = $query_rma ." and rmadt1.mi_id = rmadt.rmadt_id ))";
  }
	
	if ($rmastatus == "ALL")
	{
     
     $query_rma = $query_rma ." AND ((RMADT.RMA_TYPE = 'MI' and not exists(select 1 from rmadt as rmadt1 where rmadt1.rma_type = 'MR'";
     $query_rma = $query_rma ." and rmadt1.mi_id = rmadt.rmadt_id ))";
     $query_rma = $query_rma ." or ";
     $query_rma = $query_rma ."(exists(select 1 from rmadt as rmadt1 where rmadt1.rma_type = 'MR' and rmadt1.mi_id = rmadt.rmadt_id )))";
    }
	
	

  if ($rmastatus == "Handover")
  {
    $query_rma = $query_rma ." AND (exists(select 1 from rmadt as rmadt1 where rmadt1.rma_type = 'MR' ";
    $query_rma = $query_rma ." and rmadt1.mi_id = rmadt.rmadt_id )) ";
  }
  
  $query_rma = $query_rma ." INNER JOIN CLIENTS ON RMADT.CLIENT_CODE = CLIENTS.LEDGER_NUMBER ";
  $query_rma = $query_rma." and rma.rma_date between '$fromdate' and '$todate 23:59:59' ";
  $query_rma = $query_rma." INNER JOIN ITEMS ON RMADT.ITEM_CODE = ITEMS.ITEM_CODE ";



$query_rma = $query_rma." left join trackinghistory on trackinghistory.rma_id = rma.rma_id ";
$query_rma = $query_rma." and trackinghistory.trackingdate = (select max(trackingdate) from trackinghistory where trackinghistory.rma_id=rma.rma_id)";

if($warranty == "No Warranty")
{
$query_rma = $query_rma." where Rmadt_warranty = 1 ";
}
else
	
if ($warranty == "Warranty")
{
$query_rma = $query_rma."where Rmadt_warranty = 0 or Rmadt_warranty = '' ";
}

if ($orderby == "DATE")
{
$query_rma = $query_rma.' ORDER BY RMADT.RMA_DATE ';
}
else
$query_rma = $query_rma.' ORDER BY RMA_PromisedDate';


	//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>$RMA_DATE1</td>
<td style='padding-left:48px'>$RMA_REFERENCE</td>
<td style='padding-left:50px'>$Rma_Remark</td>
<td style='padding-left:10px'>$RMA_PromisedDate</td>
<td style='padding-left:70px'>$currency_code</td>
<td style='padding-left:10px; text-align: right'>$Rmacharge</td>
</tr>";
}

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