<?php
	include("includes/dbconfig.php");
	$pagetitle = "System Users";
	require('includes/applicationtop.php');
	require('includes/applicationmenu.php');
	
	
?>
			<!-- Content Wrapper. Contains page content -->
			<div class="content-wrapper">
				<!-- Content Header (Page header) -->
				<section class="content-header">
					<h1>Personel Performance</h1>
					<ol class="breadcrumb">
					</ol>
				</section>

				<!-- Main content -->
				<section class="content">
					<div class="card card-primary" id  ="Load_here">
						<?php
							if($_SESSION["connectedusergroup"] == 1)
							{
						?> 
						<form class="form" method="post" action="">
													
													<div class="row   ">
														<div class="col-lg-4">
															From<br />
															<input type="text" required class="form-control" value  ="<?php if(isset($_POST['start_date'])) echo $_POST['start_date'];  ?>"  name="start_date" placeholder="YYYY-MM-DD" id="start_date" >
														</div>
														<div class="col-lg-4">
															To<br />
															<input type="text"  value  ="<?php if(isset($_POST['end_date'])) echo $_POST['end_date'];  ?>" required class="form-control" name="end_date" placeholder="YYYY-MM-DD" id="end_date" >
														</div>
														<div class="col-lg-3">
															&nbsp;<br />
															<input type="submit" class="form-control btn btn-info" value="Search" />
														</div>
													</div>
														  
													</form>
													 
													
						
						
						<div class="card-body">
							 
							
							<div class="card-body table-responsive  " id  ="Load_here">	
							<?php
													
												if(isset($_POST['start_date'])){
													if(isset($_POST['start_date'])){
															$start_date = $_POST['start_date'];
													}else{
														$start_date = date('Y-m-d');
													}
													
													if(isset($_POST['end_date'])){
															$end_date = $_POST['end_date'];
													}else{
														$end_date = date('Y-m-d');
													}
							 
							 ECHO '<H4 align="center" > PERSONEL PERFORMANCE RESULTS FOR PERIOD '.$start_date.' - '.$end_date.'  </H4>';
														 ?>
								 
								<table class="table table-hover table-bordered ">
									<thead>
										<tr>
											<th>Name</th>
											<th>Role</th>
											 
											 
											<th>Branch</th>
											<th colspan="2">Sales</th>
											 
											
											<th colspan="2"> Sales (Cancelled)</th>
											 
											
											<th>Total  (Customers)</th>
											<th colspan="2"> Jobs Cancelled</th>
											 
											<th colspan="2"> Workshop Jobs </th>
											<th colspan="2"> Top ups </th>
										</tr>
									</thead>
									<tfoot>
										<tr>
											<th>Name</th>
											<th>Role</th>
											 
										 
										 
											<th>Branch</th>
											<th colspan="2">Sales</th>
											
											<th colspan="2"> Sales (Cancelled)</th>
											  
											<th>Total   (Customers)</th>
											<th colspan="2"> Jobs Cancelled</th>
											 
											<th colspan="2"> Workshop Jobs </th>
											 
											<th colspan="2"> Top ups </th>
											<th  > Customers</th>
											<th > Revenue </th>
								 
										</tr>
									</tfoot>
									<tbody>
									<?php
									$results = DB::query("SELECT personnel_details.id, personnel_details.display_name, personnel_details.phone, personnel_details.email, personnel_details.login_name, personnel_details.status, peronnel_role.role_name, shop.full_name, personnel_details.is_loggedin, personnel_details.is_locked, personnel_details.version FROM personnel_details LEFT JOIN shop ON personnel_details.branch = shop.id LEFT JOIN peronnel_role ON personnel_details.role = peronnel_role.id ORDER BY shop.full_name, peronnel_role.role_name, personnel_details.display_name");
	                               
										foreach($results as $user)
										{
											//print_r($user);
											
											$performance = DB::query(' SELECT   SUM(amount) as amount , COUNT(id) as counts from cash_receipt WHERE attendant  ='.$user['id'].' AND is_cancelled = 0  AND value_date BETWEEN "'.$start_date.'" AND "'.$end_date. '"');
											 
											 $cancelled = DB::query(' SELECT   SUM(amount) as amount , COUNT(id) as counts from cash_receipt WHERE attendant  ='.$user['id'].' AND is_cancelled = 1  AND  value_date BETWEEN "'.$start_date.'" AND "'.$end_date. '"');
											 
											 $jobs = DB::query(' SELECT   SUM(amount) as amount , COUNT(id) as counts from job_receipt WHERE attendant  ='.$user['id'].' AND is_cancelled = 1  AND  value_date BETWEEN "'.$start_date.'" AND "'.$end_date. '"');
											  $jobsc = DB::query(' SELECT   SUM(amount) as amount , COUNT(id) as counts from job_receipt WHERE attendant  ='.$user['id'].' AND is_cancelled = 0  AND  value_date BETWEEN "'.$start_date.'" AND "'.$end_date. '"');
											 $topups = DB::query(' SELECT   SUM(topup) as amount , COUNT(topid) as counts from topups WHERE enteredby  ='.$user['id'].'    AND  (date(top_up_date) BETWEEN "'.$start_date.'" AND "'.$end_date. '" )');
											$row_count = $performance[0]['counts'] -  $cancelled[0]['counts'] - $jobs[0]['counts']  + $jobsc[0]['counts'] + $topups[0]['counts'];
											$row_money = $performance[0]['amount'] -  $cancelled[0]['amount'] - $jobs[0]['amount']  + $jobsc[0]['amount'] + $topups[0]['amount'];
	
									?>
										<tr>
											<td><?php echo $user['display_name']; ?></td>
											<td><?php echo $user['role_name']; ?></td>
										 
										 
											 <td><?php echo $user['full_name']; ?></td>
											 <td><?php  echo number_format(intval($performance[0]['amount']) , 2)?> </td>
											 <td><?php  echo $performance[0]['counts']?> </td>
											 <td><?php  echo $cancelled[0]['amount']?> </td>
											 <td><?php  echo $cancelled[0]['counts']?> </td>
											  <td><?php  echo ($cancelled[0]['counts'] + $performance[0]['counts'])?> </td>
											  <td><?php  echo ($jobs[0]['counts']  )?> </td>
											   <td><?php  echo (  $jobs[0]['amount'])?> </td>
											    <td><?php  echo ($jobsc[0]['counts']  )?> </td>
											   <td><?php  echo ( number_format( intval($jobsc[0]['amount']) , 2))?> </td>
											   <td><?php  echo (  $topups[0]['counts'])?> </td>
											   <td><?php  echo (  $topups[0]['amount'])?> </td>
											   <td><?php  echo number_format($row_count , 2);?> </td>
											   <td><?php  echo number_format($row_money , 2);?> </td>
											  
											 
										</tr>
									<?php
										}
									?>
									</tbody>
								</table>
								<?php	 } else echo 'No data found'; ?>
							</div>
						</div>
						<?php
							}
							else
							{
						?>
						<div class="alert alert-danger alert-dismissable">
							<i class="fa fa-ban-circle"></i><br>
							Limited Access
						</div>
						<?php
							}
						?>
					</div>
				</section><!-- /.content -->
			</div><!-- /.content-wrapper -->

			<footer class="main-footer text-center">
				<strong>Copyright &copy; <?php echo date("Y"); ?> BSMPOS.</strong> All rights reserved.
			</footer>
		</div><!-- ./wrapper -->
		
		<div class="modal" id="waitdialogmodal"  data-backdrop="static">
            <div class="modal-dialog modal-sm">
              <div class="modal-content">
                  <div class="modal-body">
					<div class="text-center">
						<img src="<?php echo $base_url . $ajax_loading_image; ?>" />
					</div>
				  </div>
              </div>
            </div>
        </div>

	 <?php include ('includes/general_footer.php')?>
		<script src="plugins/bert/storepos.js" type="text/javascript"></script>
		<script src="plugins/bert/systemusers.js" type="text/javascript"></script>
			<!-- bootstrap datepicker -->
<script src="<?php echo $base_url; ?>plugins/datepicker/bootstrap-datepicker.js"></script>
		
		<script type="text/javascript">
		v1 = []; 
function detailed_report(){
	 
	   var add =$('[name="detailed_report"]:checked').map( function(){ return $(this).val(); }).toArray();//trm
	  
	   BetaCustom('detailed_performance.php' ,  4 , add[0] , '#Load_here' );
	  
	return false;
}

function BetaCustom(scrpt ,  com , d , loadHere ){
	 
	 
	
	 var request = $.ajax({ url: scrpt, type: "POST", data: { comand : com , dataSend:d },  dataType: "html"               });

               request.done(function(reply ) {      $(loadHere).html(reply);    });
			  
			   
			   return false;
 
	
}
		//Date picker
    $('#start_date').datepicker({
      autoclose: true, 
	  format: 'yyyy-mm-dd'
    });
	
	$('#end_date').datepicker({
      autoclose: true, 
	  format: 'yyyy-mm-dd'
    });
		</script>
		 
	</body>
</html>