<?php  

   session_start();
   error_reporting(0);
   include("../db.php");
   include("../all_query.php");
   
   //include("auto_call_init_function.php");
		
 
	
	$table = 'updates';
	
	
	$_POST['update_date'] = date('Y-m-d');
	$_POST['update_time'] = date('h:i A');
	
	if(isset($_SESSION['login_name']))
	{
		
	$_POST['update_by'] = $_SESSION['login_name'];
	
	
	 $_SESSION['timestamp'] = time(); 
	   
	   
	   
	}
	
	
	
	
	$account_holder_id = $_POST['account_holder_id'];
	
	
	
	
   /****************************  API CALL for SALESFORCE - ZEST Money  **************************************/
   
   
   // grant_type=password&client_id=3MVG9YDQS5WtC11oBAWq.mmfQwyxzp2fQ5sJoh0FylT65SJeo6BczzwT_JXh4oRsMUAaXhqJ.4.Bvf9ykkKFZ&client_secret=8576256114121081226&username=rajasekhar%40zestmoney.in&password=zmsf%40123
   
	
	$curl = curl_init();

	curl_setopt_array($curl, array(
	  CURLOPT_URL => 'https://login.salesforce.com/services/oauth2/token',
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => '',
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 0,
	  CURLOPT_FOLLOWLOCATION => true,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => 'POST',
	  CURLOPT_POSTFIELDS => 'grant_type=password&client_id=3MVG9YDQS5WtC11oBAWq.mmfQw05JNaIx1Id85RclBeZWT0eICx02gFPehNny5LtVui.mzp_.XMZvWSeA6iTX&client_secret=58B3A2717C8327599DEF201EDC67DAEAAD71CCCF8364D92A404F88BFAD7EAA5A&username=collectionagency@zestmoney.in&password=Easy@1234RlLtF5wk801b8NK43iQDl2G2',
	  CURLOPT_HTTPHEADER => array(
		'Content-Type: application/x-www-form-urlencoded',
		'Cookie: BrowserId=_azVOTIfEeyRyy_TUSvMNQ; CookieConsentPolicy=0:0; LSKey-c$CookieConsentPolicy=0:0; BrowserId=sHZo6Yl9EeysUbFVaCYNsQ; CookieConsentPolicy=0:0; LSKey-c$CookieConsentPolicy=0:0'
	  ),
	));

	$response = curl_exec($curl);

	curl_close($curl);
	 
	  $responseArray = json_decode($response,TRUE);
	  
	 $access_token =  $responseArray['access_token'];
		
	
	
	
    $loanQ	= mysqli_query($conn,"select *from `leads` where account_holder_id = '$account_holder_id' ");
	
	
	// $phone = $_POST['mobile'];
	
	if(isset($_SESSION['mobile']))
	{
	  $phone = $_SESSION['mobile'];
	}
	else
	{
		 $phone = $_POST['advisor_mobile'];
	}
	  
	  
	  
	  if(isset($_POST['advisor_mobile']))
	  {
		  unset($_POST['advisor_mobile']);
	  }
	  
	  
	$attempt_type = $_POST['attempt_type'];
	$attempt_status = $_POST['attempt_status'];
	$disposition = $_POST['disposition'];
	$sub_disposition = $_POST['sub_disposition'];
	
	$reason_for_default = '';
	$amount = '';
	$payment_mode = '';
	
	if(isset($_POST['reason_for_default']))
	{
	$reason_for_default = $_POST['reason_for_default'];
	}
	
	$remark = substr($_POST['remark'],0,245);
	
	$remark = trim(preg_replace('/\s\s+/', ' ', $remark));
	$remark = str_replace('<br>','\n',$remark);
	
	$next_update_date = '';
	if(isset($_POST['next_update_date']))
	{
	$next_update_date = $_POST['next_update_date'].'T00:00:00.000Z';
	}
	if(isset($_POST['amount']))
	{
		
	$amount = $_POST['amount'];
	}
	
	if(isset($_POST['payment_mode']))
	{
	$payment_mode = $_POST['payment_mode'];
	}
	
	
	$responseInfo = array();
	
	
	while($loanData = mysqli_fetch_assoc($loanQ))
	{
		
		$account_id = $loanData['account_id'];
	 
		
	//  create activity - update	
		
		$curl = curl_init();

	 curl_setopt_array($curl, array(
	  CURLOPT_URL => 'https://zestmoney.my.salesforce.com/services/apexrest/CreateActivity/',
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => '',
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 0,
	  CURLOPT_FOLLOWLOCATION => true,
	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	  CURLOPT_CUSTOMREQUEST => 'POST',
	  CURLOPT_POSTFIELDS =>'{
	"loanId":"'.$account_id.'",
	"phone":"'.$phone.'",
	"subject":"Calling",
	"attemptType":"'.$attempt_type.'" ,
	"attemptStatus":"'.$attempt_status.'",
	"disposition":"'.$disposition.'",
	"subdisposition1":"'.$sub_disposition.'",
	"subdisposition2":"'.$reason_for_default.'",
	"remark":"'.$remark.'",
	"popupdate":"'.$next_update_date.'",
	"amount":"'.$amount.'",
	"modeofpayment":"'.$payment_mode.'",
	"status":"Completed"
	}',
	  CURLOPT_HTTPHEADER => array(
		'Authorization: Bearer '.$access_token.'',
		'Content-Type: text/plain',
		'Cookie: BrowserId=_azVOTIfEeyRyy_TUSvMNQ; CookieConsentPolicy=0:1; LSKey-c$CookieConsentPolicy=0:1; BrowserId=sHZo6Yl9EeysUbFVaCYNsQ'
	  ),
	));



	$response = curl_exec($curl);

	curl_close($curl);
	
	
	
	$responseRow = array();
	
	$responseRow['account_id'] = $account_id;
	$responseRow['response']  = $response;
	
	$responseInfo[] = $responseRow;
	

	 // echo $response;
	
 
	
	}
	
	
	
	
	
	$_POST['api_response'] = json_encode($responseInfo);
	
	
	 
	
	
	
	insertTable($conn,$table,$_POST,'add_submit');
	
	
	
	
	
	
	
	
	
	
	
	// update last update
	
	if($_POST['next_update_date']!='')
	{
		unset($_POST['api_response']);
		$last_update = json_encode($_POST);
		
		mysqli_query($conn,"update customer set last_update = '$last_update' where account_holder_id = '$account_holder_id' ");
		
		
	}
	
	
	
	// update scrap phone
	
	
	if($_POST['disposition']=='WTR')
	{
	 $update_mobile = $_POST['mobile'];
	  
	mysqli_query($conn,"update customer set scrap_phone = CONCAT(scrap_phone,',$update_mobile')  where account_holder_id ='$account_holder_id' "); 
	
	mysqli_query($conn,"update temp_filter_customer set scrap_phone = CONCAT(scrap_phone,',$update_mobile')  where account_holder_id ='$account_holder_id' "); 
	

	}
	 
	 
	
	
	
	
	
	
	
	
	
	
	//  update count in customer table
	
	if($_POST['attempt_status'] == 'No Contact' )
	{
		mysqli_query($conn,"update customer set total_updates = total_updates + 1 , total_non_contact = total_non_contact + 1  where account_holder_id = $account_holder_id ");
	}
	else
	{
		mysqli_query($conn,"update customer set total_updates = total_updates + 1 , total_contact = total_contact + 1  where account_holder_id = $account_holder_id ");
		
		
		
		// update total total p and n
	
	 if($_POST['attempt_status'] == 'Contact P' )
	{
		mysqli_query($conn,"update customer set total_contact_p = total_contact_p + 1  where account_holder_id = $account_holder_id ");
	}
	
	if($_POST['attempt_status'] == 'Contact N' )
	{
		mysqli_query($conn,"update customer set total_contact_n = total_contact_n + 1  where account_holder_id = $account_holder_id ");
	}
	
		
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	 $advisor_name = $_POST['update_by'];
	
     mysqli_query($conn,"update `advisors` set is_busy_on_call = '' where name='$advisor_name' ");
	    
	
	 echo $account_holder_id;
	
 

?>