migrate_bookkeeping.sql 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. USE inventory_db;
  2. CREATE TABLE IF NOT EXISTS chart_of_accounts (
  3. id INT(11) AUTO_INCREMENT PRIMARY KEY,
  4. account_number VARCHAR(20) NOT NULL UNIQUE,
  5. account_name VARCHAR(255) NOT NULL,
  6. account_type ENUM('asset', 'liability', 'equity', 'revenue', 'expense') NOT NULL,
  7. parent_id INT(11) NULL,
  8. description TEXT NULL,
  9. opening_balance DECIMAL(10,2) DEFAULT 0.00,
  10. current_balance DECIMAL(10,2) DEFAULT 0.00,
  11. is_active BOOLEAN DEFAULT TRUE,
  12. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  13. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  14. FOREIGN KEY (parent_id) REFERENCES chart_of_accounts(id) ON DELETE SET NULL,
  15. INDEX idx_account_type (account_type),
  16. INDEX idx_parent (parent_id),
  17. INDEX idx_account_number (account_number)
  18. );
  19. CREATE TABLE IF NOT EXISTS journal_entries (
  20. id INT(11) AUTO_INCREMENT PRIMARY KEY,
  21. entry_number VARCHAR(50) NOT NULL UNIQUE,
  22. entry_date DATE NOT NULL,
  23. description TEXT NULL,
  24. reference_number VARCHAR(50) NULL,
  25. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  26. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  27. INDEX idx_entry_date (entry_date),
  28. INDEX idx_entry_number (entry_number)
  29. );
  30. CREATE TABLE IF NOT EXISTS account_transactions (
  31. id INT(11) AUTO_INCREMENT PRIMARY KEY,
  32. journal_entry_id INT(11) NOT NULL,
  33. account_id INT(11) NOT NULL,
  34. debit_amount DECIMAL(10,2) DEFAULT 0.00,
  35. credit_amount DECIMAL(10,2) DEFAULT 0.00,
  36. description TEXT NULL,
  37. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  38. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  39. FOREIGN KEY (journal_entry_id) REFERENCES journal_entries(id) ON DELETE CASCADE,
  40. FOREIGN KEY (account_id) REFERENCES chart_of_accounts(id) ON DELETE RESTRICT,
  41. INDEX idx_journal_entry (journal_entry_id),
  42. INDEX idx_account (account_id),
  43. INDEX idx_debit_credit (debit_amount, credit_amount)
  44. );