<?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="span9" id="content">


                     <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
                            <div class="navbar navbar-inner block-header">
                                <div class="muted pull-left">Payment Reports</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"){

                                          ?>
                                              <div class="span4">
                                                <label>Supplier</label>
                                                <select class="form-control" name="staff" id="supplier" onchange="filterPayment(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>
<script type="text/javascript">
    function filterPayment(sup){
        document.getElementById("testing").innerHTML=sup;
        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", "paymentfilter.php?supplier="+sup+"&dfrom="+dfrom+"&dto="+dto, true);
  xhttp.send();
    }
</script>
<script type="text/javascript">
    function filterPayment1(sup){
        document.getElementById("dto").disabled=false;
        document.getElementById("testing").innerHTML=sup;
        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", "paymentfilter.php?supplier="+supplier+"&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 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", "paymentfilter.php?supplier="+supplier+"&dfrom="+dfrom+"&dto="+sup, true);
  xhttp.send();
    }
</script>
                                            <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
                                        }
                                      }
                                            ?>
            
                                           
                                        </div>
                                        <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", "paymentfilter2.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", "paymentfilter2.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", "paymentfilter2.php?branc="+branc+"&dfrom="+dfromb+"&dto="+text, true);
  xhttp.send();
            
        }

       
     
        </script>
        <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="false" data-search="true" data-show-columns="true" data-show-pagination-switch="false" 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
include '../includes/connection.php';
  //if( $_SESSION['role']=="Super Admin"){ 
  if($permissions['business_id'] != 0 && $permissions['branch_id']==0){   
   $sql= mysqli_query($con,"SELECT id,amount_paid,voucher_no,AddedOn,purchase_id,(select distinct supplier from purchases where purchase_id=purchase_payments.purchase_id limit 1)as supplier,(select name from users where id=purchase_payments.AddedBy)as user,(SELECT branch_name from branches where branch_id=purchase_payments.branch_id) as branch_name from purchase_payments where business_id='$business'");                             // 
}else{
 $sql= mysqli_query($con,"SELECT id,amount_paid,voucher_no,AddedOn,purchase_id,(select distinct supplier from purchases where purchase_id=purchase_payments.purchase_id limit 1)as supplier,(select name from users where id=purchase_payments.AddedBy )as user from purchase_payments where branch_id='$branch'");   
}




if (mysqli_num_rows($sql) > 0) {
   
    


                                   ?>
                                        <thead>
                                            <tr>
                                                <th  width="10%">ID</th>
                                                
                                                   <?php
                                                    //if( $_SESSION['role']=="Super Admin"){
                                                    //if( $permissions['view_paymentlogs_branch'] == 1){ 
                                                   if($permissions['business_id'] != 0 && $permissions['branch_id']==0){
                                                      
                                                   ?>
                                                    <th  width="10%">Branch</th>
                                                    <?php
                                                    } 
                                                   ?>
                                                    <th >Date</th>
                                                    <th >Amount Paid</th>
                                                    <th >Voucher No</th>
                                                     <th >Paid To</th>
                                                    <th >Paid By</th>
                                                    <?php
                                                    //if( $_SESSION['role']!="Super Admin"){
                                                    if( $permissions['view_voucher'] == 1){ 
                                                   ?>
                                                    <th >Voucher</th>
                                                 <?php
                                                    } 
                                                   ?>
                                            </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['role']=="Super Admin"){
                                                    //if( $permissions['view_paymentlogs_branch'] == 1){ 
                                                      //if( $permissions['view_paymentlogs_branch'] == 1){
                                                if($permissions['business_id'] != 0 && $permissions['branch_id']==0){
                                                   ?>
                                                   <td><?php echo $row['branch_name']; ?></td>
                                                   <?php
                                                    } 
                                                   ?>
                                                <td><?php echo $row['AddedOn']; ?></td>
                                                <td><?php echo $row['amount_paid']; ?></td>

                                                <td><?php echo $row['voucher_no']; ?></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>
                                                <td><?php echo $row['user'];  ?></td>
                                                 <?php
                                                    //if( $_SESSION['role']!="Super Admin"){ 
                                                if( $permissions['view_voucher'] == 1){
                                                   ?> 
                                                <td>
                                                    <?php
                                                    $purchid=$row['purchase_id'];
                                                    $selpid=mysqli_fetch_array(mysqli_query($con,"SELECT max(id) as mid from purchase_payments where purchase_id='$purchid'"));
                                                    $mid=$selpid['mid'];
                                                    $selmxvoucher=mysqli_fetch_array(mysqli_query($con,"SELECT voucher_no from purchase_payments where id='$mid'"));
                                                    ?>
                                              <a href="reprint_voucher.php?voucher=<?php echo $selmxvoucher['voucher_no']; ?>"><button class="btn btn-primary"><i class="icon-print icon-white"></i></button></a>
                                                </td>
                                                <?php
                                                    } 
                                                   ?>
                                                
                                                
                                            </tr>
               <?php                             
    }
} else {
    echo "0 results";
}

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