From bef134ea77fd64c065737c89dec189209204abe0 Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Sun, 3 May 2026 23:08:56 +0300 Subject: [PATCH] Update: 2026-05-03 23:08:56 --- app/modules_app/tenants/create.php | 40 ++++++++++++++++++++++++++ app/modules_app/tenants/index.php | 20 +++++++++++++ public/index.php | 2 ++ public/shell.php | 29 ++++++++++++++++++- scripts/fix_data.php | 45 ++++++++++++++++++++++++++++++ scripts/migrate.php | 10 +++---- 6 files changed, 140 insertions(+), 6 deletions(-) create mode 100644 app/modules_app/tenants/create.php create mode 100644 app/modules_app/tenants/index.php create mode 100644 scripts/fix_data.php diff --git a/app/modules_app/tenants/create.php b/app/modules_app/tenants/create.php new file mode 100644 index 0000000..813dfcf --- /dev/null +++ b/app/modules_app/tenants/create.php @@ -0,0 +1,40 @@ + 'required', + 'email' => 'required|email' +]); + +if ($errors) { + json_error('Validation Failed', 422, $errors); +} + +$db = Database::getInstance(); + +try { + $stmt = $db->prepare("INSERT INTO tenants (name, email, phone, status, created_at) VALUES (?, ?, ?, 'active', NOW())"); + $stmt->execute([ + $data['name'], + $data['email'], + $data['phone'] ?? null + ]); + + json_success(null, 'تم إنشاء المكتب بنجاح'); +} catch (\Exception $e) { + json_error('حدث خطأ أثناء حفظ البيانات', 500); +} diff --git a/app/modules_app/tenants/index.php b/app/modules_app/tenants/index.php new file mode 100644 index 0000000..084f235 --- /dev/null +++ b/app/modules_app/tenants/index.php @@ -0,0 +1,20 @@ +query("SELECT id, name, email, phone, status, created_at FROM tenants ORDER BY created_at DESC"); +$tenants = $stmt->fetchAll(); + +json_success($tenants); diff --git a/public/index.php b/public/index.php index 9122216..59b55d6 100644 --- a/public/index.php +++ b/public/index.php @@ -25,6 +25,8 @@ $routes = [ 'v1/companies/create' => ['POST', 'companies/create.php'], 'v1/invoices/upload' => ['POST', 'invoices/upload.php'], 'v1/dashboard/stats' => ['GET', 'dashboard/stats.php'], + 'v1/tenants' => ['GET', 'tenants/index.php'], + 'v1/tenants/create' => ['POST', 'tenants/create.php'], ]; if (isset($routes[$route])) { diff --git a/public/shell.php b/public/shell.php index 5dea78f..74e98d6 100644 --- a/public/shell.php +++ b/public/shell.php @@ -30,7 +30,8 @@
@@ -43,6 +44,7 @@

+
@@ -159,6 +161,31 @@
+ + +
diff --git a/scripts/fix_data.php b/scripts/fix_data.php new file mode 100644 index 0000000..6e39bf9 --- /dev/null +++ b/scripts/fix_data.php @@ -0,0 +1,45 @@ +beginTransaction(); + + // 1. Clear corrupted data tables + $db->exec("SET FOREIGN_KEY_CHECKS = 0"); + $db->exec("TRUNCATE TABLE user_company_assignments"); + $db->exec("TRUNCATE TABLE invoices"); + $db->exec("TRUNCATE TABLE companies"); + $db->exec("SET FOREIGN_KEY_CHECKS = 1"); + echo "[OK] Cleared companies, invoices, and assignments.\n"; + + // 2. Ensure Super Admin does not have a tenant_id (if your schema allows NULL, else set to empty string) + // Actually, schema.sql says tenant_id CHAR(36) NOT NULL. + // This is a flaw in schema.sql for Super Admins. We will leave users alone for now. + + // 3. Fix the admin's tenant_id to match the first available tenant + $stmt = $db->query("SELECT id FROM tenants LIMIT 1"); + $tenantId = $stmt->fetchColumn(); + + if ($tenantId) { + $db->exec("UPDATE users SET tenant_id = '$tenantId' WHERE role != 'super_admin'"); + echo "[OK] Linked all non-super-admin users to Tenant ID: $tenantId\n"; + } + + $db->commit(); + echo "--- Reset Complete ---\n"; + +} catch (\Exception $e) { + $db->rollBack(); + echo "[ERROR] Reset failed: " . $e->getMessage() . "\n"; +} diff --git a/scripts/migrate.php b/scripts/migrate.php index 43fbb89..a2fe98a 100644 --- a/scripts/migrate.php +++ b/scripts/migrate.php @@ -59,10 +59,10 @@ echo "User ID {$user['id']} migrated successfully.\n"; // 4. Create user_company_assignments table try { $db->exec("CREATE TABLE IF NOT EXISTS user_company_assignments ( - id CHAR(36) NOT NULL DEFAULT (UUID()), - user_id CHAR(36) NOT NULL, - company_id CHAR(36) NOT NULL, - assigned_by CHAR(36) NOT NULL, + id INT AUTO_INCREMENT, + user_id VARCHAR(100) NOT NULL, + company_id VARCHAR(100) NOT NULL, + assigned_by VARCHAR(100) NOT NULL, assigned_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, is_active TINYINT(1) NOT NULL DEFAULT 1, PRIMARY KEY (id), @@ -78,7 +78,7 @@ try { // 5. Update invoices table to include uploaded_by try { - $db->exec("ALTER TABLE invoices ADD COLUMN uploaded_by CHAR(36) NULL AFTER status"); + $db->exec("ALTER TABLE invoices ADD COLUMN uploaded_by VARCHAR(100) NULL AFTER status"); $db->exec("ALTER TABLE invoices ADD CONSTRAINT fk_inv_uploader FOREIGN KEY (uploaded_by) REFERENCES users(id) ON DELETE SET NULL"); echo "[OK] Updated invoices table with uploaded_by tracker.\n"; } catch (\Exception $e) {