<?php 
session_start();
include '../db.php';
include("../all_query.php");
error_reporting(0);

	 if(isset($_SESSION['loan_crm_username']) && isset($_SESSION['loan_crm_password']))
	{
		
		 
	   	$month = $_REQUEST['month'];
		  

	function secToHour($seconds)
	{
	  return sprintf('%02d:%02d:%02d', ($seconds/ 3600),($seconds/ 60 % 60), $seconds% 60);	

	}
	
	
	
	function getPercentage($total_time,$used_time)
	{
		
			$count1 = $used_time / $total_time;
		   $count2 = $count1 * 100;
		   $count = number_format($count2, 0);
		   
		   return $count.'%';  
		
	}
	
	
					
				
		
		
		 ?>
		 
		 
		 
		 <div class="table-responsive text-nowrap">
		 
		 <table id="myTable"  class="table table-bordered " style="zoom:90%">
      <thead class="table-dark">
        <tr>
          <th>Date</th>
          <th>Advisor</th>
          <th>PAR allcoated</th>
          <th>Count Allocated</th>
          <th>Target</th>
          <th>Ach</th>
          <th>Target% Ach</th>
          
        </tr>
      </thead>
      <tbody class="">
	  
	  <?php $advisorQ = mysqli_query($conn,"select *from `advisors` ");
	  
	  
	  
	   if($_SESSION['login_as'] == 'advisor')
		 {
			 $login_name = $_SESSION['login_name'];
			 $advisorQ = mysqli_query($conn,"select *from `advisors` where name='$login_name' "); 
		 }
		 
		 
		 
		 
		 $final_mtp_paid = 0;
	  
	    while($advisorData = mysqli_fetch_assoc($advisorQ))
		{
			
			$advisor_id = $advisorData['id'];
			$advisor = $advisorData['name'];
			$daily_attempts_target = $advisorData['daily_attempts_target'];
			$username = $advisorData['username'];
			   
			 $par_allocated  = 0;
			 $count_allocated = 0;
			  
			  
			  $leadsQ = mysqli_query($conn,"select *from `leads` where account_holder_id in (select account_holder_id from customer where advisor = '$advisor') ");
			  
			  $count_allocated = mysqli_num_rows($leadsQ);
			  
			  
			  while($leadData = mysqli_fetch_assoc($leadsQ))
			  {
				  $leadArray = json_decode($leadData['lead_data'],TRUE);
				  
				  $principaldue = $leadArray['principaldue'];
				  
				  if($principaldue!='')
				  {
					  $par_allocated = $par_allocated + $principaldue;
				  }
				  
			  }
			  
			  

			   
			   
			   
			  
				$paymentQ  = mysqli_query($conn,"select *from `payments` where advisor='$advisor'  ");
				$paid_count = mysqli_num_rows($paymentQ);
				
				$mtp_paid = 0;
				
				$today_amount_collected = 0;
				
				while($paymentData = mysqli_fetch_assoc($paymentQ))
				{
				 $mtp_paid = $mtp_paid  + $paymentData['afterallocation_totalpaid'];
				 
				 }
				
				
			 
				
				$target = 0;
				
				if($par_allocated > 0)
				{
				$target = $par_allocated * 2/100;
				}
				
				
				$ach_per = 0;
				
				if($mtp_paid > 0 && $target > 0)
				{
					
					$ach_per = getPercentage($target,$mtp_paid);
					
				}
			  
			

	  ?>
	  
	  <tr>
	  
	  <td><?php echo date('F-Y',strtotime($month)); ?></td>
	  <td><?php echo $advisorData['name']; ?></td>
	  <td><?php echo number_format($par_allocated); ?></td>
	  <td><?php echo $count_allocated; ?></td>
	  <td><?php echo number_format($target);  ?></td>
	  <td><?php echo number_format($mtp_paid);  $final_mtp_paid = $final_mtp_paid + $mtp_paid; ?></td>
	  <td><?php echo $ach_per; ?></td>
	 
	 
	 
	  </tr>
       
		<?php } ?>
        
		
		<tfoot>
		
		<tr>
		
		<th colspan="5">Total</th>
		<th  ><?php echo $final_mtp_paid; ?></th>
		<th></th>
		
		</tr>
		
		</tfoot>
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
      </tbody>
    </table>
		 
	</div>	 
		 
		 
		 
		 
		 <?php
		
		
		
		
	}
	
	
	?>