<?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>
<html>
<?php include '../includes/connection.php'; 
  include("../includes/header-links.php");
?>
    
    <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_expenses'] != 1){
?>
<script>window.location="../main/redirect.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="muted pull-left">Expenses</div>
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
  									<h2>Expense Manager</h2>
  
  <ul class="nav nav-tabs">
    <?php 
                        if( $permissions['add_expense'] == 1){
                            ?>
     <li class="active"><a data-toggle="tab" href="#home">Add Expense</a></li>
     <?php
                         }
                             ?>

    <?php
                    //if($_SESSION['role']=="admin" || $_SESSION['role']=="Super Admin"  )
    if( $permissions['view_expense_categories'] == 1)
                    { 
                      ?>

    <li><a data-toggle="tab" href="#menu10">Expense Categories</a></li>
    

<?php } ?>


 <?php
                    //if($_SESSION['role']=="admin" || $_SESSION['role']=="Super Admin"  )
    if( $permissions['view_expense_Report'] == 1)
                    { 
                      ?>
    <li><a data-toggle="tab" href="#menu3">Expense Report</a></li>
    <?php } ?>
    
  </ul>

  <div class="tab-content">
    <div id="home" class="tab-pane fade in active">
      <div class="row-fluid">
      <?php $date=date('Y-m-d'); 
    ?>
       <div class="span6">

        <form class="form-horizontal" action="" method="POST"> 
        <?php 
         if($_SESSION['branch_id']=="0"){
         // if( $permissions['view_expense_branch'] == 1){
          ?>
         <div class="control-group">
          <label class="control-label" for="bra">Branch</label>
          <div class="controls">
            <select  class="input-xlarge focused" name="branch">
                                              <option disabled selected value="">Select Branch</option>
<?php
$sql = "SELECT * FROM branches where status=1 && business_id='$business'";
$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['branch_id']; ?>"><?php echo $row['branch_name']; ?></option>
        <?php
    }
} else {
    echo "0 results";
}

?>
</select>
          </div>
        </div>
         <?php
         }
         ?>
        <div class="control-group">
          <label class="control-label" for="date01">Date</label>
          <div class="controls">
            <input type="date" class="form-control" id="date01" name="date" value="<?php echo $date; ?>">
          </div>
        </div> 
      <div class="control-group">
                                          <label class="control-label" for="select01">Select Category</label>
                                          <div class="controls">
                                            <select id="select01" class="chzn-select" name="category">
                                              <option disabled="" selected>Select Categories</option>
<?php
 // if( $permissions['view_expense_categories'] == 1){
if($_SESSION['branch_id']=="0"){
      $sql = "SELECT * FROM expense_categories where business_id='$business'";
     }else{
$sql = "SELECT * FROM expense_categories 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['categoryName']; ?></option>
        <?php
    }
} else {
    echo "0 results";
}

?>

                                            </select>
                                          </div>
                                        </div>
                                        <div class="control-group">
          <label class="control-label" for="date01">Amount</label>
          <div class="controls">
            <input type="number" placeholder="Enter Amount" name="amount">
          </div>
        </div>
        <div class="control-group">
                                          <label class="control-label" for="textarea2" >Description</label>
                                          <div class="controls">
                                            <textarea class="form-control textarea" placeholder="Enter text ..." style=" height: 80px" name="description"></textarea>
                                          </div>
                                        </div>
                                        
            <div class="form-actions">
                                          <button type="submit" name="save_expense" class="btn btn-primary">Save changes</button>
                                          <button type="reset" class="btn">Cancel</button>
                                        </div>

           </form>
<?php
if(isset($_POST['save_expense'])){
    $date=$_POST['date'];
    $Category=$_POST['category'];
    $description=$_POST['description'];
    $amount=$_POST['amount'];
    $userid=$_SESSION['id'];
    $date_created=date("Y-m-d h:i:sa");
    if($_SESSION['branch_id']=="0"){
      $brancc=$_POST['branch'];
     }else{
$brancc=$_SESSION['branch_id'];
     }


    $sql = "INSERT INTO expenses (category, description, amount, expense_by, date, date_created,branch_id,business_id)
VALUES ('$Category', '$description', '$amount','$userid','$date','$date_created','$brancc','$business')";

if (mysqli_query($conn, $sql)) {
  $gtcat=mysqli_fetch_array(mysqli_query($con,"SELECT categoryName from expense_categories where id=$Category"));
  $catt=$gtcat['categoryName'];
  $activity="Made an expense of<br>
  <b>Amount: </b>".$amount."<br>
  <b>Category: </b>".$catt."<br>
  <hr>
  <b>Description: </b>".$description.";

  ";
  $reason=$catt." purchases";
  $date=date('Y-m-d');
  $log_sql=mysqli_query($conn,"INSERT INTO logs(user_id, activity,branch_id,business_id) VALUES 
('$id_user','$activity','$brancc','$business')");
  $log_sqtt=mysqli_query($con,"INSERT INTO transactions(money_out,reason,branch_id,business_id,date,added_by) VALUES 
('$amount','$catt','$brancc','$business','$date','$id_user')");

if ($log_sql) {
    ?><meta http-equiv="refresh" content="0;URL='expenses.php'" /><?php
} else {
    echo "Error: Failed to add log <br>" . mysqli_error($conn);
}
     
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

}
?>
       </div>
       
       <div class="span6">
          <!--Right div content -->
        <div class="block">
                            <div class="navbar navbar-inner block-header">
                                <div class="muted pull-left">Recent Expenses</div>
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
                                    
                                   <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"}'>
                                        <thead>
                                            <tr>
                                                <th>S/NO</th>
                                                 <?php
                                   if($_SESSION['branch_id']=="0" ){
                                                  // if( $permissions['view_expense_branch'] == 1){
                                    ?>
                                                <th>Branch</th>
                                                <?php
                                               }
                                               ?>
                                               <th>Category</th>
                                                <th>Amount</th>
                                                <th>Description</th>
                                                <th>Expense Date</th>
                                                <th>Date entered</th>
                                                
                                                <?php
                                                 //if($_SESSION['role']=="admin" || $_SESSION['role']=="Super Admin"){
                                                if( $permissions['edit_expense_catgeories'] == 1){
                                                ?>
                                                  <th>edit</th>
<?php }?>
<?php
                                                 //if($_SESSION['role']=="admin" || $_SESSION['role']=="Super Admin"){
                                                if( $permissions['delete_expense_catgeories'] == 1){
                                                ?>
                                                <th>Delete</th>
                                                <?php }?> 
                                            </tr>
                                        </thead>
                                        <tbody>
<?php

                    if($_SESSION['branch_id']=="0" ){
$sql = "SELECT id, description,date_created, (select categoryName from expense_categories where id=expenses.category) as category, amount, date,(select branch_name from branches where branch_id=expenses.branch_id) as bchname FROM expenses where business_id='$business' order by id DESC";
}else{
   $sql = "SELECT id, description,date_created, (select categoryName from expense_categories where id=expenses.category) as category, amount, date FROM expenses where  branch_id='$branch' order by id DESC ";

}
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
  $cnn=0;
    while($row = mysqli_fetch_assoc($result)) {
      $cnn++;
        ?>
        <tr class="odd gradeX">
                                                <td><?php echo $cnn; ?></td>
                                                <?php
                                 if($_SESSION['branch_id']=="0" ){
                                                // if( $permissions['view_expense_branch'] == 1){
                                    ?>
                                                <td><?php echo $row['bchname']; ?></td>
                                                <?php
                                               }
                                               ?>
                                                <td><?php echo $row['category']; ?></td>
                                                <td class="center"><?php echo $row['amount']; ?></td>
                                                <td class="center"><?php echo $row['description']; ?></td>
                                                <td class="center"><?php echo $row['date']; ?></td>
                                                <td class="center"><?php echo $row['date_created']; ?></td>
                                                

                                                <?php

                                                if( $permissions['edit_expense_catgeories'] == 1){
                                                  ?>
                                            <td class="center">
                                                <a href="#editexp<?php echo $row['id']; ?>" data-toggle="modal" ><button class="btn btn-primary"><i class="icon-edit icon-white"></i></button></a>
                                                                                  
                                                                                  
                                                  <div id="editexp<?php echo $row['id']; ?>" class="modal hide">
                                                      <div class="modal-header" style="text-align: center; background-color: #08c; color: white;">
                                                        <button data-dismiss="modal" class="close" type="button">&times;</button>
                                                        <h3>Expense</h3>
                                                      </div>
                                                      <div class="modal-body">
                                                        
                                                        <form action="" method="POST">
                                                          <input type="hidden" name="editid" value="<?php echo $row['id']; ?>">
                                                        <label style=" font-size: 20px; ">Amount</label>
                                                        <input type="text" name="ammoount" value="<?php echo $row['amount'];?>" class="input-xlarge focusedform-control"  required style="width: 95%; height: 30px; font-size: 20px;">
                                                        
                                                        <div class="span12" >
                                                          <div class="span8">
                                                                          <button type="submit" name="changeamount" class="btn btn-primary">Update </button>
                                                                        </div>
                                                                         
                                                                      </div>
                                                        </form>

                                                      </div>
                                                      <div class="modal-footer">
                                                        
                                                        <a data-dismiss="modal" class="btn btn-warning" href="#">cancel</a>
                                                      </div>
                                                    </div>
                                                </td>
                                                <?php }?>


                                                <?php
                                               if( $permissions['delete_expense_catgeories'] == 1)

                                                {?>
                                                <td class="center"><a href="delete_expense.php?id=<?php echo$row['id'];?>"><button class="btn btn-danger"><i class="icon-trash icon-white"></i></button></a></td>
                                                <?php }?>
                                            </tr>
                                            <?php
    }
} else {
    echo "0 results";
}
?>
                                            
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
<?php
if (isset($_POST['changeamount'])) {
  $editid=$_POST['editid'];
  $ammoount=$_POST['ammoount'];
  $ss=mysqli_query($con,"UPDATE expenses set amount='$ammoount' where id='$editid' ");
  if ($ss) {
    echo "<script>
  alert('Expense amount changed successfully');
  window.location='expenses.php'
  </script>";
  }else{
    echo "Failed".mysqli_error($con);
  }
}
?>
          <!--Right div content -->

      </div>   
      </div>
    </div>
    <div id="menu10" class="tab-pane fade">
      <div class="row-fluid">
      <div class="span6">
         <form action="" method="POST">
          <?php 
         if($_SESSION['branch_id']=="0"){ ?>
         <div class="control-group">
          <label class="control-label" for="branc">Branch</label>
          <div class="controls">
            <select  class="input-xlarge focused" name="branchc" class="span6 m-wrap">
                                              <option disabled selected value="">Select Branch</option>
<?php
$sql = "SELECT * FROM branches where status=1 && business_id='$business'";
$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['branch_id']; ?>"><?php echo $row['branch_name']; ?></option>
        <?php
    }
} else {
    echo "0 results";
}

?>
</select>
          </div>
        </div>
         <?php
         }
         ?>

             <div class="control-group">
                                <label class="control-label">New Category<span class="required">*</span></label>
                                <div class="controls">
                                    <input type="text" name="category" data-required="1" class="span6 m-wrap" required="required" />
                                </div>
                            </div>
                            <div class="control-group">
                                
                                <div class="controls">
                                    <button name="save_category" type="submit" class="btn btn-primary btn-rounded">Save</button>
                                    <button type="reset" class="btn btn-danger btn-rounded">Cancel</button>
                                </div>
                            </div>
         </form>
         <?php
if(isset($_POST['save_category'])){
$category=$_POST['category'];
$date=date("Y/m/d");
$userr=$_SESSION['id'];
if($_SESSION['branch_id']=="0"){
// if( $permissions['view_expense_branch'] == 1){
      $branccc=$_POST['branchc'];
     }else{
$branccc=$_SESSION['branch_id'];
     }
$checkcategory=mysqli_query($conn,"SELECT * from expense_categories where categoryName='$category' && branch_id='$branccc'");
if (mysqli_num_rows($checkcategory)>0) {
  echo "<script> alert('category $category already exixts'),history.go(-1)</script>";
}else{

    $sql = "INSERT INTO expense_categories (categoryName, created_by, date_created,branch_id,business_id)
VALUES ('$category', '$userr', '$date','$branccc','$business')";

if (mysqli_query($conn, $sql)) {
     $activity="Added a new Expense category: ".$category;
  $log_sql=mysqli_query($conn,"INSERT INTO logs(user_id, activity,branch_id,business_id) VALUES 
('$id_user','$activity','$branccc','$business')");

if ($log_sql) {
    ?>
   <script type="text/javascript">
     window.location='expenses.php';
   </script> 
   <?php
} else {
    echo "Error: Failed to add log <br>" . mysqli_error($conn);
}
  
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

}
}

         ?>
      </div>
      <div class="span6">
          <table class="table table-bordered table-striped">
                                      <thead>
                                        <tr>
                                          <th>S/No</th>
                                          <?php
                                          if ($_SESSION['branch_id']=='0') {
                                          // if( $permissions['view_expense_branch'] == 1){
                                           ?>
                                           <th>Branch</th>
                                           <?php
                                          }
                                          ?>
                                          <th>Category Name</th>
                                             <?php
                                                 // if($_SESSION['role']=="admin"){
                                                 if( $permissions['edit_expense_catgeories'] == 1){ 
                                                  ?>
                                          <th>Action</th>
                                          <?php } ?>
                                        </tr>
                                      </thead>
                                      <tbody>
               <?php
               if($_SESSION['branch_id']=="0"){
               // if( $permissions['view_expense_branch'] == 1){
$sql = "SELECT *,(select branch_name from branches where branch_id=expense_categories.branch_id) as bchname FROM expense_categories where business_id='$business'";

     }else{
$sql = "SELECT * FROM expense_categories where branch_id='$branch'";

     }
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
  $cn=0;
    while($row = mysqli_fetch_assoc($result)) {
      $cn++;
        ?>
<tr>
  <td><?php echo $cn; ?></td>
  <?php
  if ($_SESSION['branch_id']=='0') {
   ?>
   <td><?php echo $row['bchname']; ?></td>
   <?php
  }
  ?>
  <td><?php echo $row['categoryName']; ?></td>

  <?php
                                                 //if($_SESSION['role']=="admin"){
                                                 if( $permissions['edit_expense_catgeories'] == 1){ 
                                                  ?>
  <td>
    <a href="#edit<?php echo $row['id']; ?>" data-toggle="modal" ><button class="btn btn-primary"><i class="icon-edit icon-white"></i></button></a>
                                                  
                                                  
                                                  
                  <div id="edit<?php echo $row['id']; ?>" class="modal hide">
                      <div class="modal-header" style="text-align: center; background-color: #08c; color: white;">
                        <button data-dismiss="modal" class="close" type="button">&times;</button>
                        <h3>Category</h3>
                      </div>
                      <div class="modal-body">
                        <?php  
                        $sid=$row['id'];
                        $values=mysqli_fetch_array(mysqli_query($con,"SELECT * from expense_categories where id='$sid'"));
                        $eid=$values['id'];
                        $ccname=$values['categoryName'];
                      
                        ?>
                        <form action="" method="POST">
                          <input type="hidden" name="editid" value="<?php echo $eid;?>">
                        <label style=" font-size: 20px; ">Category Name</label>
                        <input type="text" name="cname" value="<?php echo $ccname;?>" class="input-xlarge focusedform-control"  required style="width: 
                        95%; height: 30px; font-size: 20px;">
                        
                        <div class="span12" >
                          <div class="span8">
                                          <button type="submit" name="editcategory" class="btn btn-primary">Update </button>
                                        </div>
                                         
                                      </div>
                        </form>

                      </div>
                      <div class="modal-footer">
                        
                        <a data-dismiss="modal" class="btn btn-warning" href="#">cancel</a>
                      </div>
                    </div>
  </td>
  <?php } ?>
  
</tr>
        <?php
    }
} else {
    echo "0 results";
}
?>
                                        
                                        
                                      </tbody>
                                    </table>
                                    <?php
                                   if(isset($_POST['editcategory'])){
                                      $cname=$_POST['cname'];
                                      $editid=$_POST['editid'];
                                      $checkduplicate=mysqli_query($con,"SELECT * from expense_categories where categoryName='$cname' && branch_id='$branch' && id!='$editid'");
                                      if (mysqli_num_rows($checkduplicate)>0) {
                                        echo"<script>alert('Failed. Category $cname already exists. Correct your inputs'),history.go(-1)</script>";
                                      }else{
                                        $sqlupc=mysqli_query($con,"UPDATE expense_categories set categoryName='$cname' where id='$editid'");
                                        if ($sqlupc) {
                                         $id_user=$_SESSION['id'];
  $activity="Edited Category ".$cname;
  $log_sql=mysqli_query($con,"INSERT INTO logs(user_id, activity,branch_id,business_id) VALUES 
('$id_user','$activity','$branch','$business')");
  
  echo "<script>
  
  window.location='expenses.php'
  </script>";
                                        }else{
                                          echo "Error <br>". mysqli_error($con);
                                        }
                                      }

                                    }
                                    ?>
      </div>
          
      </div>
    </div>
    
    <div id="menu3" class="tab-pane fade">
   
      <div class="row-fluid">
         <div class="datatable-dashv1-list custom-datatable-overright">
          <div class="span12">
                                   <div class="table-toolbar">
                                      <div class="row-fluid">
                                        <div class="span12">
                                            <div class="span3">
                                                <label>Category</label>
                                                <select class="form-control" name="staff" id="category" onchange="filterData(this.value)">
                                                    <option disabled selected value="">select Category</option>
                                                     <?php 
                                                    include '../includes/connection.php';
                                                    $sqls1=mysqli_query($con,"SELECT * FROM expense_categories where branch_id='$branch'");
                                                while ($rows1=mysqli_fetch_array($sqls1)) {
                                                    
                                                    ?>
                                                    <option value="<?php echo $rows1['id']; ?>"><?php echo $rows1['categoryName']; ?></option>
                                                    <?php
                                                }
                                                     ?>
                                                </select>
                                            </div>
                                             <div class="span3">
                                                <label>User</label>
                                                <select class="form-control" name="staff" id="user" onchange="checkByuser(this.value)">
                                                    <option disabled selected value="">select User</option>
                                                     <?php 
                                                    include '../includes/connection.php';
                                                    $sqls11=mysqli_query($con,"SELECT * from users where branch_id='$branch'");
                                                while ($rows11=mysqli_fetch_array($sqls11)) {
                                                     
                                                    ?>
                                                    <option value="<?php echo $rows11['id']; ?>"><?php echo $rows11['name']; ?></option>
                                                    <?php
                                                }
                                                     ?>
                                                </select>
                                            </div>
                                            <div class="span3">
                                                <label>Date from</label>
                                                <input type="date" name="datefrom" id="dfrom" onchange="filterData1(this.value)">
                                            </div>
                                            <div class="span3">
                                                <label>Date To</label>
                                                <input type="date" name="dateto"  id="dto" onchange="filterData2(this.value)" disabled>
                                            </div>
                                           
                                        </div>
                                        <div id="text" style="display: none;"></div>
                                       <script type="text/javascript">
            function filterData(val){
                var text=val;
                document.getElementById("text").innerHTML=text;
                var user=document.getElementById("user").value;
                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", "filterexpenses.php?category="+text+"&user="+user+"&dfrom="+dfrom+"&dto="+dto, true);
  xhttp.send();
            
        }

       
     
        </script>
   <script type="text/javascript">
            function checkByuser(val){
                var text=val;
                document.getElementById("text").innerHTML=text;
                var category=document.getElementById("category").value;
                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", "filterexpenses.php?user="+text+"&category="+category+"&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 category=document.getElementById("category").value;
                var user=document.getElementById("user").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", "filterexpenses.php?dfrom="+text+"&category="+category+"&user="+user+"&dto="+dto, true);
  xhttp.send();
            
        }

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

       
     
        </script>                                      

                                    <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="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"}'>
                                        <thead>
                                            <tr>
                                                <th data-field="id">S/No</th>
                                                  <?php
                                                if($_SESSION['branch_id']=="0"){
                                                  // if( $permissions['view_expense_branch'] == 1){
                                                  ?>
                                                  <th>Branch</th>
                                                  <?php
                                                }
                                                ?>
                                                <th>Category</th>
                                                <th>Amount</th>
                                                <th>Description</th>
                                                <th>Date</th>
                                                <th>Date Created</th>
                                                <th>Created By</th>
                                               
                                            </tr>
                                        </thead>
                                        <tbody id="filtered">
                                        
<?php
if($_SESSION['branch_id']=="0"){
// if( $permissions['view_expense_branch'] == 1){
     $sql = "SELECT id, description, (select categoryName from expense_categories where id=expenses.category) as category, amount, date, date_created, (select name from users where id=expenses.expense_by)as name,(select branch_name from branches where branch_id=expenses.branch_id) as bchname FROM expenses where business_id='$business' order by id DESC";

     }else{
      $sql = "SELECT id, description, (select categoryName from expense_categories where id=expenses.category) as category, amount, date, date_created, (select name from users where id=expenses.expense_by)as name FROM expenses where branch_id='$branch' order by id DESC";

     }
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
  $exn=0;
  $total=0;
    while($row = mysqli_fetch_assoc($result)) {
      $exn++;
  $total=$total+$row['amount'];

        ?>
<tr>
    <td><?php echo $exn?></td>
      <?php
if($_SESSION['branch_id']=="0"){
      // if( $permissions['view_expense_branch'] == 1){
  ?>
 <td><?php echo $row['bchname']; ?></td>
  <?php
}
?>
    <td><?php echo $row['category']; ?></td>
    <td><?php echo $row['amount']; ?></td>
    <td><?php echo $row['description']; ?></td>
    <td><?php echo $row['date']; ?></td>
    <td><?php echo $row['date_created']; ?></td>
    <td><?php echo $row['name']; ?></td>
   
   
</tr>
        <?php
    }
    ?><tr>
      <td colspan="2"><strong>TOTAL</strong></td>
      <td colspan="4"><strong><?php echo $total; ?></strong></td>
    </tr>
    <?php
} else {
    echo "0 results".mysqli_error($conn);
}
?>
                                            
                                           
                                        </tbody>
                                    </table>
                                </div> 
      </div>
    </div>
  </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>

        <link href="../vendors/datepicker.css" rel="stylesheet" media="screen">
        <link href="../vendors/uniform.default.css" rel="stylesheet" media="screen">
        <link href="../vendors/chosen.min.css" rel="stylesheet" media="screen">

        <link href="../vendors/wysiwyg/bootstrap-wysihtml5.css" rel="stylesheet" media="screen">

        <script src="../vendors/jquery-1.9.1.js"></script>
        <script src="../vendors/jquery.uniform.min.js"></script>
        <script src="../vendors/chosen.jquery.min.js"></script>
        <script src="../vendors/bootstrap-datepicker.js"></script>

        <script src="../vendors/wysiwyg/wysihtml5-0.3.0.js"></script>
        <script src="../vendors/wysiwyg/bootstrap-wysihtml5.js"></script>

        <script src="../vendors/wizard/jquery.bootstrap.wizard.min.js"></script>
        <script type="text/javascript" src="../vendors/jquery-validation/dist/jquery.validate.min.js"></script>
    <script src="../assets/form-validation.js"></script>
        
    <script src="../assets/scripts.js"></script>
        <script>
        $(function() {
            
        });
        </script>
        <script>

    jQuery(document).ready(function() {   
       FormValidation.init();
    });
    

        $(function() {
            $(".datepicker").datepicker();
            $(".uniform_on").uniform();
            $(".chzn-select").chosen();
            $('.textarea').wysihtml5();

            $('#rootwizard').bootstrapWizard({onTabShow: function(tab, navigation, index) {
                var $total = navigation.find('li').length;
                var $current = index+1;
                var $percent = ($current/$total) * 100;
                $('#rootwizard').find('.bar').css({width:$percent+'%'});
                // If it's the last tab then hide the last button and show the finish instead
                if($current >= $total) {
                    $('#rootwizard').find('.pager .next').hide();
                    $('#rootwizard').find('.pager .finish').show();
                    $('#rootwizard').find('.pager .finish').removeClass('disabled');
                } else {
                    $('#rootwizard').find('.pager .next').show();
                    $('#rootwizard').find('.pager .finish').hide();
                }
            }});
            $('#rootwizard .finish').click(function() {
                alert('Finished!, Starting over!');
                $('#rootwizard').find("a[href*='tab1']").trigger('click');
            });
        });
        </script>

    </body>

</html>