<?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'];
$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];
$receive_date = $receive_date." ".date("H:i:s");
$deliver_date = $_GET['deliver_date'];
$deliver_date = explode("/", $deliver_date);
$deliver_date = $deliver_date[2]."-".$deliver_date[1]."-".$deliver_date[0];
$deliver_date = $deliver_date." ".date("H:i:s");
$filter_by = $_GET['filter_by'];
$start = $_GET['start'];
$pagenumber = $_GET['pagenumber'];
$search_box = $_GET['search_box'];
$search_type = $_GET['search_type'];
$Engineer_Name = $_GET['Engineer_Name'];
$RMA_Id = $_GET['RMA_Id'];

if($actionmode == "receive"){
	$usercode = $_SESSION['useraccount'];
	//echo $receive_date;exit;
	$query_update = "update rmadt set rmadt_UserRecieved = '$usercode', 
	                                  rmadt_DateReceived = '$receive_date'
					 where rmadt_id = '$rmadt_id'";
	//echo $query_update;exit;				 
    $result_update = mysql_query($query_update, $connection);
	
	$query_select  = "select rmadt_UserRecieved, date_format(rmadt_DateReceived, '%d/%m/%Y') as rmadt_DateReceived from rmadt where rmadt_id = '$rmadt_id'";
	$result_select = mysql_query($query_select, $connection); 
	$row_select    = mysql_fetch_array($result_select);
	extract($row_select);
	drawRMAPendingItems($connection, $filter_by, $start, $pagenumber, $search_type, $search_box);
	echo "[BRK]";
	echo $rmadt_UserRecieved;
	echo "[BRK]";
	echo $rmadt_DateReceived;
	echo "[BRK]";
}
else
	if($actionmode == "clearreceive"){
		$query_update  = "update rmadt set rmadt_UserRecieved = null, rmadt_DateReceived = null where rmadt_id = '$rmadt_id'";
		$result_update = mysql_query($query_update, $connection);
		drawRMAPendingItems($connection, $filter_by, $start, $pagenumber, $search_type, $search_box);
		echo "[BRK]";
	}
	else
	if($actionmode == "cleardeliver"){
		$query_update  = "update rmadt set rmadt_UserDelivered = null, rmadt_DateDelivered = null where rmadt_id = '$rmadt_id'";
		$result_update = mysql_query($query_update, $connection);
		drawRMAPendingItems($connection, $filter_by, $start, $pagenumber, $search_type, $search_box);
		echo "[BRK]";
	}
else
	if($actionmode == "deliver"){
	$usercode = $_SESSION['useraccount'];
	//echo $receive_date;exit;
	$query_update = "update rmadt set rmadt_UserDelivered = '$usercode', 
	                                  rmadt_DateDelivered = '$deliver_date'
					 where rmadt_id = '$rmadt_id'";
	//echo $query_update;exit;				 
    $result_update = mysql_query($query_update, $connection);
	
	$query_select  = "select rmadt_UserDelivered, date_format(rmadt_DateDelivered, '%d/%m/%Y') as rmadt_DateDelivered from rmadt where rmadt_id = '$rmadt_id'";
	$result_select = mysql_query($query_select, $connection); 
	$row_select    = mysql_fetch_array($result_select);
	extract($row_select);
	drawRMAPendingItems($connection, $filter_by, $start, $pagenumber, $search_type, $search_box);
	echo "[BRK]";
	echo $rmadt_UserDelivered;
	echo "[BRK]";
	echo $rmadt_DateDelivered;
	echo "[BRK]";
}
else
	if($actionmode == "displaydates"){
		$query_select  = "select rmadt_UserRecieved, date_format(rmadt_DateReceived, '%d/%m/%Y') as rmadt_DateReceived, rmadt_UserDelivered, date_format(rmadt_DateDelivered, '%d/%m/%Y') as rmadt_DateDelivered from rmadt where rmadt_id = '$rmadt_id'";
	    $result_select = mysql_query($query_select, $connection); 
	    $row_select    = mysql_fetch_array($result_select);
		extract($row_select);
		
		$query_rma  = "select mi_id 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_rma2  = "select rma_id from rmadt where rmadt_id = '$mi_id'";
        $result_rma2 = mysql_query($query_rma2, $connection); 
        $row_rma2    = mysql_fetch_array($result_rma2);
        extract($row_rma2);	

		$query_rma3  = "select rma_engineername from rma where rma_id = '$rma_id'";
        $result_rma3 = mysql_query($query_rma3, $connection); 
        $row_rma3    = mysql_fetch_array($result_rma3);
        extract($row_rma3);	
		//echo $query_select;
		/*
		if($rmadt_UserRecieved == ""){
			$rmadt_UserRecieved = $_SESSION['useraccount'];
		}
		if($rmadt_DateReceived == ""){
			$rmadt_DateReceived = date("d/m/Y");
		}
		if($rmadt_UserDelivered == ""){
			$rmadt_UserDelivered = $_SESSION['useraccount'];
		}
		if($rmadt_DateDelivered == ""){
			$rmadt_DateDelivered = date("d/m/Y");
		}
		*/
		echo $rmadt_UserRecieved;
	    echo "[BRK]";
	    echo $rmadt_DateReceived;
	    echo "[BRK]";
		echo $rmadt_UserDelivered;
		echo "[BRK]";
		echo $rmadt_DateDelivered;
		echo "[BRK]";
		echo $rma_engineername;
		echo "[BRK]";
	}
	else
		if($actionmode == "gofilterby"){
			drawRMAPendingItems($connection, $filter_by, $start, $pagenumber);
			echo "[BRK]";
		}
		else
			if($actionmode == "movenext" || $actionmode == "moveprev"){
				drawRMAPendingItems($connection, $filter_by, $start, $pagenumber, $search_type, $search_box);
				echo "[BRK]";
			}
			else
				if($actionmode == "searchpendingitems"){
					drawRMAPendingItems($connection, $filter_by, $start, $pagenumber, $search_type, $search_box);
				    echo "[BRK]";
				}
				else
					if($actionmode == "assignengineer"){
						$query_select  = "select mi_id as mi_id from rmadt where rmadt_id = '$rmadt_id'";
						//echo $query_select; exit;
						$result_select = mysql_query($query_select, $connection); 
						$row_select    = mysql_fetch_array($result_select);
						extract($row_select);
						
						$query_select2  = "select rma_id as RmaId from rmadt where rmadt_id = '$mi_id'";
						$result_select2 = mysql_query($query_select2, $connection);
						$row_select2    = mysql_fetch_array($result_select2);
						extract($row_select2);
						
						$query_update  = "update rma set Rma_EngineerName = '$Engineer_Name' 
						                  where rma_id = '$RmaId'";
						//echo $query_update;exit;
						$result_update = mysql_query($query_update, $connection); 
						drawRMAPendingItems($connection, $filter_by, $start, $pagenumber, $search_type, $search_box);
				        echo "[BRK]";
					}
					else
						if($actionmode == "sendemail"){
							$query  = "select rma_email, rma_subject from rma where rma_id = '$RMA_Id'";
							$result = mysql_query($query, $connection);
                            $row    = mysql_fetch_array($result);	
                            extract($row);
                                    $to = $rma_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_subject;
                                    $mail->Subject = $subject;
                                    $mail->AddAddress($to," ");
									$mail->AddEmbeddedImage('SignatureRMADepartment.jpg', 'SignatureRMADepartment');
                                    //$mail->AddStringAttachment($html, "SignatureRMADepartment.jpg");
									$query = "select date_format(rma.rma_date, '%d/%m/%Y') as rma_date,clients.ledger_name,rma_reference,group_name,item_description," .
                                             "serial_number,rmadt_problem,rmadt_repaire,rmadt_solution,rmacharge_amount,rma_remark2,rmadt_notes," .
                                             "client_technicalemail,client_technicalRepresentative,rmadt_received,rmadt_price,rma.currency_code,RMADt_Remark,rmadt.item_code,items.item_description " .
                                             "from items,clients,rma,rmadt,grouping " .
                                             "where items.item_code=rmadt.item_code and " .
                                             "rma.rma_id=rmadt.rma_id and " .
                                             "grouping.group_code=items.group_code and ".
                                             "clients.ledger_number=rma.client_code and rma.rma_id=".$RMA_Id.
                                             " order by group_name,item_description";
									$result = mysql_query($query, $connection);
									$html = "Dear Sirs<br><br>";
									$html = $html."Please note your RMA status <br><br>";
									while($row = mysql_fetch_array($result)){
										extract($row);
										$html = $html."<table border='1'>";
										$html = $html."<tr><th align='left'>Date received the item</th><td>".$rma_date."</td></tr>";
										//$html = $html."<tr><th align='left'>Date received the item</th><td>'+FormatDateTime('dd/mm/yyyy',mysql.fieldbyname('rma_date').Asdatetime)+'</td></tr>'";
										$html = $html."<tr><th align='left'>Client</th><td>".$ledger_name."</td></tr>";
										$html = $html."<tr><th align='left'>Reference</th><td>".$rma_reference."</td></tr>";
										$html = $html."<tr><th align='left'>Brand</th><td>".$group_name."</td></tr>";
										$html = $html."<tr><th align='left'>Part Number</th><td>".$item_code."</td></tr>";
										$html = $html."<tr><th align='left'>Description</th><td>".$item_description."</td></tr>";
										$html = $html."<tr><th align='left'>Remark</th><td>".$RMADt_Remark."</td></tr>";
										$html = $html."<tr><th align='left'>Serial Number</th><td>".$serial_number."</td></tr>";
										$html = $html."<tr><th align='left'>Problem</th><td>".$rmadt_problem."</td></tr>";
										$html = $html."<tr><th align='left'>Received with </th><td>".$rmadt_received."</td></tr>";
										$html = $html."<tr><th align='left'>Charge ".$currency_code."</th><td>".$rmadt_price."</td></tr>";
										$html = $html."<tr><th align='left'>Notes</th><td>".$rmadt_notes."</td></tr>";
										$html = $html."</table><br><br><br>";
									}
									$html = $html."For futher assistance, please do not hesitate to contact us  <br><br>";
									$html = $html."Regards, <br><br>";
									$html = $html."<img src='cid:SignatureRMADepartment' />";
									//$html = $query;
                                    $mail->Body =  $html;
                                    //$mail->Body = iconv('windows-1256', 'utf-8', $html);
                                    //$mail->Body .= iconv('utf-8', 'windows-1256', $html);
                                    $mail->SMTPDebug = 0;
                                    $mail->Port =26;
                                    //$mail->Port =587;
                                    $mail->SMTPKeepAlive = true;  
                                    $mail->Mailer = "smtp"; 
                                    try {
	                                    $mail->Send();
	                                    $msg = 1;
	                                    //echo $msg;

                                    } catch (phpmailerException $e) {
	                                    $msg = 0;
	                                    //echo $msg;
                                    }
                                    echo $msg;
									echo "[BRK]";							
						}
?>