<?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:500px; font-size:20px; text-align: center'><b><u> $physicaldamage </u></b></td>";
//<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>Item</b></u></td>
<td style='padding-left:50px'><u><b>Item Description</b></u></td>
<td style='padding-left:100px'><u><b>RMA</b></u></td>
<td style='padding-left:70px'><u><b>Sold</b></u></td>
<td style='padding-left:70px'><u><b>Per</b></u></td>
</tr>
";

$query_rma = $query_rma."SELECT ITEMS.ITEM_CODE ";
   $query_rma = $query_rma.", ITEMS.ITEM_DESCRIPTION,sum(QTYRMA) as QTYRMA,".
      " QTYSLD,ROUND((sum(QTYRMA)*100/QTYSLD),2) AS PER ".
      " FROM ( "."select rmadt.rmadt_id,RMADT.item_code";
      $query_rma = $query_rma.",(RMADT_quantity) AS QTYRMA ".",sum(if(journal_type='DI', deliverydt_quantity, deliverydt_quantity * -1)) AS QTYSLD".
      " FROM RMADT"." INNER JOIN  DELIVERYDT ON DELIVERYDT.ITEM_CODE = RMADT.ITEM_CODE".
      " WHERE RMA_TYPE='MI'"." and rma_date between '$fromdate' and '$todate 23:59:59'";

      if($physicaldamage == "PHYSICAL DAMAGE")  //PHYSICAL DAMAGE
          $query_rma = $query_rma." and rmadt_physicalDamage=1";

      if ($physicaldamage == "DOA") //DOA
          $query_rma = $query_rma." and rmadt_doa=1";
		  
      if ($physicaldamage == "WARRANTY")  //WARRANTY
	  
           $query_rma = $query_rma." and rmadt_physicalDamage=0"." and rmadt_doa=0";
           $query_rma = $query_rma." GROUP BY rmadt.rmadt_id,RMADT.item_code";
           $query_rma = $query_rma." ) AS RMA".
           " INNER JOIN ITEMS ON ITEMS.ITEM_CODE=RMA.ITEM_CODE".
           " where 1=1";
	  
      if ($group != "")
	  	  //TEXT NEXT TO GROUP
        $query_rma = $query_rma." and  items.group_code='$group'";
        $query_rma = $query_rma.' group by ITEMS.ITEM_CODE ';
        $query_rma = $query_rma.' order by 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'>$ITEM_CODE</td>
<td style='padding-left:50px'>$ITEM_DESCRIPTION</td>
<td style='padding-left:110px; text-align:right '>$QTYRMA</td>
<td style='padding-left:50px; text-align:right '>$QTYSLD</td>
<td style='padding-left:50px; text-align: right'>$PER</td>
</tr>";
}

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