41 lines
1.1 KiB
PHP
41 lines
1.1 KiB
PHP
<?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, 'تم حذف المكتب المحاسبي بنجاح');
|