Files
Siro/backend/api/ride/get_competitor_context.php
T

82 lines
2.7 KiB
PHP

<?php
header('Content-Type: application/json; charset=utf-8');
require_once __DIR__ . '/../../connect.php';
$lat = filterRequest('passenger_lat') ?: filterRequest('lat');
$lng = filterRequest('passenger_lng') ?: filterRequest('lng');
$country = filterRequest('country') ?: filterRequest('country_code');
$distance = (float)(filterRequest('distance') ?: 0);
$siroPrice = (float)(filterRequest('siro_price') ?: 0);
if (!$lat || !$lng || !$country) {
echo json_encode(["status" => "error", "message" => "Missing parameters"]);
exit;
}
$countryCode = strtoupper($country);
if ($countryCode == 'JORDAN') $countryCode = 'JO';
if ($countryCode == 'SYRIA') $countryCode = 'SY';
if ($countryCode == 'EGYPT') $countryCode = 'EG';
// Default Competitor info based on Country
$topComp = 'TaxiF';
$multiplier = 1.15; // 15% more expensive by default
if ($countryCode === 'JO') {
$topComp = 'TaxiF';
$multiplier = 1.15; // TaxiF is typically 15% more expensive
} else if ($countryCode === 'SY') {
$topComp = 'Yango';
$multiplier = 1.12;
} else if ($countryCode === 'EG') {
$topComp = 'inDrive';
$multiplier = 1.10;
}
// Calculate the competitor's total price based on our price
if ($siroPrice > 0) {
$competitorTotalPrice = round($siroPrice * $multiplier, 2);
} else {
// Fallback if siro_price is 0
$avgPricePerKm = ($countryCode === 'JO') ? 0.35 : (($countryCode === 'SY') ? 4000 : 15);
$competitorTotalPrice = round($distance * $avgPricePerKm, 2);
}
// Calculate savings
$savingsPct = 0;
if ($competitorTotalPrice > 0 && $siroPrice < $competitorTotalPrice) {
$savingsPct = (($competitorTotalPrice - $siroPrice) / $competitorTotalPrice) * 100;
}
// Format the labels
$compNameAr = match(strtolower($topComp)) {
'careem' => 'كريم',
'uber' => 'أوبر',
'yallago' => 'يلا غو',
'jenny' => 'جيني',
'taxif' => 'تكسي إف',
'indrive' => 'إن درايف',
'yango' => 'يانغو',
default => $topComp
};
$savingsLabel = "أوفر بـ " . number_format($savingsPct, 1) . "% من $compNameAr ⚡";
$siroCommissionRate = 0.14; // Default 14% commission
if ($countryCode === 'JO') $siroCommissionRate = 0.14;
$extraEarnings = $siroPrice * $siroCommissionRate;
$driverExtraLabel = "رحلة مربحة! تكسب أكثر مقارنة بـ $compNameAr 💰";
// Return exactly what Dart expects in the root JSON
echo json_encode([
"status" => "success",
"has_competitor_data" => true,
"competitor_avg_price" => $competitorTotalPrice,
"top_competitor" => $topComp,
"savings_percent" => $savingsPct,
"savings_label" => $savingsLabel,
"driver_extra_amount" => round($extraEarnings, 2),
"driver_extra_label" => $driverExtraLabel
]);
?>