0) { $where[] = 'v.station_id = ?'; $params[] = $stationId; } if (!empty($status)) { $where[] = 'v.status = ?'; $params[] = $status; } if (!empty($driverId)) { $where[] = 'v.driver_id = ?'; $params[] = $driverId; } $clause = $where ? 'WHERE ' . implode(' AND ', $where) : ''; try { $st = $con->prepare(" SELECT v.id, v.driver_id, v.code, v.amount_authorized, v.amount_redeemed, v.currency, v.status, v.expires_at, v.redeemed_at, v.created_at, s.name_ar AS station_name FROM fuel_vouchers v LEFT JOIN fuel_stations s ON s.id = v.station_id $clause ORDER BY v.id DESC LIMIT $limit "); $st->execute($params); $rows = $st->fetchAll(PDO::FETCH_ASSOC); // ‏الملخّص على كامل المنتج لا على الصفحة المعروضة: رقمٌ يتغيّر بتغيّر // ‏الفلتر يُقرأ كأنه حقيقة عن المنصة وهو ليس كذلك. $summary = $con->query(" SELECT COALESCE(SUM(amount), 0) AS lent_total, COALESCE(SUM(amount_collected), 0) AS collected_total, COALESCE(SUM(amount_remaining), 0) AS outstanding_total, COUNT(DISTINCT driver_id) AS drivers_count FROM obligation_ledger WHERE product_code = 'fuel_wallet' ")->fetch(PDO::FETCH_ASSOC) ?: []; } catch (PDOException $e) { error_log('[admin/fuel/drawdowns] ' . $e->getMessage()); jsonError('Server error', 500); } $vouchers = array_map(static fn(array $r): array => [ 'id' => (int) $r['id'], 'driver_id' => $r['driver_id'], 'code' => $r['code'], 'amount_authorized' => (float) $r['amount_authorized'], 'amount_redeemed' => (float) $r['amount_redeemed'], 'currency' => $r['currency'], 'status' => $r['status'], 'station' => $r['station_name'], 'expires_at' => $r['expires_at'], 'redeemed_at' => $r['redeemed_at'], 'created_at' => $r['created_at'], ], $rows); jsonSuccess([ 'vouchers' => $vouchers, 'summary' => [ 'lent_total' => (float) ($summary['lent_total'] ?? 0), 'collected_total' => (float) ($summary['collected_total'] ?? 0), 'outstanding_total' => (float) ($summary['outstanding_total'] ?? 0), 'drivers_count' => (int) ($summary['drivers_count'] ?? 0), ], ], 'success');