-- ============================================
-- FASE 2 — Tabel Transaksi
-- Jalankan di phpMyAdmin setelah database.sql
-- ============================================

USE cftqptag_toko_jusron;

-- Tabel Transaksi (header)
CREATE TABLE transaksi (
    id INT AUTO_INCREMENT PRIMARY KEY,
    nomor VARCHAR(30) NOT NULL UNIQUE,
    user_id INT NOT NULL,
    tanggal DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    subtotal DECIMAL(15,2) NOT NULL DEFAULT 0,
    diskon DECIMAL(15,2) NOT NULL DEFAULT 0,
    total DECIMAL(15,2) NOT NULL DEFAULT 0,
    bayar DECIMAL(15,2) NOT NULL DEFAULT 0,
    kembalian DECIMAL(15,2) NOT NULL DEFAULT 0,
    metode_bayar ENUM('tunai','transfer','qris') NOT NULL DEFAULT 'tunai',
    catatan VARCHAR(255),
    status ENUM('selesai','batal') NOT NULL DEFAULT 'selesai',
    FOREIGN KEY (user_id) REFERENCES users(id)
);

-- Tabel Detail Transaksi (item per baris)
CREATE TABLE transaksi_detail (
    id INT AUTO_INCREMENT PRIMARY KEY,
    transaksi_id INT NOT NULL,
    barang_id INT NOT NULL,
    nama_barang VARCHAR(150) NOT NULL,
    harga_beli DECIMAL(15,2) NOT NULL DEFAULT 0,
    harga_jual DECIMAL(15,2) NOT NULL DEFAULT 0,
    qty INT NOT NULL DEFAULT 1,
    diskon_item DECIMAL(15,2) NOT NULL DEFAULT 0,
    subtotal DECIMAL(15,2) NOT NULL DEFAULT 0,
    FOREIGN KEY (transaksi_id) REFERENCES transaksi(id) ON DELETE CASCADE,
    FOREIGN KEY (barang_id) REFERENCES barang(id)
);
