<?php
session_start();
if($_SESSION["username"]=="")
{
 	echo "You are not Logged in Please <a href='index.html'>Sign in </a> First ";
}
else
{


?>
<!doctype html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang=""> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8" lang=""> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9" lang=""> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js" lang="en">
<!--<![endif]-->

<head>
   <?php include("includes/header.php") ?>
</head>

<body>


    <!-- Left Panel -->

   <!-- /#left-panel -->

    <!-- Left Panel -->

    <!-- Right Panel -->

    <div id="right-panel" class="right-panel">

        <!-- Header-->
        
        <!-- Header-->

        

        <div class="content mt-3">
            <div class="animated fadeIn">
                <div class="row">

                    <div class="col-md-14">
                        <div class="card">
						<div class="page-title">
                        <h2><center><? echo "" . $_SESSION["userid"]; ?></center></h2>
                    </div>
                            <div class="card-header">
							
                                <strong class="card-title"><center>Accouts Ledger Report</center> </strong>
                            </div>
                            <div class="card-body">
							<?php
$userid=$_SESSION["userid"];
$from_date=$_POST["from_date"];
$to_date=$_POST["to_date"];
$acc_name=$_POST["acc_name"];
include 'dbc.php';












$sqla = "delete from accledger";
$conn->query($sqla);
 $sql2 = "select * from expense_voucher where acc_name_dr='$acc_name' and user_id='$userid'";
$result2=$conn->query($sql2);
while($ppp1 = $result2->fetch_assoc())
{
$tr_no=$ppp1['tr_no'];
$recdate=$ppp1['date'];
$discription=$ppp1['discription'];
$acc_name_cr=$ppp1['acc_name_cr'];
$recamount=$ppp1['amount'];
$month_name=$ppp1['month_name'];
  $sql102 = "Insert into accledger (id,date,month_name,tr_no,acc_name,discription,acc_type,dr_amount,cr_amount,balance,user_id) values (null,'$recdate','$month_name','$tr_no','$acc_name','$discription','$acc_name_cr','$recamount','0','0','$userid')";
$conn->query($sql102);
 } 
  $sql2c = "select * from expense_voucher where  acc_name_cr='$acc_name' and user_id='$userid'";
$result2c=$conn->query($sql2c);
while($ppp1c = $result2c->fetch_assoc())
{
$tr_no1=$ppp1c['tr_no'];
$recdate1=$ppp1c['date'];
$discription1=$ppp1c['discription'];
$acc_name_dr=$ppp1c['acc_name_dr'];
$recamount1=$ppp1c['amount'];
$month_name1=$ppp1c['month_name'];
  $sql102c = "Insert into accledger (id,date,month_name,tr_no,acc_name,discription,acc_type,dr_amount,cr_amount,balance,user_id) values (null,'$recdate1','$month_name1','$tr_no1','$acc_name','$discription1','$acc_name_dr','0','$recamount1','0','$userid')";
$conn->query($sql102c);
 } 
 
  $sql2c12 = "select * from cashpaid where acc_name='$acc_name' and user_id='$userid'";
$result2c12=$conn->query($sql2c12);
while($ppp1c12 = $result2c12->fetch_assoc())
{
$id12=$ppp1c12['id'];
$date12=$ppp1c12['date'];
$ven_name=$ppp1c12['ven_name'];
$recamount112=$ppp1c12['netamount'];
$month_name112=$ppp1c12['month_name'];
  $sql102c12 = "Insert into accledger (id,date,month_name,tr_no,acc_name,discription,acc_type,dr_amount,cr_amount,balance,user_id) values (null,'$date12','$month_name112','$id12','$acc_name','Cash Paid Against Vendors','$ven_name','0','$recamount112','0','$userid')";
$conn->query($sql102c12);
 } 
 
 $sql19 = "SELECT * FROM softwaredt";
$result19=$conn->query($sql19);
$row19 = $result19->fetch_assoc();
$sdate=$row19['sdate'] ;
$from_date1 = strftime("%Y-%m-%d", strtotime("$from_date -1 day",$from_date));

$sql8 = "SELECT * FROM accledger where date between '$sdate' and '$from_date1' and  acc_name='$acc_name' and user_id='$userid'";
$result8=$conn->query($sql8);
                                       while($row8 = $result8->fetch_assoc())
									   {
$tfeerec8=$tfeerec8 + $row8['dr_amount'];
$tfeerec18=$tfeerec18 + $row8['cr_amount'];


									   }
									   $obal=$tfeerec8 - $tfeerec18;
									  
?>
                                <table id="bootstrap-data-table-export" class="table table-striped table-bordered">
								<h4><center><? echo "For the Period of From:" . $from_date . "To:" . $to_date; ?></center></h4>
								<h4><center><? echo $acc_name; ?></center></h4>
                                    <thead>
									<?php
                                       echo "<tr>";
echo "<th > Date </th>";
echo "<th > Month </th>";
echo "<th > Voucher# </th>";
echo "<th > Accounts Name </th>";
echo "<th > Discription </th>";
echo "<th > Amount Dr </th>";
echo "<th > Amount Cr </th>";



echo "</tr>";
?>
                                    </thead>
									
                                    <tbody>
									<?php
									$sql = "SELECT * FROM accledger where date between '$from_date' and '$to_date' and  acc_name='$acc_name' and user_id='$userid' order by id";
$result=$conn->query($sql);
                                       while($row = $result->fetch_assoc())
 
  {
 
   echo "<tr>";
   echo "<td>" .$row['date'] . "</td>";
   echo "<td>" .$row['month_name'] . "</td>";
echo "<td>" .$row['tr_no'] . "</td>";
   echo "<td>" .$row['acc_type'] .  "</td>";
   echo "<td>" .$row['discription'] .  "</td>";
   echo "<td>" .$row['dr_amount'] .  "</td>";
   echo "<td>" .$row['cr_amount'] .  "</td>";
  
   
  echo "</tr>";

$trecord=$trecord + 1;
$tfeerec=$tfeerec + $row['dr_amount'];
$tfeerec1=$tfeerec1 + $row['cr_amount'];
$bal=$tfeerec - $tfeerec1;

  }
 $ebal= $obal + $bal;
   echo "<tr>";
	echo "<td colspan=5 align=right><b> Total </b></td>";
	
	  
		echo "<td><b>" .$tfeerec . "</b></td>";
		echo "<td><b>" .$tfeerec1 . "</b></td>";
		
		
		
	  echo "</tr>";
	   echo "<tr>";
	echo "<td colspan=5 align=right><b> Current Date Period Balance </b></td>";
		echo "<td colspan=2><b>" .$bal . "</b></td>";
	  echo "</tr>";
	  echo "<tr>";
	echo "<td colspan=5 align=right><b> Opeining Balance </b></td>";
		echo "<td colspan=2><b>" .$obal . "</b></td>";
	  echo "</tr>";
	    echo "<tr>";
	echo "<td colspan=5 align=right><b> Ending Balance </b></td>";
		echo "<td colspan=2><b>" .$ebal . "</b></td>";
	  echo "</tr>";
	  
  ?>
                                       
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>


                </div>
            </div><!-- .animated -->
        </div> <!-- .content -->
    </div><!-- /#right-panel -->

    <!-- Right Panel -->

   <?php include("includes/footermenu.php") ?>

</body>

</html>
<?php
}
?>