schema.sql 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. CREATE DATABASE IF NOT EXISTS radionica3d;
  2. USE radionica3d;
  3. -- 1. Users table
  4. CREATE TABLE IF NOT EXISTS `users` (
  5. `id` int(11) NOT NULL AUTO_INCREMENT,
  6. `email` varchar(150) NOT NULL,
  7. `password_hash` varchar(255) NOT NULL,
  8. `first_name` varchar(100) DEFAULT NULL,
  9. `last_name` varchar(100) DEFAULT NULL,
  10. `phone` varchar(20) DEFAULT NULL,
  11. `shipping_address` text DEFAULT NULL,
  12. `role` varchar(20) DEFAULT 'user',
  13. `is_active` tinyint(1) DEFAULT 1,
  14. `is_company` tinyint(1) DEFAULT 0,
  15. `company_name` varchar(200) DEFAULT NULL,
  16. `company_pib` varchar(50) DEFAULT NULL,
  17. `company_address` text DEFAULT NULL,
  18. `can_chat` tinyint(1) DEFAULT 1,
  19. `preferred_language` varchar(5) DEFAULT 'en',
  20. `ip_address` varchar(45) DEFAULT NULL,
  21. `created_at` timestamp NULL DEFAULT current_timestamp(),
  22. `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  23. PRIMARY KEY (`id`),
  24. UNIQUE KEY `email` (`email`)
  25. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  26. -- 2. Audit Logs
  27. CREATE TABLE IF NOT EXISTS `audit_logs` (
  28. `id` int(11) NOT NULL AUTO_INCREMENT,
  29. `user_id` int(11) DEFAULT NULL,
  30. `action` varchar(255) NOT NULL,
  31. `target_type` varchar(50) DEFAULT NULL,
  32. `target_id` int(11) DEFAULT NULL,
  33. `details` text DEFAULT NULL,
  34. `ip_address` varchar(45) DEFAULT NULL,
  35. `created_at` timestamp NULL DEFAULT current_timestamp(),
  36. PRIMARY KEY (`id`),
  37. KEY `user_id` (`user_id`),
  38. CONSTRAINT `fk_audit_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL
  39. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  40. -- 3. Materials
  41. CREATE TABLE IF NOT EXISTS `materials` (
  42. `id` int(11) NOT NULL AUTO_INCREMENT,
  43. `name_en` varchar(100) NOT NULL,
  44. `name_ru` varchar(100) DEFAULT NULL,
  45. `name_ua` varchar(100) DEFAULT NULL,
  46. `name_me` varchar(100) DEFAULT NULL,
  47. `desc_en` text DEFAULT NULL,
  48. `desc_ru` text DEFAULT NULL,
  49. `desc_ua` text DEFAULT NULL,
  50. `desc_me` text DEFAULT NULL,
  51. `price_per_cm3` decimal(10,2) DEFAULT 0.00,
  52. `available_colors` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`available_colors`)),
  53. `is_active` tinyint(1) DEFAULT 1,
  54. `created_at` timestamp NULL DEFAULT current_timestamp(),
  55. `long_desc_en` text DEFAULT NULL,
  56. `long_desc_ru` text DEFAULT NULL,
  57. `long_desc_ua` text DEFAULT NULL,
  58. `long_desc_me` text DEFAULT NULL,
  59. PRIMARY KEY (`id`)
  60. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  61. -- 4. Services
  62. CREATE TABLE IF NOT EXISTS `services` (
  63. `id` int(11) NOT NULL AUTO_INCREMENT,
  64. `name_en` varchar(100) NOT NULL,
  65. `name_ru` varchar(100) DEFAULT NULL,
  66. `name_ua` varchar(100) DEFAULT NULL,
  67. `name_me` varchar(100) DEFAULT NULL,
  68. `desc_en` text DEFAULT NULL,
  69. `desc_ru` text DEFAULT NULL,
  70. `desc_ua` text DEFAULT NULL,
  71. `desc_me` text DEFAULT NULL,
  72. `tech_type` varchar(50) DEFAULT NULL,
  73. `is_active` tinyint(1) DEFAULT 1,
  74. `created_at` timestamp NULL DEFAULT current_timestamp(),
  75. PRIMARY KEY (`id`)
  76. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  77. -- 5. Orders
  78. CREATE TABLE IF NOT EXISTS `orders` (
  79. `id` int(11) NOT NULL AUTO_INCREMENT,
  80. `user_id` int(11) DEFAULT NULL,
  81. `material_id` int(11) DEFAULT NULL,
  82. `first_name` varchar(100) NOT NULL,
  83. `last_name` varchar(100) NOT NULL,
  84. `phone` varchar(20) NOT NULL,
  85. `email` varchar(150) NOT NULL,
  86. `shipping_address` text NOT NULL,
  87. `model_link` text DEFAULT NULL,
  88. `status` varchar(50) DEFAULT 'pending',
  89. `is_company` tinyint(1) DEFAULT 0,
  90. `company_name` varchar(200) DEFAULT NULL,
  91. `company_pib` varchar(50) DEFAULT NULL,
  92. `company_address` text DEFAULT NULL,
  93. `total_price` decimal(10,2) DEFAULT NULL,
  94. `estimated_price` decimal(10,2) DEFAULT NULL,
  95. `material_name` varchar(100) DEFAULT NULL,
  96. `material_price` decimal(10,4) DEFAULT NULL,
  97. `color_name` varchar(100) DEFAULT NULL,
  98. `allow_portfolio` tinyint(1) DEFAULT 0,
  99. `quantity` int(11) DEFAULT 1,
  100. `notes` text DEFAULT NULL,
  101. `invoice_path` text DEFAULT NULL,
  102. `fiscal_qr_url` text DEFAULT NULL,
  103. `ikof` varchar(100) DEFAULT NULL,
  104. `jikr` varchar(100) DEFAULT NULL,
  105. `fiscalized_at` timestamp NULL DEFAULT NULL,
  106. `review_text` text DEFAULT NULL,
  107. `rating` int(11) DEFAULT 0,
  108. `review_approved` tinyint(1) DEFAULT 0,
  109. `created_at` timestamp NULL DEFAULT current_timestamp(),
  110. `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  111. `original_params` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`original_params`)),
  112. `proforma_path` varchar(512) DEFAULT NULL,
  113. PRIMARY KEY (`id`),
  114. KEY `user_id` (`user_id`),
  115. KEY `fk_order_material` (`material_id`),
  116. CONSTRAINT `fk_order_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  117. CONSTRAINT `fk_order_material` FOREIGN KEY (`material_id`) REFERENCES `materials` (`id`) ON DELETE SET NULL
  118. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  119. -- 6. Order Files
  120. CREATE TABLE IF NOT EXISTS `order_files` (
  121. `id` int(11) NOT NULL AUTO_INCREMENT,
  122. `order_id` int(11) DEFAULT NULL,
  123. `filename` varchar(255) NOT NULL,
  124. `file_path` varchar(512) NOT NULL,
  125. `file_size` int(11) DEFAULT NULL,
  126. `quantity` int(11) DEFAULT 1,
  127. `unit_price` decimal(10,2) DEFAULT NULL,
  128. `file_hash` varchar(64) DEFAULT NULL,
  129. `print_time` varchar(50) DEFAULT NULL,
  130. `filament_g` float DEFAULT NULL,
  131. `preview_path` varchar(512) DEFAULT NULL,
  132. `created_at` timestamp NULL DEFAULT current_timestamp(),
  133. PRIMARY KEY (`id`),
  134. KEY `order_id` (`order_id`),
  135. KEY `file_hash` (`file_hash`),
  136. CONSTRAINT `fk_file_order` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON DELETE CASCADE
  137. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  138. -- 7. Order Items (for Manual Billing)
  139. CREATE TABLE IF NOT EXISTS `order_items` (
  140. `id` int(11) NOT NULL AUTO_INCREMENT,
  141. `order_id` int(11) NOT NULL,
  142. `description` text NOT NULL,
  143. `quantity` int(11) NOT NULL DEFAULT 1,
  144. `unit_price` decimal(10,2) NOT NULL,
  145. `total_price` decimal(10,2) NOT NULL,
  146. `vat_rate` decimal(5,2) DEFAULT 21.00,
  147. `created_at` timestamp NULL DEFAULT current_timestamp(),
  148. PRIMARY KEY (`id`),
  149. KEY `order_id` (`order_id`),
  150. CONSTRAINT `fk_item_order` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON DELETE CASCADE
  151. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  152. -- 8. Order Messages (Chat)
  153. CREATE TABLE IF NOT EXISTS `order_messages` (
  154. `id` int(11) NOT NULL AUTO_INCREMENT,
  155. `order_id` int(11) NOT NULL,
  156. `user_id` int(11) DEFAULT NULL,
  157. `is_from_admin` tinyint(1) DEFAULT 0,
  158. `message` text NOT NULL,
  159. `created_at` timestamp NULL DEFAULT current_timestamp(),
  160. `is_read` tinyint(1) DEFAULT 0,
  161. PRIMARY KEY (`id`),
  162. KEY `order_id` (`order_id`),
  163. CONSTRAINT `fk_msg_order` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON DELETE CASCADE
  164. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  165. -- 9. Order Photos
  166. CREATE TABLE IF NOT EXISTS `order_photos` (
  167. `id` int(11) NOT NULL AUTO_INCREMENT,
  168. `order_id` int(11) DEFAULT NULL,
  169. `file_path` varchar(512) NOT NULL,
  170. `is_public` tinyint(1) DEFAULT 0,
  171. `created_at` timestamp NULL DEFAULT current_timestamp(),
  172. PRIMARY KEY (`id`),
  173. KEY `order_id` (`order_id`),
  174. CONSTRAINT `fk_photo_order` FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`) ON DELETE CASCADE
  175. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  176. -- 10. Blog Posts
  177. CREATE TABLE IF NOT EXISTS `posts` (
  178. `id` int(11) NOT NULL AUTO_INCREMENT,
  179. `slug` varchar(255) NOT NULL,
  180. `title_en` varchar(255) NOT NULL,
  181. `title_me` varchar(255) DEFAULT NULL,
  182. `title_ru` varchar(255) DEFAULT NULL,
  183. `title_ua` varchar(255) DEFAULT NULL,
  184. `excerpt_en` text DEFAULT NULL,
  185. `excerpt_me` text DEFAULT NULL,
  186. `excerpt_ru` text DEFAULT NULL,
  187. `excerpt_ua` text DEFAULT NULL,
  188. `content_en` longtext DEFAULT NULL,
  189. `content_me` longtext DEFAULT NULL,
  190. `content_ru` longtext DEFAULT NULL,
  191. `content_ua` longtext DEFAULT NULL,
  192. `category` varchar(50) DEFAULT NULL,
  193. `image_url` varchar(512) DEFAULT NULL,
  194. `is_published` tinyint(1) DEFAULT 0,
  195. `created_at` timestamp NULL DEFAULT current_timestamp(),
  196. `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  197. PRIMARY KEY (`id`),
  198. UNIQUE KEY `slug` (`slug`)
  199. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  200. -- 11. Password Reset Tokens
  201. CREATE TABLE IF NOT EXISTS `password_reset_tokens` (
  202. `id` int(11) NOT NULL AUTO_INCREMENT,
  203. `user_id` int(11) NOT NULL,
  204. `token` varchar(255) NOT NULL,
  205. `expires_at` timestamp NOT NULL,
  206. `created_at` timestamp NULL DEFAULT current_timestamp(),
  207. PRIMARY KEY (`id`),
  208. UNIQUE KEY `token` (`token`),
  209. KEY `user_id` (`user_id`),
  210. CONSTRAINT `fk_reset_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
  211. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  212. -- Initial Data Migration
  213. INSERT IGNORE INTO materials (id, name_en, name_ru, name_me, desc_en, desc_ru, desc_me, price_per_cm3) VALUES
  214. (1, 'PLA Plastic', 'PLA Пластик', 'PLA Plastika', 'Biodegradable, ideal for prototypes', 'Биоразлагаемый, идеален для прототипов', 'Biorazgradiva, idealna za prototipove', 0.04),
  215. (2, 'ABS Plastic', 'ABS Пластик', 'ABS Plastika', 'Durable, impact resistant', 'Прочный, ударостойкий', 'Izdržljiva, otporna na udarce', 0.05),
  216. (3, 'PETG Plastic', 'PETG Пластик', 'PETG Plastika', 'High chemical resistance', 'Высокая химическая стойкость', 'Visoka hemijska otpornost', 0.06),
  217. (4, 'Photopolymer Resin', 'Фотополимерная смола', 'Fotopolimerna smola', 'Maximum detail and surface quality', 'Максимальная детализация и качество поверхности', 'Maksimalna detaljnost i kvalitet površine', 0.12);
  218. INSERT IGNORE INTO services (id, name_en, name_ru, name_me, tech_type) VALUES
  219. (1, 'FDM Printing', 'FDM Печать', 'FDM Štampa', 'FDM'),
  220. (2, 'SLA Printing', 'SLA Печать', 'SLA Štampa', 'SLA');
  221. -- Default Admin User (password: admin123)
  222. -- IMPORTANT: Change this in production!
  223. INSERT IGNORE INTO users (id, email, password_hash, first_name, last_name, role) VALUES
  224. (1, 'admin@radionica3d.me', '$2b$12$cXGLw0yUqlPMxnaDIxVuU.IdKeeTwTMHyuu.a/hKh6BAGwqQLNyxy', 'Admin', 'Root', 'admin');
  225. -- 11. Warehouse Stock
  226. CREATE TABLE IF NOT EXISTS `warehouse_stock` (
  227. `id` int(11) NOT NULL AUTO_INCREMENT,
  228. `material_id` int(11) NOT NULL,
  229. `color_name` varchar(100) NOT NULL,
  230. `quantity` decimal(10,2) DEFAULT 0.00,
  231. `notes` text DEFAULT NULL,
  232. `is_active` tinyint(1) DEFAULT 1,
  233. `created_at` timestamp NULL DEFAULT current_timestamp(),
  234. `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  235. PRIMARY KEY (`id`),
  236. KEY `material_id` (`material_id`),
  237. CONSTRAINT `fk_warehouse_material` FOREIGN KEY (`material_id`) REFERENCES `materials` (`id`) ON DELETE CASCADE
  238. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;