Files
Siro/backend/driver_assurance/get.php
T

86 lines
3.6 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
/**
* driver_assurance/get.php — حالة تأمين السائق
*
* ‏كان هذا الملف فارغاً (صفر بايت) — يُستدعى فيرد بلا شيء.
*
* ‏يرد بالوثيقة النشطة، وملخّص دفتر أقساطها، وآخر القيود. الدفتر جزء
* ‏من الرد لا نقطة منفصلة: السائق الذي يُخصم من أرباحه يجب أن يرى
* ‏مقابل ماذا في الشاشة نفسها، وإلا صار الخصم مفاجأة تُفقد الثقة.
*/
require_once __DIR__ . '/../connect.php';
require_once __DIR__ . '/eligibility.php';
$driverId = $user_id ?? '';
if (empty($driverId) || ($role ?? '') !== 'driver') {
jsonError('Unauthorized', 401);
}
$policy = assuranceActivePolicy($con, $driverId);
if (!$policy) {
jsonSuccess([
'has_policy' => false,
'policy' => null,
'ledger' => ['pending_total' => 0, 'settled_total' => 0, 'currency' => null],
'entries' => [],
], 'لا توجد وثيقة نشطة');
}
$summary = ['pending_total' => 0.0, 'settled_total' => 0.0,
'currency' => $policy['currency']];
$entries = [];
// ‏المصدر هو الدفتر الموحّد لا دفتر التأمين القديم (المجمَّد بعد ترحيل
// ‏2026_08_08). المفتاح صار (السائق + رمز المنتج) بدل رقم الوثيقة:
// ‏الدفتر الموحّد لا يعرف الوثائق، يعرف التزامات.
try {
// ‏المستحق من `amount_remaining` والمحصَّل من `amount_collected`:
// ‏قيدٌ حُصِّل نصفه يظهر في الرقمين معاً بحصّته الصحيحة، بينما جمع
// ‏`amount` حسب الحالة كان سيعدّه كاملاً في أحدهما ويكذب في كليهما.
$st = $con->prepare("
SELECT COALESCE(SUM(amount_remaining), 0) AS pending_total,
COALESCE(SUM(amount_collected), 0) AS settled_total
FROM obligation_ledger
WHERE driver_id = ? AND product_code = ?
");
$st->execute([$driverId, $policy['code']]);
if ($row = $st->fetch(PDO::FETCH_ASSOC)) {
$summary['pending_total'] = (float) $row['pending_total'];
$summary['settled_total'] = (float) $row['settled_total'];
}
// ‏آخر ثلاثين قيداً: كافية لشهر يومي، وتمنع رداً ينمو بلا حد.
$st = $con->prepare("
SELECT charge_date, amount, amount_collected, amount_remaining,
currency, status, note
FROM obligation_ledger
WHERE driver_id = ? AND product_code = ?
ORDER BY charge_date DESC
LIMIT 30
");
$st->execute([$driverId, $policy['code']]);
$entries = $st->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
error_log('[assurance/get] تعذّرت قراءة الدفتر: ' . $e->getMessage());
}
jsonSuccess([
'has_policy' => true,
'policy' => [
'id' => (int) $policy['id'],
'plan' => $policy['plan_name'],
'provider' => $policy['provider_name_ar'] ?: $policy['provider_name'],
'policy_number' => $policy['policy_number'],
'coverage' => $policy['coverage_summary'],
'billing_cycle' => $policy['billing_cycle'],
'premium' => (float) $policy['premium'],
'currency' => $policy['currency'],
'started_at' => $policy['started_at'],
'last_charged_on' => $policy['last_charged_on'],
],
'ledger' => $summary,
'entries' => $entries,
], 'success');