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

include 'dbc.php';

$sql = "delete from cus_ledger";
$conn->query($sql);
$userid=$_SESSION["userid"];
$from_date=$_POST["from_date"];
$to_date=$_POST["to_date"];
$ven_name=$_POST["ven_name"];
	$sql1 = "SELECT * FROM invoice where cname='$ven_name' and user_id='$userid'";
$result1=$conn->query($sql1);
 while($row1 = $result1->fetch_assoc())
 
  {
  $v_no= $row1['id'];
  $p_no= $row1['v_no'];
  $date= $row1['date'];
  $month_name= $row1['month_name'];
  $cname= $row1['cname'];
  $netamount= $row1['gamount'];
  $dis1= $row1['dis'];
  $sql2 = "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','Invoice No . $v_no  - $p_no','$netamount','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);*/
  }
  
  $sql1st = "SELECT * FROM invoice_st where cname='$ven_name' and user_id='$userid'";
$result1st=$conn->query($sql1st);
 while($row1st = $result1st->fetch_assoc())
 
  {
  $v_no= $row1st['id'];
  $date= $row1st['date'];
  $month_name= $row1st['month_name'];
  $cname= $row1st['cname'];
  $tnetamount= $row1st['gamount'];
  $dis= $row1st['dis'];
  $sql2st = "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','Sale Tax Invoice','$tnetamount','0','$userid')";
$conn->query($sql2st);
/* $sql2dis = "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','$dis','$userid')";
$conn->query($sql2dis);*/
 
  }
  $sql3 = "SELECT * FROM cashrec where ven_name='$ven_name' and remarks='Yes' 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 cus_ledger (id,date,tr_no,ven_name,discription,dr_amount,cr_amount,user_id)values (null,'$date','$v_no','$cname','Cash Receipt','0','$netamount','$userid')";
$conn->query($sql4);
/* $sql4 = "Insert into cus_ledger (id,date,tr_no,ven_name,discription,dr_amount,cr_amount,user_id)values (null,'$date','$v_no','$cname','With Holding Tax','0','$whamount','$userid')";
$conn->query($sql4);*/
 
  }

  $sql6 = "SELECT * FROM cdebitnote where ven_name='$ven_name' and user_id='$userid'";
$result6=$conn->query($sql6);
 while($row6 = $result6->fetch_assoc())
 
  {
  $v_no= $row6['id'];
  $date= $row6['date'];
  $month_name= $row6['month_name'];
  $cname= $row6['ven_name'];
  $netamount= $row6['netamount'];
   $disc= $row6['disc'];
  $sql7 = "Insert into cus_ledger (id,date,tr_no,ven_name,discription,dr_amount,cr_amount,user_id)values (null,'$date','$v_no','$cname','$disc','$netamount','0','$userid')";
$conn->query($sql7);
 
  }
   $sql8 = "SELECT * FROM ccreditnote where ven_name='$ven_name' and user_id='$userid'";
$result8=$conn->query($sql8);
 while($row8 = $result8->fetch_assoc())
 
  {
  $v_no= $row8['id'];
  $date= $row8['date'];
  $month_name= $row8['month_name'];
  $cname= $row8['ven_name'];
  $netamount= $row8['netamount'];
   $disc= $row8['disc'];
  $sql9 = "Insert into cus_ledger (id,date,tr_no,ven_name,discription,dr_amount,cr_amount,user_id)values (null,'$date','$v_no','$cname','$disc','0','$netamount','$userid')";
$conn->query($sql9);
 
  }


 $sql3rt = "SELECT * FROM invoice_return where cname='$ven_name' and user_id='$userid'";
$result3rt=$conn->query($sql3rt);
 while($row3rt = $result3rt->fetch_assoc())
 
  {
  $v_no= $row3rt['id'];
  $date= $row3rt['date'];
  $month_name= $row3rt['month_name'];
  $cname= $row3rt['ven_name'];
  $netamount= $row3rt['netamount'];
  $sql4rt = "Insert into cus_ledger (id,date,tr_no,ven_name,discription,dr_amount,cr_amount,user_id)values (null,'$date','$v_no','$cname','Invoice Return','0','$netamount','$userid')";
$conn->query($sql4rt);
 
  }

?>
                                <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 $ven_name; ?></center></h4>
                                    <thead>
									<?php
                                       echo "<tr>";
echo "<th > Date </th>";
echo "<th > Voucher# </th>";
echo "<th > Customer Name </th>";
echo "<th > Discription </th>";
echo "<th > Amount Dr </th>";
echo "<th > Amount Cr </th>";



echo "</tr>";
?>
                                    </thead>
									
                                    <tbody>
									<?php
									$sql = "SELECT * FROM cus_ledger where ven_name='$ven_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
}
?>