Files
whatsapp/lab_terminos_export.php
2026-04-16 23:45:58 -05:00

79 lines
2.1 KiB
PHP

<?php
/**
* Exportar aceptaciones de T&C a CSV
*/
require_once 'config/config.php';
if (!defined('APP_ROOT')) { define('APP_ROOT', __DIR__); }
if (!isUserLoggedIn()) {
header('Location: login.php');
exit;
}
$filterEstado = $_GET['estado'] ?? '';
$filterFecha = trim($_GET['fecha'] ?? '');
$filterPhone = trim($_GET['phone'] ?? '');
$where = [];
$params = [];
if ($filterEstado !== '') {
$where[] = 'ta.estado = :estado';
$params[':estado'] = $filterEstado;
}
if ($filterFecha !== '') {
$where[] = 'DATE(ta.fecha_envio) = :fecha';
$params[':fecha'] = $filterFecha;
}
if ($filterPhone !== '') {
$where[] = 'ta.phone_number LIKE :phone';
$params[':phone'] = '%' . $filterPhone . '%';
}
$whereClause = $where ? 'WHERE ' . implode(' AND ', $where) : '';
try {
$db = Database::getInstance();
$rows = $db->fetchAll(
"SELECT ta.id, ta.phone_number, u.name AS user_name, ta.estado,
tv.version AS terms_version,
ta.fecha_envio, ta.fecha_respuesta
FROM terms_acceptance ta
LEFT JOIN terms_versions tv ON tv.id = ta.terms_version_id
LEFT JOIN users u ON u.id = ta.user_id
$whereClause
ORDER BY ta.id DESC",
$params
);
} catch (Exception $e) {
http_response_code(500);
echo 'Error: ' . htmlspecialchars($e->getMessage());
exit;
}
$filename = 'terminos_aceptaciones_' . date('Ymd_His') . '.csv';
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Pragma: no-cache');
header('Expires: 0');
$out = fopen('php://output', 'w');
// BOM para Excel
fwrite($out, "\xEF\xBB\xBF");
fputcsv($out, ['ID', 'Teléfono', 'Nombre', 'Estado', 'Versión Términos', 'Fecha Envío', 'Fecha Respuesta']);
foreach ($rows as $r) {
fputcsv($out, [
$r['id'],
$r['phone_number'],
$r['user_name'] ?? '',
$r['estado'],
$r['terms_version'] ?? '',
$r['fecha_envio'] ?? '',
$r['fecha_respuesta'] ?? '',
]);
}
fclose($out);