// cron - 03:15 * * * * php /ruta/a/sif_audit.php require 'db.php'; // tu conexión mysqli/PDO require 'sif_helpers.php'; // misma función sif_calc_hash() $issuerNif = obtenerNifEmisor($db); // la misma que usas al grabar $ceros64 = str_repeat('0', 64); $sql = "SELECT * FROM sales ORDER BY document_type, document_set, document_number"; $result = $db->query($sql); $prev = []; $serieKey = ''; while ($row = $result->fetch_assoc()) { // detecta cambio de serie $key = $row['document_type'] . '/' . $row['document_set']; if ($key !== $serieKey) { $prevHash = $ceros64; // nueva serie → semilla $serieKey = $key; } // 1. validar prev_hash if ($row['prev_hash'] !== $prevHash) { registrarAlerta($db, $issuerNif, $serieKey, 'PrevHashMismatch', "Esperado $prevHash, encontrado {$row['prev_hash']}", $row['id']); } // 2. recalcular this_hash $hashReal = sif_calc_hash($row, $issuerNif); if ($hashReal !== $row['this_hash']) { registrarAlerta($db, $issuerNif, $serieKey, 'ThisHashMismatch', "Esperado $hashReal, encontrado {$row['this_hash']}", $row['id']); } // preparar para la próxima fila $prevHash = $row['this_hash']; }