-- Accounting module tables
CREATE TABLE IF NOT EXISTS accounting_journal (
    id INT AUTO_INCREMENT PRIMARY KEY,
    date DATE NOT NULL,
    description TEXT,
    reference VARCHAR(255),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS accounting_entries (
    id INT AUTO_INCREMENT PRIMARY KEY,
    journal_id INT NOT NULL,
    account_name VARCHAR(255) NOT NULL,
    debit DECIMAL(15,2) DEFAULT 0.00,
    credit DECIMAL(15,2) DEFAULT 0.00,
    FOREIGN KEY (journal_id) REFERENCES accounting_journal(id) ON DELETE CASCADE
);

-- Register accounting module in user permissions
INSERT IGNORE INTO user_permissions (user_id, page, can_view, can_add, can_edit, can_delete)
SELECT id, 'accounting', 
    IF(role = 'admin', 1, 0), 
    IF(role = 'admin', 1, 0), 
    IF(role = 'admin', 1, 0), 
    IF(role = 'admin', 1, 0) 
FROM users;