112 lines
4.4 KiB
PHP
112 lines
4.4 KiB
PHP
<?php
|
|
/**
|
|
* cron_weekly_health_report.php
|
|
* يجري تشغيله في نهاية الأسبوع (يوم الأحد ليلاً) لتلخيص صحة السوق التنافسية.
|
|
*/
|
|
|
|
set_time_limit(0);
|
|
ini_set('memory_limit', '256M');
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
|
|
|
require_once __DIR__ . '/../core/bootstrap.php';
|
|
require_once __DIR__ . '/../functions.php';
|
|
|
|
try {
|
|
$con = Database::get('main');
|
|
} catch (Exception $e) {
|
|
die("Database connection failed: " . $e->getMessage() . "\n");
|
|
}
|
|
|
|
echo "[".date('Y-m-d H:i:s')."] Starting Weekly Market Health Report...\n";
|
|
|
|
try {
|
|
$countries = ['SY', 'JO', 'EG', 'IQ']; // Countries we operate in or monitor
|
|
|
|
foreach ($countries as $countryCode) {
|
|
|
|
// 1. Calculate Average PCI and Market Share
|
|
$sql = "SELECT distance_km, total_price
|
|
FROM competitor_prices
|
|
WHERE country_code = :country
|
|
AND distance_km > 0
|
|
AND created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)";
|
|
$stmt = $con->prepare($sql);
|
|
$stmt->execute([':country' => $countryCode]);
|
|
$trips = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if (empty($trips)) {
|
|
continue; // Skip if no data for this country
|
|
}
|
|
|
|
$sqlKazan = "SELECT speedPrice FROM kazan WHERE country = :country LIMIT 1";
|
|
$stmtKazan = $con->prepare($sqlKazan);
|
|
$countryNameMap = ['SY' => 'Syria', 'JO' => 'Jordan', 'EG' => 'Egypt', 'IQ' => 'Iraq'];
|
|
$stmtKazan->execute([':country' => $countryNameMap[$countryCode] ?? 'Syria']);
|
|
$kazanRow = $stmtKazan->fetch(PDO::FETCH_ASSOC);
|
|
$currentSpeedPrice = $kazanRow ? (float)$kazanRow['speedPrice'] : 0;
|
|
|
|
$pciSum = 0;
|
|
$cheaperCount = 0;
|
|
$totalTrips = count($trips);
|
|
|
|
foreach ($trips as $trip) {
|
|
$compPrice = (float)$trip['total_price'];
|
|
$siroPrice = (float)$trip['distance_km'] * $currentSpeedPrice;
|
|
|
|
if ($compPrice > 0) {
|
|
$pciSum += ($siroPrice / $compPrice);
|
|
if ($siroPrice < $compPrice) {
|
|
$cheaperCount++;
|
|
}
|
|
}
|
|
}
|
|
|
|
$averagePci = round($pciSum / $totalTrips, 2);
|
|
$marketSharePct = round(($cheaperCount / $totalTrips) * 100, 2);
|
|
|
|
// 2. Count Anomalies (Last 7 Days)
|
|
$sqlAnomalies = "SELECT COUNT(*) as count FROM price_anomalies
|
|
WHERE country_code = :country
|
|
AND created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)";
|
|
$stmtAnomalies = $con->prepare($sqlAnomalies);
|
|
$stmtAnomalies->execute([':country' => $countryCode]);
|
|
$anomaliesCount = (int)$stmtAnomalies->fetchColumn();
|
|
|
|
// 3. Automated Campaigns / Surge Tracking (If applicable in logs)
|
|
$sqlCampaigns = "SELECT COUNT(*) as count FROM marketing_campaigns_log
|
|
WHERE JSON_EXTRACT(target_criteria, '$.country_code') = :country
|
|
AND sent_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)";
|
|
$stmtCampaigns = $con->prepare($sqlCampaigns);
|
|
$stmtCampaigns->execute([':country' => $countryCode]);
|
|
$campaignsCount = (int)$stmtCampaigns->fetchColumn();
|
|
|
|
// 4. Save to Database
|
|
$reportData = [
|
|
'total_competitor_samples' => $totalTrips,
|
|
'automated_campaigns_sent' => $campaignsCount,
|
|
'current_speed_price' => $currentSpeedPrice
|
|
];
|
|
|
|
$sqlInsert = "INSERT INTO market_health_reports
|
|
(report_date, country_code, average_pci, market_share_percent, total_anomalies, total_surge_opportunities, report_data_json)
|
|
VALUES (CURDATE(), :country, :pci, :market_share, :anomalies, :surge, :report_data)";
|
|
$stmtInsert = $con->prepare($sqlInsert);
|
|
$stmtInsert->execute([
|
|
':country' => $countryCode,
|
|
':pci' => $averagePci,
|
|
':market_share' => $marketSharePct,
|
|
':anomalies' => $anomaliesCount,
|
|
':surge' => 0, // Placeholder for actual surge count if tracked in DB
|
|
':report_data' => json_encode($reportData)
|
|
]);
|
|
|
|
echo "[".date('Y-m-d H:i:s')."] Report generated for $countryCode. PCI: $averagePci, Share: $marketSharePct%\n";
|
|
}
|
|
|
|
echo "[".date('Y-m-d H:i:s')."] cron_weekly_health_report completed successfully.\n";
|
|
|
|
} catch (Exception $e) {
|
|
echo "[".date('Y-m-d H:i:s')."] Error: " . $e->getMessage() . "\n";
|
|
}
|