-- Module: Tasa (Exchange Rate quick update)

-- 1) Module registration
INSERT IGNORE INTO phppos_modules (name_lang_key, desc_lang_key, sort, icon, module_id)
VALUES ('module_tasa','module_tasa_desc',73,'icon ti-exchange','tasa');

-- 2) Module actions
INSERT IGNORE INTO phppos_modules_actions (action_id, module_id, action_name_key, sort) VALUES
('view','tasa','tasa_view',10),
('update','tasa','tasa_update',20);

-- 3) Default permission for admin (person_id=1)
INSERT IGNORE INTO phppos_permissions (module_id, person_id) VALUES ('tasa',1);
INSERT IGNORE INTO phppos_permissions_actions (module_id, person_id, action_id) VALUES
('tasa',1,'view'),
('tasa',1,'update');

-- 4) History table
CREATE TABLE IF NOT EXISTS phppos_currency_exchange_rate_history (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  currency_exchange_rate_id INT NOT NULL,
  old_exchange_rate DECIMAL(23,10) NULL,
  new_exchange_rate DECIMAL(23,10) NOT NULL,
  changed_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  changed_by_employee_id INT(10) NOT NULL,
  INDEX idx_cerh_currency_date (currency_exchange_rate_id, changed_at),
  INDEX idx_cerh_employee_date (changed_by_employee_id, changed_at),
  CONSTRAINT fk_cerh_employee FOREIGN KEY (changed_by_employee_id)
    REFERENCES phppos_employees(person_id)
    ON UPDATE CASCADE
    ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
