Files
Siro/backend/ride/earnings/trips.php
T

122 lines
5.0 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* ride/earnings/trips.php — تفصيل أرباح كل رحلة
* ─────────────────────────────────────────────────────────────
* ‏السؤال الأول عند كل سائق في العالم: «لماذا أخذت هذا المبلغ من هذه
* ‏الرحلة؟» — ولم يكن في التطبيق ما يجيبه. `payment_history_driver_page`
* ‏يعرض حركات محفظة (شحن، سحب، تحويل)، لا تركيبة أجرة رحلة.
*
* ‏وأثر ذلك ليس شكاوى فحسب: المنصة تمنح إعفاء عمولة ٠٪ عند تحقيق سلسلة
* ‏(`hasZeroCommission` في `finish_ride_updates.php`) — هدية حقيقية
* ‏يدفع ثمنها الصندوق ولا يراها السائق في أي شاشة. نحن ندفع ولا نحصد.
*
* POST: date (اختياري YYYY-MM-DD) أو range=week|month، limit
*/
require_once __DIR__ . '/../../connect.php';
$driverId = $user_id ?? '';
if (empty($driverId) || ($role ?? '') !== 'driver') {
jsonError('Unauthorized', 401);
}
$date = filterRequest('date');
$range = filterRequest('range');
$limit = (int) (filterRequest('limit', 'int') ?: 50);
$limit = max(1, min($limit, 200));
$params = [$driverId];
if (!empty($date)) {
// ‏تاريخ اليوم الواحد: الاستعمال الأغلب — السائق ينقر عموداً في الرسم
// ‏البياني ليرى رحلات ذلك اليوم.
$dateCondition = 'DATE(r.created_at) = ?';
$params[] = $date;
} elseif ($range === 'month') {
$dateCondition = 'r.created_at >= DATE_FORMAT(NOW(), "%Y-%m-01")';
} else {
$dateCondition = 'r.created_at >= DATE(NOW()) - INTERVAL 6 DAY';
}
try {
$st = $con->prepare("
SELECT r.id, r.created_at, r.date, r.time, r.endtime,
r.rideTimeStart, r.rideTimeFinish,
r.start_location, r.end_location,
r.price, r.price_for_driver, r.price_for_passenger,
r.distance, r.carType, r.paymentMethod,
r.ai_negotiated_bonus,
GREATEST(TIMESTAMPDIFF(MINUTE, r.rideTimeStart, r.rideTimeFinish), 0) AS minutes
FROM ride r
WHERE r.driver_id = ?
AND r.status IN ('Finished', 'finished')
AND $dateCondition
ORDER BY r.created_at DESC
LIMIT $limit
");
$st->execute($params);
$rows = $st->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
error_log('[earnings/trips] ' . $e->getMessage());
jsonError('تعذّر جلب تفاصيل الرحلات', 500);
}
$trips = array_map(static function (array $r): array {
$gross = round((float) $r['price'], 2);
$net = round((float) $r['price_for_driver'], 2);
$bonus = round((float) ($r['ai_negotiated_bonus'] ?? 0), 2);
// ‏العمولة مشتقّة من الفرق لا محسوبة بنسبة اليوم: النسبة تتغيّر
// ‏بالمنطقة والوقت وخصومات العمولة، وحسابها الآن بالنسبة الحالية
// ‏يُظهر للسائق رقماً يخالف ما اقتُطع منه فعلاً وقت الرحلة.
$commission = round(max($gross - $net, 0), 2);
// ‏إعفاء العمولة: صفرٌ حقيقي لا غياب بيانات. نميّزه صراحةً ليظهر في
// ‏التطبيق كمكسب («عمولة معفاة») لا كسطر فارغ.
$zeroCommission = $gross > 0 && $commission <= 0.009;
return [
'ride_id' => (int) $r['id'],
'at' => $r['created_at'],
'from' => $r['start_location'],
'to' => $r['end_location'],
'car_type' => $r['carType'],
'payment_method' => $r['paymentMethod'],
'distance_km' => round((float) $r['distance'], 1),
'minutes' => (int) $r['minutes'],
'breakdown' => [
'gross' => $gross,
'commission' => $commission,
'bonus' => $bonus,
'net' => $net,
'zero_commission' => $zeroCommission,
],
];
}, $rows);
// ‏مجاميع القائمة المعروضة نفسها. حسابها في التطبيق كان سيعني رقماً
// ‏يختلف عن الملخّص عند اختلاف التقريب — وهو بالضبط نوع التناقض الذي
// ‏جاءت هذه النقطة لتنهيه.
$sum = [
'trips' => count($trips),
'gross' => 0.0,
'commission' => 0.0,
'net' => 0.0,
];
foreach ($trips as $t) {
$sum['gross'] += $t['breakdown']['gross'];
$sum['commission'] += $t['breakdown']['commission'];
$sum['net'] += $t['breakdown']['net'];
}
foreach (['gross', 'commission', 'net'] as $k) {
$sum[$k] = round($sum[$k], 2);
}
jsonSuccess([
'trips' => $trips,
'summary' => $sum,
'as_of' => date('c'),
], 'success');