<?php  

  session_start();
   error_reporting(0);
		include("../db.php");
		include("../all_query.php");
		
      
	 if(isset($_SESSION['loan_crm_username']) && isset($_SESSION['loan_crm_password']))
	{
		
		
		$customer_type = $_REQUEST['customer_type'];	
		
		?>
		
		
		
		

 <style>
 
 #dash_table   tr td,th
 {
	 text-align:center !important;
	 padding: 8px !important;
 }
 
		
</style>
		
		
		
		 <div class="card card-datatable table-responsive text-nowrap" style="zoom:85%;padding:10px;margin:10px;">
		 
		 
	
	
        <table id="dash_table" class=" table border-top" cellspacing="0" width="100%" >
		
         <thead>
		 <tr>
		 <th>Advisor</th>
		 <th>Unique Cust</th>
		 <th>Allocation Count</th>
		 <th>Attempted</th>
		 <th>Unattempted</th>
		 <th>Contact</th>
		 <th>Contact_P</th>
		 <th>Contact_N</th>
		 <th>Non Contact</th>
		 <th>Retained</th>
		 <th>PTP</th>
		 <th>Td PTP</th>
		 <th>Followup</th>
		 <th>Td Followup</th>
		 </tr>
		 
		 </thead>
		 
		 
		 <tbody>
		 
		 <?php 
		 
		 
		 
			 $total_total_unique_cust = 0;
			 $total_total_allocation_count = 0;
			 $total_total_attempted = 0;
			 $total_total_unattempted = 0;
			 $total_total_contact = 0;
			 $total_total_contact_p = 0;
			 $total_total_contact_n = 0;
			 $total_total_no_contact = 0;
			 $total_total_retained = 0;
			 $total_total_ptp = 0;
			 $total_total_today_ptp = 0;
			 $total_total_followup = 0;
			 $total_total_today_followup = 0;
			 
		 
		 
		 $advisorQ = mysqli_query($conn,"select *from `advisors` where customer_type_id in (select id from `customer_type` where name='$customer_type' ) "); 
		 
		 
		 
		 if($_SESSION['login_as'] == 'advisor')
		 {
			 $login_name = $_SESSION['login_name'];
			 $advisorQ = mysqli_query($conn,"select *from `advisors` where name='$login_name' "); 
		 }
		 
		 
		 
		 
		 
		 
		 $today_date = date('Y-m-d');
		 
		 
		 while($advisorData = mysqli_fetch_assoc($advisorQ))
		 {
			 
			 $advisor = $advisorData['name'];
			 
			 $calculate_real = true;
			 
			 
			 if(true)
			// if($_SESSION['login_as'] == 'admin')
			 {
			 
			   $alreadyInTableQ = mysqli_query($conn,"select *from `dashboards` where advisor = '$advisor' and type='admin_dashboard' ");
			   
			   if(mysqli_num_rows($alreadyInTableQ) > 0)
			   {
				   $calculate_real = false; 
				   
				   
				   $alreadyInTableData = mysqli_fetch_assoc($alreadyInTableQ);
				   $jsonArray = json_decode($alreadyInTableData['json_data'],true);
				   
						   
					 $total_unique_cust = $jsonArray['total_unique_cust'];
					 $total_allocation_count = $jsonArray['total_allocation_count'];
					 $total_attempted = $jsonArray['total_attempted'];
					 $total_unattempted = $jsonArray['total_unattempted'];
					 $total_contact = $jsonArray['total_contact'];
					 $total_contact_p = $jsonArray['total_contact_p'];
					 $total_contact_n =$jsonArray['total_contact_n'];
					 $total_no_contact = $jsonArray['total_no_contact'];
					 $total_retained = $jsonArray['total_retained'];
					 $total_ptp = $jsonArray['total_ptp'];
					 $total_today_ptp = $jsonArray['total_today_ptp'];
					 $total_followup = $jsonArray['total_followup'];
					 $total_today_followup = $jsonArray['total_today_followup'];
					 
				   
				   
				   
			   }
			   			 
			 }
			 
			 
			 
			 
			 
			 if($calculate_real)
			 {
			 
			 
			 $total_unique_cust = 0;
			 $total_allocation_count = 0;
			 $total_attempted = 0;
			 $total_unattempted = 0;
			 $total_contact = 0;
			 $total_contact_p = 0;
			 $total_contact_n = 0;
			 $total_no_contact = 0;
			 $total_retained = 0;
			 $total_ptp = 0;
			 $total_today_ptp = 0;
			 $total_followup = 0;
			 $total_today_followup = 0;
			 
			  
			  
			  
			 $custQ = mysqli_query($conn,"select *from `customer` where ( advisor = '$advisor' and  customer_type = '$customer_type' ) ");
			 $total_unique_cust = mysqli_num_rows($custQ);
			 
			 $allocationQ = mysqli_query($conn,"select *from `leads` where account_holder_id in (select account_holder_id from customer  where ( advisor = '$advisor' and  customer_type = '$customer_type' ) ) ");
			 $total_allocation_count = mysqli_num_rows($allocationQ);
			 
			 
			// $attemptedQ  = mysqli_query($conn,"select *from `customer` where ( advisor = '$advisor' and  customer_type = '$customer_type' ) and  account_holder_id in (select account_holder_id from updates  ) ");
			
			
			$attemptedQ  = mysqli_query($conn,"select *from `customer` where  ( advisor = '$advisor'  and  customer_type = '$customer_type'  ) and  total_updates > 0   ");
			 
			 $total_attempted = mysqli_num_rows($attemptedQ);
			 
			 
			 
			 
			 
			 // $unattemptedQ  = mysqli_query($conn,"select *from `customer` where ( advisor = '$advisor' and  customer_type = '$customer_type' ) and  account_holder_id not in (select account_holder_id from updates  ) ");
			  
			  $unattemptedQ  = mysqli_query($conn,"select *from `customer` where  ( advisor = '$advisor'  and  customer_type = '$customer_type'  ) and  total_updates = 0   ");
			 
			 $total_unattempted = mysqli_num_rows($unattemptedQ);
			 
			 
			 
			 
			//  $contactpQ  = mysqli_query($conn,"select *from `customer` where ( advisor = '$advisor' and  customer_type = '$customer_type' ) and  account_holder_id in (select account_holder_id from updates  where attempt_status = 'Contact P' ) ");
			  
			  $contactpQ  = mysqli_query($conn,"select *from `customer` where  ( advisor = '$advisor'  and  customer_type = '$customer_type'  ) and  total_contact_p > 0   ");
			  
			  
			  $total_contact_p = mysqli_num_rows($contactpQ);
			 
			//  $contactnQ  = mysqli_query($conn,"select *from `customer` where  ( advisor = '$advisor' and  customer_type = '$customer_type' ) and  account_holder_id in (select account_holder_id from updates  where attempt_status = 'Contact N'  ) ");
			
			 $contactnQ  = mysqli_query($conn,"select *from `customer` where  ( advisor = '$advisor'  and  customer_type = '$customer_type'  ) and  total_contact_n > 0   ");
			 
			  $total_contact_n = mysqli_num_rows($contactnQ);
			  
			  
			   // $nocontactQ  = mysqli_query($conn,"select *from `customer` where  ( advisor = '$advisor' and  customer_type = '$customer_type' ) and    account_holder_id in (select account_holder_id from updates  where attempt_status = 'No Contact'   )  and account_holder_id not in (select account_holder_id from updates   where attempt_status = 'Contact P' or attempt_status = 'Contact N'  ) ");
				
			   $nocontactQ  = mysqli_query($conn,"select *from `customer` where  ( advisor = '$advisor'  and  customer_type = '$customer_type'  ) and  total_contact = 0 and total_non_contact > 0     ");
				  
				  
				  
			    $total_no_contact = mysqli_num_rows($nocontactQ);
				
				
				 $contactQ  = mysqli_query($conn,"select *from `customer` where  ( advisor = '$advisor'  and  customer_type = '$customer_type'  ) and  total_contact > 0   ");
			 
			     $total_contact = mysqli_num_rows($contactQ);
			  
			  
			  
			  
			  
				 
				 
				  $retainedQ  = mysqli_query($conn,"select *from `customer` where  ( advisor = '$advisor' and  customer_type = '$customer_type' and retained = 'yes' )  ");
				  
			     $total_retained = mysqli_num_rows($retainedQ);
				  
			  
			  
			    $ptpQ  = mysqli_query($conn,"select *from `customer` where  ( advisor = '$advisor' and  customer_type = '$customer_type' ) and    account_holder_id in (select account_holder_id from updates  where (disposition = 'PTP' or disposition = 'BRPTP' or  disposition = 'FPTP' )   ) ");
			    $total_ptp = mysqli_num_rows($ptpQ);
				
				
				$today_ptpQ  = mysqli_query($conn,"select *from `customer` where  ( advisor = '$advisor' and  customer_type = '$customer_type' ) and    account_holder_id in (select account_holder_id from updates  where (disposition = 'PTP' or disposition = 'BRPTP' or  disposition = 'FPTP' ) and next_update_date = '$today_date'  ) ");
			    $total_today_ptp = mysqli_num_rows($today_ptpQ);
				
				
				
			   $followupQ  = mysqli_query($conn,"select *from `customer` where  ( advisor = '$advisor' and  customer_type = '$customer_type' ) and    account_holder_id in (select account_holder_id from updates  where  disposition='CB'   ) ");
			    $total_followup = mysqli_num_rows($followupQ);
			  
			  
			  $today_followupQ  = mysqli_query($conn,"select *from `customer` where  ( advisor = '$advisor' and  customer_type = '$customer_type' ) and    account_holder_id in ( select account_holder_id from updates  where disposition='CB'   and next_update_date = '$today_date'   ) ");
			    $total_today_followup = mysqli_num_rows($today_followupQ);
				
			  
			  
			 // $total_contact = $total_contact_p + $total_contact_n;
			  
			 }
			 
			  
			 
		 ?>
		 
		 <tbody>
		 
		 <tr>
		 <td><?php echo $advisorData['name']; ?></td>
		 
		 <td><span  onclick="fetchAllParty('0','','Unique Cust','<?php echo $advisor; ?>','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal" style="cursor:pointer;" class="badge  bg-primary"><?php echo  $total_unique_cust; $total_total_unique_cust = $total_total_unique_cust + $total_unique_cust; ?></span></span></td>
		 
		 <td><span style="cursor:pointer;" class="badge  bg-primary"><?php  echo $total_allocation_count; $total_total_allocation_count = $total_total_allocation_count + $total_allocation_count; ?></span></td>
		 
		 <td><span onclick="fetchAllParty('0','','Attempted','<?php echo $advisor; ?>','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal" style="cursor:pointer;" class="badge  bg-info"><?php echo  $total_attempted; $total_total_attempted = $total_total_attempted + $total_attempted; ?></span></td>
		 
	    <td><span onclick="fetchAllParty('0','','Unattempted','<?php echo $advisor; ?>','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal" style="cursor:pointer;" class="badge  bg-warning"><?php echo  $total_unattempted; $total_total_unattempted = $total_total_unattempted + $total_unattempted; ?></span></td>
		 
		 <td><span onclick="fetchAllParty('0','','Contact','<?php echo $advisor; ?>','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal" style="cursor:pointer;" class="badge  bg-primary"><?php echo $total_contact; $total_total_contact = $total_total_contact + $total_contact; ?></span></td>
		 
		 <td><span onclick="fetchAllParty('0','','Contact P','<?php echo $advisor; ?>','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal" style="cursor:pointer;" class="badge  bg-success"><?php echo $total_contact_p; $total_total_contact_p = $total_total_contact_p + $total_contact_p; ?></span></td>
		 
		 <td><span onclick="fetchAllParty('0','','Contact N','<?php echo $advisor; ?>','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal" style="cursor:pointer;" class="badge  bg-danger"><?php echo $total_contact_n; $total_total_contact_n = $total_total_contact_n + $total_contact_n; ?></span></td>
		 
		 
		  <td><span onclick="fetchAllParty('0','','No Contact','<?php echo $advisor; ?>','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal" style="cursor:pointer;" class="badge  bg-danger"><?php echo $total_no_contact; $total_total_no_contact = $total_total_no_contact + $total_no_contact; ?></span></td>
		 
		 
		 
		 
		 <td><span onclick="fetchAllParty('0','','Retained','<?php echo $advisor; ?>','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal" style="cursor:pointer;" class="badge  bg-info"><?php echo $total_retained;  $total_total_retained = $total_total_retained + $total_retained; ?></span></td>
		 
		 <td><span onclick="fetchAllParty('0','','PTP','<?php echo $advisor; ?>','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal" style="cursor:pointer;" class="badge  bg-success"><?php echo $total_ptp; $total_total_ptp = $total_total_ptp + $total_ptp;  ?></span></td>
		 
		 
		  <td><span onclick="fetchAllParty('0','','Today PTP','<?php echo $advisor; ?>','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal" style="cursor:pointer;" class="badge  bg-success"><?php echo $total_today_ptp; $total_total_today_ptp = $total_total_today_ptp + $total_today_ptp;  ?></span></td>
		  
		  
		 
		 <td><span onclick="fetchAllParty('0','','Followup','<?php echo $advisor; ?>','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal"  style="cursor:pointer;" class="badge  bg-warning"><?php echo $total_followup; $total_total_followup = $total_total_followup + $total_followup;    ?></span></td>
		 
		 
		 <td><span onclick="fetchAllParty('0','','Today Followup','<?php echo $advisor; ?>','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal"  style="cursor:pointer;" class="badge  bg-warning"><?php echo $total_today_followup; $total_total_today_followup = $total_total_today_followup + $total_today_followup;    ?></span></td>
		 
		 
		 
		 </tr>
		 
		 </tbody>
		 
		 <?php } ?>
		 
		 
		 
		 <tbody>
		 
		 <?php  if($_SESSION['login_as'] == 'admin')
		 { ?>
		 <tfoot>
		 <th>Total</th>
		 <td><span onclick="fetchAllParty('0','','Unique Cust','Total','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal" style="cursor:pointer;"  style="cursor:pointer;" class="badge  bg-primary"><?php echo  $total_total_unique_cust; ?></span></span></td>
		 
		 <td><span  style="cursor:pointer;" class="badge  bg-primary"><?php  echo $total_total_allocation_count; ?></span></td>
		 
		 <td><span   onclick="fetchAllParty('0','','Attempted','Total','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal" style="cursor:pointer;"   style="cursor:pointer;" class="badge  bg-info"><?php echo  $total_total_attempted; ?></span></td>
		 
		 <td><span  onclick="fetchAllParty('0','','Unattempted','Total','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal" style="cursor:pointer;" class="badge  bg-warning"><?php echo  $total_total_unattempted; ?></span></td>
		 
		 <td><span  onclick="fetchAllParty('0','','Contact','Total','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal"   style="cursor:pointer;" class="badge  bg-primary"><?php echo $total_total_contact; ?></span></td>
		 
		 
		 <td><span onclick="fetchAllParty('0','','Contact P','Total','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal"   style="cursor:pointer;" class="badge  bg-success"><?php echo $total_total_contact_p; ?></span></td>
		 
		 <td><span  onclick="fetchAllParty('0','','Contact N','Total','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal"    style="cursor:pointer;" class="badge  bg-danger"><?php echo $total_total_contact_n; ?></span></td>
		 
		 
		  <td><span  onclick="fetchAllParty('0','','No Contact','Total','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal"    style="cursor:pointer;" class="badge  bg-danger"><?php echo $total_total_no_contact; ?></span></td>
		 
		 <td><span onclick="fetchAllParty('0','','Retained','Total','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal"     style="cursor:pointer;" class="badge  bg-info"><?php echo $total_total_retained; ?></span></td>
		 
		 
		 <td><span onclick="fetchAllParty('0','','PTP','Total','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal"     style="cursor:pointer;" class="badge  bg-success"><?php echo $total_total_ptp; ?></span></td>
		 
		 <td><span  onclick="fetchAllParty('0','','Today PTP','Total','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal"    style="cursor:pointer;" class="badge  bg-success"><?php echo $total_total_today_ptp; ?></span></td>
		 
		 <td><span onclick="fetchAllParty('0','','Followup','Total','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal"     style="cursor:pointer;" class="badge  bg-warning"><?php echo $total_total_followup; ?></span></td>
		 
		 <td><span onclick="fetchAllParty('0','','Today Followup','Total','<?php echo $customer_type; ?>')" data-bs-toggle="modal" data-bs-target="#dashboardBreakupModal"     style="cursor:pointer;" class="badge  bg-warning"><?php echo $total_total_today_followup; ?></span></td>
		 
		 
		 </tfoot>
		 
		 <?php } ?>
		 
		 </table>
		 
		 
		 <?php 
	 
	              
		  if($_SESSION['login_as'] == 'admin')
		  {
	      $pending_allocation_count = 0;
	  	   
		   
		   		  
	      $tempFilterQ = mysqli_query($conn,"select *from `temp_filter_customer` where  ( advisor is NULL or advisor = '')  and customer_type = '$customer_type'   and account_holder_id not in (select account_holder_id from payments )     order by  imp_score desc  ");
		  
		  $pending_allocation_count = mysqli_num_rows($tempFilterQ);
		  
		 // echo "   Pending Allocation: ( ".$pending_allocation_count." )"." | Total Non Contact: ( ".$total_total_no_contact." ) ";
		  echo " Total Non Contact: ( ".$pending_allocation_count." )  <a href='../excel_export_query.php?type=temp_filter_customer&name=NonContactable_$customer_type' target='BLANK' >Download</a> ";
		  
		  }
	  
	
	?>
	
	
		 </div>
		
		
		<?php
		
		
		
	}


?>