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'] : 'أُلغيت الوثيقة');