Update: 2026-06-21 18:58:05
This commit is contained in:
77
backend/Admin/marketing/get_price_comparison.php
Normal file
77
backend/Admin/marketing/get_price_comparison.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../../connect.php';
|
||||
|
||||
if ($role !== 'admin' && $role !== 'super_admin') {
|
||||
http_response_code(403);
|
||||
echo json_encode(['status' => 'failure', 'message' => 'Unauthorized access.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
try {
|
||||
$countryCode = filterRequest('country_code');
|
||||
|
||||
// 1. Hourly competitor price averages (last 24h)
|
||||
$compSql = "SELECT
|
||||
DATE_FORMAT(created_at, '%Y-%m-%d %H:00:00') AS hour_bucket,
|
||||
AVG(price_per_km) AS avg_price_per_km,
|
||||
COUNT(*) AS sample_count
|
||||
FROM competitor_prices
|
||||
WHERE created_at >= DATE_SUB(NOW(), INTERVAL 24 HOUR)";
|
||||
$compParams = [];
|
||||
if ($countryCode) {
|
||||
$compSql .= " AND country_code = :country";
|
||||
$compParams[':country'] = strtoupper($countryCode);
|
||||
}
|
||||
$compSql .= " GROUP BY hour_bucket ORDER BY hour_bucket ASC LIMIT 24";
|
||||
|
||||
$stmt = $con->prepare($compSql);
|
||||
foreach ($compParams as $k => $v) {
|
||||
$stmt->bindValue($k, $v);
|
||||
}
|
||||
$stmt->execute();
|
||||
$hourlyData = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
// 2. PCI by region — group competitor prices by ~0.02° grid cells
|
||||
$pciSql = "SELECT
|
||||
ROUND(from_latitude * 50, 0) / 50 AS lat_group,
|
||||
ROUND(from_longitude * 50, 0) / 50 AS lng_group,
|
||||
competitor_name,
|
||||
AVG(price_per_km) AS avg_price_per_km,
|
||||
COUNT(*) AS samples
|
||||
FROM competitor_prices
|
||||
WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)";
|
||||
$pciParams = [];
|
||||
if ($countryCode) {
|
||||
$pciSql .= " AND country_code = :country2";
|
||||
$pciParams[':country2'] = strtoupper($countryCode);
|
||||
}
|
||||
$pciSql .= " GROUP BY lat_group, lng_group, competitor_name
|
||||
ORDER BY samples DESC LIMIT 20";
|
||||
|
||||
$stmtPci = $con->prepare($pciSql);
|
||||
foreach ($pciParams as $k => $v) {
|
||||
$stmtPci->bindValue($k, $v);
|
||||
}
|
||||
$stmtPci->execute();
|
||||
$pciData = $stmtPci->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
// 3. Siro base prices by category (from kazan table)
|
||||
$siroSql = "SELECT speedPrice, comfortPrice, awfarPrice, ladyPrice, electricPrice, vanPrice
|
||||
FROM kazan WHERE country = :country3 LIMIT 1";
|
||||
$countryNameMap = ['SY' => 'Syria', 'JO' => 'Jordan', 'EG' => 'Egypt', 'IQ' => 'Iraq'];
|
||||
$siroCountry = $countryNameMap[strtoupper($countryCode ?: 'SY')] ?? 'Syria';
|
||||
|
||||
$stmtSiro = $con->prepare($siroSql);
|
||||
$stmtSiro->execute([':country3' => $siroCountry]);
|
||||
$siroPrices = $stmtSiro->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
jsonSuccess([
|
||||
'hourly_competitor_prices' => $hourlyData,
|
||||
'pci_regions' => $pciData,
|
||||
'siro_base_prices' => $siroPrices ?: [],
|
||||
]);
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log("[get_price_comparison.php] Error: " . $e->getMessage());
|
||||
jsonError("Failed to fetch price comparison: " . $e->getMessage());
|
||||
}
|
||||
Reference in New Issue
Block a user