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

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

 
		
	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 card-datatable text-nowrap">
		 
		 <table  id="myTable"   class="table table-bordered " style="zoom:90%">
      <thead class="table-dark">
        <tr>
          <th>Advisor</th>
           <th>Commitment</th>
           <th>Payment</th>
           <th>Payment%</th>
		 
		   
		  
        </tr>
      </thead>
      <tbody >
	  
	  <?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_total_commitment = 0;
		 $final_total_payment = 0;
	  
	    while($advisorData = mysqli_fetch_assoc($advisorQ))
		{
			
		 	$advisor_id = $advisorData['id'];
			$username = $advisorData['username'];
			$advisor = $advisorData['name'];
			
			
			
			
				$paymentQ  = mysqli_query($conn,"select *from `payments` where advisor='$advisor' and accountid in (select accountid from commitment where  ( date(date_time) >= date('$from_date')  and date(date_time) <= date('$to_date') )  )    ");
				$paid_count = mysqli_num_rows($paymentQ);
				
				$total_payment = 0;
				
 				
				while($paymentData = mysqli_fetch_assoc($paymentQ))
				{
				 $total_payment = $total_payment  + $paymentData['afterallocation_totalpaid'];
				 
				 }
				 
				 $mtp_paid = $total_payment;
				  
				 $commitmentQ  = mysqli_query($conn,"select *from `commitment` where advisor='$advisor' and  ( date(date_time) >= date('$from_date')  and date(date_time) <= date('$to_date') )   ");
 				
				$total_commitment = 0;
				
				$today_amount_collected = 0;
				
				while($commitmentData = mysqli_fetch_assoc($commitmentQ))
				{
				 $total_commitment = $total_commitment  + $commitmentData['payment'];
				 
				 }
				 
				 
				
		 
			  
					
					$ach_per = getPercentage($total_commitment,$total_payment);
				 
			  
			  
				
			
		    $final_total_commitment = $final_total_commitment + $total_commitment;
		    $final_total_payment = $final_total_payment + $total_payment;
	  ?>
	  
	  <tr>
	  
	  <td><?php echo $advisorData['name']; ?></td>
      <td><?php echo $total_commitment; ?></td>
      <td><?php echo $total_payment; ?></td>
 	  <td><?php echo $ach_per; ?></td>

 	   
	 
 
	  
	  </tr>
       
		<?php } ?>
        
		
		<tfoot>
		<tr>
		<th>Total</th>
		<th><?php echo $final_total_commitment; ?></th>
		<th><?php echo $final_total_payment; ?></th>
		</tr>
		
		</tfoot>
		
		
      </tbody>
    </table>
		 
	</div>	 
		 
		 
		 
		 
		 <?php
		
		
		
		
	}
	
	
	?>
	
	 