<!DOCTYPE html>
<?php 
 ?>
<html>
    
    <head>
        <?php include("../includes/header-links.php");?>
       
        <!-- Bootstrap -->
        <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
        <link href="../bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
        <link href="../assets/styles.css" rel="stylesheet" media="screen">
        <link href="../assets/DT_bootstrap.css" rel="stylesheet" media="screen">
        <link rel="stylesheet" href="../data-table-css/bootstrap-table.css">
    <link rel="stylesheet" href="../data-table-css/bootstrap-editable.css">
        
        <script src="../vendors/modernizr-2.6.2-respond-1.1.0.min.js"></script>
    </head>
    
    <body>
        <?php include '../includes/header.php'; ?>
        <div class="container-fluid">
            <div class="row-fluid">
                <?php include '../includes/sidebar.php'; ?>
                <!--/span-->
                <div class="span10" id="content">


                     <div class="row-fluid">
                        <!-- block -->
                        <div class="block">
                            <div class="navbar navbar-inner block-header">
                                <div class="muted pull-left">Sales Reports</div>
                                
                            </div>
                            <div class="block-content collapse in">
                                <div class="span12">
                                   <div class="table-toolbar">
                            <div class="row-fluid">
                            <form method="POST" action="">
                              

                                      	<div class="span12">
                                        
                                    	<div class="span3">
                                      	<label>By Staff</label>
                                      		<select class="form-control" name="staff"  id="staf" onchange="checkBystaff(this.value)">
                                            <option value="" selected="" disabled="">Select Staff</option>
                                      			
                                            <?php
                                            include '../includes/connection.php';
                                            $sql = "SELECT * FROM staff where branch_id='$branch' && status=1";
$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['staff_code']; ?>"><?php echo $row['full_name']; ?></option>
        <?php
    }
} else {
    echo "0 results";
}
?>
                                      		</select>
                                      	</div>
                                        
                                        
                                      	<div class="span3">
                                      	<label>By Date from</label>
                                      		<input type="date" name="datefrom" class="form-control" id="dfrom" onchange="checkBystafftwo(this.value)" required="required">
                                      	</div>

                                        <div class="span3">
                                        <label>Time from</label>
                                          <input type="time" name="timefrom" class="form-control" id="dfrom" onchange="checkBystafftwo(this.value)" required="required">
                                        </div>
                                      	<div class="span3">
                                      	<label>By Date to</label>
                                      		<input type="date" name="dateto" class="form-control" id="dto" onchange="checkBystaffthree(this.value)" required="required">
                                      	</div>
                                        <div class="span3">
                                        <label>Time To</label>
                                          <input type="time" name="timeto" class="form-control" id="dfrom" onchange="checkBystafftwo(this.value)" required="required" >
                                        </div>
                                        <div class="span3">
                                        <label><br></label>
                                          <input type="submit" name="submit" class="btn btn-primary" value="SUBMIT" >
                                        </div>
                                        </div>

                                      	
                                        
                                      	</div>
                                        
                                    
                                      </div>
                                      </form>
                                      <hr>
                                        
                                   </div>
                                    
                                    <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"}'>
                                    
                                        <thead>
                                            <tr>
                                                <th>S/NO</th>
                                                
                                                    <th width="50%">
                                                    <table background="white" class="table table-bordered">
                                                    <td width="40%">Product</td>
                                                    <td width="20%">Quantity</td>
                                                    <td width="20%">Unit Price</td>
                                                    <td width="20%">Total Price</td>
                                                    
                                                    
                                                </table></th>
                                                <th  width="10%">Staff</th>
                                                <th  width="10%">User</th>
                                                <th  width="10%">Total</th>
                                                <th width="20%">Date</th>
                                                
                                                
                                            </tr>
                                        </thead>
                                        <?php
                                        if (isset($_POST['submit'])) {
                                          $datefrom1=$_POST['datefrom'];
                                          
                                          $timefrom=$_POST['timefrom'];
                                          $dateto1=$_POST['dateto'];
                                          $timeto=$_POST['timeto'];
                                          $staff=$_POST['staff'];
                                         
                                        $datefrom=$datefrom1." ".$timefrom;
                                          $dateto=$dateto1." ".$timeto;
                                       
                                      ?>
                                        <tbody>
                                        <?php
include '../includes/connection.php';
 
$sql= mysqli_query($con,"SELECT id, total_price,(select count(id) from sales_details where order_id=sales.id)as count_orders, date_time, (select full_name from staff where staff_code=sales.staff)as staffname, (select name from users where id=(select distinct user_id from sales_details where order_id=sales.id))as usernames from sales where staff='$staff' && branch_id='$branch' &&  DATE_FORMAT(date_time, '%Y-%m-%d %H:%i')>='$datefrom' && DATE_FORMAT(date_time, '%Y-%m-%d %H:%i')<='$dateto' AND EXISTS (SELECT * from sales_details where sales_details.order_id=sales.id)");

                                  
                       
                                   // Create connection

//$result = +++($con, $sql);

if (mysqli_num_rows($sql) > 0) {
    // output data of each row
    


   

$totalprice=0;

                                        $cou=1;
              while($row = mysqli_fetch_assoc($sql)) {
        $dpprice=$row['total_price'];
                 
                $iddx=$row['id'];
                       
                       $totalprice=$totalprice+$dpprice;
                    ?>
                                            <tr class="odd gradeX">
                                                <td><?php echo $cou; ?> </td>
                                                
                                                <td>
                                                    <?php include 'sales_details.php'; ?>
                                                </td>
                                                <td><?php echo $row['staffname']; ?></td>
                                                <td><?php echo $row['usernames']; ?></td>
                                                <td><?php echo $row['total_price']; ?></td>
                                                <td><?php echo $row['date_time']; ?></td>
                                                
                                                
                                                
                                            </tr>
               <?php 
               $cou++;                             
    }
    ?>
    
    <tr>
      <td <?php if( $_SESSION['branch_id']=="0"){
                                    ?>
                                    colspan="5"
                                   <?php 
                                  }else{
                                    ?>
                   colspan="4"
                                    <?php } ?>
                                  
                                    ><h4>TOTAL SALES</h4></td>
                                  
      <td colspan="3"><h4> <?php echo number_format($totalprice);?></h4></td>
    </tr>
    <?php
} else {
    echo "0 results".mysqli_error($con);
}

//mysqli_close($conn);
?>
                                            
                                        </tbody>
                                        <?php
                                        }
                                        ?>
                                    </table>
                                </div>
                            </div>
                        </div>
                        <!-- /block -->
                    </div>
                </div>
            </div>
             <script type = "text/javascript">
    function confirmationChange(anchor){
        var conf = confirm("Are you sure you want to Change the Sale?");
        if(conf){
            window.location = anchor.attr("href");
        }
    }    
    
</script>
            <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>