<?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");
        
$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_transactions_report'] != 1){
?>
<script>window.location="../main/dashboard.php"</script>
<?php
}
?>
   
<html>
    
    <head>

       
        <!-- 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">Transactions Reports</div>
                                
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
                                    <div class="datatable-dashv1-list custom-datatable-overright">
                                    <form method="POST" action="">
                                      <?php 
                        // if($_SESSION['role']=="Admin"){

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

                            ?>
                                         <div class="span12">
                                     
                                            <div class="span4">
                                                <label>Date from</label>
                                                <input type="date" name="datefrom" id="dfrom" required="required">
                                            </div>
                                            <div class="span4">
                                                <label>Date To</label>
                                                <input type="date" name="dateto" id="dto" required="required">
                                            </div>
                                            <div class="span4">
                                                <label>&nbsp; <br></label>
                                                <button type="submit" name="filter" class="btn btn-primary">GO</button>
                                            </div>
                                        </div>
                                        <?php
                         }
                         ?>
                                        </form>
                
        <p id="text"></p>
                                        <div>&nbsp;</div>
                                             <?php 
                        // if($_SESSION['role']=="Admin"){

                        if( $permissions['export_transaction_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 (isset($_POST['filter'])) {
   $datefrom=$_POST['datefrom'];
   $dateto=$_POST['dateto'];
   if ($datefrom>$dateto) {
    echo "<script>alert('Date From $datefrom Cannot Be greater than Date to $dateto'),history.go(-1)</script>";   
   }else{
    $sql= mysqli_query($con,"SELECT *,(select name from users where id=transactions.added_by)as user from transactions where branch_id='$branch' && business_id=$business && date>='$datefrom' && date<='$dateto'");
   }
}else{

 $sql= mysqli_query($con,"SELECT *,(select name from users where id=transactions.added_by)as user from transactions where branch_id='$branch' && business_id=$business");  
}
    

if (mysqli_num_rows($sql) > 0) {
   
    


                                   ?>
                                        <thead>
                                            <tr>
                                                <th  width="10%">S/NO</th>
                                                
                                                   <?php
                                                   //if( $_SESSION['role']=="Super Admin"){ 
                                                   //if( $permissions['transactions_branch'] == 1){
                                                if($permissions['business_id'] != 0 && $permissions['branch_id']==0){
                                                   ?>
                                                    <th  width="10%">Branch</th>
                                                    <?php
                                                    } 
                                                   ?>
                                                    <th >Date</th>
                                                    <th >Amount in</th>
                                                    <th >Amount Out</th>
                                                     <th >Balance</th>
                                                     <th >Reason</th>
                                                    <th >Added By</th>
                                                    
                                            </tr>
                                        </thead>
                                        <tbody id="filtered">
                                        <?php
                                        if (isset($_POST['filter'])) {
                                      $dfrom=$_POST['datefrom'];

                                      $gtbal=mysqli_fetch_array(mysqli_query($conn,"SELECT sum(money_in) as ttin, sum(money_out) as ttout from transactions where date<'$dfrom' && branch_id='$branch'"));
                                                $opbal=$gtbal['ttin']-$gtbal['ttout'];
                                                
                                      ?>
                                      <tr>
                                          <td>#</td>
                                          <td><?php echo $dfrom; ?></td>
                                          <td colspan="2">Openning balance</td>
                                          <td ><?php echo number_format($opbal);?> </td>
                                          <td ># </td>
                                      </tr>

                                       <?php
                                       
                                       }?>
                                        <?php
                                        $n=0;
              while($row = mysqli_fetch_assoc($sql)) {
                
                       $n++; 
                    ?>
                                            <tr class="odd gradeX">
                                                <td><?php echo $n; ?></td>
                                                <td><?php echo $row['date']; ?></td>
                                                <?php
                                                    //if( $_SESSION['role']=="Super Admin"){ 
                                                //if( $permissions['transactions_branch'] == 1){
                                                if($permissions['business_id'] != 0 && $permissions['branch_id']==0){
                                                   ?>
                                                   <td><?php echo $row['branch_name']; ?></td>
                                                   <?php
                                                    } 
                                                   ?>
                                                <td><?php echo $row['money_in']; ?></td>
                                                <td><?php echo $row['money_out']; ?></td>
                                                <td><?php
                                                $add=$row['added_on'];
                                                $tid=$row['id'];
                                                $gtbalance=mysqli_fetch_array(mysqli_query($conn,"SELECT sum(money_in) as ttin, sum(money_out) as ttout from transactions where added_on<='$add' && id<$tid && branch_id='$branch'"));
                                                $tbal=$gtbalance['ttin']+$row['money_in']-$gtbalance['ttout']-$row['money_out'];
                                                echo number_format($tbal);
                                                ?></td>
                                                
                                               
                                                <td><?php echo $row['reason']; ?></td>
                                                <td><?php echo $row['user'];  ?></td>
                                                
                                               
                                                
                                                
                                            </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>