<?php
require_once __DIR__ . "/../../config/db.php";

$title = "Anomalies de stock";

/*
 RECONSTITUTION STOCK THEORIQUE
 - Entrées  : appro, retour, annulation_vente, production_entree
 - Sorties  : vente, achat, annulation_achat, production_sortie
*/

$sql = "
SELECT
    p.reference,
    p.nom,
    sd.depot_id,
    sd.quantite AS stock_erp,
    COALESCE(v.stock_calcule,0) AS stock_calcule,
    (COALESCE(v.stock_calcule,0) - sd.quantite) AS ecart
FROM stock_depot sd
JOIN produits p ON p.id = sd.produit_id
LEFT JOIN vue_stock_calcule v
    ON v.produit_id = sd.produit_id
   AND (
        (sd.depot_id IS NULL AND v.depot_id IS NULL)
        OR (sd.depot_id = v.depot_id)
   )
WHERE
    sd.quantite <> COALESCE(v.stock_calcule,0)
    OR sd.quantite < 0
ORDER BY p.nom;
";

$anomalies = $conn->query($sql)->fetchAll(PDO::FETCH_ASSOC);
?>

<div class="container-fluid">
    <h1 class="mt-4">Anomalies de stock</h1>

    <div class="alert alert-warning">
        Cette vue détecte les incohérences entre le stock ERP et les mouvements réels.
        À utiliser avant clôture mensuelle.
    </div>

    <div class="card">
        <div class="card-header">
            <i class="fa fa-triangle-exclamation"></i> Anomalies détectées
        </div>

        <div class="card-body table-responsive">
            <table class="table table-bordered table-striped datatable">
                <thead class="table-dark">
                <tr>
                    <th>Réf</th>
                    <th>Produit</th>
                    <th>Dépôt</th>
                    <th>Stock ERP</th>
                    <th>Stock recalculé</th>
                    <th>Écart</th>
                    <th>Type anomalie</th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($anomalies as $a):
                    $ecart = $a['stock_calcule'] - $a['stock_erp'];

                    if ($a['stock_erp'] < 0) {
                        $type = "Stock négatif";
                        $badge = "danger";
                    } elseif ($ecart != 0) {
                        $type = "Écart stock";
                        $badge = "warning";
                    } else {
                        $type = "Inconnu";
                        $badge = "secondary";
                    }
                ?>
                <tr>
                    <td><?= htmlspecialchars($a['reference']) ?></td>
                    <td><?= htmlspecialchars($a['produit_nom']) ?></td>
                    <td class="text-center">
                        <?= $a['depot_id'] ?? '<span class="badge bg-info">Global</span>' ?>
                    </td>
                    <td class="text-end"><?= (int)$a['stock_erp'] ?></td>
                    <td class="text-end"><?= (int)$a['stock_calcule'] ?></td>
                    <td class="text-end fw-bold <?= $ecart!=0?'text-danger':'' ?>">
                        <?= (int)$ecart ?>
                    </td>
                    <td class="text-center">
                        <span class="badge bg-<?= $badge ?>">
                            <?= $type ?>
                        </span>
                    </td>
                </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include __DIR__ . "/../../includes/layout_end.php"; ?>
