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