
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php

//include 'customer_api.php';

//$customer_data_2 =  get_customer_details_2('966568966941');
//$lead_detail = get_lead_details_by_phone('966568966941');

//print_r($customer_data_2);

//print_r($lead_detail);

function searchpartyidbymobile ($data){

$curl = curl_init();

$data = "966"."-".substr($data,1,2)."-".substr($data,3,7);

curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://fa-exam-saasfaprod1.fa.ocs.oraclecloud.com:443/crmRestApi/resources/11.13.18.05/contacts?q=RawMobileNumber='.$data,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_USERPWD => 'integration.user:Integration@2023',   
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_POSTFIELDS =>'',
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'Accept: application/json'
    ),
));

$response = curl_exec($curl);
$response = json_decode($response,true); 

curl_close($curl);
//echo $response;
//print_r($response['items']['0']['PartyId']);
$PartyId= $response['items']['0']['PartyId'];

if($PartyId == ''){

$PartyId = 'Not Found';
    
}else{
$PartyId = $PartyId;   
}

return $PartyId;

}

function createleadbypartyid ($LeadName,$PartyId,$Source,$DealAmount,$BranchDetails,$PaymentMethod,$Brand,$Model,$FuelType,$Transmission,$Description,$WhenDoYouPlanToBuyYourCar_c,$SalaryType_c,$CampaignName){

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://fa-exam-saasfaprod1.fa.ocs.oraclecloud.com:443/crmRestApi/resources/11.13.18.05/leads',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_USERPWD => 'integration.user:Integration@2023',   
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS =>'{
"Name":"'.$LeadName.'",
"CustomerId":'.$PartyId.',
"ChannelType":"'.$Source.'",
"RefferedBy_Id_c":'.$PartyId.',
"LeadBelongsTo_c":"CRM",
"BusinessType_c" : "B2C",
"PrimaryContactId":'.$PartyId.',
"DealAmount":'.$DealAmount.',
"Branch_c":"'.$BranchDetails.'",
"SalaryType_c":"'.$SalaryType_c.'",
"PaymentMethod_c":"'.$PaymentMethod.'",
"CampaignName_c":"'.$CampaignName.'",
"StatusCode":"'.'QUALIFIED'.'",
"Rank":"'.'HOT'.'",
"Description":"'.$Description.'",
"WhenDoYouPlanToBuyYourCar_c":"'.$WhenDoYouPlanToBuyYourCar_c.'"
}',
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'Accept: application/json'
    ),
));


$dataa = '{
"Name":"'.$LeadName.'",
"CustomerId":'.$PartyId.',
"ChannelType":"'.$Source.'",
"RefferedBy_Id_c":'.$PartyId.',
"LeadBelongsTo_c":"CRM",
"BusinessType_c" : "B2C",
"PrimaryContactId":'.$PartyId.',
"DealAmount":'.$DealAmount.',
"Branch_c":"'.$BranchDetails.'",
"SalaryType_c":"'.$SalaryType_c.'",
"PaymentMethod_c":"'.$PaymentMethod.'",
"StatusCode":"'.'QUALIFIED'.'",
"Rank":"'.'HOT'.'",
"Description":"'.$Description.'",
"WhenDoYouPlanToBuyYourCar_c":"'.$WhenDoYouPlanToBuyYourCar_c.'"
}';

//echo $dataa;

$response1 = curl_exec($curl);
//print_r($response);
//echo $response;
$response = json_decode($response1,true); 

curl_close($curl);
//echo $response;
//print_r($response);
//print_r($response['LeadNumber']);
//print_r($response['items']['0']['LeadNumber']);
//$LeadNumber= $response['LeadNumber'];
$LeadNumber = $response['LeadId'];

if($LeadNumber == ''){

$LeadNumber = "KD".$response1;
    
}else{
$LeadNumber = $LeadNumber;   
}

return $LeadNumber;

}


function createleadbypartyid3 ($LeadName,$PartyId,$Source,$DealAmount,$BranchDetails,$PaymentMethod,$Brand,$Model,$FuelType,$Transmission,$Description,$WhenDoYouPlanToBuyYourCar_c,$SalaryType_c,$CampaignName){

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://fa-exam-saasfaprod1.fa.ocs.oraclecloud.com:443/crmRestApi/resources/11.13.18.05/leads',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_USERPWD => 'integration.user:Integration@2023',   
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS =>'{
"Name":"'.$LeadName.'",
"CustomerId":'.$PartyId.',
"ChannelType":"AD2_WEB",
"RefferedBy_Id_c":'.$PartyId.',
"LeadBelongsTo_c":"AD2",
"BusinessType_c" : "B2C",
"PrimaryContactId":'.$PartyId.',
"DealAmount":'.$DealAmount.',
"Branch_c":"'.$BranchDetails.'",
"SalaryType_c":"'.$SalaryType_c.'",
"PaymentMethod_c":"'.$PaymentMethod.'",
"CampaignName_c":"'.$CampaignName.'",
"StatusCode":"'.'QUALIFIED'.'",
"Rank":"'.'HOT'.'",
"Description":"'.$Description.'",
"WhenDoYouPlanToBuyYourCar_c":"'.$WhenDoYouPlanToBuyYourCar_c.'"
}',
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'Accept: application/json'
    ),
));


$dataa = '{
"Name":"'.$LeadName.'",
"CustomerId":'.$PartyId.',
"ChannelType":"AD2_WEB",
"RefferedBy_Id_c":'.$PartyId.',
"LeadBelongsTo_c":"CRM",
"BusinessType_c" : "B2C",
"PrimaryContactId":'.$PartyId.',
"DealAmount":'.$DealAmount.',
"Branch_c":"'.$BranchDetails.'",
"SalaryType_c":"'.$SalaryType_c.'",
"PaymentMethod_c":"'.$PaymentMethod.'",
"StatusCode":"'.'QUALIFIED'.'",
"Rank":"'.'HOT'.'",
"Description":"'.$Description.'",
"WhenDoYouPlanToBuyYourCar_c":"'.$WhenDoYouPlanToBuyYourCar_c.'"
}';

//echo $dataa;

$response1 = curl_exec($curl);
//print_r($response);
//echo $response;
$response = json_decode($response1,true); 

curl_close($curl);
//echo $response;
//print_r($response);
//print_r($response['LeadNumber']);
//print_r($response['items']['0']['LeadNumber']);
//$LeadNumber= $response['LeadNumber'];
$LeadNumber = $response['LeadId'];

if($LeadNumber == ''){

$LeadNumber = "KD".$response1;
    
}else{
$LeadNumber = $LeadNumber;   
}

return $LeadNumber;

}

function createleadbypartyid2 ($LeadName,$PartyId,$Source,$DealAmount,$BranchDetails,$PaymentMethod,$Brand,$Model,$FuelType,$Transmission,$Description,$WhenDoYouPlanToBuyYourCar_c,$SalaryType_c,$CampaignName){

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://fa-exam-saasfaprod1.fa.ocs.oraclecloud.com:443/crmRestApi/resources/11.13.18.05/leads',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_USERPWD => 'integration.user:Integration@2023',   
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS =>'{
"Name":"'.$LeadName.'",
"CustomerId":'.$PartyId.',
"ChannelType":"'.$Source.'",
"RefferedBy_Id_c":'.$PartyId.',
"LeadBelongsTo_c":"CRM",
"BusinessType_c" : "B2C",
"PrimaryContactId":'.$PartyId.',
"DealAmount":'.$DealAmount.',
"Branch_c":"'.$BranchDetails.'",
"SalaryType_c":"'.$SalaryType_c.'",
"PaymentMethod_c":"'.$PaymentMethod.'",
"CampaignName_c":"'.$CampaignName.'",
"Rank":"'.'HOT'.'",
"Description":"'.$Description.'",
"WhenDoYouPlanToBuyYourCar_c":"'.$WhenDoYouPlanToBuyYourCar_c.'"
}',
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'Accept: application/json'
    ),
));


$dataa = '{
"Name":"'.$LeadName.'",
"CustomerId":'.$PartyId.',
"ChannelType":"'.$Source.'",
"RefferedBy_Id_c":'.$PartyId.',
"LeadBelongsTo_c":"CRM",
"BusinessType_c" : "B2C",
"PrimaryContactId":'.$PartyId.',
"DealAmount":'.$DealAmount.',
"Branch_c":"'.$BranchDetails.'",
"SalaryType_c":"'.$SalaryType_c.'",
"PaymentMethod_c":"'.$PaymentMethod.'",
"StatusCode":"'.'QUALIFIED'.'",
"Rank":"'.'HOT'.'",
"Description":"'.$Description.'",
"WhenDoYouPlanToBuyYourCar_c":"'.$WhenDoYouPlanToBuyYourCar_c.'"
}';

//echo $dataa;

$response1 = curl_exec($curl);
//print_r($response);
//echo $response;
$response = json_decode($response1,true); 

curl_close($curl);
//echo $response;
//print_r($response);
//print_r($response['LeadNumber']);
//print_r($response['items']['0']['LeadNumber']);
//$LeadNumber= $response['LeadNumber'];
$LeadNumber = $response['LeadId'];

if($LeadNumber == ''){

$LeadNumber = "KD".$response1;
    
}else{
$LeadNumber = $LeadNumber;   
}

return $LeadNumber;

}

function convertlead ($LeadId,$OppName,$Agent){

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://fa-exam-saasfaprod1.fa.ocs.oraclecloud.com:443/crmRestApi/resources/11.13.18.05/leads/action/convertLeadToOpty',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_USERPWD => 'integration.user:Integration@2023',   
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS =>'{
"leadId":'.$LeadId.',
"opportunityName":"'.$OppName.'",
"opportunityOwnerNumber":"'.$Agent.'"
}',
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/vnd.oracle.adf.action+json',
        'Accept: application/json'
    ),
));






//echo $dataa;

$response1 = curl_exec($curl);
//print_r($response);
//echo $response;
$response = json_decode($response1,true); 

curl_close($curl);
//echo $response;
//print_r($response);
//print_r($response['LeadNumber']);
//print_r($response['items']['0']['LeadNumber']);
$LeadNumber= $response['result'];
$leadId = $response['leadId'];

if($LeadNumber == ''){

$LeadNumber = $response1;
    
}else{
$LeadNumber = $LeadNumber;
}

return $LeadNumber;

//echo $leadId;

}

function convertlead_2 ($LeadId,$OppName,$Agent){

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://fa-exam-saasfaprod1.fa.ocs.oraclecloud.com:443/crmRestApi/resources/11.13.18.05/leads/action/convertLeadToOpty',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_USERPWD => 'integration.user:Integration@2023',   
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS =>'{
"leadId":'.$LeadId.',
"opportunityName":"'.$OppName.'"
}',
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/vnd.oracle.adf.action+json',
        'Accept: application/json'
    ),
));






//echo $dataa;

$response1 = curl_exec($curl);
//print_r($response);
//echo $response;
$response = json_decode($response1,true); 

curl_close($curl);
//echo $response;
//print_r($response);
//print_r($response['LeadNumber']);
//print_r($response['items']['0']['LeadNumber']);
$LeadNumber= $response['result'];
$leadId = $response['leadId'];

if($LeadNumber == ''){

$LeadNumber = $response1;
    
}else{
$LeadNumber = $LeadNumber;
}

return $LeadNumber;

//echo $leadId;

}

function createaccount ($FirstName,$MiddleName,$LastName,$MobileNumber,$SalaryType_c,$Salary_c,$MonthlySalary_c,$City){

//$MobileNumber

$Mbl1 = substr($MobileNumber,1,2);
$Mbl2 = substr($MobileNumber,3,7);

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://fa-exam-saasfaprod1.fa.ocs.oraclecloud.com:443/crmRestApi/resources/11.13.18.05/contacts',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_USERPWD => 'integration.user:Integration@2023',   
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS =>'{
"FirstName":"'.$FirstName.'",
"MiddleName":"'.$MiddleName.'",
"LastName":"'.$LastName.'",
"JobTitle":"'."NA".'",
"MobileNumber":"'.$Mbl2.'",
"PersonDEO_SalaryType_c":"'.$SalaryType_c.'",
"PersonDEO_Salary_c":"'.$Salary_c.'",
"PersonDEO_MonthlySalary_c":"'.$MonthlySalary_c.'",
"MobileAreaCode":"'.$Mbl1.'",
"City":"'.$City.'",
"Country":"'."SA".'"
}',
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'Accept: application/json'
    ),
));

$dataa = '{
"FirstName":"'.$FirstName.'",
"MiddleName":"'.$MiddleName.'",
"LastName":"'.$LastName.'",
"JobTitle":"'."NA".'",
"MobileNumber":"'.$Mbl2.'",
"PersonDEO_SalaryType_c":"'.$SalaryType_c.'",
"PersonDEO_Salary_c":"'.$Salary_c.'",
"PersonDEO_MonthlySalary_c":"'.$MonthlySalary_c.'",
"MobileAreaCode":"'.$Mbl1.'",
"City":"'.$City.'",
"Country":"'."SA".'"
}';

//echo $dataa;

$response1 = curl_exec($curl);
//print_r($response);
//echo $response;
$response = json_decode($response1,true); 

curl_close($curl);
//echo $response;
//print_r($response);
//print_r($response['LeadNumber']);
//print_r($response['items']['0']['LeadNumber']);
$LeadNumber= $response['PartyId'];

if($LeadNumber == ''){

$LeadNumber = 'KD'.$response1;
    
}else{
$LeadNumber = $LeadNumber;   
}

return $LeadNumber;

}

function create_oracle_lead($data,$data2){

$mobileWithoutCountryCode = $data['mobile'];

$PartyId = searchpartyidbymobile($mobileWithoutCountryCode);

if($PartyId == 'Not Found'){
    
$FirstName = $data['full_name'];
$MiddleName =' - ';
$LastName = $data['full_name'];
$MobileNumber = $mobileWithoutCountryCode;
$SalaryType_c = $data['bank'];
$MonthlySalary_c = $data['salary_group'];
$CampaignName = $data['lead_campaign'];
$City = $data['city'];
$Salary_c = 0;
$business_unit = $data['business_unit'];

$PartyId = createaccount($FirstName,$MiddleName,$LastName,$MobileNumber,$SalaryType_c,$Salary_c,$MonthlySalary_c,$City); 

$LeadName = $data['business_unit']." - ".$data['full_name']." - ". $data['voi']." - ".$data['lead_channel'];
$Source = $data['lead_channel'];
$DealAmount = 0;
$BranchDetails = $data['branch'];
$PaymentMethod = $data['request_type'];
$Description = $data['voi'];
$WhenDoYouPlanToBuyYourCar_c = $data['plan_to_buy'];


$leadnumber =  createleadbypartyid2 ($LeadName,$PartyId,$Source,$DealAmount,$BranchDetails,$PaymentMethod,$Brand,$Model,$FuelType,$Transmission,$Description,$WhenDoYouPlanToBuyYourCar_c,$SalaryType_c,$CampaignName); 

//$cnvrtlead = convertlead($leadnumber,$LeadName,$data2);

return array("Phone" => $mobileWithoutCountryCode,"PersonPartyId" => $PartyId, "LeadNumberId" => $leadnumber, "Convert_Lead_Log" => $cnvrtlead);

//echo '1'; 
    
}else{

//echo $PartyId;

$LeadName = $data['business_unit']." - ".$data['full_name']." - ". $data['voi']." - ".$data['lead_channel'];
$Source = $data['lead_channel'];
$DealAmount = 0;
$BranchDetails = $data['branch'];
$PaymentMethod = $data['request_type'];
$SalaryType_c = $data['bank'];
$CampaignName = $data['lead_campaign'];
$business_unit = $data['business_unit'];
$Description = $data['voi'];
$WhenDoYouPlanToBuyYourCar_c = $data['plan_to_buy'];

$leadnumber = createleadbypartyid2 ($LeadName,$PartyId,$Source,$DealAmount,$BranchDetails,$PaymentMethod,$Brand,$Model,$FuelType,$Transmission,$Description,$WhenDoYouPlanToBuyYourCar_c,$SalaryType_c,$CampaignName);    

//$cnvrtlead = convertlead($leadnumber,$LeadName,$data2);

//echo createaccount($FirstName,$MiddleName,$LastName,$PersonDEO_FirstNameArabic_c,$PersonDEO_LastNameArabic_c,$JobTitle,$MobileNumber,$EmailAddress,$PersonDEO_Nationality_c,$PersonDEO_IDCardType_c,$AddressLine1,$AddressLine2,$City,$Province,$Country,$PostalCode);    

return array("Phone" => $mobileWithoutCountryCode,"PersonPartyId" => $PartyId, "LeadNumberId" => $leadnumber, "Convert_Lead_Log" => "Not Converted");
   
}    
    
}


function create_oracle_lead_n_convert($data,$data2){

$mobileWithoutCountryCode = $data['mobile'];

$PartyId = searchpartyidbymobile($mobileWithoutCountryCode);

if($PartyId == 'Not Found'){
    
$FirstName = $data['full_name'];
$MiddleName =' - ';
$LastName = $data['full_name'];
$MobileNumber = $mobileWithoutCountryCode;
$SalaryType_c = $data['bank'];
$CampaignName = $data['lead_campaign'];
$MonthlySalary_c = $data['salary_group'];
$business_unit = $data['business_unit'];
$City = $data['city'];
$Salary_c = 0;

$PartyId = createaccount($FirstName,$MiddleName,$LastName,$MobileNumber,$SalaryType_c,$Salary_c,$MonthlySalary_c,$City); 

$LeadName = $data['business_unit']." - ".$data['full_name']." - ". $data['voi']." - ".$data['lead_channel'];
$Source = $data['lead_channel'];
$DealAmount = 0;
$BranchDetails = $data['branch'];
$PaymentMethod = $data['request_type'];
$Description = $data['voi'];
$WhenDoYouPlanToBuyYourCar_c = $data['plan_to_buy'];


$leadnumber =  createleadbypartyid3 ($LeadName,$PartyId,$Source,$DealAmount,$BranchDetails,$PaymentMethod,$Brand,$Model,$FuelType,$Transmission,$Description,$WhenDoYouPlanToBuyYourCar_c,$SalaryType_c,$CampaignName); 

$cnvrtlead = convertlead($leadnumber,$LeadName,$data2);

return array("Phone" => $mobileWithoutCountryCode,"PersonPartyId" => $PartyId, "LeadNumberId" => $leadnumber, "Convert_Lead_Log" => $cnvrtlead);

//echo '1'; 
    
}else{

//echo $PartyId;

$LeadName = $data['business_unit']." - ".$data['full_name']." - ". $data['voi']." - ".$data['lead_channel'];
$Source = $data['lead_channel'];
$DealAmount = 0;
$BranchDetails = $data['branch'];
$PaymentMethod = $data['request_type'];
$SalaryType_c = $data['bank'];
$CampaignName = $data['lead_campaign'];
$Description = $data['voi'];
$WhenDoYouPlanToBuyYourCar_c = $data['plan_to_buy'];
$business_unit = $data['business_unit'];

$leadnumber = createleadbypartyid3 ($LeadName,$PartyId,$Source,$DealAmount,$BranchDetails,$PaymentMethod,$Brand,$Model,$FuelType,$Transmission,$Description,$WhenDoYouPlanToBuyYourCar_c,$SalaryType_c,$CampaignName);    

$cnvrtlead = convertlead($leadnumber,$LeadName,$data2);

//echo createaccount($FirstName,$MiddleName,$LastName,$PersonDEO_FirstNameArabic_c,$PersonDEO_LastNameArabic_c,$JobTitle,$MobileNumber,$EmailAddress,$PersonDEO_Nationality_c,$PersonDEO_IDCardType_c,$AddressLine1,$AddressLine2,$City,$Province,$Country,$PostalCode);    

return array("Phone" => $mobileWithoutCountryCode,"PersonPartyId" => $PartyId, "LeadNumberId" => $leadnumber, "Convert_Lead_Log" => $cnvrtlead);
   
}    
    
}

//print_r(create_oracle_lead($lead_detail));

?>