<?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 
$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_payment_report'] != 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">
        
        <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="muted pull-left">Openning Stock </div>
                                
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
                                
                                    
                                    <div class="datatable-dashv1-list custom-datatable-overright">
                                         <div class="span12">
                                         <?php 
                                         // if( $_SESSION['role']=="admin"){
                                         if( $permissions['filter_payment_report'] == 1){
                                         if( $_SESSION['branch_id']!="0"){

                                          ?>
                                              
                                            <!-- <p id="testing"></p>
                                            <div class="span4">
                                                <label>Date from</label>
                                                <input type="date" name="datefrom" id="dfrom" onchange="filterPayment1(this.value)">
                                            </div>
                                            <div class="span4">
                                                <label>Date To</label>
                                                <input type="date" name="dateto" id="dto" onchange="filterPayment2(this.value)" disabled>
                                            </div>
                                             -->
                                            <?php
                                          }
                                        
                                            
                                           else if( $_SESSION['branch_id']=="0"){
                                           
                                           ?>
                                            <!-- <div class="span4">
                                                <label>Branch</label>
                                                <select class="form-control" name="branc" id="branc" onchange="filterDatab(this.value)">
                                                    <option disabled selected value="">select Branch</option>
                                                     <?php 
                                                    include '../includes/connection.php';
                                                    $sqlb=mysqli_query($con,"SELECT * from branches where business_id='$business'");
                                                while ($rowsb=mysqli_fetch_array($sqlb)) {
                                                     $bn=$rowsb['branch_id'];   
                                                   
                                                    $bname=$rowsb['branch_name'];
                                                    ?>
                                                    <option value="<?php echo $bn; ?>"><?php echo $bname; ?></option>
                                                    <?php
                                                }
                                                     ?>
                                                </select>
                                            </div>
                                            <div class="span4">
                                                <label>Date from</label>
                                                <input type="date" name="datefromb" id="dfromb" onchange="filterData1b(this.value)">
                                            </div>
                                            <div class="span4">
                                                <label>Date To</label>
                                                <input type="date" name="datetob"  id="dtob" onchange="filterData2b(this.value)" disabled>
                                            </div> -->
                                              <?php
                                        }
                                      }
                                            ?>
             
                                              <script type="text/javascript">
                                                  function filterPayment1(sup){
                                                      document.getElementById("dto").disabled=false;
                                                      document.getElementById("testing").innerHTML=sup;
                                                      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", "stock_value_filters.php?dfrom="+sup+"&dto="+dto, true);
                                                xhttp.send();
                                                  }
                                              </script>
                                              <script type="text/javascript">
                                                  function filterPayment2(sup){
                                                      document.getElementById("dto").disabled=false;
                                                      document.getElementById("testing").innerHTML=sup;
                                                      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", "stock_value_filters.php?dfrom="+dfrom+"&dto="+sup, true);
                                                xhttp.send();
                                                  }
                                              </script>
                                           
                                        </div>
                                        
        <!-- <p id="text"></p> -->
                                        <div>&nbsp;</div>

                               <?php 
                        // if($_SESSION['role']=="Admin"){

                        if( $permissions['export_payment_reports'] == 1){

                        
                            ?>
                                    <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>

                                    <?php
                         }
                     //     
                             ?>
                                    <table  id="table" data-toggle="table" data-pagination="true" data-search="true" data-show-columns="true" data-show-pagination-switch="true" data-show-refresh="true" data-key-events="true" data-show-toggle="true" data-resizable="true" 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"}'>
                                    <?php
                                    if (isset($_GET['id'])) {
                                    $idd=$_GET['id'];
                                     
                                    }else{
                                    $idd='';

                                    }
include '../includes/connection.php';
  //if( $_SESSION['role']=="Super Admin"){ 
  if($_SESSION['branch_id'] == 0 && $_SESSION['business_id']!=0){   
   $sql= mysqli_query($con,"SELECT *,(SELECT branch_name from branches where branch_id=stock_values.branch_id) as branch_name from stock_values where business_id='$business'");                              
}else{
 $sql= mysqli_query($con,"SELECT *,(SELECT product_name from products where id=stock_value_details.product_id && branch_id=stock_value_details.branch_id)as product from stock_value_details where stock_value_id='$idd' && branch_id='$branch' ");   
}




if (mysqli_num_rows($sql) > 0) {
   
    


                                   ?>
                                        <thead>
                                            <tr>
                                                <th  width="10%">S/NO</th>
                                                
                                                   <?php
                                                    
                                                   if($_SESSION['branch_id'] == 0){
                                                      
                                                   ?>
                                                    <th  width="10%">Branch</th>
                                                    <?php
                                                    } 
                                                   ?>
                                                    <th >Product</th>
                                                    <th >Quanity</th>
                                                    <th >Buying Price</th>
                                                    <th >Selling Price</th>
                                                    <th >Stock Value</th>
                                                    
                                            </tr>
                                        </thead>
                                        <tbody id="filtered">
                                        <?php
                                        $n=0;
              while($row = mysqli_fetch_assoc($sql)) {
                
                       $n++; 
                    ?>
                                            <tr class="odd gradeX">
                                                <td><?php echo $n; ?></td>
                                                <?php
                                                    
                                                if($_SESSION['branch_id'] == 0 ){
                                                   ?>
                                                   <td><?php echo $row['branch_name']; ?></td>
                                                   <?php
                                                    } 
                                                   ?>
                                                <td><?php echo $row['product']; ?></td>
                                                <td><?php echo number_format($row['quantity'],3,'.',', '); ?></td>
                                                <td><?php echo number_format($row['buying_price'],3,'.',', '); ?></td>
                                                <td><?php echo number_format($row['selling_price'],3,'.',', '); ?></td>

                                                <td><?php echo number_format($row['quantity']*$row['buying_price'],3,'.',', '); ?></
                                                
                                                
                                                
                                            </tr>
               <?php                             
    }
} else {
    // echo "0 results";
}


?>
                                            
                                        </tbody>
                                    </table>
                                </div>
                                </div>
                            </div>
                        </div>
                        <!-- /block -->
                    </div>
                </div>
            </div>
            <hr>
            <?php include '../includes/footer.php'; ?>
        </div>
        <!--/.fluid-container-->

        <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>