<?php
require_once 'c:/xampp/htdocs/koperasi/config/database.php';
$db = getDB();

try {
    echo "Creating/Updating konsumsi_transaksi table...\n";
    $db->exec("CREATE TABLE IF NOT EXISTS konsumsi_transaksi (
        id INT AUTO_INCREMENT PRIMARY KEY,
        anggota_id INT NOT NULL,
        tanggal DATE NOT NULL,
        nama_barang VARCHAR(255) DEFAULT NULL,
        harga_beli DECIMAL(15, 2) DEFAULT 0,
        ppn_persen DECIMAL(5, 2) DEFAULT 0,
        harga_jual DECIMAL(15, 2) DEFAULT 0,
        jumlah_belanja DECIMAL(15, 2) NOT NULL,
        keterangan TEXT,
        created_by INT NOT NULL,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        FOREIGN KEY (anggota_id) REFERENCES anggota(id) ON DELETE CASCADE
    )");
    
    // Add setting for consumption limit if not exists
    $checkSetting = $db->prepare("SELECT * FROM pengaturan WHERE kunci = 'batas_konsumsi_bulanan'");
    $checkSetting->execute();
    if (!$checkSetting->fetch()) {
        $db->exec("INSERT INTO pengaturan (kunci, nilai, keterangan) VALUES ('batas_konsumsi_bulanan', '50000', 'Batas maksimal belanja koperasi konsumsi per bulan')");
    }

    echo "Migration completed successfully!\n";
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
?>

