38471-vm/test_view.php
2026-02-25 09:58:14 +00:00

24 lines
1022 B
PHP

<?php
require 'db/config.php';
$db = db();
try {
$db->exec("RENAME TABLE invoices TO _invoices");
$db->exec("CREATE VIEW invoices AS SELECT * FROM _invoices WHERE outlet_id = @session_outlet_id OR @session_outlet_id IS NULL");
$db->exec("SET @session_outlet_id = 1");
// Test INSERT
$db->exec("INSERT INTO invoices (customer_id, invoice_date, status, total_amount, vat_amount, total_with_vat, paid_amount) VALUES (1, '2026-01-01', 'unpaid', 100, 5, 105, 0)");
echo "Insert OK\n";
// Check if outlet_id is populated. It won't be, because the view doesn't auto-fill!
// We still need the trigger for INSERT.
$stmt = $db->query("SELECT * FROM invoices ORDER BY id DESC LIMIT 1");
print_r($stmt->fetch(PDO::FETCH_ASSOC));
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
// rollback
$db->exec("DROP VIEW IF EXISTS invoices");
$db->exec("RENAME TABLE _invoices TO invoices");
$db->exec("DELETE FROM invoices WHERE customer_id=1 AND total_amount=100");