<?php  

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

 if(isset($_SESSION['loan_crm_username']) && isset($_SESSION['loan_crm_password']))
{
	
	$table = 'updates';
	
	
	$_POST['update_date'] = date('Y-m-d');
	$_POST['update_time'] = date('h:i A');
	$_POST['update_by'] = $_SESSION['login_name'];
	
	
	
	$account_holder_id = $_POST['account_holder_id'];
	
	
	
	
   /****************************  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'];
		
	
	
	
    $loanQ	= mysqli_query($conn,"select *from `leads` where account_holder_id = '$account_holder_id' ");
	
	
	// $phone = $_POST['mobile'];
	  $phone = $_SESSION['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');
	
	
	
	
	
	
	
	
	
	
	echo $account_holder_id;
	
	
	
}
 

?>