<?php
session_start();
if($_SESSION['dbName']==""){
header("location:../index.php");
}
include("config.inc.php");
include("Functions_Trans.php");
//include("Forms_DataGrid.php");
include("Forms_RMA_DataGrid.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");

$actionmode = $_GET['actionmode'];
$rmadt_id = $_GET['rmadt_id'];
$receive_date = $_GET['receive_date'];

$receive_date = explode("/", $receive_date);
$receive_date = $receive_date[2]."-".$receive_date[1]."-".$receive_date[0]." ".date("H:i:s");

$filter_by = $_GET['filter_by'];
$rmadt_ReturnInvNo = $_GET['rmadt_ReturnInvNo'];
$rmadt_ReturnDate = $_GET['rmadt_ReturnDate'];
$rmadt_ReturnCur = $_GET['rmadt_ReturnCur'];
$rmadt_ReturnAmount = $_GET['rmadt_ReturnAmount'];
$rmadt_transferno = $_GET['rmadt_transferno'];

$rmadt_ReturnDate = explode("/", $rmadt_ReturnDate);
$rmadt_ReturnDate = $rmadt_ReturnDate[2]."-".$rmadt_ReturnDate[1]."-".$rmadt_ReturnDate[0]." ".date("H:i:s");

$rmadt_NewInvNo = $_GET['rmadt_NewInvNo'];
$rmadt_NewDate = $_GET['rmadt_NewDate'];
$rmadt_NewCur = $_GET['rmadt_NewCur'];
$rmadt_NewAmount = $_GET['rmadt_NewAmount'];
$item_coderep = $_GET['item_coderep'];
$serial_numberRep = $_GET['serial_numberRep'];

$rmadt_NewDate = explode("/", $rmadt_NewDate);
$rmadt_NewDate = $rmadt_NewDate[2]."-".$rmadt_NewDate[1]."-".$rmadt_NewDate[0]." ".date("H:i:s");

$start = $_GET['start'];
$pagenumber = $_GET['pagenumber'];

$rmadt_expectedDate = $_GET['rmadt_expectedDate'];
$rmadt_expectedDate = explode("/", $rmadt_expectedDate);
$rmadt_expectedDate = $rmadt_expectedDate[2]."-".$rmadt_expectedDate[1]."-".$rmadt_expectedDate[0]." ".date("H:i:s");

if($actionmode == "displaydates"){
	$query_date  = "select date_format(rmadt_RetRMADateRecieved, '%d/%m/%Y') as rmadt_RetRMADateRecieved, rmadt_RetRMAUserRecieved, rmadt_ReturnInvNo, date_format(rmadt_ReturnDate, '%d/%m/%Y') as rmadt_ReturnDate, rmadt_ReturnCur, rmadt_ReturnAmount, rmadt_transferno, rmadt_NewInvNo, date_format(rmadt_NewDate, '%d/%m/%Y') as rmadt_NewDate, rmadt_NewCur, rmadt_NewAmount, item_coderep, serial_numberRep, rmadt_RejectedRemark
	                from rmadt
				    where rmadt_id = '$rmadt_id'";
	$result_date = mysql_query($query_date, $connection);
	$row_date    = mysql_fetch_array($result_date);
	extract($row_date);
	if($rmadt_RetRMADateRecieved == ""){
		$rmadt_RetRMADateRecieved = date("d/m/Y");
	}
	echo $rmadt_RetRMAUserRecieved;
	echo "[BRK]";
	echo $rmadt_RetRMADateRecieved;
	echo "[BRK]";
	echo $rmadt_ReturnInvNo;
	echo "[BRK]";
	echo $rmadt_ReturnDate;
	echo "[BRK]";
	echo $rmadt_ReturnCur;
	echo "[BRK]";
	echo $rmadt_ReturnAmount;
	echo "[BRK]";
	echo $rmadt_transferno;
	echo "[BRK]";
	echo $rmadt_NewInvNo;
	echo "[BRK]";
	echo $rmadt_NewDate;
	echo "[BRK]";
	echo $rmadt_NewCur;
	echo "[BRK]";
	echo $rmadt_NewAmount;
	echo "[BRK]";
	echo $item_coderep;
	echo "[BRK]";
	echo $serial_numberRep;
	echo "[BRK]";
	echo $rmadt_RejectedRemark;
	echo "[BRK]";
}
else
	if($actionmode == "receive"){
		$query_receive  = "update rmadt set rmadt_RetRMADateRecieved = '$receive_date',
		                                    rmadt_RetRMAUserRecieved = '".$_SESSION['useraccount']."'
						   where rmadt_id = '$rmadt_id'";
		//echo $query_receive;
		$result_receive = mysql_query($query_receive, $connection);
		drawReturnRMAPendingItems($connection, $filter_by, $start, $pagenumber);
		echo "[BRK]";
		$query_date  = "select date_format(rmadt_RetRMADateRecieved, '%d/%m/%Y') as rmadt_RetRMADateRecieved, rmadt_RetRMAUserRecieved
	                from rmadt
				    where rmadt_id = '$rmadt_id'";
	    $result_date = mysql_query($query_date, $connection);
	    $row_date    = mysql_fetch_array($result_date);
	    extract($row_date);
		echo $rmadt_RetRMADateRecieved;
        echo "[BRK]";
        echo $rmadt_RetRMAUserRecieved;
        echo "[BRK]";		
	}
	else
		if($actionmode == "gofilterby"){
			drawReturnRMAPendingItems($connection, $filter_by);
		    echo "[BRK]";
		}
		else
			if($actionmode == "savecreditnote"){
				$query_update = "update rmadt set rmadt_ReturnInvNo = '$rmadt_ReturnInvNo',
				                                  rmadt_ReturnDate = '$rmadt_ReturnDate',
												  rmadt_ReturnCur = '$rmadt_ReturnCur',
												  rmadt_ReturnAmount = '$rmadt_ReturnAmount',
												  rmadt_transferno = '$rmadt_transferno',
												  rmadt_status = 'U',
												  RMADt_Repaire = 'C'
								where rmadt_id = '$rmadt_id'";
				$result_update = mysql_query($query_update, $connection);
				drawReturnRMAPendingItems($connection, $filter_by, $start, $pagenumber);
		        echo "[BRK]";
			}
			else
				if($actionmode == "savereplacement"){
					$query_update = "update rmadt set rmadt_NewInvNo = '$rmadt_NewInvNo',
					                                  rmadt_NewDate = '$rmadt_NewDate',
													  rmadt_NewCur = '$rmadt_NewCur',
													  rmadt_NewAmount = '$rmadt_NewAmount',
													  item_coderep = '$item_coderep',
													  serial_numberRep = '$serial_numberRep',
													  rmadt_status = 'R',
													  RMADt_Repaire = 'P'
									 where rmadt_id = '$rmadt_id'";
					//echo $query_update;exit;
					$result_update = mysql_query($query_update, $connection);
					drawReturnRMAPendingItems($connection, $filter_by, $start, $pagenumber);
		            echo "[BRK]";
				}
				else
					if($actionmode == "saverejected"){
						$query_update = "update rmadt set rmadt_RejectedRemark = '$rmadt_RejectedRemark', rmadt_status = 'X',
													  RMADt_Repaire = 'X'
						                 where rmadt_id = '$rmadt_id'";
						$result_update = mysql_query($query_update, $connection);
						drawReturnRMAPendingItems($connection, $filter_by, $start, $pagenumber);
		                echo "[BRK]";
					}
					else
			if($actionmode == "movenext" || $actionmode == "moveprev"){
				drawReturnRMAPendingItems($connection, $filter_by, $start, $pagenumber);
				echo "[BRK]";
			}
			else
				if($actionmode == "expecteddate"){
					$query_update = "update rmadt set rmadt_expectedDate = '$rmadt_expectedDate'
			                         where rmadt_id = '$rmadt_id'";
					$result_update = mysql_query($query_update, $connection);
					drawReturnRMAPendingItems($connection, $filter_by, $start, $pagenumber);
				    echo "[BRK]";
				}
				else
					if($actionmode == "clearstatus"){
						$query_update = "update rmadt set rmadt_status = null, 
						                                  RMADt_Repaire= null
													where rmadt_id = '$rmadt_id'";
						$result_update = mysql_query($query_update, $connection);
					    drawReturnRMAPendingItems($connection, $filter_by, $start, $pagenumber);
				        echo "[BRK]";
					}
?>