111 lines
4.3 KiB
PHP
111 lines
4.3 KiB
PHP
<?php
|
|
/**
|
|
* what_if_simulator.php
|
|
* يحاكي تأثير تغيير الأسعار على مؤشر التنافسية (PCI) وحصة السوق المتوقعة
|
|
*/
|
|
|
|
require_once __DIR__ . '/../../connect.php';
|
|
|
|
if ($role !== 'admin' && $role !== 'super_admin') {
|
|
http_response_code(403);
|
|
echo json_encode(['status' => 'failure', 'message' => 'Unauthorized']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$countryCode = resolveAdminCountry(filterRequest('country_code'), $role, $admin_country ?? null);
|
|
$proposedSpeedPrice = (float)filterRequest('speed_price');
|
|
|
|
if (!$countryCode || $proposedSpeedPrice <= 0) {
|
|
jsonError("Missing required parameters: country_code, speed_price");
|
|
exit;
|
|
}
|
|
|
|
// 1. Fetch recent competitor trips (last 7 days, limit 500 for fast simulation)
|
|
$sql = "SELECT distance_km, total_price, competitor_name
|
|
FROM competitor_prices
|
|
WHERE country_code = :country
|
|
AND distance_km > 0
|
|
AND created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)
|
|
ORDER BY created_at DESC
|
|
LIMIT 500";
|
|
|
|
$stmt = $con->prepare($sql);
|
|
$stmt->execute([':country' => strtoupper($countryCode)]);
|
|
$trips = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if (empty($trips)) {
|
|
jsonError("No competitor data available for simulation in this country.");
|
|
exit;
|
|
}
|
|
|
|
// 2. Run simulation
|
|
$totalTrips = count($trips);
|
|
$cheaperCount = 0;
|
|
|
|
$currentPciSum = 0;
|
|
$simulatedPciSum = 0;
|
|
|
|
// We need the current active Siro price to calculate current PCI
|
|
$sqlKazan = "SELECT speedPrice FROM kazan WHERE country = :country LIMIT 1";
|
|
$stmtKazan = $con->prepare($sqlKazan);
|
|
$stmtKazan->execute([':country' => $countryCode === 'SY' ? 'Syria' : ($countryCode === 'JO' ? 'Jordan' : 'Egypt')]);
|
|
$kazanRow = $stmtKazan->fetch(PDO::FETCH_ASSOC);
|
|
$currentSpeedPrice = $kazanRow ? (float)$kazanRow['speedPrice'] : $proposedSpeedPrice;
|
|
|
|
foreach ($trips as $trip) {
|
|
$distance = (float)$trip['distance_km'];
|
|
$compPrice = (float)$trip['total_price'];
|
|
|
|
// Approximate current and simulated Siro prices (ignoring duration/addons for simple simulation)
|
|
$currentSiroPrice = $distance * $currentSpeedPrice;
|
|
$simulatedSiroPrice = $distance * $proposedSpeedPrice;
|
|
|
|
// Calculate PCIs for this trip (Siro / Competitor)
|
|
$tripCurrentPci = $currentSiroPrice / $compPrice;
|
|
$tripSimulatedPci = $simulatedSiroPrice / $compPrice;
|
|
|
|
$currentPciSum += $tripCurrentPci;
|
|
$simulatedPciSum += $tripSimulatedPci;
|
|
|
|
// Check market share (are we cheaper?)
|
|
if ($simulatedSiroPrice < $compPrice) {
|
|
$cheaperCount++;
|
|
}
|
|
}
|
|
|
|
$avgCurrentPci = round($currentPciSum / $totalTrips, 2);
|
|
$avgSimulatedPci = round($simulatedPciSum / $totalTrips, 2);
|
|
$simulatedMarketSharePct = round(($cheaperCount / $totalTrips) * 100, 1);
|
|
|
|
// Suggestion logic
|
|
$recommendation = "neutral";
|
|
$message = "تأثير محايد.";
|
|
|
|
if ($avgSimulatedPci > 1.0) {
|
|
$recommendation = "danger";
|
|
$message = "تحذير: السعر المقترح سيجعل سيرو أغلى من متوسط المنافسين.";
|
|
} elseif ($avgSimulatedPci < 0.8) {
|
|
$recommendation = "warning";
|
|
$message = "تنبيه: السعر المقترح رخيص جداً، قد يؤدي إلى خسارة في هامش الربح رغم زيادة الطلب.";
|
|
} elseif ($avgSimulatedPci >= 0.9 && $avgSimulatedPci <= 0.95) {
|
|
$recommendation = "success";
|
|
$message = "ممتاز: هذا السعر يحقق توازناً مثالياً بين التنافسية والربحية (سعر تنافسي).";
|
|
}
|
|
|
|
jsonSuccess([
|
|
'total_trips_simulated' => $totalTrips,
|
|
'current_speed_price' => $currentSpeedPrice,
|
|
'proposed_speed_price' => $proposedSpeedPrice,
|
|
'current_pci' => $avgCurrentPci,
|
|
'simulated_pci' => $avgSimulatedPci,
|
|
'simulated_market_share_percent' => $simulatedMarketSharePct,
|
|
'recommendation_status' => $recommendation,
|
|
'recommendation_message' => $message
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
error_log("[what_if_simulator] Error: " . $e->getMessage());
|
|
jsonError("Simulation failed");
|
|
}
|