<?php  

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

   if(isset($_SESSION['loan_crm_username']) && isset($_SESSION['loan_crm_password']))
  {
	  
	  
	  $advisor = $_SESSION['login_name'];
	  
	  $today_date = date('Y-m-d');
	  
	  
	  // get first if advisor is busy or free
	  
	   $advisorQ  = mysqli_query($conn,"select *from `advisors` where name='$advisor'  ");
	   $advisorData = mysqli_fetch_assoc($advisorQ);
	   
	   $advisor_id = $advisorData['id'];
	   
	   $login_statusQ = mysqli_query($conn,"select *from `advisor_login_status` where advisor_id = $advisor_id order by id desc limit 1 ");
	   $loginStatusData = mysqli_fetch_assoc($login_statusQ);
	   
	   $auto_dial_type  = $loginStatusData['auto_dial_type'];
	   

       if($loginStatusData['login_status'] == 'logged_in' && $loginStatusData['login_status_type'] == 'auto')
	   {
	   
	  
	   if($advisorData['is_busy_on_call'] == '')
	   {
		   
		   if($auto_dial_type == '')  {  $auto_dial_type = 'non_contact';  }
		   
		   
		   
		      
			if($auto_dial_type == 'non_contact') {
		   
		    $customerQ  = mysqli_query($conn,"select *from `customer` where  ( advisor = '$advisor') 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'  )    and ( account_holder_id not in (select account_holder_id from call_init where date(`date_time`) = date('$today_date') ) )  order by id desc   limit 1  ");
			
			}
			
			
		   if($auto_dial_type == 'ptp') {
		   
		    $customerQ  = mysqli_query($conn,"select *from `customer` where  ( advisor = '$advisor') and  account_holder_id in (select account_holder_id from updates where   (disposition = 'PTP' or disposition = 'BRPTP' or  disposition = 'FPTP' ) ) and ( account_holder_id not in (select account_holder_id from call_init where date(`date_time`) = date('$today_date') ) )  order by id desc   limit 1  ");
			
			}
			
			
			
		   if($auto_dial_type == 'td_ptp') {
		   
		    $customerQ  = mysqli_query($conn,"select *from `customer` where  ( advisor = '$advisor')  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'  )  and ( account_holder_id not in (select account_holder_id from call_init where date(`date_time`) = date('$today_date') ) )  order by id desc   limit 1  ");
			
			}
			
			
				
		   if($auto_dial_type == 'followup') {
		   
		    $customerQ  = mysqli_query($conn,"select *from `customer` where  ( advisor = '$advisor')  and   account_holder_id in (select account_holder_id from updates  where disposition='CB' ) and ( account_holder_id not in (select account_holder_id from call_init where date(`date_time`) = date('$today_date') ) )  order by id desc   limit 1 ");
			
			}
			
			
			
			 if($auto_dial_type == 'td_followup') {
		   
		    $customerQ  = mysqli_query($conn,"select *from `customer` where  ( advisor = '$advisor')  and    account_holder_id in (select account_holder_id from updates  where disposition='CB'  and next_update_date = '$today_date' )  and ( account_holder_id not in (select account_holder_id from call_init where date(`date_time`) = date('$today_date') ) )  order by id desc   limit 1  ");
			
			}
			
			
			
			
			
			
			
			$total_customer = mysqli_num_rows($customerQ);
			
			
	
			while($customerData = mysqli_fetch_assoc($customerQ))
			{
				
				$account_holder_id = $customerData['account_holder_id'];
				 
				 
				$extra_phone_json = $customerData['extra_phone_json'];
		   
				   $extra_phone_jsonArray = array();
				   if($extra_phone_json!='')
				   {
					   $extra_phone_jsonArray = json_decode($extra_phone_json,TRUE);
				   }
				   
				   $phone_cibil = $customerData['phone_cibil'];
				   $phone_db = $customerData['mobile'];
				   $phoneArrayDb = explode(' || ',$phone_db);
				   $phoneArray = explode(' || ',$phone_cibil);
				   
				   $phoneArray1 = array();
				   
				   
				    foreach($phoneArrayDb as $phone_no)
				   {
					  
					   $phone = substr($phone_no, -10);
					   
					   if(!in_array($phone,$phoneArray1))
					   {
						$phoneArray1[] = $phone;
					   }
				   }
				   
		   
					$phoneArrayDb = $phoneArray1;
					
					
			     foreach($phoneArray as $phone_no)
		              {
			  
					   $phone = substr($phone_no, -10);
					   
					   if(!in_array($phone,$phoneArray1))
					   {
						$phoneArray1[] = $phone;
					   }
					 }
			
			
			
			
					     foreach($phoneArray1 as $phone_no)
						   {
							   
							 $advisorQ  = mysqli_query($conn,"select *from `advisors` where name='$advisor'  ");
						     $advisorData = mysqli_fetch_assoc($advisorQ);
								 
								 if($advisorData['is_busy_on_call'] == '')
								 {
							   
							   
							// call here for auto call
							
							
							  // check if scrap no
							   $is_need_call = true;
							   
							   $needCheckQ = mysqli_query($conn,"select *from `updates` where account_holder_id = '$account_holder_id' and mobile = '$phone_no' and  (disposition = 'WTR' or disposition = 'RNR' or disposition = 'NC')  and update_date = '$today_date' ");
							   
							   if(mysqli_num_rows($needCheckQ) > 0)
							   {
								   $is_need_call = false;
							   }
							   
							   
							   
							   // check if contacted then no allowed next number
							   
							   
							   
							   
							    $isCallDone  = mysqli_query($conn,"select * from updates where (attempt_status = 'Contact P' or attempt_status = 'Contact N')  and update_date = '$today_date' and account_holder_id = '$account_holder_id'  ");
								
								if(mysqli_num_rows($isCallDone) > 0)
								{
									 $is_need_call = false;
								}
								
			
						 
			
							   
							   
							   
							   
							   
							   if($is_need_call)
							   {
								   $uniqueid = uniqid(); 
								   
								   
								   $resultArray = array();
								   $resultArray['account_holder_id'] = $account_holder_id;
								   $resultArray['mobile'] = $phone_no;
								   $resultArray['uniqueid'] = $uniqueid;
								   $resultArray['customer_name'] = $customerData['name'];
								   
								   
								   echo json_encode($resultArray);
								   
								   
								   // init call here
								  
								  
								  
								  	$mobile = $phone_no;	
									  
		
	 
									$advisor_id = $advisorData['id'];
		  
								//$uniqueid = uniqid();

								$DATA = array();
								
								$DATA['customer_mobile'] = $mobile;
								$DATA['customer_name'] = $customerData['name'];
								$DATA['emp_code'] = $advisorData['username'];
								$DATA['apisipid'] = $advisorData['apisipid'];
								$DATA['uniqueid'] = $uniqueid;
								$DATA['advisor_mobile'] = $advisorData['mobile'];
								$DATA['advisor_name'] = $advisorData['name'];
								$DATA['account_holder_id'] = $account_holder_id;
								$DATA['is_hit'] = '0';
								$DATA['init_type'] = 'auto';
			 
			 
			 
			
								   insertTable($conn,'call_init',$DATA,'add_submit');
									
									
									
									mysqli_query($conn,"update `advisors` set is_busy_on_call = 'on' where id=$advisor_id ");
									
									 
								  
								  
								  /******************************************************************************************/
								  
								  
								  
							   }
								   
								    
								   
							   }
							
							
							
							
							
							
							  
							  
							  
						   }
		   
		   
		   
		   
		   
		   
		   
				
				
				
			}
			
			
			
			
				  
				  
		   
		   
		   
		   
		   
		   
	   }
	   
	   
	   
	   }
		   
	  
  }
  
  
  
  ?>