SET FOREIGN_KEY_CHECKS=0;

-- 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,
  `avatar` varchar(255) DEFAULT 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_id` int(10) unsigned DEFAULT NULL,
  `customer_name` varchar(120) DEFAULT NULL,
  `payment_method` varchar(30) NOT NULL,
  `payment_status` varchar(20) NOT NULL DEFAULT 'paid',
  `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,
  `paid_amount` decimal(10,3) NOT NULL DEFAULT 0.000,
  `due_amount` decimal(10,3) NOT NULL DEFAULT 0.000,
  `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',
  `order_type` varchar(30) NOT NULL DEFAULT 'standard',
  `delivery_status` varchar(30) NOT NULL DEFAULT 'pending',
  `delivery_date` date DEFAULT NULL,
  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`),
  KEY `idx_order_type` (`order_type`),
  KEY `idx_delivery_date` (`delivery_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,
  `avatar` varchar(255) DEFAULT 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 ('wablas_enabled', '1');
INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('wablas_token', '');
INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('wablas_secret_key', '');
INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('wablas_template_created', '');
INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('wablas_template_pending', '');
INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('wablas_template_accepted', '');
INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('wablas_template_completed', '');
INSERT IGNORE INTO `settings` (`setting_key`, `setting_value`) VALUES ('wablas_template_rejected', '');
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');


SET FOREIGN_KEY_CHECKS=1;

