<?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>Accounts Ledger Report</center> </strong>
                            </div>
                            <div class="card-body">
							<?php

include 'dbc.php';

$sql = "delete from acc_ledger";
$conn->query($sql);
$userid=$_SESSION["userid"];
$from_date=$_POST["from_date"];
$to_date=$_POST["to_date"];
$acc_name=$_POST["acc_name"];
	$sql1 = "SELECT * FROM expense_voucher where acc_name_dr='$acc_name' and user_id='$userid' ";
$result1=$conn->query($sql1);
 while($row1 = $result1->fetch_assoc())
 
  {
  $v_no= $row1['id'];
  $date= $row1['date'];
  $month_name= $row1['month_name'];
  $acc_name_dr= $row1['acc_name_dr'];
  $amount= $row1['amount'];
  $discription= $row1['discription'];
  $sql2 = "Insert into acc_ledger (id,date,month_name,tr_no,ven_name,discription,dr_amount,cr_amount,user_id)values (null,'$date','$month_name','$v_no','$acc_name_dr','$discription','$amount','0','$userid')";
$conn->query($sql2);
 /* $sql2dis1 = "Insert into cus_ledger (id,date,month_name,tr_no,ven_name,discription,dr_amount,cr_amount,user_id)values (null,'$date','$month_name','$v_no','$cname','Discount','0','$dis1','$userid')";
$conn->query($sql2dis1);*/
  }
  
 $sql2 = "SELECT * FROM expense_voucher where acc_name_cr='$acc_name' and user_id='$userid' ";
$result2=$conn->query($sql2);
 while($row2 = $result2->fetch_assoc())
 
  {
  $v_no= $row2['id'];
  $date= $row2['date'];
  $month_name= $row2['month_name'];
  $acc_name_cr= $row2['acc_name_cr'];
  $amount= $row2['amount'];
  $discription= $row2['discription'];
  $sql22 = "Insert into acc_ledger (id,date,month_name,tr_no,ven_name,discription,dr_amount,cr_amount,user_id)values (null,'$date','$month_name','$v_no','$acc_name_cr','$discription','0','$amount','$userid')";
$conn->query($sql22);
 /* $sql2dis1 = "Insert into cus_ledger (id,date,month_name,tr_no,ven_name,discription,dr_amount,cr_amount,user_id)values (null,'$date','$month_name','$v_no','$cname','Discount','0','$dis1','$userid')";
$conn->query($sql2dis1);*/
  }  
  $sql3 = "SELECT * FROM cashrec where venid='$acc_name' and user_id='$userid' ";
$result3=$conn->query($sql3);
 while($row3 = $result3->fetch_assoc())
 
  {
  $v_no= $row3['id'];
  $date= $row3['date'];
  $month_name= $row3['month_name'];
  $cname= $row3['ven_name'];
  $netamount= $row3['netamount'];
  $whamount= $row3['whamount'];
  $sql4 = "Insert into acc_ledger (id,date,tr_no,ven_name,discription,dr_amount,cr_amount,user_id)values (null,'$date','$v_no','$acc_name','Cash Receipt','$netamount','0','$userid')";
$conn->query($sql4);
 $sql4 = "Insert into acc_ledger (id,date,tr_no,ven_name,discription,dr_amount,cr_amount,user_id)values (null,'$date','$v_no','$acc_name','With Holding Tax','0','$whamount','$userid')";
$conn->query($sql4);
 
  }
  
  $sql31 = "SELECT * FROM cashpaid where venid='$acc_name' and user_id='$userid' ";
$result31=$conn->query($sql31);
 while($row31 = $result31->fetch_assoc())
 
  {
  $v_no= $row31['id'];
  $date= $row31['date'];
  $month_name= $row31['month_name'];
  $cname= $row31['ven_name'];
  $netamount= $row31['netamount'];
  $whamount= $row31['whamount'];
  $sql41 = "Insert into acc_ledger (id,date,tr_no,ven_name,discription,dr_amount,cr_amount,user_id)values (null,'$date','$v_no','$acc_name','Cash Paid','0','$netamount','$userid')";
$conn->query($sql41);
 $sql41 = "Insert into acc_ledger (id,date,tr_no,ven_name,discription,dr_amount,cr_amount,user_id)values (null,'$date','$v_no','$acc_name','With Holding Tax','$whamount','0','$userid')";
$conn->query($sql41);
 
  }


 
?>
                                <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 > 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 acc_ledger where ven_name='$acc_name' and user_id='$userid' order by date";
$result=$conn->query($sql);
                                       while($row = $result->fetch_assoc())
 
  {
 
   echo "<tr>";
   echo "<td>" .$row['date'] . "</td>";

echo "<td>" .$row['tr_no'] . "</td>";
   echo "<td>" .$row['ven_name'] .  "</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;

  }
 
   echo "<tr>";
	echo "<td colspan=4 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=4 align=right><b> Net Balance </b></td>";
	
	  
		echo "<td colspan=2><b>" .$bal . "</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
}
?>