migrate_complete.sql 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. -- Complete Database Migration Script
  2. -- This script combines all migration files for a complete database setup
  3. USE inventory_db;
  4. -- Items table with serial numbers and pictures
  5. CREATE TABLE IF NOT EXISTS items (
  6. id INT(11) AUTO_INCREMENT PRIMARY KEY,
  7. name VARCHAR(255) NOT NULL,
  8. description TEXT NULL,
  9. quantity INT(11) NOT NULL DEFAULT 0,
  10. price DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  11. serial_number VARCHAR(100) NULL,
  12. picture VARCHAR(255) NULL,
  13. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  14. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  15. INDEX idx_name (name)
  16. );
  17. -- Clients table with y-tunnus and hourly rates
  18. CREATE TABLE IF NOT EXISTS clients (
  19. id INT(11) AUTO_INCREMENT PRIMARY KEY,
  20. y_tunnus VARCHAR(255) NULL,
  21. company_name VARCHAR(255) NULL,
  22. first_name VARCHAR(100) NOT NULL,
  23. last_name VARCHAR(100) NOT NULL,
  24. email VARCHAR(255) NOT NULL UNIQUE,
  25. phone VARCHAR(20) NULL,
  26. address VARCHAR(255) NULL,
  27. city VARCHAR(100) NULL,
  28. state VARCHAR(100) NULL,
  29. postal_code VARCHAR(20) NULL,
  30. country VARCHAR(100) NULL,
  31. notes TEXT NULL,
  32. hour_price DECIMAL(10,2) DEFAULT 0.00,
  33. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  34. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  35. INDEX idx_email (email),
  36. INDEX idx_name (last_name, first_name)
  37. );
  38. -- Contact persons table for individual contacts
  39. CREATE TABLE IF NOT EXISTS contact_persons (
  40. id INT(11) AUTO_INCREMENT PRIMARY KEY,
  41. client_id INT(11) NOT NULL,
  42. first_name VARCHAR(100) NOT NULL,
  43. last_name VARCHAR(100) NOT NULL,
  44. email VARCHAR(255) NULL,
  45. phone VARCHAR(20) NULL,
  46. position VARCHAR(100) NULL,
  47. is_primary BOOLEAN DEFAULT FALSE,
  48. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  49. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  50. FOREIGN KEY (client_id) REFERENCES clients(id) ON DELETE CASCADE,
  51. INDEX idx_client_primary (client_id, is_primary),
  52. INDEX idx_name (last_name, first_name)
  53. );
  54. -- Rental prices table for item pricing
  55. CREATE TABLE IF NOT EXISTS rental_prices (
  56. id INT(11) AUTO_INCREMENT PRIMARY KEY,
  57. item_id INT(11) NOT NULL,
  58. client_id INT(11) NULL,
  59. start_date DATE NOT NULL,
  60. end_date DATE NOT NULL,
  61. daily_price DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  62. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  63. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  64. FOREIGN KEY (item_id) REFERENCES items(id) ON DELETE CASCADE,
  65. FOREIGN KEY (client_id) REFERENCES clients(id) ON DELETE SET NULL,
  66. INDEX idx_item_dates (item_id, start_date, end_date)
  67. );
  68. -- Attachments table for document management
  69. CREATE TABLE IF NOT EXISTS attachments (
  70. id INT(11) AUTO_INCREMENT PRIMARY KEY,
  71. item_id INT(11) NOT NULL,
  72. filename VARCHAR(255) NOT NULL,
  73. original_name VARCHAR(255) NOT NULL,
  74. file_type ENUM('receipt', 'warranty', 'other') NOT NULL DEFAULT 'other',
  75. file_path VARCHAR(255) NOT NULL,
  76. file_size INT(11) NOT NULL,
  77. mime_type VARCHAR(100) NOT NULL,
  78. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  79. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  80. FOREIGN KEY (item_id) REFERENCES items(id) ON DELETE CASCADE,
  81. INDEX idx_item_type (item_id, file_type)
  82. );
  83. -- Users table for authentication
  84. CREATE TABLE IF NOT EXISTS users (
  85. id INT(11) AUTO_INCREMENT PRIMARY KEY,
  86. username VARCHAR(50) NOT NULL UNIQUE,
  87. email VARCHAR(255) NOT NULL UNIQUE,
  88. password_hash VARCHAR(255) NOT NULL,
  89. first_name VARCHAR(100) NOT NULL,
  90. last_name VARCHAR(100) NOT NULL,
  91. role ENUM('admin', 'manager', 'user') DEFAULT 'user',
  92. is_active BOOLEAN DEFAULT TRUE,
  93. last_login TIMESTAMP NULL,
  94. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  95. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  96. INDEX idx_username (username),
  97. INDEX idx_email (email),
  98. INDEX idx_role (role),
  99. INDEX idx_active (is_active)
  100. );
  101. -- Projects table for customer project management
  102. CREATE TABLE IF NOT EXISTS projects (
  103. id INT(11) AUTO_INCREMENT PRIMARY KEY,
  104. customer_id INT(11) NOT NULL,
  105. project_name VARCHAR(255) NOT NULL,
  106. description TEXT NULL,
  107. status ENUM('planning', 'in_progress', 'completed', 'on_hold', 'cancelled') DEFAULT 'planning',
  108. start_date DATE NULL,
  109. end_date DATE NULL,
  110. budget DECIMAL(10,2) NULL,
  111. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  112. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  113. FOREIGN KEY (customer_id) REFERENCES clients(id) ON DELETE CASCADE,
  114. INDEX idx_customer (customer_id),
  115. INDEX idx_status (status),
  116. INDEX idx_dates (start_date, end_date)
  117. );
  118. -- Subprojects table for detailed project tracking
  119. CREATE TABLE IF NOT EXISTS subprojects (
  120. id INT(11) AUTO_INCREMENT PRIMARY KEY,
  121. project_id INT(11) NOT NULL,
  122. subproject_name VARCHAR(255) NOT NULL,
  123. description TEXT NULL,
  124. status ENUM('planning', 'in_progress', 'completed', 'on_hold', 'cancelled') DEFAULT 'planning',
  125. start_date DATE NULL,
  126. end_date DATE NULL,
  127. budget DECIMAL(10,2) NULL,
  128. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  129. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  130. FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE,
  131. INDEX idx_project (project_id),
  132. INDEX idx_status (status),
  133. INDEX idx_dates (start_date, end_date)
  134. );
  135. -- Invoices table for billing management
  136. CREATE TABLE IF NOT EXISTS invoices (
  137. id INT(11) AUTO_INCREMENT PRIMARY KEY,
  138. client_id INT(11) NOT NULL,
  139. invoice_number VARCHAR(50) NOT NULL UNIQUE,
  140. issue_date DATE NOT NULL,
  141. due_date DATE NOT NULL,
  142. status ENUM('draft', 'sent', 'paid', 'overdue', 'cancelled') DEFAULT 'draft',
  143. subtotal DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  144. tax_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  145. total_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  146. notes TEXT NULL,
  147. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  148. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  149. FOREIGN KEY (client_id) REFERENCES clients(id) ON DELETE RESTRICT,
  150. INDEX idx_client_status (client_id, status),
  151. INDEX idx_invoice_number (invoice_number),
  152. INDEX idx_due_date (due_date)
  153. );
  154. -- Invoice items table for line item management
  155. CREATE TABLE IF NOT EXISTS invoice_items (
  156. id INT(11) AUTO_INCREMENT PRIMARY KEY,
  157. invoice_id INT(11) NOT NULL,
  158. item_id INT(11) NOT NULL,
  159. description VARCHAR(255) NOT NULL,
  160. quantity DECIMAL(10,2) NOT NULL DEFAULT 1.00,
  161. unit_price DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  162. line_total DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  163. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  164. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  165. FOREIGN KEY (invoice_id) REFERENCES invoices(id) ON DELETE CASCADE,
  166. FOREIGN KEY (item_id) REFERENCES items(id) ON DELETE RESTRICT,
  167. INDEX idx_invoice (invoice_id),
  168. INDEX idx_item (item_id)
  169. );
  170. -- Payments table for transaction tracking
  171. CREATE TABLE IF NOT EXISTS payments (
  172. id INT(11) AUTO_INCREMENT PRIMARY KEY,
  173. invoice_id INT(11) NULL,
  174. client_id INT(11) NOT NULL,
  175. payment_date DATE NOT NULL,
  176. amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  177. payment_method ENUM('cash', 'check', 'credit_card', 'bank_transfer', 'other') DEFAULT 'cash',
  178. reference_number VARCHAR(50) NULL,
  179. notes TEXT NULL,
  180. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  181. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  182. FOREIGN KEY (invoice_id) REFERENCES invoices(id) ON DELETE SET NULL,
  183. FOREIGN KEY (client_id) REFERENCES clients(id) ON DELETE RESTRICT,
  184. INDEX idx_invoice (invoice_id),
  185. INDEX idx_client (client_id),
  186. INDEX idx_payment_date (payment_date)
  187. );
  188. -- Chart of accounts for bookkeeping
  189. CREATE TABLE IF NOT EXISTS chart_of_accounts (
  190. id INT(11) AUTO_INCREMENT PRIMARY KEY,
  191. account_number VARCHAR(20) NOT NULL UNIQUE,
  192. account_name VARCHAR(255) NOT NULL,
  193. account_type ENUM('asset', 'liability', 'equity', 'revenue', 'expense') NOT NULL,
  194. parent_id INT(11) NULL,
  195. description TEXT NULL,
  196. opening_balance DECIMAL(10,2) DEFAULT 0.00,
  197. current_balance DECIMAL(10,2) DEFAULT 0.00,
  198. is_active BOOLEAN DEFAULT TRUE,
  199. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  200. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  201. FOREIGN KEY (parent_id) REFERENCES chart_of_accounts(id) ON DELETE SET NULL,
  202. INDEX idx_account_type (account_type),
  203. INDEX idx_parent (parent_id),
  204. INDEX idx_account_number (account_number)
  205. );
  206. -- Journal entries table for double-entry bookkeeping
  207. CREATE TABLE IF NOT EXISTS journal_entries (
  208. id INT(11) AUTO_INCREMENT PRIMARY KEY,
  209. entry_number VARCHAR(50) NOT NULL UNIQUE,
  210. entry_date DATE NOT NULL,
  211. description TEXT NULL,
  212. reference_number VARCHAR(50) NULL,
  213. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  214. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  215. INDEX idx_entry_date (entry_date),
  216. INDEX idx_entry_number (entry_number)
  217. );
  218. -- Account transactions table for detailed bookkeeping
  219. CREATE TABLE IF NOT EXISTS account_transactions (
  220. id INT(11) AUTO_INCREMENT PRIMARY KEY,
  221. journal_entry_id INT(11) NOT NULL,
  222. account_id INT(11) NOT NULL,
  223. debit_amount DECIMAL(10,2) DEFAULT 0.00,
  224. credit_amount DECIMAL(10,2) DEFAULT 0.00,
  225. description TEXT NULL,
  226. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  227. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  228. FOREIGN KEY (journal_entry_id) REFERENCES journal_entries(id) ON DELETE CASCADE,
  229. FOREIGN KEY (account_id) REFERENCES chart_of_accounts(id) ON DELETE RESTRICT,
  230. INDEX idx_journal_entry (journal_entry_id),
  231. INDEX idx_account (account_id),
  232. INDEX idx_debit_credit (debit_amount, credit_amount)
  233. );
  234. -- Insert sample data
  235. INSERT INTO items (name, description, quantity, price) VALUES
  236. ('Laptop', 'Dell XPS 15 laptop', 5, 1299.99),
  237. ('Mouse', 'Wireless optical mouse', 20, 25.50),
  238. ('Keyboard', 'Mechanical keyboard', 15, 89.99);