[insert_php]

name;

$regdate = get_post_meta($_GET[“id”], ‘DateRegisteredWithDvla’, true);

$date = str_replace(‘/’, ‘-‘, $regdate);
$date = date(‘Y-m-d’, strtotime($date));

$regdate = new DateTime($date);
$curdate = new DateTime();

$interval = date_diff($regdate, $curdate);
$months = $interval->m + ($interval->y * 12);

if($brand == ‘Toyota’)
{
$totalwarranty = 60;
}
elseif ($brand == ‘Suzuki’)
{
$totalwarranty = 36;
}
else
{
$totalwarranty = 12;
}
$warranty = $totalwarranty – $months;

if($warranty > 12)
{
update_field(‘ronb_usedcar_total_warranty’, $warranty, $_GET[“id”]);
}
else
{
update_field(‘ronb_usedcar_total_warranty’, 12, $_GET[“id”]);
}

echo “
“;

get_finance_quotes($_GET[“id”]);
echo $_GET[“id”];
function get_finance_quotes($post_id) {
try {
$json_body = generate_json($post_id);
$api_response = call_api($json_body);
echo $api_response;
set_finance_quotes($post_id, $api_response);
} catch(Exception $e) {

}
}

function set_finance_quotes($post_id, $response) {
$details = json_decode($response, true);

if($details[“Vehicles”][0][‘FinanceQuotations’][0][“HasError”] != true)
{
$financeoption = 0;
}
else
{
$financeoption = 1;
}

echo “
“;
print_r($details[“Vehicles”]);

echo “sd”;
echo “
“;
$images = get_field(‘ronb_usedcar_gallery’, $post_id);

echo “
“;
echo “
“;

print_r($details[“Vehicles”]);

echo “
“;
echo “
“;
update_field(‘ronb_finance_type_of_finance’, $details[“Vehicles”][0][“FinanceQuotations”][$financeoption][“Finance”][“Key”], $post_id);
update_field(‘ronb_finance_total_payable’, $details[“Vehicles”][0][“FinanceQuotations”][$financeoption][“Finance”][“Quote”][“TotalAmountPayable”], $post_id);
update_field(‘ronb_finance_duration’, $details[“Vehicles”][0][“FinanceQuotations”][$financeoption][“Finance”][“Quote”][“Term”], $post_id);
update_field(‘ronb_finance_apr’, $details[“Vehicles”][0][“FinanceQuotations”][$financeoption][“Finance”][“Quote”][“Apr”], $post_id);
update_field(‘ronb_finance_final_payment’, $details[“Vehicles”][0][“FinanceQuotations”][$financeoption][“Finance”][“Quote”][“Payments”][1][“Amount”], $post_id);
update_field(‘ronb_finance_first_payment’, $details[“Vehicles”][0][“FinanceQuotations”][$financeoption][“Finance”][“Quote”][“Payments”][0][“Amount”], $post_id);
update_field(‘ronb_finance_monthly_payments’, $details[“Vehicles”][0][“FinanceQuotations”][$financeoption][“Finance”][“Quote”][“Payments”][0][“Amount”], $post_id);
update_field(‘ronb_finance_total_amount_of_credit’, $details[“Vehicles”][0][“FinanceQuotations”][$financeoption][“Finance”][“Quote”][“Balance”], $post_id);
update_field(‘ronb_finance_cash_price’, $details[“Vehicles”][0][“FinanceQuotations”][$financeoption][“Finance”][“Quote”][“TotalPrice”], $post_id);
update_field(‘ronb_finance_cash_deposit’, $details[“Vehicles”][0][“FinanceQuotations”][$financeoption][“Finance”][“Quote”][“Deposit”][“Cash”], $post_id);
update_field(‘ronb_finance_rate_of_interest_fixed’, $details[“Vehicles”][0][“FinanceQuotations”][$financeoption][“Finance”][“Quote”][“RateOfInterest”], $post_id);
update_field(‘ronb_finance_total_charge_for_credit’, $details[“Vehicles”][0][“FinanceQuotations”][$financeoption][“Finance”][“Quote”][“ChargesForCredit”], $post_id);

}

function generate_json($post_id) {
global $wpdb;
$key0 = ‘DateRegisteredWithDvla’;
$key1 = ‘ExternalVehicleId’;
$key2 = ‘Id’;-
$key3 = ‘Mileage’;-
$key4 = ‘OnTheRoadPrice’;-
$key5 = ‘RegistrationNumber’;-
$key6 = ‘Status’;-
$key7 = ‘Type’;-

$sql = ”
SELECT meta.meta_id, meta.post_id, meta.meta_key, meta.meta_value
FROM {$wpdb->postmeta} meta
WHERE meta.post_id = %d
AND meta.meta_key IN (‘{$key0}’,'{$key1}’,'{$key2}’,'{$key3}’,'{$key4}’,'{$key5}’,'{$key6}’,'{$key7}’)
ORDER BY meta.post_id, meta.meta_key”;

$import_fields = $wpdb->get_results($wpdb->prepare($sql, array($post_id)), OBJECT);

echo “

“;
echo “DateRegisteredWithDvla : “.$import_fields[0]->meta_value;
echo “

“;
echo “Id : “.$import_fields[1]->meta_value;
echo “

“;
echo “ExternalVehicleId : “.$import_fields[2]->meta_value;
echo “

“;
echo “Mileage : “.$import_fields[3]->meta_value;
echo “

“;
echo “OnTheRoadPrice : “.$import_fields[4]->meta_value;
echo “

“;
echo “RegistrationNumber : “.$import_fields[5]->meta_value;
echo “

“;
echo “Status : “.$import_fields[6]->meta_value;
echo “

“;
echo “Type : “.$import_fields[7]->meta_value;
echo “

“;
return generate_vehicle_json(
$import_fields[0]->meta_value,
$import_fields[1]->meta_value,
$import_fields[2]->meta_value,
$import_fields[3]->meta_value,
$import_fields[4]->meta_value,
$import_fields[5]->meta_value,
$import_fields[6]->meta_value,
$import_fields[7]->meta_value);
}

function generate_vehicle_json(
$DateRegisteredWithDvla,
$ExternalVehicleId,
$Id,
$Mileage,
$OnTheRoadPrice,
$RegistrationNumber,
$Status,
$Type)
{
return ‘
{
“Id”: “‘ . $Id . ‘”,
“Parameters”: {
“Term”: 60,
“CashDeposit”: 10,
“DepositType”: “Percentage”,
“AnnualMileage”: 10000
},
“PhysicalVehicle”: {
“ExternalVehicleId”: “‘ . $ExternalVehicleId . ‘”,
“Type”: “‘ . $Type . ‘”,
“Status”: “‘ . $Status . ‘”,
“OnTheRoadPrice”: ‘ . $OnTheRoadPrice . ‘,
“Mileage”: “‘ . $Mileage . ‘”,
“Registration”: {
“RegistrationNumber”: “‘ . $RegistrationNumber . ‘”,
“DateRegisteredWithDvla”: “‘ . $DateRegisteredWithDvla . ‘”
},
},
}’;
}

function call_api($json_body) {

if (strlen($json_body) == 0) return;

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => “https://services.codeweavers.net/api/finance/calculatefordisplay”,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => “”,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => “POST”,
CURLOPT_POSTFIELDS => ‘{
“Credentials”: {
“ApiKey”: “20cW404hI73oGTJmg5”
},
“VehicleRequests”: [‘ . $json_body . ‘],
}’,
CURLOPT_HTTPHEADER => array(
“Content-Type: application/json”,
“cache-control: no-cache”
),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo “cURL Error #:” . $err;
} else {
return $response;
}
}
[/insert_php]