migrate_accounting.sql 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. USE inventory_db;
  2. CREATE TABLE IF NOT EXISTS invoices (
  3. id INT(11) AUTO_INCREMENT PRIMARY KEY,
  4. client_id INT(11) NOT NULL,
  5. invoice_number VARCHAR(50) NOT NULL UNIQUE,
  6. issue_date DATE NOT NULL,
  7. due_date DATE NOT NULL,
  8. status ENUM('draft', 'sent', 'paid', 'overdue', 'cancelled') DEFAULT 'draft',
  9. subtotal DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  10. tax_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  11. total_amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  12. notes TEXT NULL,
  13. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  14. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  15. FOREIGN KEY (client_id) REFERENCES clients(id) ON DELETE RESTRICT,
  16. INDEX idx_client_status (client_id, status),
  17. INDEX idx_invoice_number (invoice_number),
  18. INDEX idx_due_date (due_date)
  19. );
  20. CREATE TABLE IF NOT EXISTS invoice_items (
  21. id INT(11) AUTO_INCREMENT PRIMARY KEY,
  22. invoice_id INT(11) NOT NULL,
  23. item_id INT(11) NOT NULL,
  24. description VARCHAR(255) NOT NULL,
  25. quantity DECIMAL(10,2) NOT NULL DEFAULT 1.00,
  26. unit_price DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  27. line_total DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  28. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  29. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  30. FOREIGN KEY (invoice_id) REFERENCES invoices(id) ON DELETE CASCADE,
  31. FOREIGN KEY (item_id) REFERENCES items(id) ON DELETE RESTRICT,
  32. INDEX idx_invoice (invoice_id),
  33. INDEX idx_item (item_id)
  34. );
  35. CREATE TABLE IF NOT EXISTS payments (
  36. id INT(11) AUTO_INCREMENT PRIMARY KEY,
  37. invoice_id INT(11) NULL,
  38. client_id INT(11) NOT NULL,
  39. payment_date DATE NOT NULL,
  40. amount DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  41. payment_method ENUM('cash', 'check', 'credit_card', 'bank_transfer', 'other') DEFAULT 'cash',
  42. reference_number VARCHAR(50) NULL,
  43. notes TEXT NULL,
  44. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  45. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  46. FOREIGN KEY (invoice_id) REFERENCES invoices(id) ON DELETE SET NULL,
  47. FOREIGN KEY (client_id) REFERENCES clients(id) ON DELETE RESTRICT,
  48. INDEX idx_invoice (invoice_id),
  49. INDEX idx_client (client_id),
  50. INDEX idx_payment_date (payment_date)
  51. );