<?PHP

session_start();

include 'connect.php';

if(isset($_SESSION['adminemail']))
{
$user=$_SESSION['adminemail'];

$gtp=mysql_query("select * from admin where username='$user'");
$gtp1=mysql_fetch_array($gtp);

$aca=mysql_query("select * from financial where user='$user' order by ID desc");
$aca1=mysql_fetch_array($aca);

$financial=$aca1['financial'];


echo '

<style type="text/css">

.main{

width:98%;

margin:auto;

font-family:arial;

font-size:12px;

}

table

{

font-family:arial;

font-size:12px;

} 

</style>';



$date=$_GET['date'];

$factory=$_GET['factory'];


echo '<div class="main">';



echo"<h3 style='padding-top:10px; text-align:center;'>Daybook - ".$date."</h3>";





$date=date('Y-m-d',strtotime($date));



echo"<table border='1' style='width:98%; margin:auto; border-collapse:collapse; border:1px solid #999;'>

	<thead><tr><th style='width:7%; height:20px; text-align:center;' valign='middle'>S.No</th><th style='width:35%; text-align:center;' valign='middle'>Description</th><th style='width:10%; text-align:center;'>Voucher No</th><th style='width:15%; text-align:center;' valign='middle'>Type</th><th style='width:15%; text-align:center;' valign='middle'>Debit</th><th style='width:15%; text-align:center;' valign='middle'>Credit</th></tr></thead>";

	


	

$ex=mysql_query("select sum(amount) as amount from expense where date<'$date' and date between '$aca1[fromdate]' and '$aca1[todate]'");
$ex1=mysql_fetch_array($ex);

$impr=mysql_query("select sum(amount) as amount from imprest where date<'$date' and date between '$aca1[fromdate]' and '$aca1[todate]' and factory='$factory'");
$impr1=mysql_fetch_array($impr);

$rec=mysql_query("select sum(amount) as amount from receipt where date<'$date' and date between '$aca1[fromdate]' and '$aca1[todate]'");
$rec1=mysql_fetch_array($rec);

$py=mysql_query("select sum(amount) as amount from payment where date<'$date' and date between '$aca1[fromdate]' and '$aca1[todate]'");
$py1=mysql_fetch_array($py);


$tex=$ex1['amount']+$py1['amount'];

$trc=$impr1['amount']+$rec1['amount'];


if($trc>$tex)

{

$bl=$trc-$tex;



echo"<tr><td colspan='3'>B/F</td><td></td><td style='text-align:right;'>".number_format($bl,2,'.','')."</td><td></td></tr>";



$deb=$bl;

$cre=0;



}

elseif($tex>$trc)

{

$bl=$tex-$trc;



echo"<tr><td colspan='3'>B/F</td><td></td><td></td><td style='text-align:right;'>".number_format($bl,2,'.','')."</td></tr>";



$deb=0;

$cre=$bl;



}

else

{

$bl=0;

$cre=0;

$deb=0;

}

	

$sel=mysql_query("

select time as time, ID as id, name as name, CONCAT(description) as details, amount as credit, null as debit, 'Expense' as type from expense where date='$date' and factory='$factory' and date between '$aca1[fromdate]' and '$aca1[todate]'

UNION

select time as time, ID as id, NULL as name, concat( description ) as details, NULL as credit, amount as debit, 'Imprest' as type from imprest where date='$date' and factory='$factory' and date between '$aca1[fromdate]' and '$aca1[todate]'

UNION

select time as time, ID as id, NULL as name, concat( description ) as details, NULL as credit, amount as debit, 'Receipt' as type from receipt where date='$date' and date between '$aca1[fromdate]' and '$aca1[todate]'

UNION

select time as time, ID as id, NULL as name, concat( description ) as details, amount as credit, null as debit, 'Payment' as type from payment where date='$date' and date between '$aca1[fromdate]' and '$aca1[todate]'

order by time asc");



$sno=0;

/*$cre=0;

$deb=0;*/

while($sel1=mysql_fetch_array($sel))

{

$sno=$sno+1;



$credit=$sel1['credit'];

$debit=$sel1['debit'];



if($debit)$debit=''.number_format($debit,2,'.','');

if($credit)$credit=''.number_format($credit,2,'.','');



echo"<tr><td>".$sno."</td><td style='width:35%;'>".$sel1['details']."</td><td>".$sel1['id']."</td><td>".$sel1['type']."</td>

<td style='text-align:right;'>".$debit."</td><td style='text-align:right;'>".$credit."</td></tr>";



$cre=$cre+$sel1['credit'];

$deb=$deb+$sel1['debit'];

}


echo"<tr><td colspan='4' style='text-align:right;'>Total</td><td style='text-align:right;'>".number_format($deb,2,'.','')."</td><td style='text-align:right;'>".number_format($cre,2,'.','')."</td></tr>";



if($cre>$deb)

{

$cb=$cre-$deb;

echo"<tr><td colspan='4' style='text-align:right;'>Closing Balance</td><td style='text-align:right;'></td><td style='text-align:right;'>".number_format($cb,2,'.','')."</td></tr>";

}

elseif($deb>$cre)

{

$cb=$deb-$cre;

echo"<tr><td colspan='4' style='text-align:right;'>Closing Balance</td><td style='text-align:right;'>".number_format($cb,2,'.','')."</td><td style='text-align:right;'></td></tr>";

}

elseif($deb==$cre)

{

echo"<tr><td colspan='4' style='text-align:right;'>Closing Balance</td><td style='text-align:right;'>NIL</td><td style='text-align:right;'>NIL</td></tr>";

}



echo"</table>";





echo"</div>";		





}



else



{







 // Not logged in



  print "<META HTTP-EQUIV = 'Refresh' Content = '1; URL =login.php'>";



}





?>