<!DOCTYPE html>
 <?php include '../includes/connection.php'; 
  include("../includes/header-links.php");
  ?>
<html>
    
    <head>
      <?php include '../includes/header.php'; ?>
      <?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_purchases_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">
        <!--[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>
        
        <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( $permissions['new_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 class="muted pull-left">puchases Report</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['branch_id']!="0"){
                                        if( $permissions['filter_purchases_report'] == 1){
                                    ?>
                                            <div class="span4">
                                                <label>Supplier</label>
                                                <select class="form-control" name="staff" id="supplier" onchange="filterData(this.value)">
                                                    <option disabled selected value="">select supplier</option>
                                                     <?php 
                                                    include '../includes/connection.php';
                                                    $sqls1=mysqli_query($con,"SELECT * from suppliers where branch_id='$branch'");
                                                while ($rows1=mysqli_fetch_array($sqls1)) {
                                                     $suply=$rows1['supplier_id'];   
                                                   
                                                    $sname=$rows1['name'];
                                                    ?>
                                                    <option value="<?php echo $suply; ?>"><?php echo $sname; ?></option>
                                                    <?php
                                                }
                                                     ?>
                                                </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 
                                         }

                                       }
                                    ?>
                                    <?php
                                     if( $permissions['filter_purchases_report'] == 1){
                                     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 
                                  }
                                }
                                    ?>
                                        </div>
                                         
                                      </div>
        <script type="text/javascript">
            function filterData(val){
                var text=val;
                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", "purchase_filter1.php?sup="+text+"&dfrom="+dfrom+"&dto="+dto, 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 supplier=document.getElementById("supplier").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", "purchase_filter1.php?sup="+supplier+"&dfrom="+text+"&dto="+dto, true);
  xhttp.send();
            
        }

       
     
        </script>
        <script type="text/javascript">
            function filterData2(valz){
                var text=valz;
                document.getElementById("text").innerHTML=text;
                var supplier=document.getElementById("supplier").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", "purchase_filter1.php?sup="+supplier+"&dfrom="+dfrom+"&dto="+text, true);
  xhttp.send();
            
        }

       
     
        </script>

        <script type="text/javascript">
            function filterDatab(val){
                var text=val;
                document.getElementById("text").innerHTML=text;
                var dfromb=document.getElementById("dfromb").value;
                var dtob=document.getElementById("dtob").value;
                var xhttp = new XMLHttpRequest();
       xhttp.onreadystatechange = function() {
         if (this.readyState == 4 && this.status == 200) {
        document.getElementById("filtered").innerHTML =
      this.responseText;
      };
  }
  xhttp.open("GET", "purchase_filter2.php?branc="+text+"&dfrom="+dfromb+"&dto="+dtob, true);
  xhttp.send();
            
        }

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

       
     
        </script>
        <script type="text/javascript">
            function filterData2b(valz){
                var text=valz;
                document.getElementById("text").innerHTML=text;
                var branc=document.getElementById("branc").value;
                var dfromb=document.getElementById("dfromb").value;
                var xhttp = new XMLHttpRequest();
       xhttp.onreadystatechange = function() {
         if (this.readyState == 4 && this.status == 200) {
        document.getElementById("filtered").innerHTML =
      this.responseText;
      };
  }
  xhttp.open("GET", "purchase_filter2.php?branc="+branc+"&dfrom="+dfromb+"&dto="+text, 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"}'>
                                    <?php
include '../includes/connection.php';
                                   // Create connection

// if( $_SESSION['role']=="Super Admin"){
//if($permissions['business_id'] != 0 && $permissions['branch_id']==0){
$sql= mysqli_query($con,"SELECT id, total_price,invoice_number, (select count(id) from sales_details where order_id=purchase_totals.id)as count_orders, AddedOn,(select  distinct supplier from purchases where purchase_id=purchase_totals.id limit 1) as supplier,(SELECT branch_name from branches where branch_id=purchase_totals.branch_id) as branch_name from purchase_totals where  business_id='$business' order by id desc");
// }else{
//   $sql= mysqli_query($con,"SELECT id, total_price,invoice_number, (select count(id) from sales_details where order_id=purchase_totals.id)as count_orders, AddedOn,(select  distinct supplier from purchases where purchase_id=purchase_totals.id limit 1) as supplier from purchase_totals where  branch_id='$branch' order by id desc");
// }



if (mysqli_num_rows($sql) > 0) {
   
    


                                   ?>
                                        <thead>
                                            <tr>
                                                <th  width="10%">S/NO</th>
                                                <?php
                                                if( $_SESSION['branch_id']=="0"){
                                                
                                                // if( $permissions['purchase_report_branch'] == 1){
                                                ?>
                                                <th width="10%">Branch</th>
                                                 <?php
                                               }
                                                ?>
                                                    <th width="50%">
                                                    <table background="white">
                                                    <td width="25%">Product</td>
                                                    <td width="15%">Quantity</td>
                                                    <td width="15%">Unit Price</td>
                                                    <td width="15%">Total Price</td>
                                                    <td width="15%">Batch NO</td>
                                                    <td width="15%">Expiry Date</td>
                                                    
                                            </table>
                                                </th>
                                                <?php
                                                if ($gbusiness_settings['record_invoice_number']==1) {
                                                   ?>
                                                <th  width="10%">Invoice Number</th>
                                                <?php }?>
                                                <th  width="10%">Total</th>
                                                <th width="20%">Date</th>
                                                <th width="20%">Supplier</th>
                                                <?php
                                                 //if( $_SESSION['role']=="admin"){
                                                if( $permissions['purchases_return_button'] ){
                                    ?>
                                     <th>Return</th>
                                        <?php 
                                              }
                                                  ?>
                                               
                                               
                                                
                                            </tr>
                                        </thead>
                                        <tbody id="filtered">
                                        <?php
                                        $nid=0;
                                        $total=0;
              while($row = mysqli_fetch_assoc($sql)) {
                $iddx=$row['id'];
                $total=$total+$row['total_price'];
                $nid++;
                        
                    ?>
                                            <tr>
                                                <td><?php echo $nid; ?></td>
                                                 <?php
                                                if( $_SESSION['branch_id']=="0"){
                                                 //if($branch==0 && $business !=0){
                                                 //if($permissions['business_id'] != 0 && $permissions['branch_id']==0){
                                                 // if( $permissions['purchase_report_branch'] == 1){
                                                ?>
                                                <td><?php echo $row['branch_name'];?></td>
                                                 <?php
                                                }
                                                ?>
                                                <td>
                                                    <?php include 'purchases_details.php'; ?>
                                                </td>
                                                <?php
                                                if ($gbusiness_settings['record_invoice_number']==1) {
                                                   ?>
                                                <td><?php echo $row['invoice_number']; ?></td>
                                                <?php }?>
                                                <td><?php echo $row['total_price']; ?></td>
                                                <td><?php echo $row['AddedOn']; ?></td>
                                                <?php 
                                              $nn=  $row['supplier'];
                                                $supname1=mysqli_fetch_array(mysqli_query($con,"SELECT * from suppliers where supplier_id ='$nn'"));
                                                    $sname1=$supname1['name'];
                                                 ?>
                                                <td><?php echo $sname1; ?></td>
                                                <?php
                                                 // if( $_SESSION['role']=="admin"){
                                                   if( $permissions['purchases_return_button'] == 1){
                                    ?>
                                    <td>
                                                <a onclick = "confirmationChange(this); return false;" href="../purchases/return_purchase.php?id=<?php echo $row['id']; ?>"><span class="label label-info"><i class="icon-edit icon-white"></i>&nbsp;Return</span></a>
                                                </td>
                                                <?php 
                                              }
                                                  ?>
                                                
                                            </tr>
                                            
               <?php                             
    }
    ?>
                                           <tr>
                                              <td colspan="3"><strong>TOTAL</strong></td>
                                              <td colspan="4"><strong><?php echo $total; ?></strong></td>
                                            </tr>
    <?php
} else {
    echo "0 results";
}

//mysqli_close($conn);
?>
                                            
                                        </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>