<?php

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

include("config.inc.php");
include("function.inc.php");


//Set Time Execution For the Report
ini_set('max_execution_time', $config['ReportTimeExecution']);

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

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

$querysetup ="select base1, base2 , decimalbase, decimalbase2, ifnull(reconcilation_AmountChecked,0) as reconcilation_AmountChecked from setup ";
//echo $querysetup; exit;
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
$rowsetup=mysql_fetch_row($resultsetup);
$base1 = $rowsetup[0]; 
$base2 = $rowsetup[1];	
$DecimalBase = $rowsetup[2];
$DecimalBase2 = $rowsetup[3];
$reconcilation_AmountChecked = $rowsetup[4];

$mode 			= $_GET['mode']; if ($mode == '') $mode = $_POST['mode']; 

$fromdate 		= $_POST['fromdate'];
$todate 		= $_POST['todate'];
$fromledger 	= $_POST['fromledger'];
$toledger 		= $_POST['toledger'];
$currency 		= $_POST['currency'];
$concail 		= $_POST['concail'];
$amntchk 		= $_POST['amntchk']; 			if ($amntchk=='') 		$amntchk 		= $_GET['amntchk'];
$endBal 		= $_POST['endBal'];	
$TotalDebit		= $_POST['TotalDebit'];
$TotalCredit	= $_POST['TotalCredit'];
$tbalance		= $_POST['tbalance'];

$reconcile_id = $_POST['reconcile_id']; 	if ($reconcile_id=='') 	$reconcile_id 	= $_GET['reconcile_id'];
$reconciledt_id = $_GET['reconciledt_id'];
$chk_reconcile = $_GET['chk_reconcile'];

if($fromledger == '')
	$fromledger = "NULL";
else 
	$fromledger = "'".$fromledger."'";

if($toledger == '')
	$toledger = "NULL";
else 
	$toledger = "'".$toledger."'";
if ($amntchk == '')
	$amntchk = "NULL";

$expdate = explode("/",$fromdate);
$expdate1 = explode("/",$todate);

$fromdatenew = $expdate[2]."-".$expdate[1]."-".$expdate[0]; 
$todatenew = $expdate1[2]."-".$expdate1[1]."-".$expdate1[0]; 

if ($chk_reconcile == '') $chk_reconcile = 0;

if ($mode == 'updateReconcile' && $reconciledt_id != '' ){
	$queryup = "update temp_reconciledt set Journaldt_reconciled = $chk_reconcile where reconciledt_id = $reconciledt_id ";
	$resup = mysql_query($queryup, $connection);
	
	$query = "update temp_reconcile set amountToCheck = $amntchk where reconcile_id = $reconcile_id ";
	$res = mysql_query($query, $connection);
	// we can get the Valances in a query and return using BRK and assign them in Javascript JP
	exit;
}
if ($mode == 'search' && $reconcile_id == ''){
	$insert = "insert into temp_reconcile (fromdate, todate, ledger_number,ledger_number2,  amountToCheck, currencycode) 
				values 
			  ('$fromdatenew','$todatenew', $fromledger, $toledger, $amntchk, '$currency')";
	$result = mysql_query($insert, $connection);		  
	$reconcile_id = mysql_insert_id();
	if ($reconcile_id != ''){
		$insertdt = "insert into temp_reconciledt(reconcile_id, Ledger_number, Journaldt_desc, Journaldt_desc1, Currency_code, Journaldt_dbcd, Journaldt_dbcdAra, Journaldt_amount, Journaldt_amount1,
					Journaldt_amount2, Journaldt_ccenter, Journal_date, Journaldt_ValueDate, Document, BankTrans_Serial, Journaldt_posted, Journaldt_reconciled,
					Journaldt_reconciledref, Journal_BelongsToGroup, Synchronized, Journaldt_JVMode, Journal_JVRef, Reconciliation_date, JournalDt_IsVat,
					Reconciliation_Sysdate, Manual_no, Detail_Code, user_account, JournalDt_OldData, journaldt_rate2, journaldt_rate, journaldt_id)
					select $reconcile_id, Ledger_number, Journaldt_desc, Journaldt_desc1, Currency_code, Journaldt_dbcd, Journaldt_dbcdAra, Journaldt_amount, Journaldt_amount1,
					Journaldt_amount2, Journaldt_ccenter, Journal_date, Journaldt_ValueDate, Document, BankTrans_Serial, Journaldt_posted, Journaldt_reconciled,
					Journaldt_reconciledref, Journal_BelongsToGroup, Synchronized, Journaldt_JVMode, Journal_JVRef, Reconciliation_date, JournalDt_IsVat,
					Reconciliation_Sysdate, Manual_no, Detail_Code, user_account, JournalDt_OldData, journaldt_rate2, journaldt_rate, journaldt_id 
					from journaldt
					where journaldt.ledger_number BETWEEN ifnull($fromledger,journaldt.ledger_number) AND ifnull($toledger,journaldt.ledger_number)
					AND date_format(journaldt.journal_date,'%Y-%m-%d') BETWEEN '$fromdatenew' AND '$todatenew' 
					order by journaldt.Journal_date, Journaldt_id";
		$result = 	mysql_query($insertdt, $connection);		
					
	}
}
else if ($mode == 'search' && $reconcile_id != ''){
	$update = "update temp_reconcile set fromdate = '$fromdatenew', todate = '$todatenew', ledger_number = $fromledger, ledger_number2 = $toledger,  amountToCheck = $amntchk, currencycode = '$currency'
			  where reconcile_id = $reconcile_id ";
	$result = mysql_query($update, $connection);	
	
	if ($result){
		$del = "delete from temp_reconciledt where reconcile_id = $reconcile_id ";
		$resultdel = mysql_query($del, $connection);
		$insertdt = "insert into temp_reconciledt(reconcile_id, Ledger_number, Journaldt_desc, Journaldt_desc1, Currency_code, Journaldt_dbcd, Journaldt_dbcdAra, Journaldt_amount, Journaldt_amount1,
						Journaldt_amount2, Journaldt_ccenter, Journal_date, Journaldt_ValueDate, Document, BankTrans_Serial, Journaldt_posted, Journaldt_reconciled,
						Journaldt_reconciledref, Journal_BelongsToGroup, Synchronized, Journaldt_JVMode, Journal_JVRef, Reconciliation_date, JournalDt_IsVat,
						Reconciliation_Sysdate, Manual_no, Detail_Code, user_account, JournalDt_OldData, journaldt_rate2, journaldt_rate, journaldt_id)
						select $reconcile_id, Ledger_number, Journaldt_desc, Journaldt_desc1, Currency_code, Journaldt_dbcd, Journaldt_dbcdAra, Journaldt_amount, Journaldt_amount1,
						Journaldt_amount2, Journaldt_ccenter, Journal_date, Journaldt_ValueDate, Document, BankTrans_Serial, Journaldt_posted, Journaldt_reconciled,
						Journaldt_reconciledref, Journal_BelongsToGroup, Synchronized, Journaldt_JVMode, Journal_JVRef, Reconciliation_date, JournalDt_IsVat,
						Reconciliation_Sysdate, Manual_no, Detail_Code, user_account, JournalDt_OldData, journaldt_rate2, journaldt_rate, journaldt_id 
						from journaldt
						where journaldt.ledger_number BETWEEN ifnull($fromledger,journaldt.ledger_number) AND ifnull($toledger,journaldt.ledger_number)
						AND date_format(journaldt.journal_date,'%Y-%m-%d') BETWEEN '$fromdatenew' AND '$todatenew' 
						order by journaldt.Journal_date, Journaldt_id";//echo $insertdt;exit;
		$result = 	mysql_query($insertdt, $connection);	
	}
}
else if($mode == 'deletepending' && $reconcile_id !=''){
	$del = "delete from temp_reconcile where reconcile_id = $reconcile_id ";
	$resultdel = mysql_query($del, $connection);
	if($resultdel){
		$del = "delete from temp_reconciledt where reconcile_id = $reconcile_id ";
		$resultdel = mysql_query($del, $connection);
	}
	header("Location:./Form_ReconcilationPending.php");
	exit;
}
else if($mode =='saveResult' && $reconcile_id !=''){
	$message = "";
	
	if ($reconcilation_AmountChecked == 1 && $amntchk != '' && $amntchk != 0){
		$totalbalance = $amntchk + $TotalDebit - $TotalCredit - $tbalance;
		if ($totalbalance != 0){
			$message = "Save Not Allowed! Please Check Statement Ending Balance!";
			$mode = 'search';
		}
	}
	
	if ($message == ''){
		$queryall = "select journaldt_id, ifnull(Journaldt_reconciled,0) from temp_reconciledt where reconcile_id = $reconcile_id ";
		$resall = mysql_query($queryall, $connection);
		while ($rwall = mysql_fetch_array($resall)){
			$queryup = "update journaldt set Journaldt_reconciled = '".$rwall[1]."' where Journaldt_id = '$rwall[0]';";
					//echo $queryup;
			$resup = mysql_query($queryup, $connection);
		}
		
		$del = "delete from temp_reconcile where reconcile_id = $reconcile_id ";
		$resultdel = mysql_query($del, $connection);
		if($resultdel){
			$del = "delete from temp_reconciledt where reconcile_id = $reconcile_id ";
			$resultdel = mysql_query($del, $connection);
		}
		$reconcile_id = '';
		$mode = '';
	}
}
echo "<form id='formaction' name='formaction' method='post' action=''>
	<input type='hidden' name='mode' id='mode' value='$mode'>
	<input type='hidden' name='reconcile_id' id='reconcile_id' value='$reconcile_id'>
	<input type='hidden' name='concail' id='concail' value='$concail'>
	<input type='hidden' name='txtmanualno' id='txtmanualno' value='$txtmanualno'>
	<input type='hidden' name='message' id='message' value='$message'>	";

echo "<script> ";
echo " document.formaction.action='reconcilation.php'; ";
echo " document.formaction.submit(); ";
echo " </script>";
exit;	 
	
	


?>