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

$query_setup = "select base1, base2 from setup";
$result_setup = mysql_query($query_setup, $connection);
$row_setup = mysql_fetch_array($result_setup);
extract($row_setup);

$actionmode        = $_GET['actionmode'];
$item_code         = $_GET['item_code'];
$rma_id            = $_GET['rma_id'];
$rmadtrpc_quantity = $_GET['rmadtrpc_quantity'];
$warehouse_code    = $_GET['warehouse_code'];
$rmadt_id          = $_GET['rmadt_id'];
$serial_number     = $_GET['serial_number'];
$rmadtrpc_price    = $_GET['rmadtrpc_price'];
$rmadtrpc_id       = $_GET['rmadtrpc_id'];

if($rmadtrpc_quantity == ""){
	$rmadtrpc_quantity = 0;
}

$query_setup = "select base1, base2 from setup";
$result_setup = mysql_query($query_setup, $connection);
$row_setup = mysql_fetch_array($result_setup);
extract($row_setup);


if($actionmode == "searchitem"){
	$query_items = "select item_code, item_description, item_sellingprice1, item_sellingprice2, item_sellingprice3, item_sellingprice4, item_sellingprice5, item_sellingprice6, currency_code as OldCurrency
	                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($item_code == ""){
		$t = 0;
	}
	else{
		$t = 1;
	}
	$query_sellingprice = "select Client_SellingPrice, rma.currency_code as New_Currency, rma.RMA_Date
	                       from clients inner join temp_rma as rma on rma.client_code = clients.ledger_number
						   where rma.rma_id = '$rma_id'";
	$result_sellingprice = mysql_query($query_sellingprice, $connection);
	$row_sellingprice = mysql_fetch_array($result_sellingprice);
	extract($row_sellingprice);
	
	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;
	}
	$currconvert = "select convertcurFunc('$OldCurrency', '$New_Currency', '$RMA_Date', '0', '0', '$base1', '$base2', '$price') as price from dual";
	$resultconvert = mysql_query($currconvert, $connection);
	$rowconvert = mysql_fetch_array($resultconvert);
	extract($rowconvert);
	$netPrice = $price * $rmadtrpc_quantity;
	echo $t;
	echo "[BRK]";
	echo $item_code;
	echo "[BRK]";
	echo $item_description;
	echo "[BRK]";
	echo $price;
	echo "[BRK]";
	echo $netPrice;
	echo "[BRK]";
}
else
	if($actionmode == "searchwarehouse"){
		$query_warehouse  = "select warehouse_code as WHCode, warehouse_name as WHName
		                     from warehouse
							 where warehouse_code = '$warehouse_code'";
		$result_warehouse = mysql_query($query_warehouse, $connection);
		$row_warehouse    = mysql_fetch_array($result_warehouse);
		extract($row_warehouse);
		
		if($WHCode == ""){
			$t = 0;
		}
		else{
			$t = 1;
		}
		echo $t;
		echo "[BRK]";
		echo $WHCode;
		echo "[BRK]";
		echo $WHName;
		echo "[BRK]";
	}
else
	if($actionmode == "insertrmadt2"){
		$query_currency  = "select currency_code as fromCurrencyCode, rma_date as RMADate from temp_rma where rma_id = '$rma_id'";
		//echo $query_currency;exit;
		$result_currency = mysql_query($query_currency, $connection);
		$row_currency    = mysql_fetch_array($result_currency);
		extract($row_currency);
		$query_convert  = "select convertcurfunc('$fromCurrencyCode', '$base1', '$RMADate', '0', '0', '$base1', '$base2', '$rmadtrpc_price') as rmadtrpc_pricebase1,
		                   convertcurfunc('$fromCurrencyCode', '$base2', '$RMADate', '0', '0', '$base1', '$base2', '$rmadtrpc_price') as rmadtrpc_pricebase2
						   from dual";
						   //echo $query_convert;exit;
		$result_convert = mysql_query($query_convert, $connection);
		$row_convert    = mysql_fetch_array($result_convert);
		extract($row_convert);
		
		$query_insertdt  = "insert into temp_rmadtrpc (rmadt_id, rma_id, item_code, rmadtrpc_quantity, rmadtrpc_price, rmadtrpc_pricebase1, rmadtrpc_pricebase2, warehouse_code, serial_number) 
		                    values ('$rmadt_id', '$rma_id', '$item_code', '$rmadtrpc_quantity', '$rmadtrpc_price', '$rmadtrpc_pricebase1', '$rmadtrpc_pricebase2', '$warehouse_code', '$serial_number')";
		//echo $query_insertdt;exit;
		$result_insertdt = mysql_query($query_insertdt, $connection);
		
		drawRMADtRPC($connection, $rma_id, $rmadt_id);
		echo "[BRK]";
}
else
	if($actionmode == "deletermadt"){
		$query_delete  = "delete from temp_rmadtrpc where rmadtrpc_id = '$rmadtrpc_id'";
		$result_delete = mysql_query($query_delete, $connection);
		drawRMADtRPC($connection, $rma_id, $rmadt_id);
		echo "[BRK]";
}
else
	if($actionmode == "updatermadt2"){
		$query_currency  = "select currency_code as fromCurrencyCode, rma_date as RMADate from temp_rma where rma_id = '$rma_id'";
		$result_currency = mysql_query($query_currency, $connection);
		$row_currency    = mysql_fetch_array($result_currency);
		extract($row_currency);
		
		$query_convert  = "select convertcurfunc('$fromCurrencyCode', '$base1', '$RMADate', '0', '0', '$base1', '$base2', '$rmadtrpc_price') as rmadtrpc_pricebase1,
		                   convertcurfunc('$fromCurrencyCode', '$base2', '$RMADate', '0', '0', '$base1', '$base2', '$rmadtrpc_price') as rmadtrpc_pricebase2
						   from dual";
		$result_convert = mysql_query($query_convert, $connection);
		$row_convert    = mysql_fetch_array($result_convert);
		extract($row_convert);
		
		$query_update = "update temp_rmadtrpc set item_code = '$item_code',
		                                          rmadtrpc_quantity = '$rmadtrpc_quantity',
												  rmadtrpc_price = '$rmadtrpc_price',
												  rmadtrpc_pricebase1 = '$rmadtrpc_pricebase1',
												  rmadtrpc_pricebase2 = '$rmadtrpc_pricebase2',
												  warehouse_code = '$warehouse_code',
												  serial_number = '$serial_number'
						 where rmadtrpc_id = '$rmadtrpc_id'";
		$result_update = mysql_query($query_update, $connection);
		drawRMADtRPC($connection, $rma_id, $rmadt_id);
		echo "[BRK]";
	}
?>