<?php  

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

 if(isset($_SESSION['loan_crm_username']) && isset($_SESSION['loan_crm_password']))
{
	 /****************************  API CALL for SALESFORCE - ZEST Money  **************************************/
   
   
   
   
	
	 $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.mmfQwyxzp2fQ5sJoh0FylT65SJeo6BczzwT_JXh4oRsMUAaXhqJ.4.Bvf9ykkKFZ&client_secret=8576256114121081226&username=rajasekhar%40zestmoney.in&password=zmsf%40123',
	  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'];
		
		
		
	
	
	
	$updateQ = mysqli_query($conn,"SELECT * FROM `updates` WHERE api_response like '%error%' ");
	
	while($updateData = mysqli_fetch_assoc($updateQ))
	{
		
		$account_holder_id = $updateData['account_holder_id'];
		
		$loanQ	= mysqli_query($conn,"select *from `leads` where account_holder_id = '$account_holder_id' ");
	
	
	 
	 $phone = $updateData['mobile'];
	$attempt_type = $updateData['attempt_type'];
	$attempt_status = $updateData['attempt_status'];
	$disposition = $updateData['disposition'];
	$sub_disposition = $updateData['sub_disposition'];
	
	$reason_for_default = '';
	$amount = '';
	$payment_mode = '';
	
	if(isset($updateData['reason_for_default']))
	{
	$reason_for_default = $updateData['reason_for_default'];
	}
	
	$remark = substr($updateData['remark'],0,245);
	
	$remark = trim(preg_replace('/\s\s+/', ' ', $remark));
	$remark = str_replace('<br>','\n',$remark);
	
	$next_update_date = '';
	if(isset($updateData['next_update_date']))
	{
	$next_update_date = $updateData['next_update_date'].'T00:00:00.000Z';
	}
	if(isset($updateData['amount']))
	{
		
	$amount = $updateData['amount'];
	}
	
	if(isset($updateData['payment_mode']))
	{
	$payment_mode = $updateData['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;
	
 
	
	}
	
	
	
	
	
		$api_response = json_encode($responseInfo);
		
		$update_id = $updateData['id'];
	 
	   mysqli_query($conn,"update updates set api_response = '$api_response' where $update_id ");
	   
	   
	   echo "******************************";
	   echo $api_response;
	   echo "My sQL ERror:".mysqli_error($conn);
	   echo $api_response;
	   echo "<br/>"
	 
                      
	}
	
	
}


?>