<?php
	require ('my_functions.php');
	 
	$pagetitle = "System Users";
	require('includes/applicationtop.php');
	require('includes/applicationmenu.php');
	  $tech = $start_date = $end_date = false;
	    
	if(isset($_POST['userfocus']))
	{
		$tech = $userfocus = $_POST['userfocus'];
		$start_date = $_SESSION['start_date'];
	    $end_date   = $_SESSION['end_date'];
	}
	 
	 
	 
	
	$results = DB::query("SELECT personnel_details.id, personnel_details.display_name, personnel_details.employee_role,  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 WHERE    personnel_details.role > 1 and  personnel_details.id =".$userfocus." ORDER BY   personnel_details.employee_role, shop.full_name, peronnel_role.role_name, personnel_details.display_name ");
	#$results = DB::query("SELECT personnel_details.id, personnel_details.display_name, personnel_details.employee_role,  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   personnel_details.employee_role, shop.full_name, peronnel_role.role_name, personnel_details.display_name ");
	
?>
			<!-- 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">
						<?php
							if($_SESSION["connectedusergroup"] == 1)
							{
								 
						?>
						<div class="card-header with-border text-center">
							<h3 class="card-title">Personel Performance between <?php  echo $start_date?> - <?php  echo $end_date;?></h3>
						</div>
						
						<div class="card-body">
							<div id="servererrormessage">
							</div>
							
							<div class="card-body table-responsive no-padding">	
							
								 
												 
							 
								 <?php
								 $role = 0;
								 $tech = $sales = $tech;
								 $total_money = 0;
										foreach($results as $user)
										{
									?>
								
								<table class="table table-hover">
									<thead>
										<tr>
											<th>Name</th>
											<th>Role</th>
											 
											<th>Phone</th>
											 
											<th class="text-center"> Category</th>
											 
											 
											<th>Branch</th>
											<th>Value</th>
											<th>Rate</th>
											<th>Commission</th> 
										</tr>
									</thead>
									 
									<tbody>
									
										<tr>
											<td><?php echo $user['display_name']; ?></td>
											<td><?php echo $user['role_name']; ?></td>
										 
											<td><?php echo $user['phone']; ?></td>
											 
											<td class="text-center">
											
											<?php
											$role_name = 'N/A';
												$employee_roledata = DB::query("SELECT * FROM employee_category  ");
												
												foreach($employee_roledata as $useemployee_role)
												{
													if($useemployee_role['id'] == $user['employee_role'] ){
														$role_name =  $useemployee_role['name'];
													}
											 
											 
												}
												echo $role_name ;
												 
												   $commission = 0;
													
													$rate = 0;
													$money = 0;
													$role = $user['employee_role']; 
													 
													if($user['employee_role'] ==1){
														
														 
														 
														  $commission = Sum_Repair_com( $tech, $start_date , $end_date);
														  $money = Sum_tech_bill($tech, $start_date , $end_date);
													}else if($user['employee_role'] ==2){
														 
														 
													  $commission = Sum_sales_com( $tech, $start_date , $end_date);
													  $total_money =  $money = Sum_bill($tech, $start_date , $end_date);
													  
													 
													}
												 
												  
											 
												
											?>
											 
											
											</td>
											
											  <td><?php echo $user['full_name']; ?></td>
											 <td> <?php    echo number_format($money, 2);?> </td>
											 
											 <td> <?php   echo number_format($commission , 2);?> </td>
											 
										</tr>
									
									</tbody>
								</table>
								<?php 
								$money = 0;
								?>
								<table class="table table-hover">
								<thead>
										<tr>
											<th>Date</th>
											<th>Sno</th>
											 
											<th>Item</th>
											 
											<th  > Qty</th>
											 
											 
											<th>Amount</th>
											<th>Commission</th>
											<th>Rate</th> 
											
											 
										</tr>
									</thead>
								
								<?php
								$itemstotalcost = 0;
						$commision_data  = array();
						if($role ==2){
							
						$commision_data  = DB::query("SELECT   *   FROM cash_receipt where is_cancelled < 1 AND  attendant = {$tech}  and value_date between '{$start_date}' and '{$end_date}'");
						}else if($role ==1){
							$commision_data  = DB::query("SELECT   * FROM cash_receipt where is_cancelled < 1 AND  repaired_by = {$tech}  and value_date between '{$start_date}' and '{$end_date}'");
						}

 				
						
		
								
									if(DB::count() != 0){	

									
									   foreach ($commision_data as $com)
										  {
											  $receipt =  $com['id'];
											  $bill =  $com['bill'];
											  ?>
											  <tr> <td><?php  echo $com['value_date'];?> </td><td><?php  echo $com['receipt_number']?> </td> <td></td><td></td> <td> </td><td> </td><td> </td></tr>
											  <?php
											    $receiptitems = DB::query("SELECT stock_item.item_name, stock_item.item_code, stock_item.bar_code, stock_item.alt_barcode, cash_sale_item.sell_price, cash_sale_item.quantity, cash_sale_item.serial_number FROM cash_sale_item INNER JOIN item_price ON cash_sale_item.price_id = item_price.id INNER JOIN stock_item ON item_price.item = stock_item.id WHERE   cash_sale_item.receipt=%i",   $receipt);
				
											  ?>
											  
											  <?php
							$itemscount = 0;
							
							foreach($receiptitems as $receiptrow)
							{  
								$userowtotal = ($receiptrow['quantity'] * $receiptrow['sell_price']);
								//$itemstotalcost +=  $userowtotal;
								
								$itemscount += $receiptrow['quantity'];
								#$itemstotalcost += $userowtotal;
						?>
						<tr>
						<td></td><td></td>
							<td   ><?php echo substr($receiptrow['item_name'], 0, 30); ?></td>
						 
							<td  ><?php echo $receiptrow['quantity']; ?></td>
							<td  ><?php echo number_format($receiptrow['sell_price'], 2); ?></td>
							<td  ></td>
						   
						</tr>
						<?php
								 
							}
							$actual_rate = $money = 0;
							if($role ==1){
													 
													 
													  //$commission = Sum_Repair_com( $tech, $start_date , $end_date);
													  //$money = Sum_tech_bill($tech, $start_date , $end_date);
													  
													   
													  $rate_code = Select_tech_commision_rate($bill);
													   
													  $actual_rate = Commission_settings(  $rate_code);
													  $money = $sub_total = $bill*$actual_rate/100 ;
													  $itemstotalcost += $money;
													   
					  
												}else if($role ==2){
													 
													 
												  //$commission = Sum_sales_com( $tech, $start_date , $end_date);
												  //$money = Sum_bill($tech, $start_date , $end_date);
												  
												  
												  $rate_code = Select_sales_commision_rate($bill);
												  
												  $actual_rate = Commission_settings(  $rate_code);
												  $money = $sub_total = $bill*$actual_rate/100 ;
												  $itemstotalcost += $money;
					  
												  
												 
												}

							
										?>
										
										<tr class='info'>
						<td></td><td></td>
							<td   > </td>
						 
							<td  > </td>
							<td  > <?php  echo $bill;?></td>
							<td  ><?php echo number_format($money, 2); ?></td>
							<td  ><?php echo $actual_rate ; ?></td>
						   
						</tr>
										<?php }
										  
										  	?>
							<tr class='success'>
						<td></td><td></td>
							<td   > Total</td>
						 
							<td  > </td>
							<td  ><?php echo number_format( $total_money, 2); ?></td>
							<td  ><?php echo number_format($itemstotalcost, 2); ?></td>
							<td></td>
							   
						</tr>
						 <?php
									}						
						 								
						
						?>
						</table>
								
								<?php
										}
									?>
							</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 -->

			 
		</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">
		//Date picker
    $('#start_date').datepicker({
      autoclose: true, 
	  format: 'yyyy-mm-dd'
    });
	
	$('#end_date').datepicker({
      autoclose: true, 
	  format: 'yyyy-mm-dd'
    });
		</script>
	</body>
</html>