<?PHP
ob_start();
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);

include 'top.php';
?>



<div class="main-content">
				<div class="main-content-inner">
					<div class="breadcrumbs ace-save-state" id="breadcrumbs">
						<ul class="breadcrumb">
							<li>
								<i class="ace-icon fa fa-home home-icon"></i>
								<a href="#">Home</a>
							</li>
							<li class="active">Daybook</li>
						</ul><!-- /.breadcrumb -->
					</div>

					<div class="page-content">
			

						<div class="page-header">
                        
<?php

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

$financial=$aca1['financial'];

?>
                        
							<table style="width:100%;">
                        
                        <tr><td style="width:50%;">
                        
							<h1>
								Daybook
							</h1>
                            
                            </td>
                            
                            <td style="width:50%; text-align:right; padding-right:15px; font-size:15px;">
                            
                            <i class="fa fa-calendar"></i>&nbsp; <?php echo $financial; ?>
                            
                            </td></tr></table>
                            
						</div><!-- /.page-header -->
<br />

						<div class="row">
							<div class="col-xs-12">
                            
								<!-- PAGE CONTENT BEGINS -->
                                
<script src="assets/js/jquery.js"></script>
<script>
	
jQuery(function($){
		   
	// simple jQuery validation script
	$('#work').submit(function(){
		
		var valid = true;
		var errormsg = 'This field is required!';
		var errorcn = 'error';
		
		$('.' + errorcn, this).remove();			
		
		$('#required', this).each(function(){
			var parent = $(this).parent();
			if( $(this).val() == '' ){
				var msg = $(this).attr('title');
				msg = (msg != '') ? msg : errormsg;
				$('<span style="font-size:13px;color:#FF0000; padding-left:3px;" class="'+ errorcn +'">'+ msg +'</span>')
					.appendTo(parent)
					.fadeIn('fast')
					.click(function(){ $(this).remove(); });
				valid = false;
			};
		});
		
		return valid;
	});
	
});	

</script>
								
<?php 



if(isset($_GET['submit']))

{

$date=$_GET['date'];

$factory=$_GET['factory'];

?>



<script type="text/javascript">

// Popup window code

function newPopup(url) {

	popupWindow = window.open(

		url,'popUpWindow','height=700,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,onscreendisplay=yes,menubar=no,location=no,directories=no,status=yes')

}

</script>



<center>



<table>

<tr>



<td style="text-align:center; padding-right:5px;">

<a href="JavaScript:newPopup('printdaybook.php?date=<?php echo $date; ?>&factory=<?php echo $factory; ?>');"><button type="button" class="btn btn-white btn-primary"><i class="fa fa-print"></i> Print</button></a>

</td>



<td style="text-align:center; padding-left:5px;">

<form method="get" action="pdf.php" target="_blank">                        

                <input type="hidden" name="date" value="<?php echo $date; ?>" />
                
                <input type="hidden" name="factory" value="<?php echo $factory; ?>" />

                <input type="hidden" name="file" value="printdaybook.php" />

                <input type="hidden" name="title" value="Daybook PDF" />                

                <button type="submit" name="submit" class="btn btn-white btn-warning"><i class="fa fa-file-pdf-o"></i> Pdf</button>

</form>

</td>



<td style="text-align:center; padding-left:7px;">



<!-- <a href='daybook-excel.php?date='><button type="submit" name="submit" value="Excel" style="margin-top:-4px;">Excel</button></a> -->



</td>



</tr>

</table>



</center>



<?php



echo"<center><h4>Daybook - ".$date."</h4></center><br />";



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



echo"<table class='table table-striped table-bordered table-condensed'>

	<thead><th width='7%'>S.No</th><th width='35%'>Description</th><th width='10%'>Voucher No</th><th width='15%'>Type</th><th width='15%'>Debit</th><th width='15%'>Credit</th></thead>";
	

$ex=mysql_query("select sum(amount) as amount from expense where date<'$date' and date between '$aca1[fromdate]' and '$aca1[todate]' and factory='$factory'");
$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") or die(mysql_error());

$sno=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>".$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"<br /><center><a href='daybook.php'>Back</a></center>";


}

else

{

?>



<center>



<form action="daybook.php" method="get" id="work">



<table width="50%">

<tr><td>Factory <span class="red">*</span></td><td>:</td><td><select name="factory" id="required" title="*" style="width:220px;"><option value="">Select Factory</option>

<?php

$fac=mysql_query("select * from factory order by factory asc");
while($fac1=mysql_fetch_array($fac))
{
	echo"<option value='".$fac1['ID']."'>".$fac1['factory']."</option>";
}

?>

</select></td></tr>

<tr><td style="padding-top:7px;">Select Date <span class="red">*</span></td><td style="padding-top:7px;">:</td><td style="padding-top:7px;"> <input type="text" name="date" id="required" title="*" class="datepicker" autocomplete="off" value="<?php echo date('d-m-Y') ?>"></td></tr>

</table><br />



<button type="submit" name="submit" class="btn btn-sm btn-primary" value="submit">Go</button>



</form>



</center>



<?php



}



?>                             
                               
							<br>
<br>
	

								<!-- PAGE CONTENT ENDS -->
							</div><!-- /.col -->
						</div><!-- /.row -->
					

</div><!-- /.page-content -->
				</div>
			</div><!-- /.main-content -->


<?php
include 'bottom.php';
}
else
{

 header("location:login.php");
}
ob_flush();

?>