<?php


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

include("config.inc.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");



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

$querylog="SELECT RA_StatementofAccount as loginaccess , ExporttoExcel from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')	";
	$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultlog)){
		extract($rows);
	}
	
if ($loginaccess=='' or $loginaccess==0)
	header("location:menu.php");
	
if ($ExporttoExcel==1)
	$ExporttoExcelStatus = 'button';
else
	$ExporttoExcelStatus = 'hidden';


	
$querylog="select * from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."'";
$resultlog=mysql_query($querylog) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
extract($rows);

}
$printaccess=$Print;

print <<< HERE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

HERE;

$querylog=" select ifNull(FI_RepRestrictProject,0) As FI_RepRestrictProject, ifNull(FI_RepRestrictCostcenter,0) As FI_RepRestrictCostcenter ".
          " from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."') ";
$resultlog=mysql_query($querylog) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_Project,0) As Module_Project, ifNull(Module_Costcenter,0) As Module_CostCenter, ifNull(module_ReferenceByCostcenter,0) As module_ReferenceByCostcenter ".
             " ,IfNull(Module_RestrictProjectReport,0) As Module_RestrictProjectReport".
			 " From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

$querysetup ="select base1, base2 , decimalbase1 , 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];

// Setting Initial Values For Parameters
$querydate=" Select Date_Format(Min(Journal_date),'%d/%m/%Y') As DefaultFromDate, ".
           " Date_Format(Max(Journal_date),'%d/%m/%Y') As DefaultToDate ".
		   " From JournalDt ";
$resultdate=mysql_query($querydate,$connection) or die(mysql_error());
$rowdate=mysql_fetch_row($resultdate);

if ($fromdate == '') $fromdate = $rowdate[0];
if ($todate == '')   $todate = $rowdate[1];


$querycurrency="select Currency_code from currency order by currency_code";
$resultcurrency=mysql_query($querycurrency,$connection) or die(mysql_error());

$queryjvtype="select type_name from jourtype order by type_name";
$resultjvtype=mysql_query($queryjvtype,$connection) or die(mysql_error());


$querydflproject="select project_code as defaultproject from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."' and default_project=1 ";
//echo$querydflproject;
$resultdflproject=mysql_query($querydflproject,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflproject)){
	extract($rows);
}
$querydflcostcent="select costcent_code as defaultcostcent from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."' and default_costcenter=1 ";
$resultdflcostcent=mysql_query($querydflcostcent,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflcostcent)){
	extract($rows);
}

if ((empty($fromproject)) && $Module_Project == 1  && $Module_RestrictProjectReport == 1 )
	$fromproject = $defaultproject;
if ((empty($fromcostcent)) && $Module_CostCenter == 1)
	$fromcostcent = $defaultcostcent;	

$query="select ifnull(ShowReportHeader,0) as ShowReportHeader from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."' ";
$result=mysql_query($query,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($result)){
	extract($rows);
}
if($ShowReportHeader=='')
	$ShowReportHeader=1;
	
print <<< HERE

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>
<script language="JavaScript" src="CalendarPopup.js" ></script><!-- Date only with year scrolling -->

<script type="text/javascript" src="js/grid/UI_DataGrid.js"></script>
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>


<link type="text/css" href="js/calendar/jquery.datepick.css" rel="stylesheet">
<script type="text/javascript" src="js/calendar/jquery.datepick.js"></script>

<link type="text/css" href="js/grid/scripts/jquery-ui.css" rel="stylesheet">
<script type="text/javascript" src="js/grid/scripts/jquery-ui.min.js"></script>



<script type="text/javascript" language="javascript">

function cgr7(){
x=document.form1;
//code1a=x.code1a.value;
document.form1.action="select_item.php?item=1";
document.form1.btncode1.submit();

}

function cgr8(){
x=document.form1;
//code2a=x.code2a.value;
document.form1.action="select_item.php?item=2";
document.form1.btncode2.submit();

}

all = 0;  // 0 means 'no', 1 means 'yes'


function showresult(){
	var x = document.form1;
	if(x.fromledger.value==''&&x.toledger.value==''){
		alert("Please select the account number!");
		return 0;
		
	}
	x.action='reconcilation_action.php?mode=search';
	x.submit();
}

function saveresult(){
	var x = document.form1;
	
	x.action='reconcilation_action.php?mode=saveResult';
	x.submit();
}



function kdClient(choice,e,app)
{   
	 x = document.form1;
	if(app==0){
		var clientcode = x.fromledger.value;
		if(clientcode=='')
		var clientcode = x.fromledgername.value;
	} else {
		var clientcode = x.toledger.value;
		if(clientcode=='')
		var clientcode = x.toledgername.value;
	}	
    var intKey = (window.Event) ? e.which : e.keyCode;
	
		
    if (intKey == 13) { //enter key
	
	
		var xmlhttp = getXMLHttpRequest();	
		
		
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				var data = xmlhttp.responseText.split("[BRK]");
				
					if(app==0){
						if(data[0]!=''){
								x.fromledger.value = data[0];
								x.fromledgername.value = data[1];
									if(x.toledger.value==''){
									x.toledger.value = data[0];
									x.toledgername.value = data[1];
									
									}
						}
						else showlistledger(1);
					} else {
						if(data[0]!=''){
								x.toledger.value = data[0];
								x.toledgername.value = data[1];
							//if(x.toledgername.value!=''){
							//	x.submit();	
							//} 
						}
						else showlistledger(2);						
					}
			}
		}
	    
		xmlhttp.open("GET","Form_Statementofaccount_Action.php?mode=getclientname&clientcode="+clientcode+"&choice="+choice);
		xmlhttp.send();
	
      
			
			
    }
	

}



function showlistledger(str){

	if(str==1){
		varledger = document.form1.fromledger.value;
		varledgername = document.form1.fromledgername.value;
	} else {
		varledger   = document.form1.toledger.value;
		varledgername = document.form1.toledgername.value;
	}

  var str1, selectpage;
  for (var i=0; i < document.form1.ledgeroption.length; i++)
  {
    if (document.form1.ledgeroption[i].checked)
    {
      str1 = document.form1.ledgeroption[i].value;
	  if (str1 == 1)
	    selectpage = 'select_ledger.php?item='+str+'&PLedger_Number='+varledger+'&PLedger_Name='+varledgername;
	  else
        selectpage = 'select_document.php?item='+str;	  
    }
  }

  
  window.open(selectpage,'mywindow','width=800,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showPending(){
	win = window.open('Form_ReconcilationPending.php', 'Form_ReconcilationPending', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=950,height=560,top=100');		
	win.creator = self;
	win.focus();	
}
function clearpage(){
	var x = document.form1;
	x.mode.value='';
	if(x.amntchk)
		x.amntchk.value='';
	x.fromledger.value='';
	x.toledger.value='';
	x.fromledgername.value='';
	x.toledgername.value='';
	x.fromdate.value='';
	x.todate.value='';
	x.concail.value='';
	x.currency.value='';
	x.txtmanualno.value='';
	x.reconcile_id.value='';
	x.action = 'reconcilation.php';
	x.submit();
}


function printPrev(printaccess){
	
	var fromledger=document.form1.fromledger.value;
	var toledger=document.form1.toledger.value;
	var fromledgername=document.form1.fromledgername.value;
	var toledgername=document.form1.toledgername.value;
	var fromdate=document.form1.fromdate.value;
	var todate=document.form1.todate.value;
	var concail=document.form1.concail.value;
	var currency=document.form1.currency.value;
	var txtmanualno = document.getElementById("txtmanualno").value;
	var reconcile_id = document.form1.reconcile_id.value;
	str='';
		   
    window.open('Rep_reconcilation.php?&recid="+reconcile_id+"&fromledger='+fromledger+'&toledger='+toledger+'&fromledgername='+fromledgername
           +'&toledgername='+toledgername+'&fromdate='+fromdate+'&todate='+todate+'&currency='+currency+'&txtmanualno='+txtmanualno+'&concail='+concail);
		   
}
function showManual(){
	if(document.getElementById('txtmanualno').checked)
		document.getElementById('txtmanualno').value=1;
	else document.getElementById('txtmanualno').value=0;
	document.form1.submit();
}
function refreshPage(){
	document.form1.submit();
}
function updateChk(id){
	var reconcileval = 'reconcileval'+id;
	var chk_reconcile = 0;
	var amntchk 	 = document.form1.amntchk.value;
	var reconcile_id = document.form1.reconcile_id.value;
	
	if(document.getElementById(reconcileval).checked){
		document.getElementById(reconcileval).value=1;
		chk_reconcile = 1;
	}
	else {
		document.getElementById(reconcileval).value=0;
		chk_reconcile = 0;
	}
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			var data = xmlhttp.responseText.split("[BRK]");
			document.form1.submit();
			// document.getElementById('reconcileval'+id).focus();
		}
	  }

  
	xmlhttp.open("GET","reconcilation_action.php?mode=updateReconcile&reconciledt_id="+id+"&chk_reconcile="+chk_reconcile+"&amntchk="+amntchk+"&reconcile_id="+reconcile_id,true);
	xmlhttp.send();
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Reconcilation</title>
<style type="text/css">
<!--
body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 14px;
}
a:link {
	text-decoration: none;
}
a:visited {
	text-decoration: none;
}
a:hover {
	text-decoration: none;
}
a:active {
	text-decoration: none;
}
.style1 {
	color: #FF0000;
	font-family: "Times New Roman", Times, serif;
	font-size: 20px;
}
.style8 {font-size: 11px; color: #999999; font-weight: bold; }
.style11 {font-size: 11px}
body {
	margin-top: 0px;
}

-->
</style>

<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
	<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>
</head>
<body>
HERE;

echo "<form id='form1' name='form1' method='post' action=''>
	<input type='hidden' name='mode' id='mode' value='$mode' >
	<input type='hidden' name='reconcile_id' id='reconcile_id' value='$reconcile_id' >";

echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' class='layout-width'>";
include_once("header.php");
echo" <tr><td  valign='top'>";

include_once("sidemenu.php");
echo"  </td></tr>";


echo" <tr style='height:26px;'>
		<td  valign='top' id='content-top'>
			<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed; height:26px;' class='layout-width'>
			<tr style='height:26px;'>
				<td style='width:38px; height:26px;'>&nbsp;</td>
				<td valign='middle' style='width:300px; height:26px;'><span class='title'>Reconcilation</span></td>
				<td class='message' valign='middle' style='height:26px;'>".htmlspecialchars($message, ENT_QUOTES)."</td>
			</tr></table>
		</td>
	  </tr>";
	  
echo"<tr>
		<td  height='34'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width'>
			<tr>
				<td width='30'  height='34'>&nbsp;</td>
				<td valign='middle' height='34' width='720px' align='left'>";
		
	//echo" <button type='button' id='btnClear' name='btnClear' title='Clear' onclick=\"clearpage();\"><span><span>Clear</span></span></button>";
	if($reconcile_id != '')
	echo"<button type='button' id='btnSave' name='btnSave'  onClick=\"saveresult()\"; title='Save'><span><span>Save</span></span></button>";
	echo"<button type='button' id='btnSearch' name='btnSearch'  onClick=\"showresult('".$printaccess."')\"; title='Search'><span><span>Search</span></span></button>";
	if($reconcile_id == '')
	echo" <button type='button' id='btnPending' name='btnPending' title='Pending' onclick=\"showPending();\"><span><span>Pending</span></span></button>";
	echo"<button type='button' id='btnPreview' name='btnPreview'  onClick=\"printPrev('".$printaccess."')\"; title='Preview'><span><span>Preview</span></span></button>";
  
echo"	</td></tr></table>";
echo"</td></tr>";				

		
echo"<tr><td id='content-body'>
<table border='0'  height='534' align='center' valign='top' width=100% class='tablecontent'>
   <tr bgcolor='#F7F7F7'><td valign='top' align='left' colspan='10'>";		
   
echo '  <table border="0" align="center" width="100%">';
//title date
echo" <tr>";	  
echo"   <td width='20%' height='19' colspan='2' align='left' bgcolor='#F5F5F5'><span class='style8'>";
echo"      <label><input name='date' type='radio' value='1' checked='checked'>Date</label>&nbsp;&nbsp;&nbsp;";
echo"   </td>";
echo "<td colspan='2'></td>";
//should be check to get the method ledger number
echo"   <td colspan='3' bgcolor='#F5F5F5'><span class='style8'>Ledger No. / Name ";
echo"   <label><input name='ledgeroption' type='radio' value='1' checked='checked'> Ledger No. </label>";
echo"   <label hidden><input name='ledgeroption' type='radio' value='2' hidden> Document </label></span>
		</td>";
echo" </tr>";

echo" <tr>";
echo "<td colspan=6></td>";

echo" </tr>";


if ($reconcile_id != ''){
	$query = "select date_format(fromdate,'%d/%m/%Y'), date_format(todate,'%d/%m/%Y'), temp_reconcile.ledger_number, l1.ledger_name,
			temp_reconcile.ledger_number2, l2.ledger_name, amountToCheck, currencycode
			from temp_reconcile
			left join ledger l1 on l1.ledger_number = temp_reconcile.ledger_number
			left join ledger l2 on l2.ledger_number = temp_reconcile.ledger_number2
			where reconcile_id = $reconcile_id ";//echo $query;
	$result = mysql_query($query, $connection);
	$row = mysql_fetch_row($result);	
	$fromdate = $row[0];
	$todate = $row[1];
	$fromledger = $row[2];
	$fromledgername = $row[3];
	$toledger = $row[4];
	$toledgername = $row[5];
	$amntchk = $row[6];
	$currency = $row[7];
}
//from date
echo" <tr height='15px'>";	  
echo"   <td width='5%'  valign='middle' bgcolor='#F5F5F5' ><span class='style8'>From</span></td>";
echo"   <td width='15%'  valign='middle' bgcolor='#F5F5F5' ><span class='style8'>";
echo"      <input type='text' name='fromdate'  id='fromdate' value='$fromdate' maxlength='10' size='10'>";
echo"      <A HREF=\"#\" onClick=\"displayCalendar(document.form1.fromdate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\">
			<image src='./img/cal.gif' border=0 align=center></image></A></span></td>";
//todate			
echo"   <td width='5%' valign='top' bgcolor='#F5F5F5'><span class='style8'>To</span></td>";
echo"   <td width='15%' valign='top' bgcolor='#F5F5F5'><span class='style8'>";
echo"      <input type='text' name='todate'  id='todate' value='$todate'  maxlength='10' size='10'>";
echo"      <A HREF=\"#\" onClick=\"displayCalendar(document.form1.todate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor1\" ID=\"anchor1\">
			<image src='./img/cal.gif' border=0 align=center></image></A></span></td>";
		
//from ledger			
echo"   <td width='5%'  align='left' bgcolor='#F5F5F5' ><span class='style8'>From</span></td>";
echo"   <td  colspan='3' valign='middle' bgcolor='#F5F5F5' ><span class='style8'>";
echo"   <input name='fromledger' type='text' id='fromledger' size='10' value='".$fromledger."'    onkeydown='return kdClient(0,event,0);'  />
        <input name='fromledgername' type='text' id='fromledgername' size='30' value='".$fromledgername."' onkeydown='return kdClient(1,event,0);' />
        <input name='btncode1' type='hidden' id='btncode1' value='...' onclick='cgr7()' style='width:20px; height:20px; border:thin;' title='Select Ledger From' />
		</span>
		<label><span class='style11'><a href='javascript:void(0);' NAME='My Window Name' title=' Select Ledger From ' 
		onClick='showlistledger(1);'><img src='img/btnopen.jpg' width='25' height='20' border='0' /></a> </span></label>";

//dropdown list show
															// -- Rirchard 7/5/1016 --
		echo "	<td> <select name='currency' id='currency'>";
				echo " <option value='$base1' ";
				if($currency == $base1 ) echo " selected ";
				echo " > $base1 </option>";
				
				echo "<option value='$base2' ";
				if($currency == $base2 ) echo " selected ";
				echo" > $base2 </option>
				</select>
				</td>
				
		<td >
				<select name='concail' id='concail' onchange=\"refreshPage();\">";    // -- Rirchard 7/5/1016 --
					echo "  <option value='' ";
					if($concail == '' ) echo " selected "; 
					echo">All</option>";
					
					echo " <option value='1' ";
					if($concail == '1' ) echo "selected "; 
					echo " >Reconciled</option>";
					
					echo "<option value='0' ";
					if($concail == '0' ) echo  " selected "; 
					echo ">Un-Reconciled</option>
										</select>
				</td>";

echo "</tr>";
//to date

echo" <tr height='15px' >";

echo"   <td width='5%'  align='left' bgcolor='#F5F5F5' colspan='2'></td>";
echo"   <td   colspan='2' valign='middle' bgcolor='#F5F5F5' ></td>";	
echo"   <td width='5%'  align='left' bgcolor='#F5F5F5' ><span class='style8'>To</span></td>";
echo"   <td   colspan='3' valign='middle' bgcolor='#F5F5F5' ><span class='style8'>";
echo"   <input name='toledger' type='text' id='toledger' size='10' value='".$toledger."'   onkeydown='return kdClient(0,event,1);' />
        <input name='toledgername' type='text' id='toledgername' size='30' value='".$toledgername."'     onkeydown='return kdClient(1,event,1);' />
        <input name='btncode2' type='hidden' id='btncode2' value='...' onclick='cgr8()' style='width:20px; height:20px; border:thin;' title='Select Ledger To' />
      </span>
      <label><span class='style11'><a href='javascript:void(0);' NAME='My Window Name' title=' Select Ledger To ' 
		onClick='showlistledger(2); '><img src='img/btnopen.jpg' width='25' height='20' border='0' /></a>
		</span></label>";	
if($txtmanualno == '') $txtmanualno = 0;		
echo "<td colspan='2'><span class='style8'><input type='checkbox' name='txtmanualno' id='txtmanualno' value='$txtmanualno' ".($txtmanualno == '1'?'checked':'')." onclick=\"showManual();\">Show Manual No.</span></td>";	
echo" </tr>";

echo" <tr height='30px' >";
echo"   <td width='5%'  align='left' bgcolor='#F5F5F5' ></td>";
echo"   <td   colspan='3' valign='middle' bgcolor='#F5F5F5' ></td>";		
echo" </tr>";

$expdateX = explode("/",$fromdate);
$fromdatenewX = $expdateX[2]."-".$expdateX[1]."-".$expdateX[0]; 
// echo"<BR>".
// querying prev Balance
$qrPrvBal ="SELECT
			ifnull(sum( IF ( journaldt_dbcd = 'D', if('$currency' = '$base1' , Journaldt_amount1 , Journaldt_amount2), - 1 * if('$currency' = '$base1' , Journaldt_amount1 , Journaldt_amount2) ) ) ,0) 
		FROM
			journaldt 
		WHERE
			ledger_number = '$fromledger'
			and Journal_date <= concat(DATE_ADD('$fromdatenewX' ,INTERVAL -1 DAY) , ' 23:59:59')
			and currency_code = '$currency'";
$respvBal = mysql_query($qrPrvBal,$connection);
$rowPrvBal = mysql_fetch_array($respvBal);
$prvBalance = $rowPrvBal[0];
if($prvBalance != 0){
	echo "<tr>";
		echo"<td colspan='4'><span class='style8'>Previous Balance : </span>". number_format($prvBalance,$DecimalBase)."<td>";
	echo"</tr>";
}
				
echo "<tr>";

//table result

  
if($mode=='search'){
$treconcail='';
$tunreconcail='';
$i=0;

//Table search result
echo '<table class="scroll" border=1 width=1100px style="border:0 solid black;">';
	echo "		<col width=90>
				<col width=80>
				<col width=250>
				<col width=80>
				<col width=120>
				<col width=120>
				<col width=120>";
	if($txtmanualno == 1) echo "<col width=80>";
	echo "<col width=80>
			<tr stle=' background-color:darkgray;'>
				<th style='font-family:cursive;'>Date</th>
				<th style='font-family:cursive;'>Ref</th>
				<th style='font-family:cursive;'>Description</th>
				<th style='font-family:cursive;'>Curr</th>
				<th style='font-family:cursive;'>Debit</th>
				<th style='font-family:cursive;'>Credit</th>
				<th style='font-family:cursive;'>Balance</th>";
	if($txtmanualno == 1) 
		echo "<th style='font-family:cursive;'>Manual No</th>";
	echo "<th style='font-family:cursive;'>Check</th>
				</tr>
			</thead>";
			
	
		
$expdate = explode("/",$fromdate);
$expdate1 = explode("/",$todate);

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

		$queryall="	select temp_reconciledt.journaldt_id,temp_reconciledt.Ledger_number,date_format(temp_reconciledt.Journal_date,'%d-%m-%Y'),a.Journal_refgovernment ,temp_reconciledt.Journaldt_ValueDate,
						temp_reconciledt.Journaldt_desc,temp_reconciledt.Journaldt_dbcd, temp_reconciledt.Currency_code,temp_reconciledt.Journaldt_amount,temp_reconciledt.Journaldt_reconciled, temp_reconciledt.manual_no,
						reconciledt_id
						from temp_reconciledt
						inner join journaldt on journaldt.journaldt_id = temp_reconciledt.journaldt_id
						left join journal a on a.journal_id=journaldt.journal_id
						where if('$concail'='',1=1, temp_reconciledt.Journaldt_reconciled ='$concail') 
						AND temp_reconciledt.ledger_number BETWEEN if('$fromledger' = '',temp_reconciledt.ledger_number,'$fromledger') AND if('$toledger'='',temp_reconciledt.ledger_number,'$toledger') 
						AND temp_reconciledt.Currency_code ='$currency' AND date_format(temp_reconciledt.journal_date,'%Y-%m-%d') BETWEEN '$fromdatenew' AND '$todatenew' 
						order by temp_reconciledt.Journal_date, temp_reconciledt.journaldt_id ";
						
	//echo $queryall;
			
			$queryreconcail="select temp_reconciledt.journaldt_id,temp_reconciledt.Ledger_number,date_format(temp_reconciledt.Journal_date,'%d-%m-%Y'),a.Journal_refgovernment ,temp_reconciledt.Journaldt_ValueDate,
						temp_reconciledt.Journaldt_desc,temp_reconciledt.Journaldt_dbcd, temp_reconciledt.Currency_code, temp_reconciledt.Journaldt_amount,temp_reconciledt.Journaldt_reconciled, temp_reconciledt.manual_no,
						reconciledt_id
						from temp_reconciledt
						inner join journaldt on journaldt.journaldt_id = temp_reconciledt.journaldt_id
						left join journal a on a.journal_id=journaldt.journal_id
						where temp_reconciledt.Journaldt_reconciled='1'
						AND temp_reconciledt.ledger_number BETWEEN if('$fromledger' = '',temp_reconciledt.ledger_number,'$fromledger') AND if('$toledger'='',temp_reconciledt.ledger_number,'$toledger') 
						AND temp_reconciledt.Currency_code ='$currency' AND date_format(temp_reconciledt.journal_date,'%Y-%m-%d') BETWEEN '$fromdatenew' AND '$todatenew' 
						order by temp_reconciledt.Journal_date, temp_reconciledt.journaldt_id ";
					
		//echo $queryreconcail;
		
						
			$queryunreconcail="select temp_reconciledt.journaldt_id,temp_reconciledt.Ledger_number,date_format(temp_reconciledt.Journal_date,'%d-%m-%Y'),a.Journal_refgovernment ,temp_reconciledt.Journaldt_ValueDate,
								temp_reconciledt.Journaldt_desc,temp_reconciledt.Journaldt_dbcd, temp_reconciledt.Currency_code,temp_reconciledt.Journaldt_amount,temp_reconciledt.Journaldt_reconciled, temp_reconciledt.manual_no,
								reconciledt_id
								from temp_reconciledt
								inner join journaldt on journaldt.journaldt_id = temp_reconciledt.journaldt_id
								left join journal a on a.journal_id=journaldt.journal_id
								where temp_reconciledt.Journaldt_reconciled='0'
								AND temp_reconciledt.ledger_number BETWEEN if('$fromledger' = '',temp_reconciledt.ledger_number,'$fromledger') AND if('$toledger'='',temp_reconciledt.ledger_number,'$toledger') 
								AND temp_reconciledt.Currency_code ='$currency' AND date_format(temp_reconciledt.journal_date,'%Y-%m-%d') BETWEEN '$fromdatenew' AND '$todatenew' 
								order by temp_reconciledt.Journal_date, temp_reconciledt.journaldt_id ";
			
		
			//echo $queryunreconcail;
			$result=mysql_query($queryunreconcail,$connection) or die(mysql_error());
			$num_rowss = mysql_num_rows($result);
			$tunreconcail=$num_rowss;
			
			$result2=mysql_query($queryreconcail,$connection) or die(mysql_error());
			$num_rows = mysql_num_rows($result2);
			$treconcail=$num_rows;	

			$result=mysql_query($queryall,$connection) or die(mysql_error());
			$debit=0;
			$credit=0;
			$balance=0;
			$balance2=0;
			$bal2=0;
			$bal=0;
			$counter=0;
			$unrec=0;
			$unrec2=0;
			// setting Previous Balance
			$balance2 = $prvBalance;
			while($row = mysql_fetch_row($result)){
			$journaldt_id = $row[0];
			$ledger_number = $row[1];;
			$journal_date = $row[2];
			$journal_date=explode(" ", $journal_date);
			$Journaldt_reconciledref=$row[3];
			$Journaldt_ValueDate=$row[4];
			$Journaldt_ValueDate=explode(" ", $Journaldt_ValueDate);
			$Journaldt_desc=$row[5];
			$journaldt_dbcd=$row[6];
			$Currency_code=$row[7]; 
			$Journaldt_amount = $row[8];
			$Journaldt_reconciled = $row[9];
			$Journaldt_maunalno = $row[10];
			$reconciledt_id = $row[11];
			
			if($Currency_code == $base2) {
				$DecimalBase = $DecimalBase2;
			}
			
			echo"	<tr >";
			echo"		<td style='font-family:monospace; font-size:13px;'>".$journal_date[0]."</td>";
			echo"		<td style='font-family:monospace; font-size:13px;'>".$Journaldt_reconciledref."</td>";
			//echo"		<td style='font-family:monospace; font-size:15px;'>".$Journaldt_ValueDate[0]."</td>";
			echo"		<td style='font-family:monospace; font-size:13px;'>".$Journaldt_desc."</td>";
			echo"		<td style='font-family:monospace; font-size:13px;'>".$Currency_code."</td>";
				if($journaldt_dbcd == 'D'){
			echo"		<td style='font-family:monospace; font-size:13px;'>". number_format($Journaldt_amount,$DecimalBase)."</td>";
				$debit=$Journaldt_amount;
			echo"		<td style='font-family:monospace; font-size:13px;'></td>";
				}
				if($journaldt_dbcd == 'C'){
					 echo"		<td style='font-family:monospace; font-size:13px;'></td>";
					 $Credit=$Journaldt_amount;
					 echo"		<td style='font-family:monospace; font-size:13px;'>".number_format($Journaldt_amount,$DecimalBase)."</td>";
				}
				if($journaldt_dbcd ==''){
					 echo"		<td style='font-family:monospace; font-size:13px;'></td>";
					 echo"		<td style='font-family:monospace; font-size:13px;'></td>";
				}
			
				$balance=$balance2 + ($debit-$Credit);
				
				$balance2 = $balance;
				$debit = 0;
				$Credit = 0;
				
				if($balance<0){
					$balance_format=abs($balance);
			echo "<td style='font-family:monospace; font-size:13px;'>(".number_format($balance_format,$DecimalBase).")</td>";
				}
				else{
			echo"		<td style='font-family:monospace; font-size:13px;'>".number_format($balance,$DecimalBase)."</td>";
				}
				
			
			$reconcileval = 'reconcileval'.$reconciledt_id;
			if($$reconcileval == '') $$reconcileval = 0;
			
			if($Journaldt_reconciled == 1) {
				$chktrue = ' checked ';
				$$reconcileval = 1;
			}
			else {
				$chktrue = ' '; 
				$$reconcileval = 0;
			}
			
			if($Journaldt_reconciled == 1){
				$counter++;
				if($journaldt_dbcd == 'D'  ){
			
				$debit=$Journaldt_amount;
	
				}
				if($journaldt_dbcd == 'C' ){
					
					 $Credit=$Journaldt_amount;	 
				}
					
				$bal=$bal2 + ($debit-$Credit);
				
				$bal2 = $bal;
				$debit = 0;
				$Credit = 0;
				}	
			else {
				if($journaldt_dbcd == 'D' ){
			
				$debit=$Journaldt_amount;
	
				}
				if($journaldt_dbcd == 'C' ){
					
					 $Credit=$Journaldt_amount;	 
				}
					
				$unrec=$unrec2 + ($debit-$Credit);
				
				$unrec2 = $unrec;
				$debit = 0;
				$Credit = 0;
				
			}
			
			if($txtmanualno == 1)
			echo "		<td style='font-family:monospace; font-size:13px;'>$Journaldt_maunalno</td>";
			echo "		<td style='text-align:center'><input type='checkbox' id='$reconcileval' name='$reconcileval' value='".$$reconcileval."' onclick=\"updateChk('$reconciledt_id')\" width=5px height=50px $chktrue></td>
					</tr>";
					//	$treconcail=$treconcail+1;
						$tamount=$balance2;
						$trec=$bal2;
						$tunrec=$unrec2;
						$i++;
				
					
			}
			echo "</tr>";




      
			
		
		
		if($txtmanualno == 1) $colspan = 9;
		else $colspan = 8;
		echo "<tr><td colspan=$colspan><div>";	
		$queryNon=" select sum(if(temp_reconciledt.journaldt_dbcd='D',temp_reconciledt.Journaldt_amount,0)) as TotalDebit,sum(if(temp_reconciledt.journaldt_dbcd='C',temp_reconciledt.Journaldt_amount,0)) as TotalCredit 
					from temp_reconciledt where temp_reconciledt.Journaldt_reconciled='0'
					AND temp_reconciledt.ledger_number BETWEEN if('$fromledger' = '',temp_reconciledt.ledger_number,'$fromledger') AND if('$toledger'='',temp_reconciledt.ledger_number,'$toledger') 
					AND temp_reconciledt.Currency_code ='$currency' AND date_format(temp_reconciledt.journal_date,'%Y-%m-%d') BETWEEN '$fromdatenew' AND '$todatenew'   "; 
			
		//echo $queryNon;exit; 
		$resultNon=mysql_query($queryNon,$connection) or die(mysql_error());
		while($rowsNon=mysql_fetch_array($resultNon)){
		extract($rowsNon);
		}		

		$queryBalance=" select sum(if(temp_reconciledt.journaldt_dbcd='D',temp_reconciledt.Journaldt_amount,0)) as TotalD,sum(if(temp_reconciledt.journaldt_dbcd='C',temp_reconciledt.Journaldt_amount,0)) as TotalC 
					from temp_reconciledt 
					where temp_reconciledt.ledger_number BETWEEN if('$fromledger' = '',temp_reconciledt.ledger_number,'$fromledger') AND if('$toledger'='',temp_reconciledt.ledger_number,'$toledger') 
					AND temp_reconciledt.Currency_code ='$currency' AND date_format(temp_reconciledt.journal_date,'%Y-%m-%d') BETWEEN '$fromdatenew' AND '$todatenew'   "; 
			
		//echo $queryNon;exit; 
		$resultBalance=mysql_query($queryBalance,$connection) or die(mysql_error());
		while($rowsBalance=mysql_fetch_array($resultBalance)){
		extract($rowsBalance);
		}	
		echo '<table class="scroll" border=0 width=1100px style="border:0 solid black;">';
		echo "	<col width=90>
				<col width=80>
				<col width=250>
				<col width=80>
				<col width=120>
				<col width=120>
				<col width=120>
				<col width=80>";
		if($txtmanualno == 1) echo "<col width=80>";		
		
		
		if($reconcilation_AmountChecked == 1){
		echo "<tr>
				<td align='left'><b>Difference = </td>
			<td align='left'><b></td>";
		} else {
			
			
		echo "<tr>
				<td align='left'><b></td>
			<td align='left'><b></td>";
			
			
		}	
			
		if($reconcilation_AmountChecked == 1){
			echo"   <td align='center' >Statement Ending Balance</td>";
		
		} else { 
			echo "<td align='center'>Statement Ending Balance =</td>";
			echo" <input type='hidden' name='amntchk' id='amntchk' value='' >";	
		}
	
		echo "<td align='left'></td>
				<td align='center'> + Deposit</td>
				<td align='center'> - Oustanding </td>
				<td align='center'> - TB Balance</td>
				<td align='left'></td>
			</tr>	";	
		echo "<tr>
		<td align='left'><b>".number_format($amntchk+$TotalDebit-$TotalCredit-($TotalD-$TotalC+$prvBalance),$DecimalBase)."</td>
		<td align='left'><b></td>";
		
		if($reconcilation_AmountChecked == 1){
			echo"   <td align='center'><input type='text' name='amntchk' id='amntchk' value='$amntchk' style='width:150px;'></td>";	
		} else {
			if (($TotalDebit-$TotalCredit-($TotalD-$TotalC+$prvBalance))>0)
			echo"<td align='center'><b>".number_format($TotalDebit-$TotalCredit-($TotalD-$TotalC+$prvBalance),$DecimalBase)."</b></td>";
			else echo"<td align='center'><b>(".number_format(abs($TotalDebit-$TotalCredit-($TotalD-$TotalC+$prvBalance)),$DecimalBase).")</b></td>";
			echo "<input type='hidden' name='endBal' value = '".round(abs($TotalDebit-$TotalCredit-($TotalD-$TotalC+$prvBalance)),$DecimalBase)."'>";
		}	
		echo"<td align='left'><b></td>
			<td align='center'><b>".number_format($TotalDebit,$DecimalBase)."<input type='hidden' name='TotalDebit' id='TotalDebit' value='".round($TotalDebit,$DecimalBase)."'></td>
			<td align='center'><b>(".number_format($TotalCredit,$DecimalBase).") <input type='hidden' name='TotalCredit' id='TotalCredit' value='".round($TotalCredit,$DecimalBase)."'></td>";
		
		if (($TotalD-$TotalC+$prvBalance)>0){
			echo "<td align='center'><b>".number_format($TotalD-$TotalC+$prvBalance,$DecimalBase)."</td>";
			$tbalance = $TotalD-$TotalC;
		}
		else {
			echo "<td align='center'><b>(".number_format(abs($TotalD-$TotalC+$prvBalance),$DecimalBase).")</td>";
			$tbalance = abs($TotalD-$TotalC);
		}
		$tbalance += $prvBalance;
		echo "<input type='hidden' name='tbalance' id='tbalance' value='".round($tbalance,$DecimalBase)."'>";
		echo"<td align='left'><b></td>
		</tr>	";
		echo "</table>";
echo "</div></td></tr>";		
			
			
//		echo "<tr><td colspan='3' align='left'>Total Amount:<b> ";
//			if($tamount<0){
//				$tamount=abs($tamount);
//		echo "(".number_format($tamount,$DecimalBase)." )";
//			}
//			else{
//				echo number_format($tamount,$DecimalBase);
//			}
//		echo "</b></td><td colspan='3'> Unreconciled Amount: <b>". number_format($tunrec,$DecimalBase) . "</b> <br> <hr>
//							   Reconciled Amount:<b> ". number_format($trec,$DecimalBase) ."</b></td>";		
//		echo "<td  colspan='2'>Total Unreconciled:<b>$tunreconcail</b><hr>Total Reconciled:<b>$treconcail</b></td>";
//		echo "</tr>";
//echo"</tr>";




echo" </table>";

echo "</tr>";

echo" </table>";
echo"</td>";		
echo" </tr>";}


echo" </table></td></tr>"; 
			
echo" </table></td></tr><tr height='30'><td align='center' id='content-bottom' ></td></tr>"; 
echo"</table>
 
</form>
</body>
</html>";

?>

