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

?>
<!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>
								<h4><center><? echo "For the Period of From:" . $from_date . "To:" . $to_date; ?></center></h4>
								<h4><center><? echo $ven_name; ?></center></h4>
                            </div>
                            <div class="card-body">
							<?php

include 'dbc.php';

$sql99 = "delete from inv_ledger";
$conn->query($sql99);
$userid=$_SESSION["userid"];

$sql88 = "SELECT * FROM items where user_id='$userid'";
$result88=$conn->query($sql88);
while($ppp88 = $result88->fetch_assoc())
{
  $item_name=$ppp88["item_name"];
  $tfeerec=0;
  $tfeerec1=0;
  $bal=0;
	$sql1 = "SELECT * FROM invoice_details where item_name='$item_name'  and date between '$from_date' and '$to_date' and user_id='$userid' ";
$result1=$conn->query($sql1);
 while($row1 = $result1->fetch_assoc())
 
  {
  $v_no= $row1['inv_no'];
  $date= $row1['date'];
  $qty= $row1['qty'];
  $netamount= $row1['netamount'];
  $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','$qty','$netamount','$userid')";
$conn->query($sql2);
  
  }
  
  $sql1st = "SELECT * FROM invoice_details_st where item_name='$item_name' and date between '$from_date' and '$to_date' and user_id='$userid' ";
$result1st=$conn->query($sql1st);
 while($row1st = $result1st->fetch_assoc())
 
  {
  $v_no= $row1st['inv_no'];
  $date= $row1st['date'];
  
 
  $qty= $row1st['qty'];
  $netamount= $row1st['netamount'];
  $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','$qty','$netamount','$userid')";
$conn->query($sql2st);
  }
  

?>
                                <table id="bootstrap-data-table-export" class="table table-striped table-bordered">
								
                                    <thead>
									<?php
									 echo "<tr>";
echo "<th ><h2> $item_name </h2></th>";



echo "</tr>";
                                       echo "<tr>";
echo "<th > Date </th>";
echo "<th > Voucher# </th>";
echo "<th > Item Name </th>";
echo "<th > Discription </th>";
echo "<th > Sale Qty </th>";
echo "<th >Sale Amount</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>";
	 
	  }
  ?>
                                       
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>


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

    <!-- Right Panel -->

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

</body>

</html>
<?php
}
?>