<?php 
require_once('includes/check-login.php');
require_once('includes/header.php'); 
require_once('includes/menu.php'); 
require_once('includes/config.php'); 
$tablename="save_order";	
$addurl="service-view.php";
if($_SESSION['role_id']==1){
$sql="SELECT save_order.order_date,SUM(save_order.grand_total) as total,user_details.name from ".$tablename." LEFT JOIN user_details ON user_details.user_id=save_order.vendor_id WHERE 1=1 and job_status='2'";
$from_date=isset($_GET['from_date']) ? $_GET['from_date'] : '';
$to_date=isset($_GET['to_date']) ? $_GET['to_date'] : '';
$vendor=isset($_GET['vendor']) ? $_GET['vendor'] : ''; 
if(!empty($from_date) && empty($to_date)){
  $sql .=" and order_date='$from_date'";  
}
if(!empty($from_date) && !empty($to_date)){
  $sql .=" and order_date BETWEEN '$from_date' AND '$to_date'";  
}
if(!empty($vendor)){
  $sql .=" and vendor_id='$vendor'";  
}
$sql .=' group by save_order.vendor_id,save_order.order_date order by order_date asc';
}else{
$sql="SELECT save_order.order_date,SUM(save_order.grand_total) as total,user_details.name from ".$tablename." LEFT JOIN user_details ON user_details.user_id=save_order.vendor_id WHERE vendor_id = '".$_SESSION['userid']."' and job_status='2'";
$from_date=isset($_GET['from_date']) ? $_GET['from_date'] : '';
$to_date=isset($_GET['to_date']) ? $_GET['to_date'] : '';
$vendor=isset($_GET['vendor']) ? $_GET['vendor'] : ''; 
if(!empty($from_date) && empty($to_date)){
  $sql .=" and order_date='$from_date'";  
}
if(!empty($from_date) && !empty($to_date)){
  $sql .=" and order_date BETWEEN '$from_date' AND '$to_date'";  
}
if(!empty($vendor)){
  $sql .=" and vendor_id='$vendor'";  
}
$sql .=' group by save_order.vendor_id,save_order.order_date order by order_date asc';  
}
$result = $con->query($sql);
$sql_rslt="SELECT u.user_id, ud.name,r.role_name AS type, ud.phone, u.active,u.created_at,u.updated_at FROM users u JOIN user_details ud ON u.user_id = ud.user_id JOIN user_roles ur ON ud.user_id = ur.user_id JOIN roles r ON r.role_id = ur.role_id WHERE ur.role_id IN(2)";
$result_rslt = $con->query($sql_rslt);
?>
<style>.ui-datepicker select.ui-datepicker-month, .ui-datepicker select.ui-datepicker-year {color: #000;}</style>
<!-- Datatable CSS -->
<link href='data_tables/datatables.min.css' rel='stylesheet' type='text/css' />
<link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel= "stylesheet">
<!-- Content Wrapper. Contains page content -->
        <div class="content-wrapper">
            <!-- Content Header (Page header) -->
            <section class="content-header">                                
                    <ol class="breadcrumb">
                        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>                    
                        <li class="active"><a href="#">Vendor Sales Report</a></li>           
                    </ol>                
            </section>

            <!-- Main content -->
            <section class="content">                
                <!-- Default box -->
                <div class="box box-form">
                    <div class="box-header with-border">
                        <h3 class="box-title">Vendor Sales Report</h3>
                        <div class="box-tools pull-right">
                            <button type="button" class="btn btn-box-tool" data-widget="collapse" data-toggle="tooltip" title="Collapse">
                                <i class="fa fa-minus"></i></button>
                            <button type="button" class="btn btn-box-tool" data-widget="remove" data-toggle="tooltip" title="Remove">
                                <i class="fa fa-times"></i></button>
                        </div>
                    </div>
                    <div class="box-body">    
                    <div class="container">
  <form method="get">
    <div class="form-group row">
      <div class="col-xs-3">
        <label for="ex1">From Date</label>
        <input class="form-control from" type="text" name="from_date" value="<?php echo $from_date;?>" readonly>
      </div>
      <div class="col-xs-3">
        <label for="ex2">To Date</label>
        <input class="form-control to" type="text" name="to_date" value="<?php echo $to_date;?>" readonly>
      </div>
      <?php if($_SESSION['role_id']==1){?>
      <div class="col-xs-3">
        <label for="ex3">Vendor</label>
        <select class="form-control" name="vendor">
        <option Value="" selected> Choose vendor</option>
        <?php if ($result_rslt->num_rows > 0) {
                while($row = $result_rslt->fetch_assoc()) {?>
				<option value="<?php echo $row['user_id'];?>" <?php if($vendor==$row['user_id']){ echo "selected";}?>><?php echo $row['name'];?></option>
		<?php }}?>
        </select>
      </div>
      <?php }?>
      <div class="col-xs-2">
        <label for="ex3"></label>
        <input class="form-control btn btn-success" type="submit" value="Search">
      </div>
    </div>
  </form>
</div>
                        <!-- Table -->
                        <table id='workapp' class='display dataTable table table-bordered table-hover'>
                            <thead>
                            <tr>
                                <th>S.No</th>                    
                                <th>Order Date</th>  
                                <th>Vendor Name</th> 
                                <th>Total</th>
                            </tr>
                            </thead> 
                              <tbody>
							<?php $i=1;$grand_total=0;
							if(!empty($from_date) || !empty($to_date) || !empty($vendor)){
							if ($result->num_rows > 0) {
                            while($row = $result->fetch_assoc()) {
                            $grand_total+=$row['total'];?>
						   <tr>
						   <td><?php echo $i;?></td>
						   <td><?php echo $row['order_date'];?></td>
						   <td><?php echo $row['name'];?></td>
						   <td><?php echo $row['total'];?></td>
						    </tr>								  
							 <?php $i++;}}}?>
							 <tfoot>
                             <tr>
                             <td></td>
                             <td></td>
                             <td><b>Total</b></td>
                             <td><b><?php echo $grand_total;?></b></td>
                              </tr>
                             </tfoot>
                           </tbody>							
                        </table>
                    </div>
                    <!-- /.box-footer-->                    
                </div>
                <!-- /.box -->
            </section>
            <!-- /.content -->                                        
        </div>
        <!-- /.content-wrapper -->        
<script src="js/common.js"></script>  
<script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
$(function(){
        $(".to").datepicker({ changeYear: true,
           dateFormat:'yy-mm-dd',
           showButtonPanel: true,
           closeText:'Clear',
	beforeShow: function( input ) {
		setTimeout(function() {
		var clearButton = $(input )
			.datepicker( "widget" )
			.find( ".ui-datepicker-close" );
		clearButton.unbind("click").bind("click",function(){$.datepicker._clearDate( input );});
		}, 1 );
	} });
        $(".from").datepicker({ 
		changeYear: true,
           dateFormat:'yy-mm-dd',
           showButtonPanel: true,
           closeText:'Clear',
	beforeShow: function( input ) {
		setTimeout(function() {
		var clearButton = $(input )
			.datepicker( "widget" )
			.find( ".ui-datepicker-close" );
		clearButton.unbind("click").bind("click",function(){$.datepicker._clearDate( input );});
		}, 1 );
	} }).bind("change",function(){
            var minValue = $(this).val();
            minValue = $.datepicker.parseDate("yy-mm-dd", minValue);
            minValue.setDate(minValue.getDate()+0);
            $(".to").datepicker( "option", "minDate", minValue );
        })
});
</script>
<script src="js/vendor-report.js"></script> 
<script src="https://cdn.datatables.net/buttons/1.6.0/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.6.0/js/buttons.flash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
<script src="https://cdn.datatables.net/buttons/1.6.0/js/buttons.html5.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.6.0/js/buttons.print.min.js"></script>
<?php require_once('includes/footer.php'); ?>