diff --git a/app/modules_app/audit/index.php b/app/modules_app/audit/index.php index 2335e2b..8983a75 100644 --- a/app/modules_app/audit/index.php +++ b/app/modules_app/audit/index.php @@ -39,86 +39,78 @@ if ($action) { $whereClause = $where ? 'WHERE ' . implode(' AND ', $where) : ''; -// Total count -$countStmt = $db->prepare("SELECT COUNT(*) FROM audit_log a $whereClause"); -$countStmt->execute($params); -$total = $countStmt->fetchColumn(); +try { + // Total count + $countStmt = $db->prepare("SELECT COUNT(*) FROM audit_logs a $whereClause"); + $countStmt->execute($params); + $total = (int)$countStmt->fetchColumn(); -// Fetch logs -$params[] = $limit; -$params[] = $offset; + // Fetch logs + $stmt = $db->prepare(" + SELECT a.*, u.name as user_name + FROM audit_logs a + LEFT JOIN users u ON a.user_id = u.id + $whereClause + ORDER BY a.created_at DESC + LIMIT $limit OFFSET $offset + "); + $stmt->execute($params); + $logs = $stmt->fetchAll(); -$stmt = $db->prepare(" - SELECT a.*, u.name as user_name - FROM audit_log a - LEFT JOIN users u ON a.user_id = u.id - $whereClause - ORDER BY a.created_at DESC - LIMIT ? OFFSET ? -"); -$stmt->execute($params); -$logs = $stmt->fetchAll(); + // Format logs + foreach ($logs as &$log) { + $log['old_values'] = json_decode($log['old_data'] ?? '{}', true); + $log['details'] = json_decode($log['new_data'] ?? '{}', true); + unset($log['old_data'], $log['new_data'], $log['user_agent'], $log['ip_address']); + + // Generate human-readable summary + $a = $log['action'] ?? ''; + if (str_starts_with($a, 'invoice.')) { + $log['summary'] = match($a) { + 'invoice.approved' => 'تم اعتماد فاتورة', + 'invoice.updated' => 'تم تعديل فاتورة', + 'invoice.bulk_approved' => 'اعتماد جماعي', + 'invoice.uploaded' => 'تم رفع فاتورة', + 'invoice.extracted' => 'تم استخراج بيانات فاتورة', + default => $a, + }; + } elseif (str_starts_with($a, 'user.')) { + $log['summary'] = match($a) { + 'user.created' => 'تم إنشاء مستخدم جديد', + 'user.updated' => 'تم تعديل بيانات مستخدم', + 'user.deleted' => 'تم حذف مستخدم', + 'user.login' => 'تسجيل دخول', + default => $a, + }; + } elseif (str_starts_with($a, 'company.')) { + $log['summary'] = match($a) { + 'company.created' => 'تم إنشاء شركة جديدة', + 'company.updated' => 'تم تعديل بيانات شركة', + default => $a, + }; + } elseif (str_starts_with($a, 'payment.')) { + $log['summary'] = match($a) { + 'payment.created' => 'تم إنشاء طلب دفع', + 'payment.uploaded' => 'تم رفع وصل دفع', + 'payment.approved' => 'تم اعتماد دفعة', + default => $a, + }; + } else { + $log['summary'] = $a; + } + } + unset($log); -// Format logs -foreach ($logs as &$log) { - $log['details'] = json_decode($log['details'] ?? '{}', true); - $log['old_values'] = json_decode($log['old_values'] ?? '{}', true); - - // Generate human-readable summary - $log['summary'] = match(true) { - str_starts_with($log['action'], 'invoice.') => _invoiceSummary($log), - str_starts_with($log['action'], 'user.') => _userSummary($log), - str_starts_with($log['action'], 'company.') => _companySummary($log), - str_starts_with($log['action'], 'payment.') => _paymentSummary($log), - default => $log['action'], - }; -} -unset($log); - -json_success([ - 'logs' => $logs, - 'pagination' => [ - 'page' => $page, - 'limit' => $limit, - 'total' => (int)$total, - 'pages' => ceil($total / $limit), - ], -]); - -function _invoiceSummary(array $log): string { - return match($log['action']) { - 'invoice.approved' => 'تم اعتماد فاتورة', - 'invoice.updated' => 'تم تعديل فاتورة', - 'invoice.bulk_approved' => 'اعتماد جماعي', - 'invoice.uploaded' => 'تم رفع فاتورة', - 'invoice.extracted' => 'تم استخراج بيانات فاتورة', - default => $log['action'], - }; -} - -function _userSummary(array $log): string { - return match($log['action']) { - 'user.created' => 'تم إنشاء مستخدم جديد', - 'user.updated' => 'تم تعديل بيانات مستخدم', - 'user.deleted' => 'تم حذف مستخدم', - 'user.login' => 'تسجيل دخول', - default => $log['action'], - }; -} - -function _companySummary(array $log): string { - return match($log['action']) { - 'company.created' => 'تم إنشاء شركة جديدة', - 'company.updated' => 'تم تعديل بيانات شركة', - default => $log['action'], - }; -} - -function _paymentSummary(array $log): string { - return match($log['action']) { - 'payment.created' => 'تم إنشاء طلب دفع', - 'payment.uploaded' => 'تم رفع وصل دفع', - 'payment.approved' => 'تم اعتماد دفعة', - default => $log['action'], - }; + json_success([ + 'logs' => $logs, + 'pagination' => [ + 'page' => $page, + 'limit' => $limit, + 'total' => $total, + 'pages' => $total > 0 ? (int)ceil($total / $limit) : 1, + ], + ]); +} catch (\Exception $e) { + error_log("Audit log error: " . $e->getMessage()); + json_error('خطأ في جلب سجل النشاط: ' . $e->getMessage(), 500); } diff --git a/musadaq-app/lib/features/audit/views/audit_log_view.dart b/musadaq-app/lib/features/audit/views/audit_log_view.dart index 13b99a2..ee7c6ae 100644 --- a/musadaq-app/lib/features/audit/views/audit_log_view.dart +++ b/musadaq-app/lib/features/audit/views/audit_log_view.dart @@ -25,7 +25,7 @@ class AuditLogView extends StatelessWidget { color: isDark ? const Color(0xFF1E1E2E) : Colors.white, child: SingleChildScrollView( scrollDirection: Axis.horizontal, - child: Obx(() => Row( + child: Row( children: [ _filterChip('الكل', 'all', controller, isDark), _filterChip('الفواتير', 'invoice', controller, isDark), @@ -33,7 +33,7 @@ class AuditLogView extends StatelessWidget { _filterChip('الشركات', 'company', controller, isDark), _filterChip('المدفوعات', 'payment', controller, isDark), ], - )), + ), ), ),