<?php
if(($_GET['key']??'')!=='mgz2026'){die('Access Denied');}
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once __DIR__.'/app/auth.php';
require_once __DIR__.'/app/db.php';
require_once __DIR__.'/app/helpers.php';
require_once __DIR__.'/app/csrf.php';
require_once __DIR__.'/app/docno.php';
require_once __DIR__.'/app/upload.php';
$me=require_role(['ADMIN','WAREHOUSE']);
$pdo=db();
echo "role=".$me['role']."<br>";
echo "Testing wh_receipts query:<br>";
$rows=$pdo->query("SELECT r.*,u1.name AS creator_name,u2.name AS manager_name
    FROM warehouse_receipts r
    LEFT JOIN users u1 ON u1.id=r.created_by
    LEFT JOIN users u2 ON u2.id=r.manager_by
    ORDER BY r.id DESC")->fetchAll(PDO::FETCH_ASSOC);
echo "WHR count: ".count($rows)."<br>";
echo "Testing dispatch query:<br>";
$rows2=$pdo->query("SELECT d.*,dist.name AS dist_name,u.name AS creator_name
    FROM distributor_dispatches d
    LEFT JOIN distributors dist ON dist.id=d.distributor_id
    LEFT JOIN users u ON u.id=d.created_by
    ORDER BY d.id DESC")->fetchAll(PDO::FETCH_ASSOC);
echo "DIS count: ".count($rows2)."<br>";
echo "Testing outlet_deliveries:<br>";
$cols=$pdo->query("DESCRIBE outlet_deliveries")->fetchAll(PDO::FETCH_ASSOC);
foreach($cols as $c)echo "&nbsp;&nbsp;".$c['Field']." (".$c['Type'].")<br>";
echo "<br>Testing outlet_delivery_items:<br>";
$cols2=$pdo->query("DESCRIBE outlet_delivery_items")->fetchAll(PDO::FETCH_ASSOC);
foreach($cols2 as $c)echo "&nbsp;&nbsp;".$c['Field']." (".$c['Type'].")<br>";
echo "<br>Testing outlet_settlements:<br>";
$cols3=$pdo->query("DESCRIBE outlet_settlements")->fetchAll(PDO::FETCH_ASSOC);
foreach($cols3 as $c)echo "&nbsp;&nbsp;".$c['Field']." (".$c['Type'].")<br>";
echo "<br>Testing invoices:<br>";
$cols4=$pdo->query("DESCRIBE invoices")->fetchAll(PDO::FETCH_ASSOC);
foreach($cols4 as $c)echo "&nbsp;&nbsp;".$c['Field']." (".$c['Type'].")<br>";
echo "<br>Testing payments:<br>";
$cols5=$pdo->query("DESCRIBE payments")->fetchAll(PDO::FETCH_ASSOC);
foreach($cols5 as $c)echo "&nbsp;&nbsp;".$c['Field']." (".$c['Type'].")<br>";
echo "<br>Testing warehouse_returns:<br>";
$cols6=$pdo->query("DESCRIBE warehouse_returns")->fetchAll(PDO::FETCH_ASSOC);
foreach($cols6 as $c)echo "&nbsp;&nbsp;".$c['Field']." (".$c['Type'].")<br>";
echo "<br><strong style='color:green'>ALL DONE</strong>";
?>
