<?PHP
ob_start();
session_start();
include 'connect.php';
if(isset($_SESSION['adminemail']))
{
$user=$_SESSION['adminemail'];

$gtp=mysqli_query($conn,"select * from admin where username='$user'");
$gtp1=mysqli_fetch_assoc($gtp);

include 'top.php';
?>


<div id="page-wrapper" >
          <div id="page-inner">
<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">Cash Ledger</li>
						</ul><!-- /.breadcrumb -->
					</div>

					<div class="page-content">
			

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

$aca=mysqli_query($conn,"select * from financial where user='$user' order by ID desc");
$aca1=mysqli_fetch_assoc($aca);

$financial=$aca1['financial'];

?>                        
                        
							<table style="width:100%;">
                        
                        <tr><td style="width:50%;">
                        
							<h1>
								Cash Ledger
							</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>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css">

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js"></script>
<script>
  $(function() {
  // Initialize the datepicker for the "From Date" input
  $("#from-datepicker").datepicker({
    format: "mm/dd/yyyy",
    autoclose: true,
    todayHighlight: true
  }).on("show", function(e) {
    var inputOffset = $(this).offset();
    var inputHeight = $(this).outerHeight();
    var datepickerDropdown = $(".datepicker-dropdown");

    datepickerDropdown.css({
      top: inputOffset.top + inputHeight + 10,
      left: inputOffset.left
    });
  });

  // Initialize the datepicker for the "To Date" input
  $("#to-datepicker").datepicker({
    format: "mm/dd/yyyy",
    autoclose: true,
    todayHighlight: true
  }).on("show", function(e) {
    var inputOffset = $(this).offset();
    var inputHeight = $(this).outerHeight();
    var datepickerDropdown = $(".datepicker-dropdown");

    datepickerDropdown.css({
      top: inputOffset.top + inputHeight + 10,
      left: inputOffset.left
    });
  });
});
</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']))

{

$fromdate=$_GET['fromdate'];

$todate=$_GET['todate'];



?>



<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('printcashledger.php?fromdate=<?php echo $fromdate; ?>&todate=<?php echo $todate; ?>');"><button class="btn btn-white btn-primary"><i class="fa fa-print"></i> Print</button></a>

</td>



</tr>

</table>



</center><br />



<?php



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

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





echo"



<p>Period : <b>".$fromdate." - ".$todate."</b></p>



<center><table class='table table-striped table-bordered table-condensed'>

<thead><th width='10%'>Date</th><th width='30%'>Description</th><th style='width:12%;'>Type</th><th style='width:10%;'>Voucher No</th><th width='12%'>Debit</th><th width='12%'>Credit</th><th width='12%'>Closing Balance</th></thead>";





$ex=mysqli_query($conn,"select sum(amount) as amount from expense where date<'$frm' and paymentmode='Cash' and date between '$aca1[fromdate]' and '$aca1[todate]'");

$ex1=mysqli_fetch_assoc($ex);


$rc=mysqli_query($conn,"select sum(amount) as amount from receipt where date<'$frm' and paymentmode='Cash' and date between '$aca1[fromdate]' and '$aca1[todate]'");

$rc1=mysqli_fetch_assoc($rc);



$pym=mysqli_query($conn,"select sum(amount) as amount from payment where date<'$frm' and paymentmode='Cash' and date between '$aca1[fromdate]' and '$aca1[todate]'");

$pym1=mysqli_fetch_assoc($pym);



$opl=mysqli_query($conn,"select * from cash_ledger where date<'$frm' and date between '$aca1[fromdate]' and '$aca1[todate]'");

$opl1=mysqli_fetch_assoc($opl);



$jur=mysqli_query($conn,"select sum(amount) as amount from journal where date<'$frm' and date between '$aca1[fromdate]' and '$aca1[todate]' and debit='petty cash'");

$jur1=mysqli_fetch_assoc($jur);



$juri=mysqli_query($conn,"select sum(amount) as amount from journal where date<'$frm' and date between '$aca1[fromdate]' and '$aca1[todate]' and credit='petty cash'");

$juri1=mysqli_fetch_assoc($juri);



$opcr=0;

$opdb=0;

if($opl1['paymenttype']=='debit')

{

$opdb=$opl1['amount'];

}

elseif($opl1['paymenttype']=='credit')

{

$opcr=$opl1['amount'];

}



$tex=$ex1['amount']+$py1['amount']+$opcr+$juri1['amount']+$pym1['amount'];

$trc=$rc1['amount']+$jur1['amount']+$opdb;



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><td style='text-align:right;'>".number_format($bl,2,'.','')."</td></tr>";



$db=$bl;

$cr=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><td style='text-align:right;'>-".number_format($bl,2,'.','')."</td></tr>";



$db=0;

$cr=$bl;



}

else

{

$bl=0;

$cr=0;

$db=0;

}





$sql=mysqli_query($conn,"

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

UNION

select time as time, ID as ID, date as date, customer as name, amount as debit, NULL as credit, CONCAT(description) as details, 'Receipt' as type from receipt where paymentmode='Cash' and date between '$frm' and '$to' and date between '$aca1[fromdate]' and '$aca1[todate]'

UNION

select time as time, ID as ID, date as date, supplier as name, NULL as debit, amount as credit, CONCAT(description) as details, 'Payment' as type from payment where paymentmode='Cash' and date between '$frm' and '$to' and date between '$aca1[fromdate]' and '$aca1[todate]'

UNION

select NULL as time, ID as ID, date as date, NULL as name, NULL as debit, amount as credit, CONCAT('Opening Balance') as details, 'Opening Balance' as type from cash_ledger where paymenttype='credit' and date between '$frm' and '$to' and date between '$aca1[fromdate]' and '$aca1[todate]'

UNION

select NULL as time, ID as ID, date as date, NULL as name, amount as debit, NULL as credit, CONCAT('Opening Balance') as details, 'Opening Balance' as type from cash_ledger where paymenttype='debit' and date between '$frm' and '$to' and date between '$aca1[fromdate]' and '$aca1[todate]'

UNION

select time as time, ID as ID, date as date, NULL as name, NULL as debit, amount as credit, CONCAT(description) as details, 'Journal' as type from journal where credit='petty cash' and date between '$frm' and '$to' and date between '$aca1[fromdate]' and '$aca1[todate]'

UNION

select time as time, ID as ID, date as date, NULL as name, amount as debit, NULL as credit, CONCAT(description) as details, 'Journal' as type from journal where debit='petty cash' and date between '$frm' and '$to' and date between '$aca1[fromdate]' and '$aca1[todate]'

order by date asc, time asc");



/*$cr=0;

$db=0;*/

while($result=mysqli_fetch_assoc($sql))

{



$credit=$result['credit'];

$debit=$result['debit'];



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

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



if($result['type']=='Receipt')

{

$gn=mysqli_query($conn,"select * from customer where ID='$result[name]'");

$gn1=mysqli_fetch_assoc($gn);

}

elseif($result['type']=='Payment')

{

$gnn=mysqli_query($conn,"select * from supplier where ID='$result[name]'");

$gnn1=mysqli_fetch_assoc($gnn);

}

elseif($result['type']=='Wages')

{

$wgn=mysqli_query($conn,"select * from weaver where ID='$result[name]'");

$wgn1=mysqli_fetch_assoc($wgn);

}



$cr=$cr+$result['credit'];

$db=$db+$result['debit'];



$bl=$db-$cr;



echo"<tr><td>".date('d-m-Y',strtotime($result['date']))."</td><td>";



if($result['type']!='Opening Balance' && $result['type']!='Journal')

{

echo"".$result['details']." ";



if($result['type']=='Receipt')

{

 echo"from ";

 echo"".$gn1['name']."";

}

elseif($result['type']=='Payment')

{

 echo"to ";

 echo"".$gnn1['name']."";

}

elseif($result['type']=='Wages')

{

 echo"to ";

 echo"".$wgn1['name']."";

} 

elseif($result['type']=='Expense')

{

 echo"to ";

 

 echo"".$result['name']."";

}  

 

 

}



else

{

echo"".$result['details']." ";

}



echo" </td><td>".$result['type']."";

if($result['type']=='Opening Balance')
{
	
	echo" &nbsp; <a href='editcash_obl.php?id=".$result['ID']."'><i class='ace-icon fa fa-pencil bigger-100 green'></i></a>";
	
}

echo"</td><td>".$result['ID']."</td><td style='text-align:right;'>".$debit."</td><td style='text-align:right;'>".$credit."</td><td style='text-align:right;'>".number_format($bl,2,'.','')."</td></tr>";



}





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



if($db>$cr)

{

$cb=$db-$cr;



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

}

elseif($cr>$db)

{

$cb=$cr-$db;



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

}

else

{

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

}



echo"</table><br /><br />



<center><a href='cash_ledger.php'>Back</a></center>";



}



else

{





?>



<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('printcashledger.php');"><button class="btn btn-white btn-primary"><i class="fa fa-print"></i> Print</button></a>

</td>



</tr>

</table>



</center><br />



<?php



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

	{

	 $status=$_GET['s'];

		 if($status=='1')

		 {

		 echo '<div class="alert alert-success">

				<a class="close" data-dismiss="alert">x</a>

					<strong>Values inserted successfully.</strong>

			  </div>';

		}

		elseif($status=='2')

		 {

		 echo '<div class="alert alert-error">

				<a class="close" data-dismiss="alert">x</a>

					<strong>Values Not inserted. Please try again.</strong>

			  </div>';

		}

		elseif($status=='3')

		 {

		 echo '<div class="alert alert-success">

				<a class="close" data-dismiss="alert">x</a>

					<strong>Values edited successfully.</strong>

			  </div>';

		}

		elseif($status=='4')

		 {

		 echo '<div class="alert alert-error">

				<a class="close" data-dismiss="alert">x</a>

					<strong>Values Not Edited. Please try again.</strong>

			  </div>';

		}

	}

	

	

echo"



<center>	



<form action='cash_ledger.php' method='get' id='wrk'>



	<table>

	

	<tr><td>From Date  <input class='form-control' type='text' name='fromdate' class='fromdate' id='from-datepicker' title='*' /required></td>

	

	<td style='padding-left:15px;'>To Date <input class='form-control' type='text' name='todate' class='todate' id='to-datepicker' title='*' /required></td></tr>

	

	</table>

	<br>

	<button type='submit' name='submit' value='submit' class='btn btn btn-primary'>Submit</button><br>

	

</form>



	</center><br>";	

	



$ch=mysqli_query($conn,"select * from cash_ledger");

$ch1=mysqli_num_rows($ch);



if($ch1==0)

{

echo"<a href='cash_obl.php'>Opening Balance</a>";

}



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

<thead><th width='10%'>Date</th><th width='30%'>Description</th><th style='width:12%;'>Type</th><th style='width:10%;'>Voucher No</th><th width='12%'>Debit</th><th width='12%'>Credit</th><th width='12%'>Closing Balance</th></thead>";



$sql=mysqli_query($conn,"

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

UNION

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

UNION

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

UNION

select NULL as time, ID as ID, date as date, NULL as name, NULL as debit, amount as credit, CONCAT('Opening Balance') as details, 'Opening Balance' as type from cash_ledger where paymenttype='credit' and date between '$aca1[fromdate]' and '$aca1[todate]'

UNION

select NULL as time, ID as ID, date as date, NULL as name, amount as debit, NULL as credit, CONCAT('Opening Balance') as details, 'Opening Balance' as type from cash_ledger where paymenttype='debit' and date between '$aca1[fromdate]' and '$aca1[todate]'

UNION

select time as time, ID as ID, date as date, NULL as name, NULL as debit, amount as credit, CONCAT(description) as details, 'Journal' as type from journal where credit='petty cash' and date between '$aca1[fromdate]' and '$aca1[todate]'

UNION

select time as time, ID as ID, date as date, NULL as name, amount as debit, NULL as credit, CONCAT(description) as details, 'Journal' as type from journal where debit='petty cash' and date between '$aca1[fromdate]' and '$aca1[todate]'

order by date asc, time asc");



$cr=0;

$db=0;

while($result=mysqli_fetch_assoc($sql))

{



$credit=$result['credit'];

$debit=$result['debit'];



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

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



if($result['type']=='Receipt')

{

$gn=mysqli_query($conn,"select * from customer where ID='$result[name]'");

$gn1=mysqli_fetch_assoc($gn);

}

elseif($result['type']=='Payment')

{

$gnn=mysqli_query($conn,"select * from supplier where ID='$result[name]'");

$gnn1=mysqli_fetch_assoc($gnn);

}

elseif($result['type']=='Wages')

{

$wgn=mysqli_query($conn,"select * from weaver where ID='$result[name]'");

$wgn1=mysqli_fetch_assoc($wgn);

}



$cr=$cr+$result['credit'];

$db=$db+$result['debit'];



$bl=$db-$cr;



echo"<tr><td>".date('d-m-Y',strtotime($result['date']))."</td><td>";



if($result['type']!='Opening Balance' && $result['type']!='Journal')

{

echo"".$result['details']." ";




if($result['type']=='Receipt')

{

 echo"from ";

 

 echo"".$gn1['name']."";

}

elseif($result['type']=='Payment')

{

 echo"to ";

 

 echo"".$gnn1['name']."";

}

elseif($result['type']=='Wages')

{

 echo"to ";

 echo"".$wgn1['name']."";

}

elseif($result['type']=='Expense')

{

 echo"to ";

 

 echo"".$result['name']."";

} 

  

 

}



else

{

echo"".$result['details']." ";

}



echo" </td><td>".$result['type']."";

if($result['type']=='Opening Balance')
{
	
	echo" &nbsp; <a href='editcash_obl.php?id=".$result['ID']."'><i class='ace-icon fa fa-pencil bigger-100 green'></i></a>";
	
}

echo"</td><td>".$result['ID']."</td><td style='text-align:right;'>".$debit."</td><td style='text-align:right;'>".$credit."</td><td style='text-align:right;'>".number_format($bl,2,'.','')."</td></tr>";



}





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



if($db>$cr)

{

$cb=$db-$cr;



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

}

elseif($cr>$db)

{

$cb=$cr-$db;



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

}

else

{

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

}



echo"</table><br /><br />";



}

?>                                
                               
							<br>
<br>
	

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

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


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

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

?>