<?php


session_start();
if($_SESSION['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");

header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");



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


$querylog="SELECT RA_StatementofProject as loginaccess, ExporttoExcel as exportaccess 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);
	}
	//echo $loginaccess;
if ($loginaccess=='' or $loginaccess==0)
	header("location:menu.php");
$base1="";
$base2="";
$querysetup ="select base1, base2, grid_color1, grid_color2, if(DefaultBase=1, base1, base2) as defaultcurr from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
$rowsetup=mysql_fetch_row($resultsetup);{
$defaultcurr = $rowsetup[4];
$base1=$rowsetup[0];
$base2=$rowsetup[1];
}

$query_Module="select Module_Costcenter from  module";
$result_Module = mysql_query($query_Module);
$Module_Costcenter=0;
while($row_Module = mysql_fetch_array($result_Module)){
	$Module_Costcenter=$row_Module[0];
	
} 
$hide="";

if($Module_Costcenter ==0){
	
	$hide="class='hide'";
}
	$query_language  = "select user_account, Language from ".$_SESSION['dbLabel'].".login  where user_account = '".$_SESSION['useraccount']."'";
$result_language = mysql_query($query_language, $connection); 
$row_language    = mysql_fetch_array($result_language);
extract($row_language);
$query_labels = "SELECT LANGUAGE,
       TransactionJournalVoucher,
       Datee,
       Reference,
       Folio,
       Fromm,
       Too,
       OPTIONS,
       AllTypes,
       Exclude,
       CostcenterProject,
       CheckBill,
       PrintManualNo,
       ForceNewPage,
       WithMonthlyCalculation,
       JournalListing,
       Currency as Currencyy,
       NGN,
       USD,
       Typee
FROM  ".$_SESSION['dbLabel'].".transactionjv_labels
where language = '".$Language."'";
$result_labels = mysql_query($query_labels, $connection);
$row_labels    = mysql_fetch_array($result_labels);
extract($row_labels);


$query_navigator = "SELECT LANGUAGE,
       ClearPage,
       Excel,
       Save,
       Print,
       Addd,
       Search,
       Pending,
       CopyJV,
       PaymentRequests,
       ExportExcel,
	   cancell,
	   Preview
FROM ".$_SESSION['dbLabel'].".mainform_label
WHERE LANGUAGE = '".$Language."'";
$result_navigator = mysql_query($query_navigator, $connection);
$row_navigator    = mysql_fetch_array($result_navigator);
extract($row_navigator);

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


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

$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 (empty($fromdate)) $fromdate = $rowdate[0];
if (empty($todate))   $todate   = displayToday();
$jourdate= displayToday();
if($isloaded=='') $isloaded=0;


print <<< HERE


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

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<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 checkPer(indx){
	var txt = 'txtchecked'+indx;
	var txt2 = 'incamt'+indx;
	var txt3 = 'expamt'+indx;
	if(txt != 'txtcheckedExceededCredit'){
		if(document.getElementById(txt).checked){
			document.getElementById(txt).value=1;
			document.getElementById(txt2).value=document.getElementById(txt3).value;
			
		}
		else
		{ 
			document.getElementById(txt).value=0;
			document.getElementById(txt2).value='';
		}
	}
}

function checkPerVat(indx){
	var txt = 'invvat'+indx;
	var txt1 = 'vatchk'+indx;
		if(document.getElementById(txt1).checked)
			document.getElementById(txt).value=1;
		else document.getElementById(txt).value=0;
	
}

function showlistCostcenter(str){

  window.open('List_SorCostcenter.php?frm=29&item='+str,'mywindow','width=850,height=550,,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}
function showlistProject(str){

    window.open('List_SorProject.php?frm='+str+'&item='+str,'mywindow','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
  }

  function showlistledger(str){

	var str1 = 1;
  
	var selectpage;
	selectpage = 'select_ledger.php?item='+str+'&accountoption='+str1;	  
  
	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 Generate(){
	
	let isloaded = document.form1.isloaded.value;
	document.form1.actionmode='closeshipment'; 
	if (isloaded=='0')
	alert("Please load !");
	else{
		document.form1.action = "form_shipmentclosing_action.php?actionmode=closeshipment";
		document.form1.submit();		
	}

	
}


function GenerateAll(){
	
	let isloaded = document.form1.isloaded.value;
	document.form1.actionmode='closeshipment'; 
	if (isloaded=='0')
	alert("Please load !");
	else{
		document.form1.action = "form_shipmentclosing_action.php?actionmode=generateall";
		document.form1.submit();		
	}

	
}

function showlistledger1(str,index){

	var str1 = 1;
  
	var selectpage;
	selectpage = 'select_ledger.php?item='+str+'&accountoption='+str1+'&index='+index;	  
  
	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 Load(){
	if(document.form1.fromclient.value==''){
		alert('Please Fill the client ');
		document.form1.fromclient.focus();
	}
	else if(document.form1.jourdate.value==''){
		alert('Please Fill the Journal Date ');
		document.form1.jourdate.focus();
	}
	else{
	document.form1.isloaded.value='1';
	document.form1.action = 'Form_shipmentclosing1.php';
	document.form1.submit();
	}
}

function showlistClient(str,index){
	var vdate, vcode;
	window.open('List_SorClient.php?frm='+str+'&item='+str,'mywindow','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}
	
</script>


<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;
}
.hide {
	display: none;
	
}
</style>

<title>Shipment Closing</title>

<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20101112" media="screen"></link>
<script type="text/javascript" src="dhtmlgoodies_calendar.js?random=20100118">
function showlistCostcenter(str){

  window.open('List_SorCostcenter.php?frm=29&item='+str,'mywindow','width=850,height=550,,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}
</script>
<script type="text/javascript" language="javascript">



function kdClient(choice,e,app)
{   
	 x = document.form1;
	if(app==0){
		var clientcode = x.fromledger.value;
		if(clientcode=='')
		var clientcode = x.fromledgername.value;
	} else if(app==1) {
		var clientcode = x.toledger.value;
		if(clientcode=='')
		var clientcode = x.toledgername.value;
	}	else if(app==2) {
		var clientcode = x.fromclient.value;
		if(clientcode=='')
		var clientcode = x.fromclientname.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(22);
					} else if(app==1) {
						if(data[0]!=''){
								x.toledger.value = data[0];
								x.toledgername.value = data[1];
							//if(x.toledgername.value!=''){
							//	x.submit();	
							//} 
						}
						else showlistledger(23);						
					}
					else if(app==2) {
						if(data[0]!=''){
								x.fromclient.value = data[0];
								x.fromclientname.value = data[1];
							//if(x.toledgername.value!=''){
							//	x.submit();	
							//} 
						}
						else showlistledger(23);						
					}
			}
		}
	    
		xmlhttp.open("GET","Form_shipmentclosing_Action.php?mode=getclientname&clientcode="+clientcode+"&choice="+choice);
		xmlhttp.send();
	
      
			
			
    }
	

}
function kdProject(choice,e,app)
{   
	 x = document.form1;
	if(app==0){
		var projectcode = x.fromproject.value;
		if(projectcode=='')
		var projectcode = x.fromprojectname.value;
	} else {
		var projectcode = x.toproject.value;
		if(projectcode=='')
		var projectcode = x.toprojectname.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.fromproject.value = data[0];
								x.fromprojectname.value = data[1];
									if(x.toproject.value==''){
									x.toproject.value = data[0];
									x.toprojectname.value = data[1];
									
									}
						}
					} else {
						if(data[0]!=''){
								x.toproject.value = data[0];
								x.toprojectname.value = data[1];
							//if(x.toprojectname.value!=''){
							//	x.submit();	
							//} 
						}
					}
			}
		}
	    
		xmlhttp.open("GET","Form_shipmentclosing_Action.php?mode=getprojectname&projectcode="+projectcode+"&choice="+choice);
		xmlhttp.send();
	
      
			
			
    }
	

}

function ClearPage() {

	
	if (confirm ( "Are you sure you want to Clear Page?"))
	{
		x = document.form1;
		x.fromdate.value='';
		x.todate.value='';
		
		x.fromledger.value='';
		x.toledger.value='';
		
		x.fromledgername.value='';
		x.toledgername.value='';

		x.fromproject.value='';
		x.toproject.value='';
		x.fromprojectname.value='';
		x.toprojectname.value='';
		
		x.fromclient.value='';
		x.fromclientname.value='';
		x.isloaded.value='';
		
		x.action='form_shipmentClosing1.php';
		x.submit();
	}	
}

</script>

</head>
<body>
HERE;

echo "<form id='form1' name='form1' method='post' action='$_SERVER[PHP_SELF]'>";

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;'>
            <input type=hidden id='isloaded' name='isloaded' value='$isloaded'>

				<td style='width:38px; height:26px;'>&nbsp;</td>
				<td valign='middle' style='width:300px; height:26px;'><span class='title'>Shipment Closing</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='reset' id='btnClear' name='btnClear'  title='Clear' onClick=\"ClearPage();\"><span><span>$ClearPage</span></span></button>";
		echo"<button type='button' id='btnLoad' name='btnLoad'  onClick=\"Load()\"; title='Load'><span><span>Load</span></span></button>";

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

		
echo"<tr><td id='content-body'>
<table border='0'  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%'>";

	  
echo"<td width='20%' height='19' colspan='2' align='left' bgcolor='#F5F5F5'><span class='style8'> ";

echo"$Datee</span></td>";
echo"<td width='40%' height='19' colspan='2' align='left' bgcolor='#F5F5F5'><span class='style8'>";
echo"<label>";

echo"</label>";
echo"Ledger</span></td>";
echo"<td width='40%' colspan='2' align='left' bgcolor='#F5F5F5'><span class='style8'>";
echo"<label>";

echo"</label>";
echo"Project </span></td>";




echo"    <tr>";
echo"      <td width='5%'  valign='middle' bgcolor='#F5F5F5' ><span class='style8'>$Fromm</span></td>";
echo"      <td width='15%'  valign='middle' bgcolor='#F5F5F5' ><span class='style8'>";
echo"      <input type='text' name='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>";




echo"      <td width='5%' align='center' bgcolor='#F5F5F5' class='style8'>$Fromm</td>";
echo"      <td width='35%' bgcolor='#F5F5F5' class='style8'><label>";
echo"      <input type='text' name='fromledger' id='fromledger' value='$fromledger' size='10' onkeydown='return kdClient(0,event,0);'>";
echo"      <input type='text' name='fromledgername' id='fromledgername' value='$fromledgername' size='20' onkeydown='return kdClient(1,event,0);' >";
echo"<label><span class='style11'><a href='javascript:void(0);' NAME='My Window Name' title=' Select Ledger From ' 
              onClick='showlistledger(22); '><img src='img/btnopen.jpg' width='15' height='20' border='0' /></a> </span></label>";
echo"      </label></td>";

echo"      <td width='5%' align='center' bgcolor='#F5F5F5' class='style8'>$Fromm</td>";
echo"      <td width='35%' bgcolor='#F5F5F5' class='style8'><input type='text' name='fromproject' value='$fromproject' size='15'  onkeydown='return kdProject(0,event,0);'>
<input type='text' name='fromprojectname' value='$fromprojectname' size='20'  onkeydown='return kdProject(1,event,0);' >";
echo " <label><span class='style1'><img src='img/btnopen.jpg' title=' Select Project ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistProject(47); '  />";
echo"</td>";






echo"    <tr>";
echo"      <td width='5%' valign='top' bgcolor='#F5F5F5'><span class='style8'>$Too</span></td>";
echo"      <td width='15%' valign='top' bgcolor='#F5F5F5'><span class='style8'>";
echo"      <input type='text' name='todate' value='$todate'  maxlength='10' size='10'>";
echo"      <A HREF=\"#\" onClick=\"displayCalendar(document.form1.todate,'dd/mm/yyyy',this); return false;\" NAME=\"anchor2\" ID=\"anchor2\"><image src='./img/cal.gif' border=0 align=center></image></A>";
echo"      </span></td>";

echo"      <td width='5%' align='center' bgcolor='#F5F5F5' class='style8'>$Too</td>";
echo"      <td width='35%' bgcolor='#F5F5F5' class='style8'><label>";
echo"      <input type='text' name='toledger' id='toledger'  value='$toledger' size='10' onkeydown='return kdClient(0,event,1);' >";
echo"      <input type='text' name='toledgername' id='toledgername'  value='$toledgername' size='20' onkeydown='return kdClient(1,event,1);'>";
echo"<label><span class='style11'><a href='javascript:void(0);' NAME='My Window Name' title=' Select Ledger To ' 
              onClick='showlistledger(23); '><img src='img/btnopen.jpg' width='15' height='20' border='0' /></a> </span></label>";
echo"      </label></td>";
echo"      <td width='5%' align='center' bgcolor='#F5F5F5' class='style8'><label>$Too</label></td>";
echo"      <td width='35%' bgcolor='#F5F5F5' class='style8'><input type='text' name='toproject'  value='$toproject' size='15' onkeydown='return kdProject(0,event,1);'>
<input type='text' name='toprojectname'  value='$toprojectname' size='20' onkeydown='return kdProject(1,event,1);'>";
echo " <label><span class='style1'><img src='img/btnopen.jpg' title=' Select Project ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistProject(48); '  />";

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





	  
echo"<td width='20%' height='19' colspan='2' align='left' bgcolor='#F5F5F5'><span class='style8'> ";

echo"</span></td>";
echo"<td width='40%' height='19' colspan='2' align='left' bgcolor='#F5F5F5'><span class='style8'>";
echo"<label>";

echo"</label>";
echo"</span></td>";
echo"<td width='40%' colspan='2' align='left' bgcolor='#F5F5F5'><span class='style8'>";
echo"<label>";

echo"</label>";
echo"</span></td>";




echo"    <tr>";
echo"      <td width='5%'  valign='middle' bgcolor='#F5F5F5' ><span class='style8'>Journal date</span></td>";
echo"      <td width='15%'  valign='middle' bgcolor='#F5F5F5' ><input type='text' name='jourdate' id='jourdate' value='$jourdate' maxlength='6' size='10'>
<a href='#' onClick=\"displayCalendar(document.form1.jourdate,'dd/mm/yyyy',this); return false; \" name='anchor1' id='anchor1'><image src='./img/cal.gif' border=0 align=center></image></a></span>
<span class='style8'>";


echo"      <td width='5%' align='center' bgcolor='#F5F5F5' class='style8'>Client</td>";
echo"      <td width='35%' bgcolor='#F5F5F5' class='style8'><label>";
echo"      <input type='text' name='fromclient' id='fromclient' value='$fromclient' size='10'  onkeydown='return kdClient(0,event,2);'>";
echo"      <input type='text' name='fromclientname' id='fromclientname' value='$fromclientname' size='20'  onkeydown='return kdClient(1,event,2);'>";
echo"<label><span class='style11'><a href='javascript:void(0);' NAME='My Window Name' title=' Select Ledger From ' 
              onClick='showlistledger(29); '><img src='img/btnopen.jpg' width='15' height='20' border='0' /></a> </span></label>";
echo"      </label></td>";

echo"      <td width='5%' align='center' bgcolor='#F5F5F5' class='style8'></td>";
echo"      <td width='35%' bgcolor='#F5F5F5' class='style8'>";
echo " <label><span class='style1'>";
echo"</td>";






// echo"    <tr>";
// echo"      <td width='5%' valign='top' bgcolor='#F5F5F5'><span class='style8'></span></td>";
// echo"      <td width='15%' valign='top' bgcolor='#F5F5F5'><span class='style8'>";
// echo"      </span></td>";

// echo"      <td width='5%' align='center' bgcolor='#F5F5F5' class='style8'>$Too</td>";
// echo"      <td width='35%' bgcolor='#F5F5F5' class='style8'><label>";
// echo"      <input type='text' name='toclient' id='toclient'  value='$toclient' size='10'>";
// echo"      <input type='text' name='toclientname' id='toclientname'  value='$toclientname' size='20'>";
// echo"<label><span class='style11'><a href='javascript:void(0);' NAME='My Window Name' title=' Select Ledger To ' 
//               onClick='showlistledger(23); '><img src='img/btnopen.jpg' width='15' height='20' border='0' /></a> </span></label>";
// echo"      </label></td>";
// echo"      <td width='5%' align='center' bgcolor='#F5F5F5' class='style8'><label></label></td>";
// echo"      <td width='35%' bgcolor='#F5F5F5' class='style8'>";
// echo " <label><span class='style1'>";

echo"      </td>";

if($isloaded == "1"){
	
	echo"<tr><table  style='font-size:10px' width='1100px' cellspacing=0 width='1100px' cellpadding=0>";
echo "<col width='20px'>
<col width='70px'>
<col width='150px'>
<col width='110px'>
<col width='90px'>
<col width='100px'>
<col width='110px'>
<col width='100px'>
<col width='95px'>
<col width='20px'>";
echo"<tr height='25px' bgcolor=#E8E8E8>";
echo"<td ></td>";
echo"<td  align='center'><span class='style4'>Ledger </span></td>";
echo"<td  align='center'><span class='style4'>Ledger Name </span></td>";
echo"<td  align='center'><span class='style4'>Project  </span></td>";
echo"<td  align='center'><span class='style4'>Amount </span></td>";
echo"<td  align='center'><span class='style4'>Expenses Ledger </span></td>";
echo"<td  align='center'><span class='style4'> Expenses Amount </span></td>";
echo"<td  align='center'><span class='style4'>Income Ledger  </span></td>";
echo"<td  align='center'><span class='style4'>Amount To Inv </span></td>";
echo"<td  align='center'><span class='style4'>Vat </span></td>";
echo"</tr>";

 if($fromclient != "" )  $fromclient;
 if($fromledger != "" &&  $toledger == "") $toledger = $fromledger;
 if($fromproject != "" &&  $toproject == "") $toproject = $fromproject;

 if (!empty($fromdate))
   $fromdate = explodeword($fromdate);
if (!empty($todate))
   $todate = explodeword($todate);  
   
   $fromdate .= ' 00:00:00';
   $todate .= ' 23:59:59';

		$query="SELECT
		ledger.ledger_number, 
		ledger.ledger_name,
		ifnull(sum( IF ( journaldt_dbcd = 'D',
	 Journaldt_amount1 
			, (-1) *  Journaldt_amount1 
		) ),0) AS Balance,cstjour.project_code,
		expences_ledger,income_ledger
		
		FROM journaldt
		
		LEFT JOIN ledger ON ledger.ledger_number = journaldt.ledger_number
		left join ShipmentClosing_setup on ShipmentClosing_setup.ledger_number = journaldt.ledger_number
		LEFT JOIN journal ON journal.journal_id = journaldt.journal_id
		left join cstjour on cstjour.journaldt_id= journaldt.journaldt_id
		LEFT JOIN project ON cstjour.project_code = project.project_code
		where journaldt.journal_date between '".$fromdate."' and '".$todate."' and 
		if('$fromledger' = '' , 1=1 , ledger.ledger_number between  '$fromledger' and '$toledger')
		and if('$fromproject' = '' , 1=1 , cstjour.project_code between  '$fromproject' and '$toproject')
		GROUP BY cstjour.project_code,ledger.ledger_number";
		
   
 $query.="         having Balance<>0 
  ORDER BY   cstjour.project_code,ledger.ledger_number  ";
  // echo $query;	
	 
        $ct=0;
$totalamount=0;		
$totalexpense=0;		
$result=mysql_query($query, $connection) or die(mysql_error());
while($row=mysql_fetch_array($result)){
    if($ct%2) $bgcolor = "#e0e0e0";
	else $bgcolor = "#f7f7f7";
	
	$ind = $row[0]."".$row[3];
	$txtchecked = 'txtchecked'.$ind;	

	
	$totalamount=$totalamount+$row[2];
	$totalexpense=$totalexpense+$row[2];
		echo"<tr style='background-color:$bgcolor;'>";
		echo"<td align='center' style='$top;$bottom;$left;$right;'>
        <input type='hidden'  type='text' name='ledgernum".$row[0]."".$row[3]."' id='ledgernum".$row[0]."".$row[3]."' value='$row[0]'>
        <input type='hidden'  type='text' name='projcode".$row[0]."".$row[3]."' id='projcode".$row[0]."".$row[3]."' value='$row[3]'>
		<input type='hidden'  type='text' name='invvat".$row[0]."".$row[3]."' id='invvat".$row[0]."".$row[3]."' value='0'>
		<input type='hidden'  type='text' name='cliledger".$row[0]."".$row[3]."' id='cliledger".$row[0]."".$row[3]."' value='$fromclient'>
		<input type='hidden'  type='text' name='jourdate".$row[0]."".$row[3]."' id='jourdate".$row[0]."".$row[3]."' value='$jourdate'>
        <input type='checkbox' name='$txtchecked' id='$txtchecked' value='1' onclick=\"checkPer('".$row[0]."".$row[3]."');\" ></span></td>";
        echo"<td align='left' style='$top;$bottom;$right;'><span class='style9'>$row[0]</span></td>";
        echo"<td align='left' style='$top;$bottom;$right;'><span class='style9'>$row[1]</span></td>";
        echo"<td align='left' style='$top;$bottom;$right;'><span class='style9'>$row[3]</span></td>";
        echo"<td align='right' style='$top;$bottom;$right;'><span class='style9'>".number_format($row[2] , 2)."</span></td>";
        echo"<td align='left' style='$top;$bottom;$right;'><span class='style9'><input type='text' name='expledger".$row[0]."".$row[3]."' id='expledger".$row[0]."".$row[3]."' value='$row[4]' style='width:70px'>
        <label><span class='style11'><a href='javascript:void(0);' NAME='My Window Name' title=' Select Ledger  ' 
              onClick=\"showlistledger1(24,'$ind'); \"><img src='img/btnopen.jpg' width='15' height='20' border='0' /></a> </span></label></span></td>";
        echo"<td align='left' style='$top;$bottom;$right;'><span class='style9'><input type='text' name='expamt".$row[0]."".$row[3]."' id='expamt".$row[0]."".$row[3]."' value='$row[2]'  style='width:120px'></span></td>";

        echo"<td align='left' style='$top;$bottom;$right;'><span class='style9'><input type='text' name='incledger".$row[0]."".$row[3]."' id='incledger".$row[0]."".$row[3]."' value='$row[5]'  style='width:70px'>
        <label><span class='style11'><a href='javascript:void(0);' NAME='My Window Name' title=' Select Ledger  ' 
        onClick=\"showlistledger1(25,'$ind'); \"><img src='img/btnopen.jpg' width='15' height='20' border='0' /></a> </span></label></span></td>";
        echo"<td align='left' style='$top;$bottom;$right;'><span class='style9'><input type='text' name='incamt".$row[0]."".$row[3]."' id='incamt".$row[0]."".$row[3]."'  style='width:120px'></span></span></td>";
        echo"<td align='left' style='$top;$bottom;$right;'>
		<span class='style9'>

		<input type='checkbox' name='vatchk".$row[0]."".$row[3]."' id='vatchk".$row[0]."".$row[3]."'  value='1' onclick=\"checkPerVat('".$row[0]."".$row[3]."');\">
        </span></td>";
        echo"</tr>";
    
	$ct++;
}

echo"<tr style='background-color:$bgcolor;'>";
		echo"<td align='center' colspan='3' style='$top;$bottom;$left;$right;'></td>";
		echo"<td align='center' style='$top;$bottom;$left;$right;'><span class='style9'><b>Total Amount:</b></span></td>";
		echo"<td align='center' style='$top;$bottom;$left;$right;'><span class='style9'><b>".number_format($totalamount,2)."</b></span></td>";
		echo"<td align='center' colspan='7' style='$top;$bottom;$left;$right;'></td>";

echo "<tr>";		
echo"<tr height='20'><td align='center' ></td></tr>";

echo"<tr ><td colspan='11' align='center'><button type='button' id='btnGenerate' name='btnGenerate'  onClick=\"Generate()\"; title='Generate'><span><span>Generate 1 by 1</span></span></button>
<button type='button' id='btnGenerateAll' name='btnGenerateAll'  onClick=\"GenerateAll()\"; title='GenerateAll'><span><span>Generate All</span></span></button></td></tr>";

 

}




	
echo"<tr></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>";

//Return Date in DD/MM/YYYY format
function displayFromDate($data){
	return "01/01/".substr($data, 6, 4);		
}
//Return Today's Date in DD/MM/YYYY format
function displayToday(){
	return strftime("%d")."/".strftime("%m")."/".strftime("%Y");
}

function explodeword($date)
{

$trim = explode('/',$date);
$trim = "$trim[2]"."-"."$trim[1]"."-"."$trim[0]";
return $trim;
}


?>
