<?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'],true,65536) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

$actionmode = $_GET['actionmode'];
$rma_id2    = $_GET['rma_id2'];
$list_dt    = $_GET['list_dt'];
//echo $list_dt; 
$list_dt = substr($list_dt, 0, -1);
$start = $_GET['start'];
$pagenumber = $_GET['pagenumber'];
$reference = $_GET['reference'];
$date = $_GET['date'];
$client_code = $_GET['client_code'];
//echo $list_dt; 
if($actionmode == "showrmadt"){
	drawRMADtSearch2($connection, $rma_id);
	echo "[BRK]";
}
else
	if($actionmode == "loadrma"){
		$query_sequence  = "select RMA_Id as seqRMAId from sequence";
	$result_sequence = mysql_query($query_sequence, $connection);
	$row_sequence    = mysql_fetch_array($result_sequence);
	extract($row_sequence);
	//echo $query_sequence;exit;
		$query_selectref  = "select RMA_Reference as RMARef, date_format(RMA_Date, '%d/%m/%Y %h:%i:%s %p') as RMA_Date from rma where rma_id = '$rma_id2'";
		$result_selectref = mysql_query($query_selectref, $connection);
		$row_selectref    = mysql_fetch_array($result_selectref);
		extract($row_selectref);
		/*
		$query_sequence  = "select ReturnRma_reference from sequence";
					$result_sequence = mysql_query($query_sequence, $connection); 
					$row_sequence    = mysql_fetch_array($result_sequence);
					extract($row_sequence);
					*/
		$query_start  = "start transaction";
	$result_start = mysql_query($query_start, $connection);
	
	$query_sequence  = "call nextSequence1('RMA_Id', null, @id)";
	$result_sequence = mysql_query($query_sequence, $connection);
    
	$query_selectid  = "select @id as seqRMAId from dual";
	$result_selectid = mysql_query($query_selectid, $connection); 
	$row_selectid    = mysql_fetch_array($result_selectid);
	extract($row_selectid);
	
	$query_end  = "end transaction";
	$result_end = mysql_query($query_end, $connection);
	
		$query_insert = "insert into temp_rma (RMA_Id, RMA_Date, Rma_RMAREF, RMA_PROMISEDDATE, Client_Code, Currency_code,
	                     Rmacharge_amount, Rmacharge_amountbase1, Rmacharge_amountbase2, Rma_Remark, RMA_subject, Rma_EngineerName, User_Code, rma_type, rma_remark2, Client_Tel, Client_Cell, rma_email, client_email, RMA_TotalQuantityUsed, RMA_TotalQuantity)
		                 select '$seqRMAId', now(), Rma_RMAREF, RMA_PROMISEDDATE, Client_Code, Currency_code,
	                     Rmacharge_amount, Rmacharge_amountbase1, Rmacharge_amountbase2, Rma_Remark, RMA_subject, Rma_EngineerName, '".$_SESSION['useraccount']."', 'MR', rma_remark2, Client_Tel, Client_Cell, rma_email, client_email, RMA_TotalQuantityUsed, RMA_TotalQuantity
						 from rma
						 where rma_id = '$rma_id2'";
						 //echo $query_insert;exit;
		$result_insert = mysql_query($query_insert, $connection);
		$rma_id = $seqRMAId;
		
		//$query_updatesequence  = "update sequence set rma_id = rma_id +1";
	    //$result_updatesequence = mysql_query($query_updatesequence, $connection); 
		
		$list_dt = explode(",", $list_dt);
		foreach($list_dt as $key => $val){
		$query_start  = "start transaction";
	$result_start = mysql_query($query_start, $connection);
	
	$query_sequence  = "call nextSequence1('RMADt_Id', null, @id)";
	$result_sequence = mysql_query($query_sequence, $connection);
    
	$query_selectid  = "select @id as seqRMADt from dual";
	$result_selectid = mysql_query($query_selectid, $connection); 
	$row_selectid    = mysql_fetch_array($result_selectid);
	extract($row_selectid);
	
	$query_end  = "end transaction";
	$result_end = mysql_query($query_end, $connection);
		$query_warehouse  = "select RMA_IN as WarehouseCodeUser, RMA_Out as WarehouseCodeUserOut from ".$_SESSION['dbLabel'].".login where user_account = '".$_SESSION['useraccount']."'";
	$result_warehouse = mysql_query($query_warehouse, $connection); 
	$row_warehouse    = mysql_fetch_array($result_warehouse);
	extract($row_warehouse);
		$query_insertdt = "insert into temp_rmadt (RMADt_id, MI_ID, RMA_Id, Item_code, RMADt_Remark, rmadt_price, rmadt_price2,
	                      RMADt_Remark2, rmadt_notes, rmadt_received, rmadt_problem, rmadt_solution, rmadt_physicalDamage,
					      rmadt_DOA, rmadt_informedon, rmadt_repliedon, rmadt_backfromsrvon, rmadt_servicecenter, RMADT_PROBLEMCATEGORY, RMADt_Quantity, Outside_Item, Serial_Number, Invoice_Date, Invoice_Number, Rmadt_Warranty, rma_type, client_code, rmadt_return, warehouse_code, RMADT_SRETURNWAREHOUSE, rmadt_techcheckupdate, rmadt_techcheckupdatedate, rmadt_techquotation, ramdt_techquotationdate, rmadt_approved, rmadt_approveddate, rmadt_repStartDateTime, rmadt_repEndDateTime, rmadt_operatorSpareParts, rmadt_qcremark, rmadt_qcduration, rmadt_qcapproved, rmadt_qcapproveddate, rmadt_operatoraditionalcomments, rmadt_qcStartDateTime, rmadt_qcEndDateTime, rmadt_repStartDateTime2, rmadt_repEndDateTime2, rmadt_qcStartDateTime2, rmadt_qcduration2, rmadt_qcEndDateTime2, rmadt_techquotation2, rmadt_techquotationdate2, rmadt_DateReceived, rmadt_receivedby)
						  select '$seqRMADt', RMAdt_Id, '$rma_id', Item_code, RMADt_Remark, rmadt_price, rmadt_price2,
	                      'RMA No. $rma_id2 $RMA_Date', rmadt_notes, rmadt_received, rmadt_problem, rmadt_solution, rmadt_physicalDamage,
					      rmadt_DOA, rmadt_informedon, rmadt_repliedon, rmadt_backfromsrvon, rmadt_servicecenter, RMADT_PROBLEMCATEGORY, RMADt_Quantity, Outside_Item, Serial_Number, Invoice_Date, Invoice_Number, Rmadt_Warranty, 'MR', client_code, rmadt_return, '$WarehouseCodeUser', '$WarehouseCodeUserOut', rmadt_techcheckupdate, rmadt_techcheckupdatedate, rmadt_techquotation, ramdt_techquotationdate, rmadt_approved, rmadt_approveddate, rmadt_repStartDateTime, rmadt_repEndDateTime, rmadt_operatorSpareParts, rmadt_qcremark, rmadt_qcduration, rmadt_qcapproved, rmadt_qcapproveddate, rmadt_operatoraditionalcomments, rmadt_qcStartDateTime, rmadt_qcEndDateTime, rmadt_repStartDateTime2, rmadt_repEndDateTime2, rmadt_qcStartDateTime2, rmadt_qcduration2, rmadt_qcEndDateTime2, rmadt_techquotation2, rmadt_techquotationdate2, rmadt_DateReceived, rmadt_receivedby
						  from rmadt
						  where rma_id = '$rma_id2' and rmadt_id = '$val'";
				//echo $query_insertdt;exit;		  
		 $result_insertdt = mysql_query($query_insertdt, $connection);
		 
		 //$query_updatesequence  = "update sequence set rmadt_id = rmadt_id +1";
	    // $result_updatesequence = mysql_query($query_updatesequence, $connection); 
		}
		 $query_select  = "select RMA_Id, RMA_Reference, date_format(RMA_Date, '%d/%m/%Y') as RMA_Date, Rma_RMAREF, date_format(RMA_PROMISEDDATE, '%d/%m/%Y') as RMA_PROMISEDDATE, temp_rma.Client_Code, clients.ledger_name as ClientName, temp_rma.Currency_code,
	                    Rmacharge_amount, Rmacharge_amountbase1, Rmacharge_amountbase2, temp_rma.client_Email as Client_Email, temp_rma.client_tel as RMA_ClientTelephone,
					    Rma_Remark, RMA_subject, Rma_EngineerName, rma_type, temp_rma.rma_remark2, temp_rma.client_tel as RMA_ClientTelephone, temp_rma.client_cell as RMA_ClientMobile, temp_rma.rma_email, problemcategory.PROBLEMCATEGORY_NAME as EngName
					    from temp_rma left join clients on clients.ledger_number = temp_rma.client_code
						left join problemcategory on problemcategory.user_code = temp_rma.Rma_EngineerName
					    where rma_id = '$rma_id'";
	      //echo $query_select;exit;
	      $result_select = mysql_query($query_select, $connection);
	      $row_select    = mysql_fetch_array($result_select);
		  extract($row_select);
		  $query_sum = "select rmadt_price as rmadtp1, rmadt_price2  as rmadtp2 from temp_rmadt where rma_id = '$rma_id'";
		  $result_sum = mysql_query($query_sum, $connection);
		  //echo $query_sum;
		  $RMA_TotalCharge = 0;
		  while($row_sum = mysql_fetch_array($result_sum)){
			  	extract($row_sum);
				$RMA_TotalCharge = $RMA_TotalCharge + $rmadtp1;
				$RMA_TotalCharge = $RMA_TotalCharge + $rmadtp2;
		  }
		  
		  
		  echo $RMA_Id;
	      echo "[BRK]";
	      echo $RMA_Reference;
	      echo "[BRK]";
	      echo $RMA_Date;
	      echo "[BRK]";
	      echo $Rma_RMAREF;
	      echo "[BRK]";
	      echo $RMA_PROMISEDDATE;
	      echo "[BRK]";
	      echo $ClientName;
	      echo "[BRK]";
	      echo $Currency_code;
	      echo "[BRK]";
	      echo number_format($Rmacharge_amount, 2);
	      echo "[BRK]";
	      echo $rma_email;
	      echo "[BRK]";
	      echo $RMA_ClientTelephone;
	      echo "[BRK]";
	      echo $RMA_ClientMobile;
	      echo "[BRK]";
	      echo $Rma_Remark;
	      echo "[BRK]";
	      echo $RMA_subject;
	      echo "[BRK]";
	      echo $Rma_EngineerName;
	      echo "[BRK]";
	      drawReturnRMADt($connection, $rma_id);
	      echo "[BRK]";
	      echo $Client_Code;
	      echo "[BRK]";
		  echo $rma_remark2;
		  echo "[BRK]";
		  echo $EngName;
		  echo "[BRK]";
		  //echo $query_sum;
		  echo $RMA_TotalCharge;
	}
	else
		if($actionmode == "fillarray"){
			insertInArrayDetails($connection, $rma_id2);
			echo "[BRK]";
		}
		else
			if($actionmode == "moveprev"){
				drawListRMAToLoad($connection, $reference, $date, $client_code, $start, $pagenumber);
				echo "[BRK]";
			}
			else
				if($actionmode == "movenext"){
					drawListRMAToLoad($connection, $reference, $date, $client_code, $start, $pagenumber);
				    echo "[BRK]";
				}
				else
					if($actionmode == "searchrma"){
						drawListRMAToLoad($connection, $reference, $date, $client_code, $start, $pagenumber);
				        echo "[BRK]";
					}
?>