<?php
require_once __DIR__ . '/../includes/db.php';
global $pdo;

$cols = $pdo->query("SHOW COLUMNS FROM payment_terms LIKE 'due_days_offset'")->fetchAll();
if (empty($cols)) {
    $pdo->exec("ALTER TABLE payment_terms ADD COLUMN due_days_offset INT NULL DEFAULT NULL AFTER requires_installment");
    echo "Added due_days_offset column\n";
} else {
    echo "Column already exists\n";
}

$updates = [
    ['Advance Payment',    0],
    ['Full Payment',       0],
    ['Installment Payment', null],
    ['COD',                0],
    ['Net 30',             30],
    ['Net 60',             60],
    ['Open Credit',        null],
];
foreach ($updates as [$name, $offset]) {
    if ($offset === null) {
        $pdo->prepare('UPDATE payment_terms SET due_days_offset = NULL WHERE name = ?')->execute([$name]);
    } else {
        $pdo->prepare('UPDATE payment_terms SET due_days_offset = ? WHERE name = ?')->execute([$offset, $name]);
    }
    echo "Updated: $name => " . ($offset === null ? 'NULL' : $offset) . "\n";
}
echo "Done.\n";
