<?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'];
$Client_Code = $_GET['Client_Code'];
$Currency_Code = $_GET['Currency_Code'];
$Invoicing_Amount = $_GET['Invoicing_Amount'];
$headerinvoicing_id = $_GET['headerinvoicing_id'];
$Invoicing_Main_Date = $_GET['Invoicing_Main_Date'];
$Invoicing_Reference = $_GET['Invoicing_Reference'];
$invoicing_id = $_GET['invoicing_id'];


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

if($headerinvoicing_id == "") $headerinvoicing_id = 'null';

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

$query_salessetup  = "select CreditAccount from salessetup";
$result_salessetup = mysql_query($query_salessetup, $connection);
$row_salessetup    = mysql_fetch_array($result_salessetup);
extract($row_salessetup);

if($actionmode == "insertinvoicing"){
	

	$query_client  = "select Client_EndofMonthPay, Client_PaymentTerms, Client_PaymentMode, Client_paymentTerms2 from clients where ledger_number = '$Client_Code'";
	$result_client = mysql_query($query_client, $connection); 
	$row_client    = mysql_fetch_array($result_client);
	extract($row_client);
	//echo $query_client;exit;
	if($Client_EndofMonthPay == 1){
		$query_selectlastday  = "select LAST_DAY('$Invoicing_Main_Date') as Invoicing_ValueDate
		                         from dual";
		//echo $query_selectlastday;exit;
		$result_selectlastday = mysql_query($query_selectlastday, $connection);
		$row_selectlastday    = mysql_fetch_array($result_selectlastday);
		extract($row_selectlastday);
		$Invoicing_ValueDate = $Invoicing_ValueDate." ".date("H:i:s");
		$query_amountbase  = "select convertcurFunc('$Currency_Code', '$base1', '$Invoicing_Main_Date', 0,0,'$base1', '$base2', '$Invoicing_Amount') as Invoicing_AmountBase1,
	                             convertcurFunc('$Currency_Code', '$base2', '$Invoicing_Main_Date', 0,0,'$base1', '$base2', '$Invoicing_Amount') as Invoicing_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('Invoicing_Id', '') as InvNewId from dual";
	$result_sequence = mysql_query($query_sequence, $connection);
	$row_sequence    = mysql_fetch_array($result_sequence);
	extract($row_sequence);
	
	$query_insert  = "insert into temp_invoicing (Invoicing_Id, Invoicing_Date, Client_Code, Currency_Code, Invoicing_Amount, Invoicing_AmountBase1, Invoicing_AmountBase2, User_Code, headerinvoicing_id, Invoicing_ValueDate, Invoicing_Reference) 
	                  values('$InvNewId','$Invoicing_Main_Date', '$Client_Code', '$Currency_Code', '$Invoicing_Amount', '$Invoicing_AmountBase1', '$Invoicing_AmountBase2', '".$_SESSION['useraccount']."', $headerinvoicing_id, '$Invoicing_ValueDate', '$Invoicing_Reference')";
	//echo $query_insert;exit;
	$result_insert = mysql_query($query_insert, $connection);
	$invoicing_id  = $InvNewId;
	
	$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_insertdt  = "insert into temp_invoicingdt (InvoicingDt_Id, Invoicing_Id, InvoicingDt_DueDate, InvoicingDt_Amount, InvoicingDt_AmountBase1, InvoicingDt_AmountBase2) 
	                    values ('$InvDtNewId', '$invoicing_id', '$Invoicing_ValueDate', '$Invoicing_Amount', '$Invoicing_AmountBase1', '$Invoicing_AmountBase2')";
    $result_insertdt = mysql_query($query_insertdt, $connection);
	}
	else
		if($Client_PaymentTerms > 0){
			$query_selectvaluedate  = "SELECT DATE_ADD('$Invoicing_Main_Date', INTERVAL $Client_PaymentTerms DAY) as Invoicing_ValueDate
			                          from dual";
			$result_selectvaluedate = mysql_query($query_selectvaluedate, $connection);
			$row_selectvaluedate    = mysql_fetch_array($result_selectvaluedate);
			extract($row_selectvaluedate);
			$query_amountbase  = "select convertcurFunc('$Currency_Code', '$base1', '$Invoicing_Main_Date', 0,0,'$base1', '$base2', '$Invoicing_Amount') as Invoicing_AmountBase1,
	                             convertcurFunc('$Currency_Code', '$base2', '$Invoicing_Main_Date', 0,0,'$base1', '$base2', '$Invoicing_Amount') as Invoicing_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('Invoicing_Id', '') as InvNewId from dual";
	$result_sequence = mysql_query($query_sequence, $connection);
	$row_sequence    = mysql_fetch_array($result_sequence);
	extract($row_sequence);
	
	$query_insert  = "insert into temp_invoicing (Invoicing_Id, Invoicing_Date, Client_Code, Currency_Code, Invoicing_Amount, Invoicing_AmountBase1, Invoicing_AmountBase2, User_Code, headerinvoicing_id, Invoicing_ValueDate, Invoicing_Reference) 
	                  values('$InvNewId', '$Invoicing_Main_Date', '$Client_Code', '$Currency_Code', '$Invoicing_Amount', '$Invoicing_AmountBase1', '$Invoicing_AmountBase2', '".$_SESSION['useraccount']."', $headerinvoicing_id, '$Invoicing_ValueDate', '$Invoicing_Reference')";
	//echo $query_insert;exit;
	$result_insert = mysql_query($query_insert, $connection);
	$invoicing_id  = $InvNewId;
	
	$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_insertdt  = "insert into temp_invoicingdt (InvoicingDt_Id, Invoicing_Id, InvoicingDt_DueDate, InvoicingDt_Amount, InvoicingDt_AmountBase1, InvoicingDt_AmountBase2) 
	                    values ('$InvDtNewId', '$invoicing_id', '$Invoicing_ValueDate', '$Invoicing_Amount', '$Invoicing_AmountBase1', '$Invoicing_AmountBase2')";
    $result_insertdt = mysql_query($query_insertdt, $connection);
		}
		else
			if($Client_PaymentMode != "" && $Client_paymentTerms2 > 0){
				$query_amountbase  = "select convertcurFunc('$Currency_Code', '$base1', '$Invoicing_Main_Date', 0,0,'$base1', '$base2', '$Invoicing_Amount') as Invoicing_AmountBase1,
	                             convertcurFunc('$Currency_Code', '$base2', '$Invoicing_Main_Date', 0,0,'$base1', '$base2', '$Invoicing_Amount') as Invoicing_AmountBase2
					     from dual";
	//echo $query_amountbase;exit;
	$result_amountbase = mysql_query($query_amountbase, $connection);
	$row_amountbase    = mysql_fetch_array($result_amountbase);
    extract($row_amountbase);
				$TotalAmount = $Invoicing_Amount;
				$query_selectlastamountid  = "select ifNull(max(invoicingdt_id), 0) as PrevInvoicDtId
from ( select invoicingdt_id
from temp_invoicingdt inner join temp_invoicing on temp_invoicing.invoicing_id = temp_invoicingdt.invoicing_id
where temp_invoicing.Client_Code = '$Client_Code' union all
select invoicingdt_id
from invoicingdt inner join invoicing on invoicing.invoicing_id = invoicingdt.invoicing_id
where invoicing.Client_Code = '$Client_Code') as Q";
				$result_selectlastamountid = mysql_query($query_selectlastamountid, $connection);
				$row_selectlastamountid    = mysql_fetch_array($result_selectlastamountid);
				extract($row_selectlastamountid);
				if($PrevInvoicDtId > 0){
					$query_selectlastamountdate  = "select InvoicingDt_DueDate as LastDueDate, InvoicingDt_Amount as LastAmount
from temp_invoicingdt
where invoicingdt_id = '$PrevInvoicDtId' union all
select InvoicingDt_DueDate as LastDueDate, InvoicingDt_Amount as LastAmount
from invoicingdt
where invoicingdt_id = '$PrevInvoicDtId'";
					$result_selectlastamountdate = mysql_query($query_selectlastamountdate, $connection);
					$row_selectlastamountdate    = mysql_fetch_array($result_selectlastamountdate);
					extract($row_selectlastamountdate);
					if($LastDueDate > $Invoicing_Main_Date && $LastAmount < $Client_paymentTerms2){
						$Invoicing_ValueDate = $LastDueDate;
					}
					else{
						$query_selectvaluedate  = "select DATE_ADD('$Invoicing_Main_Date', INTERVAL if(($Client_PaymentMode - DAYOFWEEK('$Invoicing_Main_Date')) < 0, ($Client_PaymentMode - DAYOFWEEK('$Invoicing_Main_Date')) + 7, ($Client_PaymentMode - DAYOFWEEK('$Invoicing_Main_Date'))) DAY)  as Invoicing_ValueDate 
						                           from dual";
						$result_selectvaluedate = mysql_query($query_selectvaluedate, $connection);
						$row_selelectvaluedate  = mysql_fetch_array($result_selectvaluedate);
						extract($row_selelectvaluedate);
					}
				}
				else{
					    $query_selectvaluedate  = "select DATE_ADD('$Invoicing_Main_Date', INTERVAL if(($Client_PaymentMode - DAYOFWEEK('$Invoicing_Main_Date')) < 0, ($Client_PaymentMode - DAYOFWEEK('$Invoicing_Main_Date')) + 7, ($Client_PaymentMode - DAYOFWEEK('$Invoicing_Main_Date'))) DAY)  as Invoicing_ValueDate 
						                           from dual";
						$result_selectvaluedate = mysql_query($query_selectvaluedate, $connection);
						$row_selelectvaluedate  = mysql_fetch_array($result_selectvaluedate);
						extract($row_selelectvaluedate);
				}
				$query_sequence  = "select NextSequence('Invoicing_Id', '') as InvNewId from dual";
	$result_sequence = mysql_query($query_sequence, $connection);
	$row_sequence    = mysql_fetch_array($result_sequence);
	extract($row_sequence);
				$query_insert  = "insert into temp_invoicing (Invoicing_Id, Invoicing_Date, Client_Code, Currency_Code, Invoicing_Amount, Invoicing_AmountBase1, Invoicing_AmountBase2, User_Code, headerinvoicing_id, Invoicing_ValueDate, Invoicing_Reference) 
	                  values('$InvNewId', '$Invoicing_Main_Date', '$Client_Code', '$Currency_Code', '$Invoicing_Amount', '$Invoicing_AmountBase1', '$Invoicing_AmountBase2', '".$_SESSION['useraccount']."', $headerinvoicing_id, '$Invoicing_ValueDate', '$Invoicing_Reference')";
	            //echo $query_insert;exit;
	            $result_insert = mysql_query($query_insert, $connection);
	            $invoicing_id  = $InvNewId;
				$LastDueDate2 = $Invoicing_ValueDate;
				if($LastDueDate > $Invoicing_Main_Date && $LastAmount < $Client_paymentTerms2){
					
					$diffAmount = $LastAmount;
					$query_amountbase  = "select convertcurFunc('$Currency_Code', '$base1', '$LastDueDate2', 0,0,'$base1', '$base2', '$diffAmount') as diffAmountBase1,
	                                      convertcurFunc('$Currency_Code', '$base2', '$LastDueDate2', 0,0,'$base1', '$base2', '$diffAmount') as diffAmountBase2
					                      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_insertdt  = "insert into temp_invoicingdt (InvoicingDt_Id, Invoicing_Id, InvoicingDt_DueDate, InvoicingDt_Amount, InvoicingDt_AmountBase1, InvoicingDt_AmountBase2) 
					                    values ('$InvDtNewId', '$invoicing_id', '$LastDueDate2', '$diffAmount', '$diffAmountBase1', '$diffAmountBase2')";
					$result_insertdt = mysql_query($query_insertdt, $connection);
					$TotalAmount = $TotalAmount - $diffAmount;
					
					$query_lastdate2  = "select DATE_ADD('$LastDueDate2', INTERVAL 7 DAY) as LastDueDate2 from dual";
					$result_lastdate2 = mysql_query($query_lastdate2, $connection);
                    $row_lastdate2    = mysql_fetch_array($result_lastdate2);
                    extract($row_lastdate2);					
				}
				$numberOfPayments = ceil($TotalAmount / $Client_paymentTerms2);
				for($i = 1; $i<=$numberOfPayments; $i++){
					if($TotalAmount > $Client_paymentTerms2){
						$amountPayment = $Client_paymentTerms2;
					}
					else{
						$amountPayment = $TotalAmount;
					}
					$query_amountbase  = "select convertcurFunc('$Currency_Code', '$base1', '$LastDueDate2', 0,0,'$base1', '$base2', '$amountPayment') as amountPaymentBase1,
	                                      convertcurFunc('$Currency_Code', '$base2', '$LastDueDate2', 0,0,'$base1', '$base2', '$amountPayment') as amountPaymentBase2
					                      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_insertdt  = "insert into temp_invoicingdt (InvoicingDt_Id, Invoicing_Id, InvoicingDt_DueDate, InvoicingDt_Amount, InvoicingDt_AmountBase1, InvoicingDt_AmountBase2) 
					                    values ('$InvDtNewId', '$invoicing_id', '$LastDueDate2', '$amountPayment', '$amountPaymentBase1', '$amountPaymentBase2')";
					$result_insertdt = mysql_query($query_insertdt, $connection);
					
					$query_lastdate2  = "select DATE_ADD('$LastDueDate2', INTERVAL 7 DAY) as LastDueDate2 from dual";
					$result_lastdate2 = mysql_query($query_lastdate2, $connection);
                    $row_lastdate2    = mysql_fetch_array($result_lastdate2);
                    extract($row_lastdate2);
					
					if($TotalAmount > $Client_paymentTerms2){
						$TotalAmount = $TotalAmount - $Client_paymentTerms2;
					}
					
				}
				
				
			}
		else{
			 $Invoicing_ValueDate = $Invoicing_Main_Date;
			 $query_amountbase  = "select convertcurFunc('$Currency_Code', '$base1', '$Invoicing_Main_Date', 0,0,'$base1', '$base2', '$Invoicing_Amount') as Invoicing_AmountBase1,
	                             convertcurFunc('$Currency_Code', '$base2', '$Invoicing_Main_Date', 0,0,'$base1', '$base2', '$Invoicing_Amount') as Invoicing_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('Invoicing_Id', '') as InvNewId from dual";
	$result_sequence = mysql_query($query_sequence, $connection);
	$row_sequence    = mysql_fetch_array($result_sequence);
	extract($row_sequence);
	$query_insert  = "insert into temp_invoicing (Invoicing_Id, Invoicing_Date, Client_Code, Currency_Code, Invoicing_Amount, Invoicing_AmountBase1, Invoicing_AmountBase2, User_Code, headerinvoicing_id, Invoicing_ValueDate, Invoicing_Reference) 
	                  values('$InvNewId', '$Invoicing_Main_Date', '$Client_Code', '$Currency_Code', '$Invoicing_Amount', '$Invoicing_AmountBase1', '$Invoicing_AmountBase2', '".$_SESSION['useraccount']."', $headerinvoicing_id, '$Invoicing_ValueDate', '$Invoicing_Reference')";
	//echo $query_insert;exit;
	$result_insert = mysql_query($query_insert, $connection);
	$invoicing_id  = $InvNewId;
	
	$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_insertdt  = "insert into temp_invoicingdt (InvoicingDt_Id, Invoicing_Id, InvoicingDt_DueDate, InvoicingDt_Amount, InvoicingDt_AmountBase1, InvoicingDt_AmountBase2) 
	                    values ('$InvDtNewId', '$invoicing_id', '$Invoicing_ValueDate', '$Invoicing_Amount', '$Invoicing_AmountBase1', '$Invoicing_AmountBase2')";
    $result_insertdt = mysql_query($query_insertdt, $connection);
		}
	//echo $query_chkclient;
	drawTempInvoicingGrid($connection, $Invoicing_Main_Date, $headerinvoicing_id);
    echo "[BRK]";
    echo $response;
	echo "[BRK]";
}
else
	if($actionmode == "updateinvoicing"){
	
	$query_invoicing     = "select Invoicing_Amount as MainAmount from temp_invoicing where invoicing_id = '$invoicing_id'";
	$query_invoicingdt   = "select sum(InvoicingDt_Amount) as DetAmount from temp_invoicingdt where Invoicing_Id = '$invoicing_id'";
	$result_invoicing    = mysql_query($query_invoicing, $connection);
	$result_invoicingdt  = mysql_query($query_invoicingdt, $connection);
	$row_invoicing       = mysql_fetch_array($result_invoicing);
	$row_invoicingdt     = mysql_fetch_array($result_invoicingdt);
	extract($row_invoicing);
	extract($row_invoicingdt);
	if($Invoicing_Amount != $DetAmount){
   // $query_deletedt = "delete from temp_invoicingdt where invoicing_id = ''";
	$query_client  = "select Client_EndofMonthPay, Client_PaymentTerms, Client_PaymentMode, Client_paymentTerms2 from clients where ledger_number = '$Client_Code'";
	$result_client = mysql_query($query_client, $connection); 
	$row_client    = mysql_fetch_array($result_client);
	extract($row_client);
	//echo $query_client;exit;
	if($Client_EndofMonthPay == 1){
		$query_selectlastday  = "select LAST_DAY('$Invoicing_Main_Date') as Invoicing_ValueDate
		                         from dual";
		//echo $query_selectlastday;exit;
		$result_selectlastday = mysql_query($query_selectlastday, $connection);
		$row_selectlastday    = mysql_fetch_array($result_selectlastday);
		extract($row_selectlastday);
		$Invoicing_ValueDate = $Invoicing_ValueDate." ".date("H:i:s");
		$query_amountbase  = "select convertcurFunc('$Currency_Code', '$base1', '$Invoicing_Main_Date', 0,0,'$base1', '$base2', '$Invoicing_Amount') as Invoicing_AmountBase1,
	                             convertcurFunc('$Currency_Code', '$base2', '$Invoicing_Main_Date', 0,0,'$base1', '$base2', '$Invoicing_Amount') as Invoicing_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_invoicing set Invoicing_Date = '$Invoicing_Main_Date',
	                                           Client_Code = '$Client_Code',
											   Currency_Code = '$Currency_Code',
											   Invoicing_Amount = '$Invoicing_Amount',
											   Invoicing_AmountBase1 = '$Invoicing_AmountBase1',
											   Invoicing_AmountBase2 = '$Invoicing_AmountBase2',
											   Invoicing_ValueDate = '$Invoicing_ValueDate',
											   Invoicing_Reference = '$Invoicing_Reference'
					 where invoicing_id = '$invoicing_id'";
	$result_update = mysql_query($query_update, $connection);
	
	$query_deletedt  = "delete from temp_invoicingdt where invoicing_id = '$invoicing_id'";
	$result_deletedt = mysql_query($query_deletedt, $connection); 
	$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_insertdt  = "insert into temp_invoicingdt (InvoicingDt_Id, Invoicing_Id, InvoicingDt_DueDate, InvoicingDt_Amount, InvoicingDt_AmountBase1, InvoicingDt_AmountBase2) 
	                    values ('$InvDtNewId', '$invoicing_id', '$Invoicing_ValueDate', '$Invoicing_Amount', '$Invoicing_AmountBase1', '$Invoicing_AmountBase2')";
    $result_insertdt = mysql_query($query_insertdt, $connection);
	}
	else
		if($Client_PaymentTerms > 0){
			$query_selectvaluedate  = "SELECT DATE_ADD('$Invoicing_Main_Date', INTERVAL $Client_PaymentTerms DAY) as Invoicing_ValueDate
			                          from dual";
			$result_selectvaluedate = mysql_query($query_selectvaluedate, $connection);
			$row_selectvaluedate    = mysql_fetch_array($result_selectvaluedate);
			extract($row_selectvaluedate);
			$query_amountbase  = "select convertcurFunc('$Currency_Code', '$base1', '$Invoicing_Main_Date', 0,0,'$base1', '$base2', '$Invoicing_Amount') as Invoicing_AmountBase1,
	                             convertcurFunc('$Currency_Code', '$base2', '$Invoicing_Main_Date', 0,0,'$base1', '$base2', '$Invoicing_Amount') as Invoicing_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_invoicing set Invoicing_Date = '$Invoicing_Main_Date',
	                                           Client_Code = '$Client_Code',
											   Currency_Code = '$Currency_Code',
											   Invoicing_Amount = '$Invoicing_Amount',
											   Invoicing_AmountBase1 = '$Invoicing_AmountBase1',
											   Invoicing_AmountBase2 = '$Invoicing_AmountBase2',
											   Invoicing_ValueDate = '$Invoicing_ValueDate',
											   Invoicing_Reference = '$Invoicing_Reference'
					 where invoicing_id = '$invoicing_id'";
	$result_update = mysql_query($query_update, $connection);
	
	$query_deletedt  = "delete from temp_invoicingdt where invoicing_id = '$invoicing_id'";
	$result_deletedt = mysql_query($query_deletedt, $connection); 
	$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_insertdt  = "insert into temp_invoicingdt (InvoicingDt_Id, Invoicing_Id, InvoicingDt_DueDate, InvoicingDt_Amount, InvoicingDt_AmountBase1, InvoicingDt_AmountBase2) 
	                    values ('$InvDtNewId', '$invoicing_id', '$Invoicing_ValueDate', '$Invoicing_Amount', '$Invoicing_AmountBase1', '$Invoicing_AmountBase2')";
    $result_insertdt = mysql_query($query_insertdt, $connection);
		}
		else
			if($Client_PaymentMode != "" && $Client_paymentTerms2 > 0){
				$query_amountbase  = "select convertcurFunc('$Currency_Code', '$base1', '$Invoicing_Main_Date', 0,0,'$base1', '$base2', '$Invoicing_Amount') as Invoicing_AmountBase1,
	                             convertcurFunc('$Currency_Code', '$base2', '$Invoicing_Main_Date', 0,0,'$base1', '$base2', '$Invoicing_Amount') as Invoicing_AmountBase2
					     from dual";
	//echo $query_amountbase;exit;
	$result_amountbase = mysql_query($query_amountbase, $connection);
	$row_amountbase    = mysql_fetch_array($result_amountbase);
    extract($row_amountbase);
				$TotalAmount = $Invoicing_Amount;
				$query_deletedt  = "delete from temp_invoicingdt where invoicing_id = '$invoicing_id'";
	            $result_deletedt = mysql_query($query_deletedt, $connection); 
				$query_selectlastamountid  = "select ifNull(max(invoicingdt_id), 0) as PrevInvoicDtId
from ( select invoicingdt_id
from temp_invoicingdt inner join temp_invoicing on temp_invoicing.invoicing_id = temp_invoicingdt.invoicing_id
where temp_invoicing.Client_Code = '$Client_Code' union all
select invoicingdt_id
from invoicingdt inner join invoicing on invoicing.invoicing_id = invoicingdt.invoicing_id
where invoicing.Client_Code = '$Client_Code') as Q";
				$result_selectlastamountid = mysql_query($query_selectlastamountid, $connection);
				$row_selectlastamountid    = mysql_fetch_array($result_selectlastamountid);
				extract($row_selectlastamountid);
				if($PrevInvoicDtId > 0){
					$query_selectlastamountdate  = "select InvoicingDt_DueDate as LastDueDate, InvoicingDt_Amount as LastAmount
from temp_invoicingdt
where invoicingdt_id = '$PrevInvoicDtId' union all
select InvoicingDt_DueDate as LastDueDate, InvoicingDt_Amount as LastAmount
from invoicingdt
where invoicingdt_id = '$PrevInvoicDtId'";
					$result_selectlastamountdate = mysql_query($query_selectlastamountdate, $connection);
					$row_selectlastamountdate    = mysql_fetch_array($result_selectlastamountdate);
					extract($row_selectlastamountdate);
					if($LastDueDate > $Invoicing_Main_Date && $LastAmount < $Client_paymentTerms2){
						$Invoicing_ValueDate = $LastDueDate;
					}
					else{
						$query_selectvaluedate  = "select DATE_ADD('$Invoicing_Main_Date', INTERVAL if(($Client_PaymentMode - DAYOFWEEK('$Invoicing_Main_Date')) < 0, ($Client_PaymentMode - DAYOFWEEK('$Invoicing_Main_Date')) + 7, ($Client_PaymentMode - DAYOFWEEK('$Invoicing_Main_Date'))) DAY)  as Invoicing_ValueDate 
						                           from dual";
						$result_selectvaluedate = mysql_query($query_selectvaluedate, $connection);
						$row_selelectvaluedate  = mysql_fetch_array($result_selectvaluedate);
						extract($row_selelectvaluedate);
					}
				}
				else{
					    $query_selectvaluedate  = "select DATE_ADD('$Invoicing_Main_Date', INTERVAL if(($Client_PaymentMode - DAYOFWEEK('$Invoicing_Main_Date')) < 0, ($Client_PaymentMode - DAYOFWEEK('$Invoicing_Main_Date')) + 7, ($Client_PaymentMode - DAYOFWEEK('$Invoicing_Main_Date'))) DAY)  as Invoicing_ValueDate 
						                           from dual";
						$result_selectvaluedate = mysql_query($query_selectvaluedate, $connection);
						$row_selelectvaluedate  = mysql_fetch_array($result_selectvaluedate);
						extract($row_selelectvaluedate);
				}
				$query_update = "update temp_invoicing set Invoicing_Date = '$Invoicing_Main_Date',
	                                           Client_Code = '$Client_Code',
											   Currency_Code = '$Currency_Code',
											   Invoicing_Amount = '$Invoicing_Amount',
											   Invoicing_AmountBase1 = '$Invoicing_AmountBase1',
											   Invoicing_AmountBase2 = '$Invoicing_AmountBase2',
											   Invoicing_ValueDate = '$Invoicing_ValueDate',
											   Invoicing_Reference = '$Invoicing_Reference'
					 where invoicing_id = '$invoicing_id'";
	            $result_update = mysql_query($query_update, $connection);
	            $query_deletedt  = "delete from temp_invoicingdt where invoicing_id = '$invoicing_id'";
	$result_deletedt = mysql_query($query_deletedt, $connection); 
	            
				$LastDueDate2 = $Invoicing_ValueDate;
				if($LastDueDate > $Invoicing_Main_Date && $LastAmount < $Client_paymentTerms2){
					
					$diffAmount = $Client_paymentTerms2 - $LastAmount;
					$query_amountbase  = "select convertcurFunc('$Currency_Code', '$base1', '$LastDueDate2', 0,0,'$base1', '$base2', '$diffAmount') as diffAmountBase1,
	                                      convertcurFunc('$Currency_Code', '$base2', '$LastDueDate2', 0,0,'$base1', '$base2', '$diffAmount') as diffAmountBase2
					                      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_insertdt  = "insert into temp_invoicingdt (InvoicingDt_ID, Invoicing_Id, InvoicingDt_DueDate, InvoicingDt_Amount, InvoicingDt_AmountBase1, InvoicingDt_AmountBase2) 
					                    values ('$InvDtNewId', '$invoicing_id', '$LastDueDate2', '$diffAmount', '$diffAmountBase1', '$diffAmountBase2')";
					$result_insertdt = mysql_query($query_insertdt, $connection);
					$TotalAmount = $TotalAmount - $diffAmount;
					
					$query_lastdate2  = "select DATE_ADD('$LastDueDate2', INTERVAL 7 DAY) as LastDueDate2 from dual";
					$result_lastdate2 = mysql_query($query_lastdate2, $connection);
                    $row_lastdate2    = mysql_fetch_array($result_lastdate2);
                    extract($row_lastdate2);					
				}
				$numberOfPayments = ceil($TotalAmount / $Client_paymentTerms2);
				for($i = 1; $i<=$numberOfPayments; $i++){
					if($TotalAmount > $Client_paymentTerms2){
						$amountPayment = $Client_paymentTerms2;
					}
					else{
						$amountPayment = $TotalAmount;
					}
					$query_amountbase  = "select convertcurFunc('$Currency_Code', '$base1', '$LastDueDate2', 0,0,'$base1', '$base2', '$amountPayment') as amountPaymentBase1,
	                                      convertcurFunc('$Currency_Code', '$base2', '$LastDueDate2', 0,0,'$base1', '$base2', '$amountPayment') as amountPaymentBase2
					                      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_insertdt  = "insert into temp_invoicingdt (InvoicingDt_Id, Invoicing_Id, InvoicingDt_DueDate, InvoicingDt_Amount, InvoicingDt_AmountBase1, InvoicingDt_AmountBase2) 
					                    values ('$InvDtNewId', '$invoicing_id', '$LastDueDate2', '$amountPayment', '$amountPaymentBase1', '$amountPaymentBase2')";
					$result_insertdt = mysql_query($query_insertdt, $connection);
					
					$query_lastdate2  = "select DATE_ADD('$LastDueDate2', INTERVAL 7 DAY) as LastDueDate2 from dual";
					$result_lastdate2 = mysql_query($query_lastdate2, $connection);
                    $row_lastdate2    = mysql_fetch_array($result_lastdate2);
                    extract($row_lastdate2);
					
					if($TotalAmount > $Client_paymentTerms2){
						$TotalAmount = $TotalAmount - $Client_paymentTerms2;
					}
					
				}
				
				
			}
		else{
			 $Invoicing_ValueDate = $Invoicing_Main_Date;
			 $query_amountbase  = "select convertcurFunc('$Currency_Code', '$base1', '$Invoicing_Main_Date', 0,0,'$base1', '$base2', '$Invoicing_Amount') as Invoicing_AmountBase1,
	                             convertcurFunc('$Currency_Code', '$base2', '$Invoicing_Main_Date', 0,0,'$base1', '$base2', '$Invoicing_Amount') as Invoicing_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_invoicing set Invoicing_Date = '$Invoicing_Main_Date',
	                                           Client_Code = '$Client_Code',
											   Currency_Code = '$Currency_Code',
											   Invoicing_Amount = '$Invoicing_Amount',
											   Invoicing_AmountBase1 = '$Invoicing_AmountBase1',
											   Invoicing_AmountBase2 = '$Invoicing_AmountBase2',
											   Invoicing_ValueDate = '$Invoicing_ValueDate',
											   Invoicing_Reference = '$Invoicing_Reference'
					 where invoicing_id = '$invoicing_id'";
	$result_update = mysql_query($query_update, $connection);
	
	$query_deletedt  = "delete from temp_invoicingdt where invoicing_id = '$invoicing_id'";
	$result_deletedt = mysql_query($query_deletedt, $connection); 
	$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_insertdt  = "insert into temp_invoicingdt (InvoicingDt_Id, Invoicing_Id, InvoicingDt_DueDate, InvoicingDt_Amount, InvoicingDt_AmountBase1, InvoicingDt_AmountBase2) 
	                    values ('$InvDtNewId', '$invoicing_id', '$Invoicing_ValueDate', '$Invoicing_Amount', '$Invoicing_AmountBase1', '$Invoicing_AmountBase2')";
    $result_insertdt = mysql_query($query_insertdt, $connection);
		}
	}
	else{
		$query_update = "update temp_invoicing set Invoicing_Date = '$Invoicing_Main_Date',
	                                           Client_Code = '$Client_Code',
											   Currency_Code = '$Currency_Code',
											   Invoicing_Amount = '$Invoicing_Amount',
											   Invoicing_AmountBase1 = '$Invoicing_AmountBase1',
											   Invoicing_AmountBase2 = '$Invoicing_AmountBase2',
											   Invoicing_Reference = '$Invoicing_Reference'
					 where invoicing_id = '$invoicing_id'";
	$result_update = mysql_query($query_update, $connection);
	}
	//echo $query_chkclient;
	drawTempInvoicingGrid($connection, $Invoicing_Main_Date, $headerinvoicing_id);
    echo "[BRK]";
    echo $response;
	echo "[BRK]";
}
else
	if($actionmode == "insert"){
		//if($headerinvoicing_id == ""){
			$query_invoicinglist  = "select invoicing_id as InvoicingId, Invoicing_Amount as InvAmount
			                         from temp_invoicing
									 where headerinvoicing_id is null";
			$result_invoicinglist = mysql_query($query_invoicinglist, $connection);
            while($row_invoicinglist = mysql_fetch_array($result_invoicinglist)){
				extract($row_invoicinglist);
				$query_sumdt  = "select sum(InvoicingDt_Amount) as SumAmountDt
				                 from temp_invoicingdt
								 where invoicing_id = '$InvoicingId'";
				$result_sumdt = mysql_query($query_sumdt, $connection);
				$row_sumdt    = mysql_fetch_array($result_sumdt);
				extract($row_sumdt);
				if($SumAmountDt != $InvAmount){
					$responseTotal = 0;
					$responseInvId = $InvoicingId;
					break;
				}
				else{
					$responseTotal = 1;
					$responseInvId = "";
				}
			}
			if($responseTotal == 1){
			$query_start  = "start transaction";
			$result_start = mysql_query($query_start, $connetcion);
			$query_sequence  = "select NextSequence('HeaderInvoicing_Id', '') as HeadInvId from dual";
	$result_sequence = mysql_query($query_sequence, $connection);
	$row_sequence    = mysql_fetch_array($result_sequence);
	extract($row_sequence);
			$query_insert  = "insert into headerinvoicing (HeaderInvoicing_Id, headerinvoicing_date) values ('$HeadInvId', '$Invoicing_Main_Date')";
			//echo $query_insert."[BRK]";exit;
			$result_insert = mysql_query($query_insert, $connection);
			$headerinvoicing_id = $HeadInvId;
		//}
		
		$query_updateinvoicing  = "update temp_invoicing set headerinvoicing_id = '$headerinvoicing_id' where headerinvoicing_id is null";
		//echo $query_updateinvoicing;exit;
		$result_updateinvoicing = mysql_query($query_updateinvoicing, $connection);
        
		$query_selectinvoicing  = "select invoicing_id as InvoicingId, Invoicing_Amount as InvoicingAmount, Invoicing_AmountBase1 as InvoicingAmountBase1, Invoicing_AmountBase2 as InvoicingAmountBase2, Invoicing_Date as InvoicingDate 
		                           from temp_invoicing 
								   where headerinvoicing_id = '$headerinvoicing_id'";
		//echo $query_selectinvoicing;exit;
		$result_selectinvoicing = mysql_query($query_selectinvoicing, $connection);
		while($row_selectinvoicing = mysql_fetch_array($result_selectinvoicing)){
			extract($row_selectinvoicing);
			$query_copydata  = "select CopyData_TempInvoicing('$InvoicingId', '".$_SESSION['useraccount']."') from dual";
			echo $query_copydata;
			$result_copydata = mysql_query($query_copydata, $connection);
			
			$JvId="";
			$query_invoicingdt  = "select invoicing.Invoicing_Reference as InvRefNo, invoicingdt.InvoicingDt_DueDate as ValueDate, invoicing.client_code as ClientCode, invoicing.currency_code as CurrencyCode, invoicingdt.InvoicingDt_Amount as Amount, InvoicingDt_AmountBase1 as AmountBase1, InvoicingDt_AmountBase2 as AmountBase2
                                           from invoicing inner join invoicingdt on invoicing.invoicing_id = invoicingdt.invoicing_id
                                           where invoicingdt.invoicing_id = '$InvoicingId'";
			//echo $query_invoicingdt."[BRK]";exit;
		    $result_invoicingdt = mysql_query($query_invoicingdt, $connection);
			while($row_invoicingdt = mysql_fetch_array($result_invoicingdt)){
				//echo "1<br>";
				extract($row_invoicingdt);
				if($InvoicingAmount > 0) $JvType = "SI"; else $JvType = "SR";
			
			    $Description = "Invoice No. ".$InvRefNo;
			    if($Amount < 0){
					$typedbcd = "C";
				}
				else{
					$typedbcd = "D";
				}
				$Amount = abs($Amount);
				$AmountBase1 = abs($AmountBase1);
				$AmountBase2 = abs($AmountBase2);
			    $query_calljv  = "call Create_Jv ('$JvType', '$JvId', '$InvoicingDate', '$ClientCode', '$CurrencyCode', '$typedbcd', '$Description', '', '$ValueDate', null, null, '$Amount', '$AmountBase1', '$AmountBase2', '1', '".$_SESSION['dbName']."', '".$_SESSION['dbLabel']."', '".$_SESSION['useraccount']."', '".$_SESSION['useraccount']."', '$InvRefNo', '', '0', @jvid)";
			    //echo $query_calljv."<br>";
				$result_calljv = mysql_query($query_calljv, $connection); 
			
			    $query_selectjv  = "select @jvid as JvId from dual";
			    $result_selectjv = mysql_query($query_selectjv, $connection); 
			    $row_selectjv    = mysql_fetch_array($result_selectjv);
			    extract($row_selectjv);
			}
			//nvoicingAmount = abs($InvoicingAmount);
			if($InvoicingAmount < 0){
				$typedbcd = "D";
			}
			else{
				$typedbcd = "C";
			}
			$InvoicingAmount = abs($InvoicingAmount);
			$InvoicingAmountBase1 = abs($InvoicingAmountBase1);
			$InvoicingAmountBase2 = abs($InvoicingAmountBase2);
			$query_calljv  = "call Create_Jv ('$JvType', '$JvId', '$InvoicingDate', '$CreditAccount', '$CurrencyCode', '$typedbcd', '$Description', '', '$ValueDate', null, null, '$InvoicingAmount', '$InvoicingAmountBase1', '$InvoicingAmountBase2', '1', '".$_SESSION['dbName']."', '".$_SESSION['dbLabel']."', '".$_SESSION['useraccount']."', '".$_SESSION['useraccount']."', '$InvRefNo', '', '0', @jvid)";
			$result_calljv = mysql_query($query_calljv, $connection); 
			//echo "[BRK]";exit;
			$query_updatejournal  = "update invoicing set journal_id = '$JvId' where invoicing_id = '$InvoicingId'";
			$result_updatejournal = mysql_query($query_updatejournal, $connection); 
		}
		$query_end  = "commit";
		$result_end = mysql_query($query_end, $connection);
		}
		echo $responseTotal;
            echo "[BRK]";
            echo $responseInvId;
            echo "[BRK]";	
		
	}
	else
		if($actionmode == "edit"){
			
			$query_invoicinglist  = "select invoicing_id as InvoicingId, Invoicing_Amount as InvAmount
			                         from temp_invoicing
									 where headerinvoicing_id = '$headerinvoicing_id'";
			$result_invoicinglist = mysql_query($query_invoicinglist, $connection);
            while($row_invoicinglist = mysql_fetch_array($result_invoicinglist)){
				extract($row_invoicinglist);
				$query_sumdt  = "select sum(InvoicingDt_Amount) as SumAmountDt
				                 from temp_invoicingdt
								 where invoicing_id = '$InvoicingId'";
				$result_sumdt = mysql_query($query_sumdt, $connection);
				$row_sumdt    = mysql_fetch_array($result_sumdt);
				extract($row_sumdt);
				if($SumAmountDt != $InvAmount){
					$responseTotal = 0;
					$responseInvId = $InvoicingId;
					break;
				}
				else{
					$responseTotal = 1;
					$responseInvId = "";
				}
			}				
			if($responseTotal == 1){
			$query_start  = "start transaction";
			$result_start = mysql_query($query_start, $connection); 
			
			$query_chkheader  = "select count(*) as HeaderExist from headerinvoicing where headerinvoicing_id = '$headerinvoicing_id'";
			$result_chkheader = mysql_query($query_chkheader, $connection); 
			$row_chkheader    = mysql_fetch_array($result_chkheader);
			extract($row_chkheader);
			
			if($HeaderExist > 0){
			$query_update  = "update headerinvoicing set headerinvoicing_date = '$Invoicing_Main_Date'
			                  where headerinvoicing_id = '$headerinvoicing_id'";
			$result_update = mysql_query($query_update, $connection);
			}
			else{
				$query_insert = "insert into headerinvoicing (headerinvoicing_id, headerinvoicing_date) 
				                 select headerinvoicing_id, headerinvoicing_date
								 from temp_invoicing
								 where headerinvoicing_id = '$headerinvoicing_id'";
				$result_insert = mysql_query($query_insert, $connection);
			}
			
			$query_deletejournal  = "delete from journal where journal_id in
                    (select invoicing.Journal_id
								    from invoicing
                    inner join headerinvoicing  on headerinvoicing.headerinvoicing_id=invoicing.headerinvoicing_id
                    and headerinvoicing.headerinvoicing_id ='$headerinvoicing_id'
                    left join temp_invoicing on temp_invoicing.invoicing_id=invoicing.invoicing_id
                    where temp_invoicing.invoicing_id is null)";
			//echo $query_deletejournal;exit;
			$result_deletejournal = mysql_query($query_deletejournal, $connection);
			$query_deletedt  = "delete from invoicing 
			                    where invoicing_id not in 
							    (select invoicing_id 
							    from temp_invoicing 
							    where headerinvoicing_id ='$headerinvoicing_id') and headerinvoicing_id ='$headerinvoicing_id'";
		    $result_deletedt = mysql_query($query_deletedt, $connection);
			
			$query_selectjournaltemp  = "select Journal_Id as JournalId, invoicing_id  as InvoicingId, Invoicing_Date as InvoicingDate, Invoicing_Amount as InvoicingAmount, Invoicing_AmountBase1 as InvoicingAmountBase1, Invoicing_AmountBase2 as InvoicingAmountBase2
			                             from temp_invoicing
										 where headerinvoicing_id = '$headerinvoicing_id'";
			$result_selectjournaltemp = mysql_query($query_selectjournaltemp, $connection);
			while($row_selectjournaltemp = mysql_fetch_array($result_selectjournaltemp)){
				extract($row_selectjournaltemp);
				
				if($JournalId != "" && $JournalId != 0){
				$query_deletedt  = "delete from journaldt where journal_id = '$JournalId'";
				$result_deletedt = mysql_query($query_deletedt, $connection);
				}
                
				$query_copydata  = "select CopyData_TempInvoicing('$InvoicingId', '".$_SESSION['useraccount']."') from dual";
			   
				$result_copydata = mysql_query($query_copydata, $connection);
				 //echo $result_copydata."<br>";
				
				$JvId = $JournalId;
				$query_invoicingdt  = "select invoicing.Invoicing_Reference as InvRefNo, invoicingdt.InvoicingDt_DueDate as ValueDate, invoicing.client_code as ClientCode, invoicing.currency_code as CurrencyCode, invoicingdt.InvoicingDt_Amount as Amount, InvoicingDt_AmountBase1 as AmountBase1, InvoicingDt_AmountBase2 as AmountBase2
                                           from invoicing inner join invoicingdt on invoicing.invoicing_id = invoicingdt.invoicing_id
                                           where invoicingdt.invoicing_id = '$InvoicingId'";
			//echo $query_invoicingdt."[BRK]";exit;
		    $result_invoicingdt = mysql_query($query_invoicingdt, $connection);
			while($row_invoicingdt = mysql_fetch_array($result_invoicingdt)){
				//echo "1<br>";
				extract($row_invoicingdt);
				if($InvoicingAmount > 0) $JvType = "SI"; else $JvType = "SR";
			
			    $Description = "Invoice No. ".$InvRefNo;
			     if($Amount < 0){
					$typedbcd = "C";
				}
				else{
					$typedbcd = "D";
				}
				$Amount = abs($Amount);
				$AmountBase1 = abs($AmountBase1);
				$AmountBase2 = abs($AmountBase2);
			    $query_calljv  = "call Create_Jv ('$JvType', '$JvId', '$InvoicingDate', '$ClientCode', '$CurrencyCode', '$typedbcd', '$Description', '', '$ValueDate', null, null, '$Amount', '$AmountBase1', '$AmountBase2', '1', '".$_SESSION['dbName']."', '".$_SESSION['dbLabel']."', '".$_SESSION['useraccount']."', '".$_SESSION['useraccount']."', '$InvRefNo', '', '0', @jvid)";
			  //  echo $query_calljv."<br>";exit;
				$result_calljv = mysql_query($query_calljv, $connection); 
			
			    $query_selectjv  = "select @jvid as JvId from dual";
			    $result_selectjv = mysql_query($query_selectjv, $connection); 
			    $row_selectjv    = mysql_fetch_array($result_selectjv);
			    extract($row_selectjv);
				
			}
			//nvoicingAmount = abs($InvoicingAmount);
			if($InvoicingAmount < 0){
				$typedbcd = "D";
			}
			else{
				$typedbcd = "C";
			}
			$InvoicingAmount = abs($InvoicingAmount);
			$InvoicingAmountBase1 = abs($InvoicingAmountBase1);
			$InvoicingAmountBase2 = abs($InvoicingAmountBase2);
			$query_calljv  = "call Create_Jv ('$JvType', '$JvId', '$InvoicingDate', '$CreditAccount', '$CurrencyCode', '$typedbcd', '$Description', '', '$ValueDate', null, null, '$InvoicingAmount', '$InvoicingAmountBase1', '$InvoicingAmountBase2', '1', '".$_SESSION['dbName']."', '".$_SESSION['dbLabel']."', '".$_SESSION['useraccount']."', '".$_SESSION['useraccount']."', '$InvRefNo', '', '0', @jvid)";
			$result_calljv = mysql_query($query_calljv, $connection); 
			
            $query_updatejv  = "update invoicing set journal_id = '$JvId' where invoicing_id = '$InvoicingId'";
            $result_updatejv = mysql_query($query_updatejv, $connection); 			
                				
			}
			
			
			
			
			$query_delete  = "delete from temp_headerinvoicing where headerinvoicing_id = '$headerinvoicing_id'";
			$result_delete = mysql_query($query_delete, $connection);

            $query_end  = "commit";
            $result_end = mysql_query($query_end, $connection); 
			}
            echo $responseTotal;
            echo "[BRK]";
            echo $responseInvId;
            echo "[BRK]";			
		}
	else
		if($actionmode == "deleteinvoicing"){
			$query_delete  = "delete from temp_invoicing where invoicing_id = '$invoicing_id'";
			$result_delete = mysql_query($query_delete, $connection); 
			drawTempInvoicingGrid($connection, $Invoicing_Main_Date, $headerinvoicing_id);
            echo "[BRK]";
		}
		else
			if($actionmode == "clearpage"){
				if($headerinvoicing_id == ""){
					$query_sequence  = "select NextSequence('HeaderInvoicing_Id', '') as HeadInvId from dual";
	$result_sequence = mysql_query($query_sequence, $connection);
	$row_sequence    = mysql_fetch_array($result_sequence);
	extract($row_sequence);
					$query_insertheader  = "insert into temp_headerinvoicing (HeaderInvoicing_Id, headerinvoicing_date) values ('$HeadInvId', '$Invoicing_Main_Date')";
					$result_insertheader = mysql_query($query_insertheader, $connection);
					$headerinvoicing_id  = mysql_insert_id($result_insertheader);
					
					$query_update  = "update temp_invoicing set headerinvoicing_id = '$headerinvoicing_id' where headerinvoicing_id is null";
					$result_update = mysql_query($query_update, $connection);
				}
			}
			else
				if($actionmode == "chkclientcode"){
					$query_chk  = "select ledger_number as ClientCode, ledger_name as ClientName, currency_code as CurrencyCode, clients.Client_EndofMonthPay, clients.Client_PaymentTerms, clients.Client_PaymentMode, clients.Client_paymentTerms2, CLIENT_CASHMODE as CashMode 
					              from clients
								  where ledger_number = '$Client_Code'";
					$result_chk = mysql_query($query_chk, $connection); 
					$row_chk    = mysql_fetch_array($result_chk);
					extract($row_chk);
					if($ClientName == ""){
						$response = 0;
					}
					else{
						$response = 1;
					}
					if($Client_EndofMonthPay == 1){
		$client_status  = "End of Month";
		$query_lastday  = "select date_format(last_day('$Invoicing_Main_Date'), '%d/%m/%Y') as ValueDate from dual";
		$result_lastday =  mysql_query($query_lastday, $connection);
		$row_lastday    =  mysql_fetch_array($result_lastday);
		extract($row_lastday);
	}
	else
		if($Client_PaymentTerms > 0){
			$client_status =  "Payment Term";
			$query_paymentterm  = "select date_format(DATE_ADD('$Invoicing_Main_Date', INTERVAL $Client_PaymentTerms DAY), '%d/%m/%Y') as ValueDate from dual";
			$result_payemntterm = mysql_query($query_paymentterm, $connection); 
			$row_paymmentterm   = mysql_fetch_array($result_payemntterm);
			extract($row_paymmentterm);
	}
	else
			if($Client_PaymentMode != "" && $Client_paymentTerms2 > 0){
				$client_status =  "Weekly";
			}
			else
				if($CashMode == 1){
					$client_status = "Cash Mode";
				}
					echo $response;
					echo "[BRK]";
					echo $ClientCode;
					echo "[BRK]";
					echo $ClientName;
					echo "[BRK]";
					echo $CurrencyCode;
					echo "[BRK]";
					echo $client_status;
					echo "[BRK]";
					echo $ValueDate;
					echo "[BRK]";
					}
					else
						if($actionmode == "cancelinvoicing"){
							if($headerinvoicing_id != "null"){
							   $query_delete  = "delete from temp_headerinvoicing where headerinvoicing_id = '$headerinvoicing_id'";
							   $result_delete = mysql_query($query_delete, $connection); 
							}
							$query_delete = "delete from temp_invoicing where 1=1 ";
							if($headerinvoicing_id != "null"){
								$query_delete = $query_delete." and headerinvoicing_id = '$headerinvoicing_id'";
							}
							else{
								$query_delete = $query_delete." and headerinvoicing_id is null";
							}
							//echo $query_delete;
							$result_delete = mysql_query($query_delete, $connection);
						} 
?>