<?php
require 'includes/db.php';

try {
    // Add columns if they do not exist
    $cols = [
        'invoice_number' => 'VARCHAR(50) NOT NULL UNIQUE',
        'buyer_details' => 'TEXT DEFAULT NULL',
        'po_reference' => 'VARCHAR(100) DEFAULT NULL',
        'product_details' => 'TEXT DEFAULT NULL',
        'taxes_vat' => 'DECIMAL(15,2) DEFAULT NULL',
        'discounts' => 'DECIMAL(15,2) DEFAULT NULL',
        'shipping_charges' => 'DECIMAL(15,2) DEFAULT NULL',
        'payment_terms' => 'VARCHAR(100) DEFAULT NULL'
    ];

    foreach ($cols as $col => $definition) {
        // Check if column exists
        $check = $pdo->query("SHOW COLUMNS FROM production_invoices LIKE '$col'")->fetch();
        if (!$check) {
            $pdo->exec("ALTER TABLE production_invoices ADD COLUMN $col $definition");
            echo "Added column: $col\n";
        } else {
            echo "Column already exists: $col\n";
        }
    }
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
