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

include 'dbc.php';

$sql = "delete from inv_ledger";
$conn->query($sql);
$userid=$_SESSION["userid"];
$from_date=$_POST["from_date"];
$to_date=$_POST["to_date"];
$item_name=$_POST["item_name"];
	$sql1 = "SELECT * FROM invoice_details where item_name='$item_name' and user_id='$userid' ";
$result1=$conn->query($sql1);
 while($row1 = $result1->fetch_assoc())
 
  {
  $v_no= $row1['inv_no'];
  $date= $row1['date'];
  $qty= $row1['qty'];
  $sql2 = "Insert into inv_ledger (id,date,tr_no,ven_name,discription,dr_amount,cr_amount,user_id)values (null,'$date','$v_no','$item_name','Sale Invoice','0','$qty','$userid')";
$conn->query($sql2);
  
  }
  
  $sql1st = "SELECT * FROM invoice_details_st where item_name='$item_name' and user_id='$userid' ";
$result1st=$conn->query($sql1st);
 while($row1st = $result1st->fetch_assoc())
 
  {
  $v_no= $row1st['inv_no'];
  $date= $row1st['date'];
  
 
  $qty= $row1st['qty'];
 
  $sql2st = "Insert into inv_ledger (id,date,tr_no,ven_name,discription,dr_amount,cr_amount,user_id)values (null,'$date','$v_no','$item_name','Sale Tax Invoice','0','$qty','$userid')";
$conn->query($sql2st);
  }
  $sql3 = "SELECT * FROM invoice_details_return where item_name='$item_name' and user_id='$userid' ";
$result3=$conn->query($sql3);
 while($row3 = $result3->fetch_assoc())
 
  {
  $v_no= $row3['inv_no'];
  $date= $row3['date'];
  $qty= $row3['qty'];
  $sql4 = "Insert into inv_ledger (id,date,tr_no,ven_name,discription,dr_amount,cr_amount,user_id)values (null,'$date','$v_no','$item_name','Invoice Return','$qty','0','$userid')";
$conn->query($sql4);
 
  }

  $sql6 = "SELECT * FROM purchase_details where item_name='$item_name' and user_id='$userid' ";
$result6=$conn->query($sql6);
 while($row6 = $result6->fetch_assoc())
 
  {
  $v_no= $row6['inv_no'];
  $date= $row6['date'];
 
  $qty= $row6['qty'];
   
  $sql7 = "Insert into inv_ledger (id,date,tr_no,ven_name,discription,dr_amount,cr_amount,user_id)values (null,'$date','$v_no','$item_name','Purchase','$qty','0','$userid')";
$conn->query($sql7);
 
  }
   $sql8 = "SELECT * FROM purchase_details_return where item_name='$item_name' and user_id='$userid' ";
$result8=$conn->query($sql8);
 while($row8 = $result8->fetch_assoc())
 
  {
  $v_no= $row8['inv_no'];
  $date= $row8['date'];

  $qty= $row8['qty'];
  
  $sql9 = "Insert into inv_ledger (id,date,tr_no,ven_name,discription,dr_amount,cr_amount,user_id)values (null,'$date','$v_no','$item_name','$Purchase Return','0','$qty','$userid')";
$conn->query($sql9);
 
  }



?>
                                <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 > Item Name </th>";
echo "<th > Discription </th>";
echo "<th > Qty Dr </th>";
echo "<th > Qty Cr </th>";



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