CREATE DATABASE IF NOT EXISTS eo_erp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE eo_erp;

CREATE TABLE roles (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 name VARCHAR(60) NOT NULL UNIQUE,
 label VARCHAR(100) NOT NULL
) ENGINE=InnoDB;

CREATE TABLE users (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 name VARCHAR(150) NOT NULL,
 email VARCHAR(190) NOT NULL UNIQUE,
 password VARCHAR(255) NOT NULL,
 role_id BIGINT UNSIGNED NOT NULL,
 is_active TINYINT(1) NOT NULL DEFAULT 1,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 CONSTRAINT fk_users_role FOREIGN KEY(role_id) REFERENCES roles(id)
) ENGINE=InnoDB;

CREATE TABLE clients (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 name VARCHAR(180) NOT NULL,
 company_name VARCHAR(180) NULL,
 npwp VARCHAR(40) NULL,
 email VARCHAR(190) NULL,
 phone VARCHAR(40) NULL,
 address TEXT NULL,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE vendors (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 name VARCHAR(180) NOT NULL,
 company_name VARCHAR(180) NULL,
 npwp VARCHAR(40) NULL,
 email VARCHAR(190) NULL,
 phone VARCHAR(40) NULL,
 address TEXT NULL,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE projects (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 code VARCHAR(40) NOT NULL UNIQUE,
 name VARCHAR(180) NOT NULL,
 client_id BIGINT UNSIGNED NULL,
 start_date DATE NULL,
 end_date DATE NULL,
 budget DECIMAL(16,2) NOT NULL DEFAULT 0,
 status ENUM('planning','active','completed','cancelled') NOT NULL DEFAULT 'planning',
 notes TEXT NULL,
 created_by BIGINT UNSIGNED NOT NULL,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 CONSTRAINT fk_projects_client FOREIGN KEY(client_id) REFERENCES clients(id),
 CONSTRAINT fk_projects_user FOREIGN KEY(created_by) REFERENCES users(id)
) ENGINE=InnoDB;

CREATE TABLE purchase_orders (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 po_number VARCHAR(50) NOT NULL UNIQUE,
 vendor_id BIGINT UNSIGNED NOT NULL,
 project_id BIGINT UNSIGNED NULL,
 order_date DATE NOT NULL,
 due_date DATE NULL,
 subtotal DECIMAL(16,2) NOT NULL DEFAULT 0,
 discount DECIMAL(16,2) NOT NULL DEFAULT 0,
 ppn_rate DECIMAL(7,4) NOT NULL DEFAULT 0,
 ppn_amount DECIMAL(16,2) NOT NULL DEFAULT 0,
 pph_rate DECIMAL(7,4) NOT NULL DEFAULT 0,
 pph_amount DECIMAL(16,2) NOT NULL DEFAULT 0,
 total DECIMAL(16,2) NOT NULL DEFAULT 0,
 notes TEXT NULL,
 status ENUM('draft','pending_approval','approved','rejected','closed') NOT NULL DEFAULT 'draft',
 created_by BIGINT UNSIGNED NOT NULL,
 approved_by BIGINT UNSIGNED NULL,
 approved_at DATETIME NULL,
 rejection_reason TEXT NULL,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 CONSTRAINT fk_po_vendor FOREIGN KEY(vendor_id) REFERENCES vendors(id),
 CONSTRAINT fk_po_project FOREIGN KEY(project_id) REFERENCES projects(id),
 CONSTRAINT fk_po_creator FOREIGN KEY(created_by) REFERENCES users(id),
 CONSTRAINT fk_po_approver FOREIGN KEY(approved_by) REFERENCES users(id)
) ENGINE=InnoDB;

CREATE TABLE purchase_order_items (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 purchase_order_id BIGINT UNSIGNED NOT NULL,
 description VARCHAR(255) NOT NULL,
 qty DECIMAL(12,2) NOT NULL DEFAULT 1,
 unit VARCHAR(30) NOT NULL DEFAULT 'unit',
 unit_price DECIMAL(16,2) NOT NULL DEFAULT 0,
 line_total DECIMAL(16,2) NOT NULL DEFAULT 0,
 CONSTRAINT fk_poi_po FOREIGN KEY(purchase_order_id) REFERENCES purchase_orders(id) ON DELETE CASCADE
) ENGINE=InnoDB;

CREATE TABLE sales_invoices (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 invoice_number VARCHAR(50) NOT NULL UNIQUE,
 client_id BIGINT UNSIGNED NOT NULL,
 project_id BIGINT UNSIGNED NULL,
 invoice_date DATE NOT NULL,
 due_date DATE NULL,
 subtotal DECIMAL(16,2) NOT NULL DEFAULT 0,
 discount DECIMAL(16,2) NOT NULL DEFAULT 0,
 ppn_rate DECIMAL(7,4) NOT NULL DEFAULT 0,
 ppn_amount DECIMAL(16,2) NOT NULL DEFAULT 0,
 pph_rate DECIMAL(7,4) NOT NULL DEFAULT 0,
 pph_amount DECIMAL(16,2) NOT NULL DEFAULT 0,
 total DECIMAL(16,2) NOT NULL DEFAULT 0,
 paid_amount DECIMAL(16,2) NOT NULL DEFAULT 0,
 notes TEXT NULL,
 status ENUM('draft','pending_approval','approved','partially_paid','paid','rejected','overdue') NOT NULL DEFAULT 'draft',
 created_by BIGINT UNSIGNED NOT NULL,
 approved_by BIGINT UNSIGNED NULL,
 approved_at DATETIME NULL,
 rejection_reason TEXT NULL,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 CONSTRAINT fk_si_client FOREIGN KEY(client_id) REFERENCES clients(id),
 CONSTRAINT fk_si_project FOREIGN KEY(project_id) REFERENCES projects(id),
 CONSTRAINT fk_si_creator FOREIGN KEY(created_by) REFERENCES users(id),
 CONSTRAINT fk_si_approver FOREIGN KEY(approved_by) REFERENCES users(id)
) ENGINE=InnoDB;

CREATE TABLE sales_invoice_items (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 sales_invoice_id BIGINT UNSIGNED NOT NULL,
 description VARCHAR(255) NOT NULL,
 qty DECIMAL(12,2) NOT NULL DEFAULT 1,
 unit VARCHAR(30) NOT NULL DEFAULT 'unit',
 unit_price DECIMAL(16,2) NOT NULL DEFAULT 0,
 line_total DECIMAL(16,2) NOT NULL DEFAULT 0,
 CONSTRAINT fk_sii_invoice FOREIGN KEY(sales_invoice_id) REFERENCES sales_invoices(id) ON DELETE CASCADE
) ENGINE=InnoDB;

CREATE TABLE payments (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 invoice_id BIGINT UNSIGNED NOT NULL,
 payment_date DATE NOT NULL,
 amount DECIMAL(16,2) NOT NULL,
 method VARCHAR(50) NULL,
 reference_no VARCHAR(100) NULL,
 notes TEXT NULL,
 recorded_by BIGINT UNSIGNED NOT NULL,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 CONSTRAINT fk_pay_invoice FOREIGN KEY(invoice_id) REFERENCES sales_invoices(id),
 CONSTRAINT fk_pay_user FOREIGN KEY(recorded_by) REFERENCES users(id)
) ENGINE=InnoDB;

CREATE TABLE employees (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 employee_no VARCHAR(40) NOT NULL UNIQUE,
 name VARCHAR(150) NOT NULL,
 email VARCHAR(190) NULL,
 phone VARCHAR(40) NULL,
 position VARCHAR(100) NULL,
 department VARCHAR(100) NULL,
 employment_status ENUM('permanent','contract','freelance','intern') DEFAULT 'permanent',
 join_date DATE NULL,
 base_salary DECIMAL(16,2) NOT NULL DEFAULT 0,
 is_active TINYINT(1) NOT NULL DEFAULT 1,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE payroll_runs (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 period_month TINYINT UNSIGNED NOT NULL,
 period_year SMALLINT UNSIGNED NOT NULL,
 status ENUM('draft','pending_approval','approved','paid') DEFAULT 'draft',
 created_by BIGINT UNSIGNED NOT NULL,
 approved_by BIGINT UNSIGNED NULL,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 UNIQUE KEY uq_payroll_period(period_month,period_year),
 FOREIGN KEY(created_by) REFERENCES users(id),
 FOREIGN KEY(approved_by) REFERENCES users(id)
) ENGINE=InnoDB;

CREATE TABLE payroll_items (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 payroll_run_id BIGINT UNSIGNED NOT NULL,
 employee_id BIGINT UNSIGNED NOT NULL,
 base_salary DECIMAL(16,2) NOT NULL DEFAULT 0,
 allowances DECIMAL(16,2) NOT NULL DEFAULT 0,
 deductions DECIMAL(16,2) NOT NULL DEFAULT 0,
 pph21 DECIMAL(16,2) NOT NULL DEFAULT 0,
 bpjs_employee DECIMAL(16,2) NOT NULL DEFAULT 0,
 net_salary DECIMAL(16,2) NOT NULL DEFAULT 0,
 FOREIGN KEY(payroll_run_id) REFERENCES payroll_runs(id) ON DELETE CASCADE,
 FOREIGN KEY(employee_id) REFERENCES employees(id)
) ENGINE=InnoDB;

CREATE TABLE audit_logs (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 user_id BIGINT UNSIGNED NULL,
 action VARCHAR(100) NOT NULL,
 entity_type VARCHAR(80) NOT NULL,
 entity_id BIGINT UNSIGNED NULL,
 details JSON NULL,
 ip_address VARCHAR(45) NULL,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 FOREIGN KEY(user_id) REFERENCES users(id)
) ENGINE=InnoDB;

INSERT INTO roles(name,label) VALUES
('super_admin','Super Admin'),('director','Direktur'),('finance','Finance'),
('project_manager','Project Manager'),('hr','HR'),('staff','Staff');
