<?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'];
$InvoicingDt_Amount  = $_GET['InvoicingDt_Amount'];
$InvoicingDt_DueDate = $_GET['InvoicingDt_DueDate'];
$invoicing_id        = $_GET['invoicing_id'];
$invoicingdt_id      = $_GET['invoicingdt_id'];

$InvoicingDt_DueDate = explode("/", $InvoicingDt_DueDate);
$InvoicingDt_DueDate = $InvoicingDt_DueDate[2]."-".$InvoicingDt_DueDate[1]."-".$InvoicingDt_DueDate[0]." ".date("H:i:s");

$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 == "insertdt"){
	$query_chk  = "select (ifNull(sum(InvoicingDt_Amount), 0) + $InvoicingDt_Amount) as TotalAmount
	               from temp_invoicingdt
				   where invoicing_id = '$invoicing_id'";
	$result_chk = mysql_query($query_chk, $connection);
	$row_chk    = mysql_fetch_array($result_chk);
	extract($row_chk);
	
	$query_chk2  = "select Invoicing_Amount as HeaderAmount, Currency_Code
	                from temp_invoicing
				    where invoicing_id = '$invoicing_id'";
	$result_chk2 = mysql_query($query_chk2, $connection);
	$row_chk2    = mysql_fetch_array($result_chk2);
	extract($row_chk2);
	
	if($HeaderAmount < $TotalAmount){
		$response = 0;
	}
	else{
		$query_amountbase  = "select convertcurFunc('$Currency_Code', '$base1', '$InvoicingDt_DueDate', 0,0,'$base1', '$base2', '$InvoicingDt_Amount') as Invoicingdt_AmountBase1,
	                                 convertcurFunc('$Currency_Code', '$base2', '$InvoicingDt_DueDate', 0,0,'$base1', '$base2', '$InvoicingDt_Amount') as Invoicingdt_AmountBase2
					     from dual";
	//echo $query_amountbase;exit;
	$result_amountbase = mysql_query($query_amountbase, $connection);
	$row_amountbase    = mysql_fetch_array($result_amountbase);
    extract($row_amountbase);
	$query_sequence  = "select NextSequence('InvoicingDt_Id', '') as InvDtNewId from dual";
	$result_sequence = mysql_query($query_sequence, $connection);
	$row_sequence    = mysql_fetch_array($result_sequence);
	extract($row_sequence);
		$query_insert  = "insert into temp_invoicingdt(InvoicingDt_Id, Invoicing_Id, InvoicingDt_DueDate, InvoicingDt_Amount, InvoicingDt_AmountBase1, InvoicingDt_AmountBase2) 
		                 values ('$InvDtNewId', '$invoicing_id', '$InvoicingDt_DueDate', '$InvoicingDt_Amount', '$Invoicingdt_AmountBase1', '$Invoicingdt_AmountBase2')";
		$result_insert = mysql_query($query_insert, $connection);
		$response = 1;
	}
	echo $response;
	echo "[BRK]";
	drawInvoicingDt($connection, $invoicing_id);
	echo "[BRK]";
}
else
	if($actionmode == "deletedt"){
		$query_deletedt  = "delete from temp_invoicingdt where invoicingdt_id = '$invoicingdt_id'";
		$result_deletedt = mysql_query($query_deletedt, $connection); 
		drawInvoicingDt($connection, $invoicing_id);
	    echo "[BRK]";
	}
	else
		if($actionmode == "editdt"){
			$query_chk  = "select (ifNull(sum(InvoicingDt_Amount), 0) + $InvoicingDt_Amount) as TotalAmount
	               from temp_invoicingdt
				   where invoicing_id = '$invoicing_id' and invoicingdt_id not in ($invoicingdt_id)";
	        $result_chk = mysql_query($query_chk, $connection);
	        $row_chk    = mysql_fetch_array($result_chk);
	        extract($row_chk);
			
			$query_chk2  = "select Invoicing_Amount as HeaderAmount, Currency_Code
	                from temp_invoicing
				    where invoicing_id = '$invoicing_id'";
	        $result_chk2 = mysql_query($query_chk2, $connection);
	        $row_chk2    = mysql_fetch_array($result_chk2);
	        extract($row_chk2);
			if($HeaderAmount < $TotalAmount){
		        $response = 0;
	        }
	       else{
		         $query_amountbase  = "select convertcurFunc('$Currency_Code', '$base1', '$InvoicingDt_DueDate', 0,0,'$base1', '$base2', '$InvoicingDt_Amount') as Invoicingdt_AmountBase1,
	                                          convertcurFunc('$Currency_Code', '$base2', '$InvoicingDt_DueDate', 0,0,'$base1', '$base2', '$InvoicingDt_Amount') as Invoicingdt_AmountBase2
					                   from dual";
	             //echo $query_amountbase;exit;
	             $result_amountbase = mysql_query($query_amountbase, $connection);
	             $row_amountbase    = mysql_fetch_array($result_amountbase);
                 extract($row_amountbase);
		         $query_update  = "update temp_invoicingdt set InvoicingDt_DueDate = '$InvoicingDt_DueDate',
       				                                           InvoicingDt_Amount = '$InvoicingDt_Amount', 
															   InvoicingDt_AmountBase1 = '$Invoicingdt_AmountBase1', 
															   InvoicingDt_AmountBase2 = '$Invoicingdt_AmountBase2' 
		                           where invoicingdt_id = '$invoicingdt_id'";
		         $result_update = mysql_query($query_update, $connection);
		         $response = 1;
	        }
			echo $response;
			echo "[BRK]";
			//echo $query_update;
			drawInvoicingDt($connection, $invoicing_id);
			echo "[BRK]";
		}
		else
			if($actionmode == "chktotalamounts"){
				$query_invoicing  = "select Invoicing_Amount as InvoicingAmount
           				             from temp_invoicing 
									 where invoicing_id = '$invoicing_id'";
				$result_invoicing = mysql_query($query_invoicing, $connection);
				$row_invoicing    = mysql_fetch_array($result_invoicing);
				extract($row_invoicing);
				
				$query_invoicingdt  = "select sum(InvoicingDt_Amount) as TotalAmount
				                       from temp_invoicingdt
									   where invoicing_id = '$invoicing_id'";
				$result_invoicingdt = mysql_query($query_invoicingdt, $connection);
				$row_invoicingdt    = mysql_fetch_array($result_invoicingdt);
				extract($row_invoicingdt);
				
				if($InvoicingAmount != $TotalAmount){
					$response = 0;
				}
				else{
					$response = 1;
				}
				echo $response;
				echo "[BRK]";
			}
?>