Update: 2026-05-08 01:52:24
This commit is contained in:
40
app/modules_app/tenants/delete.php
Normal file
40
app/modules_app/tenants/delete.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/**
|
||||
* Delete Tenant
|
||||
* POST /v1/tenants/delete
|
||||
*/
|
||||
|
||||
use App\Core\Database;
|
||||
use App\Core\AuditLogger;
|
||||
use App\Middleware\RoleMiddleware;
|
||||
|
||||
$decoded = RoleMiddleware::require(['super_admin']);
|
||||
|
||||
$data = input();
|
||||
$id = $data['id'] ?? null;
|
||||
if (!$id) json_error('معرّف المكتب مطلوب', 422);
|
||||
|
||||
$db = Database::getInstance();
|
||||
|
||||
// Check if tenant exists
|
||||
$stmt = $db->prepare("SELECT * FROM tenants WHERE id = ?");
|
||||
$stmt->execute([$id]);
|
||||
$tenant = $stmt->fetch();
|
||||
|
||||
if (!$tenant) json_error('المكتب غير موجود', 404);
|
||||
|
||||
// Check for linked users
|
||||
$stmtUsers = $db->prepare("SELECT COUNT(*) FROM users WHERE tenant_id = ?");
|
||||
$stmtUsers->execute([$id]);
|
||||
$userCount = $stmtUsers->fetchColumn();
|
||||
|
||||
if ($userCount > 0) {
|
||||
json_error("لا يمكن حذف المكتب — يوجد $userCount مستخدم مرتبط به. احذف المستخدمين أولاً.", 422);
|
||||
}
|
||||
|
||||
// Delete
|
||||
$db->prepare("DELETE FROM tenants WHERE id = ?")->execute([$id]);
|
||||
|
||||
AuditLogger::log('tenant.deleted', 'tenant', $id, ['name' => $tenant['name']], null, $decoded);
|
||||
|
||||
json_success(null, 'تم حذف المكتب المحاسبي بنجاح');
|
||||
Reference in New Issue
Block a user