<?php
session_start();
error_reporting(0);
@ini_set('display_errors', 0);
$id_user=$_SESSION['id'];
$branch=$_SESSION['branch_id'];
$business=$_SESSION['business_id'];
?>
<!DOCTYPE html>
 <?php include '../includes/connection.php'; 
  include("../includes/header-links.php");
  ?>
<html>
    
    <head>
       <?php include("../includes/header-links.php");
$permissions = mysqli_fetch_array(mysqli_query($con,"SELECT * FROM  
  permissions,users WHERE  permissions.role_id = users.role 
  AND users.id='$id_user' AND users.business_id ='$business' AND users.branch_id ='$branch'"));

if($permissions['view_individaul_product_purchases'] != 1){
?>
<script>window.location="../main/dashboard.php"</script>
<?php
}
?>
       
        <!-- Bootstrap -->
        <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
        <link href="../bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
        <link href="../assets/styles.css" rel="stylesheet" media="screen">
        <link href="../assets/DT_bootstrap.css" rel="stylesheet" media="screen">
        <link rel="stylesheet" href="../data-table-css/bootstrap-table.css">
    <link rel="stylesheet" href="../data-table-css/bootstrap-editable.css">
        <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="vendors/flot/excanvas.min.js"></script><![endif]-->
        <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
            <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <script src="../vendors/modernizr-2.6.2-respond-1.1.0.min.js"></script>
    </head>
    
    <body>
        <?php include '../includes/header.php'; ?>
        <div class="container-fluid">
            <div class="row-fluid">
                <?php include '../includes/sidebar.php'; ?>
                <!--/span-->
                <div class="span10" id="content">


                     <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
                            <div class="navbar navbar-inner block-header">
                            <div class="pull-right">
                            <?php
                             // if( $_SESSION['role']=="admin"){
                            if( $permissions['new_individual_product_purchases'] == 1){
                              
                                    ?>
                                         <a href="../purchases/new_purchase.php"><button class="btn btn-info">New Purchase <i class="icon-plus icon-white"></i></button></a>
                                         <?php
                                       }
                                         ?>
                                      </div>
                                
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
                                   <div class="table-toolbar">
                                      <div class="row-fluid">
                                        <div class="span12">
                                        <?php
                                        // if( $_SESSION['role']=="admin"){
                                        if( $permissions['filter_individual_product_purchases'] == 1){
                                    ?>
                                               <div class="span4">
                                        <label>By Product</label>
                                          <select class="form-control" id="selectedProduct" name="product" onchange="filterData1(this.value)">
                                            <option value="" selected="" disabled="">Select product</option>
                                            
                                            <?php
                                            include '../includes/connection.php';
                                            $sql = "SELECT * FROM products where branch_id='$branch'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        ?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['product_name']; ?></option>
        <?php
    }
} else {
    echo "0 results";
}
?>
                                          </select>
                                        </div>
                                            <div class="span4">
                                                <label>Date from</label>
                                                <input type="date" name="datefrom" id="dfrom" onchange="filterData1(this.value)">
                                            </div>
                                            <div class="span4">
                                                <label>Date To</label>
                                                <input type="date" name="dateto"  id="dto" onchange="filterData2(this.value)" disabled>
                                            </div>

                                           <?php 
                                         }
                                    ?>
                                    
                                        </div>
                                         
                                      </div>
<script type="text/javascript">
            function filterData(valt){
                var text=valt;
                document.getElementById("dto").disabled=false;
                document.getElementById("text").innerHTML=text;
                var dfrom=document.getElementById("dfrom").value;
                var dto=document.getElementById("dto").value;
                var xhttp = new XMLHttpRequest();
       xhttp.onreadystatechange = function() {
         if (this.readyState == 4 && this.status == 200) {
        document.getElementById("filtered").innerHTML =
      this.responseText;
      };
  }
  xhttp.open("GET", "puroductpurchases_filter.php?dfrom="+dfrom+"&dto="+dto+"&product="+text, true);
  xhttp.send();
            
        }

       
     
        </script>

<script type="text/javascript">
            function filterData1(valt){
                var text=valt;
                document.getElementById("dto").disabled=false;
                document.getElementById("text").innerHTML=text;
                var product=document.getElementById("selectedProduct").value;
                var dto=document.getElementById("dto").value;
                var xhttp = new XMLHttpRequest();
       xhttp.onreadystatechange = function() {
         if (this.readyState == 4 && this.status == 200) {
        document.getElementById("filtered").innerHTML =
      this.responseText;
      };
  }
  xhttp.open("GET", "puroductpurchases_filter.php?dfrom="+text+"&dto="+dto+"&product="+product, true);
  xhttp.send();
            
        }

       
     
        </script>
        <script type="text/javascript">
            function filterData2(valt){
                var text=valt;
                document.getElementById("dto").disabled=false;
                document.getElementById("text").innerHTML=text;
                var product=document.getElementById("selectedProduct").value;
                var dfrom=document.getElementById("dfrom").value;
                var xhttp = new XMLHttpRequest();
       xhttp.onreadystatechange = function() {
         if (this.readyState == 4 && this.status == 200) {
        document.getElementById("filtered").innerHTML =
      this.responseText;
      };
  }
  xhttp.open("GET", "puroductpurchases_filter.php?dfrom="+dfrom+"&dto="+text+"&product="+product, true);
  xhttp.send();
            
        }
       
     
        </script>

        
                                      <div id="text"></div>
                                      <div id="txt"></div>
                                      <hr>
                                     <div id="toolbar">
                                        <select class="form-control">
                                                <option value="">Export Basic</option>
                                                <option value="all">Export All</option>
                                                <option value="selected">Export Selected</option>
                                            </select>
                                    </div>
                                    <table  id="table" data-toggle="table" data-pagination="false" data-search="true" data-show-columns="false" data-show-pagination-switch="false" data-show-refresh="false" data-key-events="true" data-show-toggle="false" data-resizable="false" data-cookie="true"
                                        data-cookie-id-table="saveId" data-show-export="true" data-click-to-select="true" data-toolbar="#toolbar" data-export-options='{"fileName": "Testing_filename"}'>
                                    
                                        <thead>
                                            <tr>
                                                <th  width="10%">S/NO</th>
                                                <th width="40%">Date</th>
                                                <th width="40%">Product</th>
                                                <th width="20%">Quantity</th>
                                                <th width="20%">Unit Price</th>
                                                <th width="20%">Total Price</th>
                                            </tr>
                                        </thead>
                                        <tbody id="filtered">
                                        <?php
                                        $sqlxy = "SELECT id,date_time, (select product_name from products where id=purchases.product_id)as product_name, quantity, unit_price, (select symbol from units where UnitID=(select Units from products where id=purchases.product_id))as units  from purchases where branch_id='$branch'";
                                       $resultp = mysqli_query($conn, $sqlxy);
                                        $nid=0;
                                        $totalq=0;
                                        $totalp=0;
                                      // output data of each row
                                     while($rown = mysqli_fetch_assoc($resultp)) {
                                      $nid++;
                                      $totalp=$totalp+$rown['quantity'];
                                      $totalq=$totalq+($rown['quantity']*$rown['unit_price']);

                                  ?>
                            <tr>
                                                      
                              <td width="40%"><?php echo $nid; ?></td>
                              <td width="40%"><?php echo $rown['date_time']; ?></td>
                              <td width="40%"><?php echo $rown['product_name']; ?></td>
                              <td width="20%"><?php echo $rown['quantity']." ".$rown['units']; ?></td>
                              <td width="20%"><?php echo $rown['unit_price']; ?></td>
                              <td width="20%"><?php echo $rown['quantity']*$rown['unit_price']; ?></td>
                              
                            </tr>
                            
                            <?php
       
    }

?>
                                           
                            <tr>
                            <td colspan="2">TOTAL</td>
                              <td colspan="2"><?php echo $totalp; ?></td>
                              <td><?php echo $totalq; ?></td>
                            </tr> 
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                        <!-- /block -->
                    </div>
                </div>
            </div>
            </div>            <hr>
           <?php include '../includes/footer.php'; ?>
        </div>
        <!--/.fluid-container-->
        <script type = "text/javascript">
    function confirmationChange(anchor){
        var conf = confirm("Are you sure you want to Change the Purchase?");
        if(conf){
            window.location = anchor.attr("href");
        }
    }    
    
</script>

        <script src="../vendors/jquery-1.9.1.js"></script>
        <script src="../bootstrap/js/bootstrap.min.js"></script>
        <script src="../vendors/datatables/js/jquery.dataTables.min.js"></script>
<script src="../data-table-js/bootstrap-table.js"></script>
    <script src="../data-table-js/tableExport.js"></script>
    <script src="../data-table-js/data-table-active.js"></script>
    <script src="../data-table-js/bootstrap-table-editable.js"></script>
    <script src="../data-table-js/bootstrap-editable.js"></script>
    <script src="../data-table-js/bootstrap-table-resizable.js"></script>
    <script src="../data-table-js/colResizable-1.5.source.js"></script>
    <script src="../data-table-js/bootstrap-table-export.js"></script>

        <script src="../assets/scripts.js"></script>
        <script src="../assets/DT_bootstrap.js"></script>
        <script>
        $(function() {
            
        });
        </script>
    </body>

</html>