<?php
session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}


include("config.inc.php");
include("Functions_Trans.php");
//include("Forms_DataGrid.php");
include("Forms_Invoicing_DataGrid.php");
include("function.inc.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'];
$headerinvoicing_id = $_GET['headerinvoicing_id'];
$search_id = $_GET['search_id'];
$search_date = $_GET['search_date'];

if($actionmode == "delete"){
	$query_invoicing  = "select Journal_Id from invoicing where headerinvoicing_id = '$headerinvoicing_id'";
	$result_invoicing = mysql_query($query_invoicing, $connection);
	while($row_invoicing = mysql_fetch_array($result_invoicing)){
		extract($row_invoicing);
		$query_deletejournal  = "delete from journal where journal_id = '$Journal_Id'";
		$result_deletejournal = mysql_query($query_deletejournal, $connection);
	}
	$query_delete  = "delete from headerinvoicing where headerinvoicing_id = '$headerinvoicing_id'";
	$result_delete = mysql_query($query_delete, $connection);
	
	drawListInvoicing($connection);
	echo "[BRK]";
}
else
	if($actionmode == "deletetemp"){
	$query_delete  = "delete from temp_headerinvoicing where headerinvoicing_id = '$headerinvoicing_id'";
	$result_delete = mysql_query($query_delete, $connection);
	
	$query_deleteinvoicing  = "delete from temp_invoicing where headerinvoicing_id = '$headerinvoicing_id'";
	$result_deleteinvoicing = mysql_query($query_deleteinvoicing, $connection);
	
	drawListTempInvoicing($connection);
	echo "[BRK]";
}
else
	if($actionmode == "displaydetails"){
		drawListInvoicingSearch($connection, $headerinvoicing_id);
		echo "[BRK]";
	}
	else
	if($actionmode == "displaytempdetails"){
		drawListTempInvoicingSearch($connection, $headerinvoicing_id);
		echo "[BRK]";
	}
	else
		if($actionmode == "searchinvociing"){
			drawListInvoicing($connection, $search_id, $search_date);
		}
		else
		if($actionmode == "searchtempinvociing"){
			drawListTempInvoicing($connection, $search_id, $search_date);
		}
		else
			if($actionmode == "editinvoicingheader"){
				$query_insertmainheader  = "insert into temp_headerinvoicing (headerinvoicing_id, headerinvoicing_date)
				                            select headerinvoicing_id, headerinvoicing_date
										    from headerinvoicing
										    where headerinvoicing_id = '$headerinvoicing_id'";
											//echo $query_insertmainheader;exit;
				$result_insertmainheader = mysql_query($query_insertmainheader, $connection);
				
				$query_selectinvoicing  = "select invoicing_id as InvoicingId
				                           from invoicing
										   where headerinvoicing_id = '$headerinvoicing_id'";
				$result_selectinvoicing = mysql_query($query_selectinvoicing, $connection);
				while($row_selectinvoicing = mysql_fetch_array($result_selectinvoicing)){
					extract($row_selectinvoicing);
					$query_insertinvoicing  = "insert into temp_invoicing (Invoicing_Id, Invoicing_Reference, Invoicing_Date, Client_Code, Currency_Code, Invoicing_Amount, Invoicing_AmountBase1, Invoicing_AmountBase2, User_Code, headerinvoicing_id, Invoicing_ValueDate, Journal_Id)
					                           select Invoicing_Id, Invoicing_Reference, Invoicing_Date, Client_Code, Currency_Code, Invoicing_Amount, Invoicing_AmountBase1, Invoicing_AmountBase2, User_Code, headerinvoicing_id, Invoicing_ValueDate, Journal_Id
											   from invoicing
											   where invoicing_id = '$InvoicingId'";
					$result_insertinvoicing = mysql_query($query_insertinvoicing, $connection);
					
					$query_insertinvoicingdt  = "insert into temp_invoicingdt (InvoicingDt_Id, Invoicing_Id, InvoicingDt_DueDate, InvoicingDt_Amount, InvoicingDt_AmountBase1, InvoicingDt_AmountBase2)
					                             select InvoicingDt_Id, Invoicing_Id, InvoicingDt_DueDate, InvoicingDt_Amount, InvoicingDt_AmountBase1, InvoicingDt_AmountBase2
												 from invoicingdt
												 where invoicing_id = '$InvoicingId'";
					$result_insertinvoicingdt = mysql_query($query_insertinvoicingdt, $connection);
				}
				
				$query_select  = "select headerinvoicing_id, date_format(headerinvoicing_date, '%d/%m/%Y') as headerinvoicing_date
								  from temp_headerinvoicing
							      where headerinvoicing_id = '$headerinvoicing_id'";
				$result_select = mysql_query($query_select, $connection);
				$row_select    = mysql_fetch_array($result_select);
				extract($row_select);
				echo trim($headerinvoicing_id);
				echo "[BRK]";
				echo $headerinvoicing_date;
				echo "[BRK]";
				drawTempInvoicingGrid($connection, $headerinvoicing_date, $headerinvoicing_id);
				echo "[BRK]";
			}
			else
			if($actionmode == "edittempinvoicingheader"){
				
				$query_select  = "select headerinvoicing_id, date_format(headerinvoicing_date, '%d/%m/%Y') as headerinvoicing_date
								  from temp_headerinvoicing
							      where headerinvoicing_id = '$headerinvoicing_id'";
				$result_select = mysql_query($query_select, $connection);
				$row_select    = mysql_fetch_array($result_select);
				extract($row_select);
				echo trim($headerinvoicing_id);
				echo "[BRK]";
				echo $headerinvoicing_date;
				echo "[BRK]";
				drawTempInvoicingGrid($connection, $headerinvoicing_date, $headerinvoicing_id);
				echo "[BRK]";
			}
?>