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) {