<?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'];
$actionmode2 = $_GET['actionmode2'];
$RMA_Id = $_GET['RMA_Id'];
$RMA_Date = $_GET['RMA_Date'];
$RMA_No = $_GET['RMA_No'];
$RMA_PromissDate = $_GET['RMA_PromissDate'];
$client_code = $_GET['client_code'];
$client_name = $_GET['client_name'];
$Currency_code = $_GET['Currency_code'];
$Rmacharge_amount = $_GET['Rmacharge_amount'];
$Client_Email = $_GET['Client_Email'];
$RMA_ClientTelephone = $_GET['RMA_ClientTelephone'];
$RMA_ClientMobile = $_GET['RMA_ClientMobile'];
$Rma_Remark = $_GET['Rma_Remark'];
$txtemail = $_GET['txtemail'];
$txtsubject = $_GET['txtsubject'];
$textengineer = $_GET['textengineer'];
$textengineer2 = $_GET['textengineer2'];
$item_code = $_GET['item_code'];
$item_description = $_GET['item_description'];
$RMADt_Remark = urldecode($_GET['RMADt_Remark']);
$RMADt_Charge = $_GET['RMADt_Charge'];
$RMADt_Charge2 = $_GET['RMADt_Charge2'];
$RMADt_PickedUpBy = urldecode($_GET['RMADt_PickedUpBy']);
$rmadt_notes = urldecode($_GET['rmadt_notes']);
$rmadt_received = $_GET['rmadt_received'];
$rmadt_problem = urldecode($_GET['rmadt_problem']);
$rmadt_solution = urldecode($_GET['rmadt_solution']);
$rmadt_physicalDamage = $_GET['rmadt_physicalDamage'];
$rmadt_DOA = $_GET['rmadt_DOA'];
$rmadt_informedon = $_GET['rmadt_informedon'];
$rmadt_repliedon = $_GET['rmadt_repliedon'];
$rmadt_backfromsrvon = $_GET['rmadt_backfromsrvon'];
$rmadt_servicecenter = $_GET['rmadt_servicecenter'];
$RMADT_PROBLEMCATEGORY = $_GET['RMADT_PROBLEMCATEGORY'];
$RMADt_Quantity = $_GET['RMADt_Quantity'];
$Outside_Item = $_GET['Outside_Item'];
$RMADt_Id = $_GET['RMADt_Id'];
$Serial_Number = $_GET['Serial_Number'];
$Invoice_Date = $_GET['Invoice_Date'];
$Invoice_Number = $_GET['Invoice_Number'];
$Rmadt_Warranty = $_GET['Rmadt_Warranty'];
$RMADt_Repaire = $_GET['RMADt_Repaire'];
$NewSerial_Number = $_GET['NewSerial_Number'];
$RmaDt_Return = $_GET['RmaDt_Return'];
$Rma_Remark2 = $_GET['Rma_Remark2'];
$query_setup = "select base1, base2 from setup";
$result_setup = mysql_query($query_setup, $connection);
$row_setup = mysql_fetch_array($result_setup);
extract($row_setup);
//echo "charge 1= ".$RMADt_Charge."charge 2= ".$RMADt_Charge2;exit;
//echo "rmacharge_amount: ".$Rmacharge_amount." rmadt_charge: ".$RMADt_Charge." rmadt_charge2: ".$RMADt_Charge2;exit;
$RMA_Date = explode("/", $RMA_Date);
$RMA_Date = $RMA_Date[2]."-".$RMA_Date[1]."-".$RMA_Date[0];

$RMA_PromissDate = explode("/", $RMA_PromissDate);
$RMA_PromissDate = $RMA_PromissDate[2]."-".$RMA_PromissDate[1]."-".$RMA_PromissDate[0];

$query_amountBase1  = "select convertcurFunc('$Currency_code', '$base1', '$RMA_Date', '0', '0', '$base1', '$base2', '$Rmacharge_amount') as Rmacharge_amountbase1 from dual";
$result_amountBase1 = mysql_query($query_amountBase1, $connection); 
$row_amountBase1    = mysql_fetch_array($result_amountBase1);
extract($row_amountBase1);

$query_amountBase2  = "select convertcurFunc('$Currency_code', '$base2', '$RMA_Date', '0', '0', '$base1', '$base2', '$Rmacharge_amount') as Rmacharge_amountbase1 from dual";
$result_amountBase2 = mysql_query($query_amountBase2, $connection); 
$row_amountBase2    = mysql_fetch_array($result_amountBase2);
extract($row_amountBase2);
//echo $RMADt_Id;
if($actionmode == "insert"){
	
	$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_belongstogrp  = "select login_belongstogrp from ".$_SESSION['dbLabel'].".login where User_account = '".$_SESSION['useraccount']."'";
	$result_belongstpgrp = mysql_query($query_belongstogrp, $connection); 
	$row_belongstogrp    = mysql_fetch_array($result_belongstpgrp);
	extract($row_belongstogrp);
	
	$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_BelongsToGroup, Rma_Remark2, Client_Tel, Client_Cell, RMA_email) values ('$seqRMAId', '$RMA_Date',
					  '$RMA_No', '$RMA_PromissDate', '$client_code', '$Currency_code',
	                  '$Rmacharge_amount', '$Rmacharge_amountbase1', '$Rmacharge_amountbase2',
					  '$Rma_Remark', '$RMA_subject', '$Rma_EngineerName', '".$_SESSION['useraccount']."', 'MR', '$login_belongstogrp', '$Rma_Remark2', '$RMA_ClientTelephone', '$RMA_ClientMobile', '$txtemail')";
	//echo $query_insert;exit;
	$result_insert = mysql_query($query_insert, $connection);
	$id = $seqRMAId;
	
	$query_updatesequence  = "update sequence set rma_id = rma_id + 1";
	$result_updatesequence = mysql_query($query_updatesequence, $connection); 
}
else
	if($actionmode == "setedit"){
		//echo "test";exit;
		$query_insert = "insert into temp_rma (RMA_Id, RMA_Reference, 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_TotalQuantity, RMA_TotalQuantityUsed, RMA_BelongsToGroup, Rma_Remark2, Client_Tel, Client_Cell, RMA_email)
						 select RMA_Id, RMA_Reference, 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_TotalQuantity, RMA_TotalQuantityUsed, RMA_BelongsToGroup, Rma_Remark2, Client_Tel, Client_Cell, RMA_email
						 from rma
						 where rma_id = '$RMA_Id'";
		//echo $query_insert;exit;
	   $result_insert = mysql_query($query_insert, $connection);
	   $query_warehouse  = "select RMA_IN as WarehouseCodeUser 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, 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, MI_ID, NewSerial_Number, RMADt_Repaire, client_code, rma_type, rmadt_return, warehouse_code)
						  select RMAdt_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, MI_ID, NewSerial_Number, RMADt_Repaire, client_code, rma_type, rmadt_return, ifNull(warehouse_code, '$WarehouseCodeUser')
						  from rmadt
						  where rma_id = '$RMA_Id'";
						  
						  //echo $query_insertdt;exit;
	  $result_insertdt = mysql_query($query_insertdt, $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,
					    temp_rma.Client_Cell as RMA_ClientMobile, Rma_Remark, RMA_email as RMA_email, RMA_subject, Rma_EngineerName, rma_type, RMA_BelongsToGroup, Rma_Remark2, 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);
	  
	  if($rma_type == "MR"){
		  $query_insertdt = "insert into temp_rmadtrpc (rma_id, rmadt_id, item_code, rmadtrpc_quantity, rmadtrpc_price, rmadtrpc_pricebase1, rmadtrpc_pricebase2, warehouse_code, serial_number)
		                     select rma_id, rmadt_id, item_code, rmadtrpc_quantity, rmadtrpc_price, rmadtrpc_pricebase1, rmadtrpc_pricebase2, warehouse_code, serial_number
							 from rmadtrpc
							 where rma_id = '$RMA_Id'";
							// echo $query_insertdt;exit;
		  $result_inserdt = mysql_query($query_insertdt, $connection);
	  }
	  
	  $query_selectrmadt  = "select mi_id as RmaDtId
	                         from rmadt
							 where rma_id = '$RMA_Id'";
	  $result_selectrmadt = mysql_query($query_selectrmadt, $connection);
	  $row_selectrmadt    = mysql_fetch_array($result_selectrmadt);
	  extract($row_selectrmadt);
	  
	  $query_selectrma  = "select rma_id as RmaId
	                       from rmadt
						   where rmadt_id = '$RmaDtId'";
	  $result_selectrma = mysql_query($query_selectrma, $connection);
	  $row_selectrma    = mysql_fetch_array($result_selectrma);
	  extract($row_selectrma);
	  
	  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 $Client_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 $RmaId;
	  echo "[BRK]";
	}
	else
		if($actionmode == "setedit2"){
		//echo "test";exit;
		
	  
	  $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, clients.client_Email as Client_Email, clients.Client_Telephone1 as RMA_ClientTelephone,
					    '' as RMA_ClientMobile, Rma_Remark, '' as RMA_email, RMA_subject, Rma_EngineerName
					    from temp_rma left join clients on clients.ledger_number = temp_rma.client_code
					    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);
	  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 $Client_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]";
	}
	else
		if($actionmode == "inserttotemp"){
			$query_insert = "insert into temp_rma (RMA_Id, RMA_Reference, 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_TotalQuantity, RMA_TotalQuantityUsed, RMA_BelongsToGroup, Rma_Remark2, Client_Tel, Client_Cell, RMA_email)
						 select RMA_Id, RMA_Reference, 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_TotalQuantity, RMA_TotalQuantityUsed, RMA_BelongsToGroup, Rma_Remark2, Client_Tel, Client_Cell, RMA_email
						 from rma
						 where rma_id = '$RMA_Id'";
		//echo $query_insert;exit;
	   $result_insert = mysql_query($query_insert, $connection);
	    $query_warehouse  = "select RMA_IN as WarehouseCodeUser 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, 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, MI_ID, NewSerial_Number, RMADt_Repaire, client_code, rma_type, rmadt_return, warehouse_code)
						  select RMAdt_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, MI_ID, NewSerial_Number, RMADt_Repaire, client_code, rma_type, rmadt_return, ifNull(warehouse_code, '$WarehouseCodeUser')
						  from rmadt
						  where rma_id = '$RMA_Id'";
						  
						  //echo $query_insertdt;exit;
	  $result_insertdt = mysql_query($query_insertdt, $connection);
	   $query_insertdt = "insert into temp_rmadtrpc (rma_id, rmadt_id, item_code, rmadtrpc_quantity, rmadtrpc_price, rmadtrpc_pricebase1, rmadtrpc_pricebase2, warehouse_code, serial_number)
		                     select rma_id, rmadt_id, item_code, rmadtrpc_quantity, rmadtrpc_price, rmadtrpc_pricebase1, rmadtrpc_pricebase2, warehouse_code, serial_number
							 from rmadtrpc
							 where rma_id = '$RMA_Id'";
							// echo $query_insertdt;exit;
		  $result_inserdt = mysql_query($query_insertdt, $connection);
		}
else
	if($actionmode == "searchitem"){
		$query_client = "select Client_SellingPrice from clients where ledger_number = '$client_code'";
		$result_client = mysql_query($query_client, $connection);
		$row_client = mysql_fetch_array($result_client);
		extract($row_client);
		$query_items  = "select item_code, item_description, Currency_Code as ItemCurrency, Item_SellingPrice1, Item_SellingPrice2, Item_SellingPrice3, Item_SellingPrice4, Item_SellingPrice5, Item_SellingPrice6 from items where item_code = '$item_code'";
		$result_items = mysql_query($query_items, $connection); 
		$row_items    = mysql_fetch_array($result_items);
		extract($row_items);
		if($Client_SellingPrice == "I"){
			$price = $Item_SellingPrice1;
		}
		else
			if($Client_SellingPrice == "II"){
			$price = $Item_SellingPrice2;
		}
		else
			if($Client_SellingPrice == "III"){
			$price = $Item_SellingPrice3;
		}
		else
			if($Client_SellingPrice == "IV"){
			$price = $Item_SellingPrice4;
		}
		else
			if($Client_SellingPrice == "V"){
			$price = $Item_SellingPrice5;
		}
		else
			if($Client_SellingPrice == "VI"){
			$price = $Item_SellingPrice6;
		}
		$query_changeprice  = "select convertcurFunc('$ItemCurrency', '$Currency_code', '$RMA_Date', '0', '0', '$base1', '$base2', '$price') as priceFinal from dual";
		$result_changeprice = mysql_query($query_changeprice, $connection); 
		$row_changeprice    = mysql_fetch_array($result_changeprice);
		extract($row_changeprice);
		if($RMADt_Quantity == ""){
			$RMADt_Quantity = 0;
		}
		$priceFinal = $priceFinal * $RMADt_Quantity;
		$number_items = mysql_num_rows($result_items);
		echo $number_items;
		echo "[BRK]";
		echo $item_code;
		echo "[BRK]";
		echo $item_description;
		echo "[BRK]";
		echo $priceFinal;
		echo "[BRK]";
	}
	else
		if($actionmode == "changecurrency"){
			$query_oldcurrency  = "select currency_code as OldCurrency, Rmacharge_amount from temp_rma where rma_id = '$RMA_Id'";
			$result_oldcurrency = mysql_query($query_oldcurrency, $connection); 
			$row_oldcurrency    = mysql_fetch_array($result_oldcurrency);
			extract($row_oldcurrency);
			
			$query_rmadt  = "select rmadt_id, item_code, rmadt_price, rmadt_price2 from temp_rmadt where rma_id = '$RMA_Id'";
			$result_rmadt = mysql_query($query_rmadt, $connection);
			while($row_rmadt = mysql_fetch_array($result_rmadt)){
				extract($row_rmadt);
				$query_update  = "update temp_rmadt set rmadt_price = convertcurFunc('$OldCurrency', '$Currency_code', '$RMA_Date', '0', '0', '$base1', '$base2', rmadt_price),
				                  rmadt_price2 = convertcurFunc('$OldCurrency', '$Currency_code', '$RMA_Date', '0', '0', '$base1', '$base2', rmadt_price2)
								  where rmadt_id = '$rmadt_id'";
				//echo $query_update."<br>";
				$result_update = mysql_query($query_update, $connection);
			}
			//exit;
			
			$query_update2  = "update temp_rma set Currency_code = '$Currency_code',  
			                   Rmacharge_amount = convertcurFunc('$OldCurrency', '$Currency_code', '$RMA_Date', '0', '0', '$base1', '$base2', Rmacharge_amount)
			                   where rma_id = '$RMA_Id'";
			$result_update2 = mysql_query($query_update2, $connection); 
			
			$query_select  = "select Rmacharge_amount from temp_rma where rma_id = '$RMA_Id'";
			$result_select = mysql_query($query_select, $connection); 
			$row_select    = mysql_fetch_array($result_select);
			extract($row_select);
			drawReturnRMADt($connection, $RMA_Id);
			echo "[BRK]";
			echo number_format($Rmacharge_amount, 2, '.', '');
			echo "[BRK]";
		}
	else
			if($actionmode == "cancelrma"){
				$query_delete     = "delete from temp_rma where RMA_Id = '$RMA_Id'";
				$query_deletedt   = "delete from temp_rmadt where RMA_Id = '$RMA_Id'";
				$query_deletedt2  = "delete from temp_rmadtrpc where RMA_Id = '$RMA_Id'";
				$result_delete    = mysql_query($query_delete, $connection);
				$result_deletedt  = mysql_query($query_deletedt, $connection);
				$result_deletedt2 = mysql_query($query_deletedt2, $connection);
				if($result_delete == true && $result_deletedt == true){
					$response = 1;
				}
				else{
					$response = 0;
				}
				echo $response;
				echo "[BRK]";
			}
			else
				if($actionmode == "saverma"){
					$query_sequence  = "select (ReturnRma_reference + 1) as ReturnRma_reference from sequence";
	            $result_sequence = mysql_query($query_sequence, $connection);
	            $row_sequence    = mysql_fetch_array($result_sequence);
	            extract($row_sequence);
				
					$query_chkref  = "select RMA_reference as chkref from temp_rma where rma_id = '$RMA_Id'";
				$result_chkref = mysql_query($query_chkref, $connection);
				$row_chkref    = mysql_fetch_array($result_chkref);
				extract($row_chkref);
				$query_belongstogrp  = "select login_belongstogrp from ".$_SESSION['dbLabel'].".login where User_account = '".$_SESSION['useraccount']."'";
	            $result_belongstpgrp = mysql_query($query_belongstogrp, $connection); 
	            $row_belongstogrp    = mysql_fetch_array($result_belongstpgrp);
	            extract($row_belongstogrp);
					$query_update = "update temp_rma set RMA_Date = '$RMA_Date', 
					                                     Rma_RMAREF = '$RMA_No', 
														 RMA_PROMISEDDATE = '$RMA_PromissDate', 
														 Client_Code = '$client_code', 
														 Currency_code = '$Currency_code',
	                                                     Rmacharge_amount = '$Rmacharge_amount', 
														 Rmacharge_amountbase1 = '$Rmacharge_amountbase1', 
														 Rmacharge_amountbase2 = '$Rmacharge_amountbase2', 
														 Rma_Remark = '$Rma_Remark', 
														 RMA_subject = '$txtsubject', 
														 Rma_EngineerName = '$textengineer2', 
														 rma_type = 'MR',
														 User_Code = '".$_SESSION['useraccount']."',
														 RMA_BelongsToGroup = '$login_belongstogrp',
														 Client_Tel = '$RMA_ClientTelephone',
														 Client_Cell = '$RMA_ClientMobile',
														 Rma_Remark2 = '$Rma_Remark2',
														 RMA_email = '$txtemail'";
					if($chkref == ""){
				    	$query_update = $query_update." , RMA_Reference = '$ReturnRma_reference'";
					}
					$query_update = $query_update." where rma_id = '$RMA_Id'";
					//echo $Rmacharge_amount." ".$Rmacharge_amountbase1." ".$Rmacharge_amountbase2;exit;
					//echo $query_update;exit;
					$result_update = mysql_query($query_update, $connection);
					
					$query_clientupdate  = "update temp_rmadt set client_code = '$client_code', rma_date = '$RMA_Date' where rma_id = '$RMA_Id'";
					$result_clientupdate = mysql_query($query_clientupdate, $connection); 
					$query_reference  = "select RMA_Reference from temp_rma where rma_id = '$RMA_Id'";
					$result_reference = mysql_query($query_reference, $connection); 
					$row_reference    = mysql_fetch_array($result_reference);
					extract($row_reference);
					
					$query_save  = "select CopyData_TempRMA('".$RMA_Id."', '".$_SESSION['useraccount']."', 'MR')";
					//echo $query_save;exit;
					$result_save = mysql_query($query_save, $connection); 
					if($result_save){
						$message = "Return RMA with reference $RMA_Reference saved successfully!";
						$t = 1;
						if($chkref == ""){
						$query_update_seq  = "update sequence set ReturnRma_reference = ReturnRma_reference+1";
						$result_update_seq = mysql_query($query_update_seq);
						}
					}
					else{
						$message = "Error in saving the current Return RMA!";
						$t=0;
					}
					addToDelivery($connection, $RMA_Id);
					//include("testFunction.php?RMA_Id="+$RMA_Id+"&connection="+$connection);
					addToAdjustment2($connection, $RMA_Id);
					echo $message;
					echo "[BRK]";
					echo $t;
					echo "[BRK]";
					echo $RMA_Id;
					echo "[BRK]";
				}
if($actionmode2 == "insertdt"){
	if($RMA_Id == ""){
		$RMA_Id = $id;
	}
	
	if($rmadt_informedon == ""){
		$rmadt_informedon = "null";
	}
	else{
		$rmadt_informedon = explode("/", $rmadt_informedon);
		$rmadt_informedon = $rmadt_informedon[2]."-".$rmadt_informedon[1]."-".$rmadt_informedon[0];
		$rmadt_informedon = "'".$rmadt_informedon."'";
	}
	if($rmadt_repliedon == ""){
		$rmadt_repliedon = "null";
	}
	else{
		$rmadt_repliedon = explode("/", $rmadt_repliedon);
		$rmadt_repliedon = $rmadt_repliedon[2]."-".$rmadt_repliedon[1]."-".$rmadt_repliedon[0];
		$rmadt_repliedon = "'".$rmadt_repliedon."'";
	}
	
	if($rmadt_backfromsrvon == ""){
		$rmadt_backfromsrvon = "null";
	}
	else{
		$rmadt_backfromsrvon = explode("/", $rmadt_backfromsrvon);
		$rmadt_backfromsrvon = $rmadt_backfromsrvon[2]."-".$rmadt_backfromsrvon[1]."-".$rmadt_backfromsrvon[0];
		$rmadt_backfromsrvon = "'".$rmadt_backfromsrvon."'";
	}
	
	if($Invoice_Date == ""){
		$Invoice_Date = "null";
	}
	else{
		$Invoice_Date = explode("/", $Invoice_Date);
		$Invoice_Date = $Invoice_Date[2]."-".$Invoice_Date[1]."-".$Invoice_Date[0];
		$Invoice_Date = "'".$Invoice_Date."'";
	}
	$query_reference  = "select RMA_Reference from temp_rma where RMA_Id = '$RMA_Id'";
	$result_reference = mysql_query($query_reference, $connection);
    $row_reference    = mysql_fetch_array($result_reference);
    extract($row_reference);

    $query_sequence	 = "select (rmadt_id + 1) as seqRMADt from sequence";
	$result_sequence = mysql_query($query_sequence, $connection);
	$row_sequence    = mysql_fetch_array($result_sequence);
	extract($row_sequence);
	$query_client  = "select client_code as ClientCode from temp_rma where rma_id = '$RMA_Id'";
	$result_client = mysql_query($query_client, $connection); 
	$row_client    = mysql_fetch_array($result_client);
	extract($row_client);
	$query_insertdt  = "insert into temp_rmadt (RMADt_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, client_code) 
					   values('$seqRMADt', '$RMA_Id', '$item_code', '$RMADt_Remark', '$RMADt_Charge', '$RMADt_Charge2',
	                   '$RMADt_PickedUpBy', '$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', '$ClientCode')";
	//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); 
	drawRMADt($connection, $RMA_Id);
	echo "[BRK]";
	echo $RMA_Id;
	echo "[BRK]";
	echo $RMA_Reference;
	echo "[BRK]";
}
else
	if($actionmode2 == "delete"){
		$query_delete  = "delete from temp_rmadt where rmadt_id = '$RMADt_Id'";
		$result_delete = mysql_query($query_delete, $connection); 
		drawReturnRMADt($connection, $RMA_Id);
	    echo "[BRK]";
	}
	else
		if($actionmode2 == "edit"){
			if($rmadt_informedon == ""){
			$rmadt_informedon = "null";
			}
			else{
				$rmadt_informedon = explode("/", $rmadt_informedon);
				$rmadt_informedon = $rmadt_informedon[2]."-".$rmadt_informedon[1]."-".$rmadt_informedon[0];
				$rmadt_informedon = "'".$rmadt_informedon."'";
			}

			if($rmadt_repliedon == ""){
				$rmadt_repliedon = "null";
			}
			else{
				$rmadt_repliedon = explode("/", $rmadt_repliedon);
				$rmadt_repliedon = $rmadt_repliedon[2]."-".$rmadt_repliedon[1]."-".$rmadt_repliedon[0];
				$rmadt_repliedon = "'".$rmadt_repliedon."'";
			}
			
			if($rmadt_backfromsrvon == ""){
				$rmadt_backfromsrvon = "null";
			}
			else{
				$rmadt_backfromsrvon = explode("/", $rmadt_backfromsrvon);
				$rmadt_backfromsrvon = $rmadt_backfromsrvon[2]."-".$rmadt_backfromsrvon[1]."-".$rmadt_backfromsrvon[0];
				$rmadt_backfromsrvon = "'".$rmadt_backfromsrvon."'";
			}

			if($Invoice_Date == ""){
				$Invoice_Date = "null";
			}
			else{
				$Invoice_Date = explode("/", $Invoice_Date);
				$Invoice_Date = $Invoice_Date[2]."-".$Invoice_Date[1]."-".$Invoice_Date[0];
				$Invoice_Date = "'".$Invoice_Date."'";
			}

			$query_warehouse  = "select RMA_IN as WarehouseCodeUser 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_updatedt = "update temp_rmadt set item_code = '$item_code',
														RMADt_Remark = '$RMADt_Remark',
														rmadt_price = '$RMADt_Charge',
														rmadt_price2 = '$RMADt_Charge2',
														RMADt_Remark2 = '$RMADt_PickedUpBy',
														rmadt_notes = '$rmadt_notes',
														rmadt_received = '$rmadt_received',
														rmadt_problem = '$rmadt_problem',
														rmadt_solution = '$rmadt_solution',
														rmadt_physicalDamage = '$rmadt_physicalDamage',
														rmadt_DOA = '$rmadt_DOA',
														rmadt_informedon = $rmadt_informedon,
														rmadt_repliedon = $rmadt_repliedon,
														rmadt_backfromsrvon = $rmadt_backfromsrvon,
														rmadt_servicecenter = '$rmadt_servicecenter',
														RMADT_PROBLEMCATEGORY = '$RMADT_PROBLEMCATEGORY',
														RMADt_Quantity = '$RMADt_Quantity',
														Outside_Item = '$Outside_Item',
														Serial_Number = '$Serial_Number',
														Invoice_Date = $Invoice_Date,
														Invoice_Number = '$Invoice_Number',
														Rmadt_Warranty = '$Rmadt_Warranty',
														RMADt_Repaire = '$RMADt_Repaire',
														NewSerial_Number = '$NewSerial_Number',
														Rmadt_Return = '$RmaDt_Return',
														warehouse_code = '$WarehouseCodeUser'
														where RMADt_Id = '$RMADt_Id'";
					//echo $query_updatedt;exit;
					$result_updatedt = mysql_query($query_updatedt, $connection);
					drawReturnRMADt($connection, $RMA_Id);
					echo "[BRK]";
		} 
		else
   if($actionmode == "searchclient"){
	   $query_client  = "select ledger_number as ClientCode, ledger_name as ClientName
	                     from clients
						 where ledger_number = '$client_code'";
	   $result_client = mysql_query($query_client, $connection);
	   $row_client    = mysql_fetch_array($result_client);
	   extract($row_client);
	   if($ClientCode == ""){
		   $t  = 0;
	   }
	   else{
		   $t = 1;
	   }
	   echo $t;
	   echo "[BRK]";
	   echo $ClientCode;
	   echo "[BRK]";
	   echo $ClientName;
	   echo "[BRK]";
   }
?>