<?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");

require('PHPMailer-master/class.phpmailer.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'];
$search_type = $_GET['search_type'];
$search_reception = $_GET['search_reception'];
$start = $_GET['start'];
$pagenumber = $_GET['pagenumber'];
$reception_date = $_GET['reception_date'];
$reception_date = explode("/", $reception_date);
$reception_date = $reception_date[2]."-".$reception_date[1]."-".$reception_date[0]." ".date("H:i:s");
$rmadt_id = $_GET['rmadt_id'];
$textengineer2 = $_GET['textengineer2'];
$Service_Center = $_GET['Service_Center'];

if($actionmode == "searchrmareception"){
	//echo "test";
	drawReturnRMAReception($connection, $search_type, $search_reception);
	echo "[BRK]";
}
else
	if($actionmode == "moveprev" || $actionmode == "movenext"){
		drawReturnRMAReception($connection, $search_type, $search_reception, $start, $pagenumber);
		echo "[BRK]";
}
else
	if($actionmode == "receptionrmadt"){
		$query_update = "update rmadt set rmadt_datereceived = '$reception_date',
                                          rmadt_receivedby = '".$_SESSION['useraccount']."'		
		                 where rmadt_id = '$rmadt_id'";
		$result_update = mysql_query($query_update, $connection);
		drawReturnRMAReception($connection, $search_type, $search_reception, $start, $pagenumber);
		echo "[BRK]";
	}
	else
		if($actionmode == "assignengineer"){
			$query_rma  = "select rma_id as RmaId from rmadt where rmadt_id = '$rmadt_id'";
			$result_rma = mysql_query($query_rma, $connection); 
			$row_rma    = mysql_fetch_array($result_rma);
			extract($row_rma);
			
			$query_update  = "UPDATE rma set Rma_EngineerName = '$textengineer2' where rma_id = '$RmaId'";
			$result_update = mysql_query($query_update, $connection); 

			$query_user_onhand = "UPDATE rmadt set user_onhand = '$textengineer2' where rmadt_id = '$rmadt_id'";
			$result_user_onhand = mysql_query($query_user_onhand, $connection);
			
			$query_selectemail  = "select user_email from problemcategory where user_code = '$textengineer2'";
			$result_selectemail = mysql_query($query_selectemail, $connection);
			$row_selectemail    = mysql_fetch_array($result_selectemail);
			extract($row_selectemail);
			
			$query_rmaref  = "select RMA_Reference as RMARef from rma where rma_id = '$RmaId'";
			$result_rmaref = mysql_query($query_rmaref, $connection); 
			$row_rmaref    = mysql_fetch_array($result_rmaref);
			extract($row_rmaref);
			$to = $user_email;
			$mail = new PHPMailer(true);
			$mail->IsSMTP();
            $mail->Host = $config['host_rma'];
            $mail->SMTPAuth = true;
			$mail->Username = $config['username_rma'];
            $mail->Password = $config['password_rma'];
		    $mail->CharSet = 'windows-1256';
            $mail->From = $config['fromemail_rma'];
            $mail->FromName = $config['fromname_rma'];
            $mail->WordWrap = 50;
            $mail->IsHTML(true);
            if ($cc != '')
	            $mail->addCC($cc);
            $subject = "RMA $RMARef to be repaired";
            $mail->Subject = $subject;
            $mail->AddAddress($to," ");
			$html = "Dear Sirs<br><br>Kindly check the current RMA Reference in order to be repaired: <br>RMA Ref: $RMARef<br><br>Best Regards";
			$mail->Body =  $html;
            $mail->SMTPDebug = 0;
            $mail->Port =26;
            $mail->SMTPKeepAlive = true;  
            $mail->Mailer = "smtp"; 
            try {
	            $mail->Send();
	            $msg = 1;
	                                    

            } catch (phpmailerException $e) {
	            $msg = 0;
	                                    
            }
            									
			drawReturnRMAReception($connection, $search_type, $search_reception, $start, $pagenumber);
		    echo "[BRK]";
		}
		else
			if($actionmode == "saveservicecenter"){
				$query_update = "update rmadt set rmadt_servicecenter = '$Service_Center'
				                              where rmadt_id = '$rmadt_id'";
				//echo $query_update;
				$result_update = mysql_query($query_update, $connection);
				drawReturnRMAReception($connection, $search_type, $search_reception, $start, $pagenumber);
		        echo "[BRK]";
			}
?>