From aabdb7b681d5f03291fa6292c212592a0841db00 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Mon, 20 Apr 2026 05:05:37 +0000 Subject: [PATCH] update setup file --- db/schema.sql | 238 ++++++++++++++++++++++++++++++++++++++++++++++++ db/setup.php | 40 ++++++++ db_seed.php | 36 -------- dump_schema.php | 14 +++ patch.php | 90 ------------------ 5 files changed, 292 insertions(+), 126 deletions(-) create mode 100644 db/schema.sql create mode 100644 db/setup.php delete mode 100644 db_seed.php create mode 100644 dump_schema.php delete mode 100644 patch.php diff --git a/db/schema.sql b/db/schema.sql new file mode 100644 index 0000000..2209a95 --- /dev/null +++ b/db/schema.sql @@ -0,0 +1,238 @@ +-- Table structure for `branches` +CREATE TABLE IF NOT EXISTS `branches` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `code` varchar(50) NOT NULL, + `name_ar` varchar(100) NOT NULL, + `name_en` varchar(100) NOT NULL, + `city_ar` varchar(100) DEFAULT NULL, + `city_en` varchar(100) DEFAULT NULL, + `created_at` datetime DEFAULT current_timestamp(), + PRIMARY KEY (`id`), + UNIQUE KEY `code` (`code`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- Table structure for `categories` +CREATE TABLE IF NOT EXISTS `categories` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `name_ar` varchar(150) NOT NULL, + `name_en` varchar(150) NOT NULL, + `description` text DEFAULT NULL, + `created_at` datetime NOT NULL DEFAULT current_timestamp(), + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- Table structure for `customers` +CREATE TABLE IF NOT EXISTS `customers` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `name` varchar(150) NOT NULL, + `phone` varchar(50) DEFAULT NULL, + `email` varchar(150) DEFAULT NULL, + `address` text DEFAULT NULL, + `created_at` datetime NOT NULL DEFAULT current_timestamp(), + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- Table structure for `expense_categories` +CREATE TABLE IF NOT EXISTS `expense_categories` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name_ar` varchar(255) NOT NULL, + `name_en` varchar(255) NOT NULL, + `created_at` timestamp NULL DEFAULT current_timestamp(), + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- Table structure for `expenses` +CREATE TABLE IF NOT EXISTS `expenses` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `branch_code` varchar(50) DEFAULT NULL, + `category_id` int(11) NOT NULL, + `amount` decimal(10,3) NOT NULL, + `expense_date` date NOT NULL, + `description` text DEFAULT NULL, + `created_by` int(11) DEFAULT NULL, + `created_at` timestamp NULL DEFAULT current_timestamp(), + PRIMARY KEY (`id`), + KEY `category_id` (`category_id`), + CONSTRAINT `expenses_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `expense_categories` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- Table structure for `items` +CREATE TABLE IF NOT EXISTS `items` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `sku` varchar(50) NOT NULL, + `name` varchar(200) NOT NULL, + `price` decimal(10,3) NOT NULL, + `cost_price` decimal(10,2) DEFAULT 0.00, + `base_stock` int(11) NOT NULL DEFAULT 0, + `vat` decimal(5,3) NOT NULL DEFAULT 5.000, + `category_id` int(10) unsigned DEFAULT NULL, + `supplier_id` int(10) unsigned DEFAULT NULL, + `image_url` varchar(255) DEFAULT NULL, + `created_at` datetime DEFAULT current_timestamp(), + `unit_id` int(10) unsigned DEFAULT NULL, + `in_catalog` tinyint(1) NOT NULL DEFAULT 0, + PRIMARY KEY (`id`), + UNIQUE KEY `sku` (`sku`), + KEY `category_id` (`category_id`), + KEY `supplier_id` (`supplier_id`), + KEY `items_unit_fk` (`unit_id`), + CONSTRAINT `items_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE SET NULL, + CONSTRAINT `items_ibfk_2` FOREIGN KEY (`supplier_id`) REFERENCES `suppliers` (`id`) ON DELETE SET NULL, + CONSTRAINT `items_unit_fk` FOREIGN KEY (`unit_id`) REFERENCES `units` (`id`) ON DELETE SET NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- Table structure for `online_orders` +CREATE TABLE IF NOT EXISTS `online_orders` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `customer_name` varchar(255) NOT NULL, + `customer_phone` varchar(50) NOT NULL, + `customer_address` text NOT NULL, + `items_json` longtext NOT NULL, + `subtotal` decimal(10,2) DEFAULT 0.00, + `vat_amount` decimal(10,2) DEFAULT 0.00, + `total_amount` decimal(10,2) NOT NULL, + `status` varchar(20) NOT NULL DEFAULT 'pending', + `created_at` datetime DEFAULT current_timestamp(), + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- Table structure for `purchase_orders` +CREATE TABLE IF NOT EXISTS `purchase_orders` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `reference_no` varchar(50) NOT NULL, + `branch_code` varchar(30) NOT NULL, + `user_username` varchar(60) NOT NULL, + `user_name` varchar(120) NOT NULL, + `role_name` varchar(40) NOT NULL, + `supplier_name` varchar(120) DEFAULT NULL, + `items_json` longtext NOT NULL, + `item_count` int(10) unsigned NOT NULL DEFAULT 0, + `subtotal` decimal(10,2) NOT NULL DEFAULT 0.00, + `vat_amount` decimal(10,3) NOT NULL DEFAULT 0.000, + `total_amount` decimal(10,2) NOT NULL DEFAULT 0.00, + `notes` text DEFAULT NULL, + `status` varchar(20) NOT NULL DEFAULT 'completed', + `purchase_date` datetime NOT NULL DEFAULT current_timestamp(), + `created_at` datetime NOT NULL DEFAULT current_timestamp(), + PRIMARY KEY (`id`), + UNIQUE KEY `reference_no` (`reference_no`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- Table structure for `sales_orders` +CREATE TABLE IF NOT EXISTS `sales_orders` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `receipt_no` varchar(50) NOT NULL, + `sale_mode` varchar(20) NOT NULL, + `branch_code` varchar(30) NOT NULL, + `cashier_username` varchar(60) NOT NULL, + `cashier_name` varchar(120) NOT NULL, + `role_name` varchar(40) NOT NULL, + `customer_name` varchar(120) DEFAULT NULL, + `payment_method` varchar(30) NOT NULL, + `items_json` longtext NOT NULL, + `item_count` int(10) unsigned NOT NULL DEFAULT 0, + `subtotal` decimal(10,2) NOT NULL DEFAULT 0.00, + `vat_amount` decimal(10,3) NOT NULL DEFAULT 0.000, + `total_amount` decimal(10,2) NOT NULL DEFAULT 0.00, + `notes` text DEFAULT NULL, + `sale_date` datetime NOT NULL DEFAULT current_timestamp(), + `created_at` datetime NOT NULL DEFAULT current_timestamp(), + `status` varchar(20) NOT NULL DEFAULT 'completed', + PRIMARY KEY (`id`), + UNIQUE KEY `receipt_no` (`receipt_no`), + KEY `idx_sale_mode` (`sale_mode`), + KEY `idx_branch_code` (`branch_code`), + KEY `idx_sale_date` (`sale_date`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- Table structure for `settings` +CREATE TABLE IF NOT EXISTS `settings` ( + `setting_key` varchar(50) NOT NULL, + `setting_value` text DEFAULT NULL, + PRIMARY KEY (`setting_key`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- Table structure for `suppliers` +CREATE TABLE IF NOT EXISTS `suppliers` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `name` varchar(150) NOT NULL, + `contact_person` varchar(150) DEFAULT NULL, + `phone` varchar(50) DEFAULT NULL, + `email` varchar(150) DEFAULT NULL, + `address` text DEFAULT NULL, + `created_at` datetime NOT NULL DEFAULT current_timestamp(), + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- Table structure for `units` +CREATE TABLE IF NOT EXISTS `units` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `name_ar` varchar(150) NOT NULL, + `name_en` varchar(150) NOT NULL, + `created_at` datetime DEFAULT current_timestamp(), + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- Table structure for `users` +CREATE TABLE IF NOT EXISTS `users` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `username` varchar(50) NOT NULL, + `password` varchar(255) NOT NULL, + `role` varchar(50) NOT NULL, + `permissions` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`permissions`)), + `branch_code` varchar(50) NOT NULL, + `allowed_branches` varchar(255) DEFAULT NULL, + `name_ar` varchar(100) NOT NULL, + `name_en` varchar(100) NOT NULL, + `created_at` timestamp NULL DEFAULT current_timestamp(), + PRIMARY KEY (`id`), + UNIQUE KEY `username` (`username`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- Data for table `branches` +INSERT IGNORE INTO `branches` (`id`, `code`, `name_ar`, `name_en`, `city_ar`, `city_en`, `created_at`) VALUES ('1', 'muscat', 'الفرع الرئيسي', 'Main Branch', 'مسقط', 'Muscat', '2026-04-19 15:16:52'); +INSERT IGNORE INTO `branches` (`id`, `code`, `name_ar`, `name_en`, `city_ar`, `city_en`, `created_at`) VALUES ('2', 'sohar', 'فرع صحار', 'Sohar Branch', 'صحار', 'Sohar', '2026-04-19 15:16:52'); +INSERT IGNORE INTO `branches` (`id`, `code`, `name_ar`, `name_en`, `city_ar`, `city_en`, `created_at`) VALUES ('3', 'nizwa', 'فرع نزوى', 'Nizwa Branch', 'نزوى', 'Nizwa', '2026-04-19 15:16:52'); + +-- Data for table `categories` +INSERT IGNORE INTO `categories` (`id`, `name_ar`, `name_en`, `description`, `created_at`) VALUES ('1', 'إلكترونيات', 'Electronics', NULL, '2026-04-19 02:39:32'); +INSERT IGNORE INTO `categories` (`id`, `name_ar`, `name_en`, `description`, `created_at`) VALUES ('2', 'إكسسوارات', 'Accessories', NULL, '2026-04-19 02:39:32'); +INSERT IGNORE INTO `categories` (`id`, `name_ar`, `name_en`, `description`, `created_at`) VALUES ('3', 'ملابس', 'Clothing', NULL, '2026-04-19 02:39:32'); + +-- Data for table `expense_categories` +INSERT IGNORE INTO `expense_categories` (`id`, `name_ar`, `name_en`, `created_at`) VALUES ('1', 'رواتب', 'Salary', '2026-04-20 02:33:54'); +INSERT IGNORE INTO `expense_categories` (`id`, `name_ar`, `name_en`, `created_at`) VALUES ('2', 'كهرباء وماء', 'Water & Electricity ', '2026-04-20 02:34:28'); +INSERT IGNORE INTO `expense_categories` (`id`, `name_ar`, `name_en`, `created_at`) VALUES ('3', 'أجور عمال', 'Labour', '2026-04-20 02:34:53'); +INSERT IGNORE INTO `expense_categories` (`id`, `name_ar`, `name_en`, `created_at`) VALUES ('4', 'ضرائب', 'Taxes', '2026-04-20 02:35:13'); + +-- Data for table `settings` +INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('company_address', ''); +INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('company_email', ''); +INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('company_favicon', ''); +INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('company_logo', ''); +INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('company_name_ar', 'حلوى الريامي'); +INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('company_name_en', 'Al Riyami Sweets'); +INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('company_phone', ''); +INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('company_vat_number', ''); +INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('mail_from', ''); +INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('mail_from_name', ''); +INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('smtp_host', ''); +INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('smtp_pass', ''); +INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('smtp_port', '587'); +INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('smtp_secure', ''); +INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('smtp_user', ''); +INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('timezone', 'Asia/Muscat'); +INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('vat_percentage', '5'); + +-- Data for table `units` +INSERT IGNORE INTO `units` (`id`, `name_ar`, `name_en`, `created_at`) VALUES ('1', 'حبة', 'piece ', '2026-04-19 05:03:16'); +INSERT IGNORE INTO `units` (`id`, `name_ar`, `name_en`, `created_at`) VALUES ('2', 'كيلوم جرام', 'Kilogram', '2026-04-19 05:03:44'); +INSERT IGNORE INTO `units` (`id`, `name_ar`, `name_en`, `created_at`) VALUES ('3', 'جرام', 'gram', '2026-04-19 05:04:01'); +INSERT IGNORE INTO `units` (`id`, `name_ar`, `name_en`, `created_at`) VALUES ('4', 'كرتون', 'Carton', '2026-04-19 05:04:21'); +INSERT IGNORE INTO `units` (`id`, `name_ar`, `name_en`, `created_at`) VALUES ('5', 'كيس', 'bag', '2026-04-19 05:04:36'); + +-- Data for table `users` +INSERT IGNORE INTO `users` (`id`, `username`, `password`, `role`, `permissions`, `branch_code`, `allowed_branches`, `name_ar`, `name_en`, `created_at`) VALUES ('1', 'owner', '$2y$10$QpW18WHHU8wSKQrYdYTRJuaNPw1v1puJgBpvfc6m9H8gnsws5C9/q', 'owner', NULL, 'muscat', NULL, 'مالك النظام', 'System Owner', '2026-04-19 09:23:45'); +INSERT IGNORE INTO `users` (`id`, `username`, `password`, `role`, `permissions`, `branch_code`, `allowed_branches`, `name_ar`, `name_en`, `created_at`) VALUES ('2', 'manager_muscat', '$2y$10$mXI290vI7qEIP.At1YFeFOt/pHf096S3h7CWHYfJYluw7QQlM7uDm', 'manager', NULL, 'muscat', NULL, 'مدير فرع مسقط', 'Muscat Branch Manager', '2026-04-19 09:23:45'); +INSERT IGNORE INTO `users` (`id`, `username`, `password`, `role`, `permissions`, `branch_code`, `allowed_branches`, `name_ar`, `name_en`, `created_at`) VALUES ('3', 'cashier_sohar', '$2y$10$.iWxSZWkRuWhuNrmt/LAv./HCWlHQJSpoYqa9pJMoobHCWbMpvXZe', 'cashier', NULL, 'sohar', NULL, 'كاشير فرع صحار', 'Sohar Cashier', '2026-04-19 09:23:46'); + diff --git a/db/setup.php b/db/setup.php new file mode 100644 index 0000000..0a2ee56 --- /dev/null +++ b/db/setup.php @@ -0,0 +1,40 @@ +exec($sql); + $message = "Database schema and default data successfully migrated!"; + if (php_sapi_name() === 'cli') { + echo $message . "\n"; + } else { + echo "

$message

"; + echo "

Go to App

"; + } +} catch (PDOException $e) { + $error = "Migration Failed: " . $e->getMessage(); + if (php_sapi_name() === 'cli') { + echo $error . "\n"; + } else { + echo "

$error

"; + } +} + diff --git a/db_seed.php b/db_seed.php deleted file mode 100644 index 6f99dac..0000000 --- a/db_seed.php +++ /dev/null @@ -1,36 +0,0 @@ -exec(" -CREATE TABLE IF NOT EXISTS items ( - id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, - sku VARCHAR(50) NOT NULL UNIQUE, - name VARCHAR(200) NOT NULL, - price DECIMAL(10,3) NOT NULL, - base_stock INT NOT NULL DEFAULT 0, - vat DECIMAL(5,3) NOT NULL DEFAULT 5.000, - category_id INT UNSIGNED NULL, - supplier_id INT UNSIGNED NULL, - image_url VARCHAR(255) NULL, - created_at DATETIME DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE SET NULL, - FOREIGN KEY (supplier_id) REFERENCES suppliers(id) ON DELETE SET NULL -); -"); - -// 2. Insert Categories -$db->exec("INSERT IGNORE INTO categories (id, name_ar, name_en) VALUES -(1, 'إلكترونيات', 'Electronics'), -(2, 'إكسسوارات', 'Accessories'), -(3, 'ملابس', 'Clothing');"); - -// 3. Insert Suppliers -$db->exec("INSERT IGNORE INTO suppliers (id, name, contact_person, phone) VALUES -(1, 'TechCorp', 'John Doe', '123456789'), -(2, 'ElectroWholesale', 'Jane Smith', '987654321'), -(3, 'StyleCo', 'Mike Johnson', '555666777');"); - -echo "Database schema and base entities created.\n"; - diff --git a/dump_schema.php b/dump_schema.php new file mode 100644 index 0000000..5cf6897 --- /dev/null +++ b/dump_schema.php @@ -0,0 +1,14 @@ +query("SHOW TABLES"); +$tables = $stmt->fetchAll(PDO::FETCH_COLUMN); + +$sql = ""; +foreach ($tables as $table) { + $createStmt = $pdo->query("SHOW CREATE TABLE `$table`")->fetch(PDO::FETCH_ASSOC); + $sql .= "-- Table structure for `$table`\n"; + $sql .= $createStmt['Create Table'] . ";\n\n"; +} +echo $sql; + diff --git a/patch.php b/patch.php deleted file mode 100644 index 72beac7..0000000 --- a/patch.php +++ /dev/null @@ -1,90 +0,0 @@ -prepare("UPDATE online_orders SET customer_name = ?, customer_phone = ?, customer_address = ? WHERE id = ?"); - $stmt->execute([$customer_name, $customer_phone, $customer_address, $id]); - set_flash('success', tr('تم تعديل الطلب بنجاح', 'Order updated successfully')); - } else { - set_flash('danger', tr('الرجاء تعبئة جميع الحقول', 'Please fill all fields')); - } - redirect_to('online_orders.php'); -PHP; - -$content = str_replace("redirect_to('online_orders.php');\n }\n", "redirect_to('online_orders.php');\n }\n" . $phpBackend . "\n }\n", $content); - -$editButton = <<<'HTML' - -HTML; - -$content = str_replace(' - ', "\n \n" . $editButton, $content); - -$editModal = <<<'HTML' - - -HTML; - -$content = str_replace('