65 lines
2.4 KiB
PHP
65 lines
2.4 KiB
PHP
<?php
|
|
/**
|
|
* driver_assurance/cancel.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) {
|
|
jsonError('لا توجد وثيقة نشطة');
|
|
}
|
|
|
|
$reason = filterRequest('reason') ?: 'بطلب السائق';
|
|
|
|
try {
|
|
// الشرط على driver_id إلى جانب المعرّف: الوثيقة أتت من استعلام
|
|
// مقيَّد بالسائق أصلاً، لكن الشرط هنا يجعل الاستعلام آمناً بذاته
|
|
// لو أعيد استعماله يوماً في سياق آخر.
|
|
$st = $con->prepare("
|
|
UPDATE driver_insurance_policies
|
|
SET status = 'cancelled', ended_at = CURDATE(), cancel_reason = ?
|
|
WHERE id = ? AND driver_id = ? AND status = 'active'
|
|
");
|
|
$st->execute([mb_substr($reason, 0, 255), $policy['id'], $driverId]);
|
|
|
|
if ($st->rowCount() === 0) {
|
|
jsonError('تعذّر إلغاء الوثيقة');
|
|
}
|
|
} catch (PDOException $e) {
|
|
error_log('[assurance/cancel] ' . $e->getMessage());
|
|
jsonError('Server error');
|
|
}
|
|
|
|
$pending = 0.0;
|
|
try {
|
|
$st = $con->prepare("
|
|
SELECT COALESCE(SUM(amount), 0) FROM insurance_premium_ledger
|
|
WHERE policy_id = ? AND status = 'pending'
|
|
");
|
|
$st->execute([$policy['id']]);
|
|
$pending = (float) $st->fetchColumn();
|
|
} catch (PDOException $e) {
|
|
error_log('[assurance/cancel] تعذّرت قراءة المستحق: ' . $e->getMessage());
|
|
}
|
|
|
|
error_log("[assurance] أُلغيت وثيقة #{$policy['id']} للسائق $driverId");
|
|
|
|
jsonSuccess([
|
|
'cancelled' => true,
|
|
'pending_balance' => $pending,
|
|
'currency' => $policy['currency'],
|
|
], $pending > 0
|
|
? 'أُلغيت الوثيقة. يبقى عليك مستحق أقساط سابقة قدره ' . $pending . ' ' . $policy['currency']
|
|
: 'أُلغيت الوثيقة');
|