Update: 2026-05-08 01:45:04

This commit is contained in:
Hamza-Ayed
2026-05-08 01:45:04 +03:00
parent ed8203a02e
commit 7680847e8c
2 changed files with 73 additions and 81 deletions

View File

@@ -39,86 +39,78 @@ if ($action) {
$whereClause = $where ? 'WHERE ' . implode(' AND ', $where) : ''; $whereClause = $where ? 'WHERE ' . implode(' AND ', $where) : '';
// Total count try {
$countStmt = $db->prepare("SELECT COUNT(*) FROM audit_log a $whereClause"); // Total count
$countStmt->execute($params); $countStmt = $db->prepare("SELECT COUNT(*) FROM audit_logs a $whereClause");
$total = $countStmt->fetchColumn(); $countStmt->execute($params);
$total = (int)$countStmt->fetchColumn();
// Fetch logs // Fetch logs
$params[] = $limit; $stmt = $db->prepare("
$params[] = $offset; 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(" // Format logs
SELECT a.*, u.name as user_name foreach ($logs as &$log) {
FROM audit_log a $log['old_values'] = json_decode($log['old_data'] ?? '{}', true);
LEFT JOIN users u ON a.user_id = u.id $log['details'] = json_decode($log['new_data'] ?? '{}', true);
$whereClause unset($log['old_data'], $log['new_data'], $log['user_agent'], $log['ip_address']);
ORDER BY a.created_at DESC
LIMIT ? OFFSET ?
");
$stmt->execute($params);
$logs = $stmt->fetchAll();
// Format logs // Generate human-readable summary
foreach ($logs as &$log) { $a = $log['action'] ?? '';
$log['details'] = json_decode($log['details'] ?? '{}', true); if (str_starts_with($a, 'invoice.')) {
$log['old_values'] = json_decode($log['old_values'] ?? '{}', true); $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);
// Generate human-readable summary json_success([
$log['summary'] = match(true) { 'logs' => $logs,
str_starts_with($log['action'], 'invoice.') => _invoiceSummary($log), 'pagination' => [
str_starts_with($log['action'], 'user.') => _userSummary($log), 'page' => $page,
str_starts_with($log['action'], 'company.') => _companySummary($log), 'limit' => $limit,
str_starts_with($log['action'], 'payment.') => _paymentSummary($log), 'total' => $total,
default => $log['action'], 'pages' => $total > 0 ? (int)ceil($total / $limit) : 1,
}; ],
} ]);
unset($log); } catch (\Exception $e) {
error_log("Audit log error: " . $e->getMessage());
json_success([ json_error('خطأ في جلب سجل النشاط: ' . $e->getMessage(), 500);
'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'],
};
} }

View File

@@ -25,7 +25,7 @@ class AuditLogView extends StatelessWidget {
color: isDark ? const Color(0xFF1E1E2E) : Colors.white, color: isDark ? const Color(0xFF1E1E2E) : Colors.white,
child: SingleChildScrollView( child: SingleChildScrollView(
scrollDirection: Axis.horizontal, scrollDirection: Axis.horizontal,
child: Obx(() => Row( child: Row(
children: [ children: [
_filterChip('الكل', 'all', controller, isDark), _filterChip('الكل', 'all', controller, isDark),
_filterChip('الفواتير', 'invoice', controller, isDark), _filterChip('الفواتير', 'invoice', controller, isDark),
@@ -33,7 +33,7 @@ class AuditLogView extends StatelessWidget {
_filterChip('الشركات', 'company', controller, isDark), _filterChip('الشركات', 'company', controller, isDark),
_filterChip('المدفوعات', 'payment', controller, isDark), _filterChip('المدفوعات', 'payment', controller, isDark),
], ],
)), ),
), ),
), ),