126 lines
5.7 KiB
PHP
126 lines
5.7 KiB
PHP
<?php
|
|
$pageTitle = 'Importar Drive (Test)';
|
|
$pageDescription = 'Vista previa de los últimos 10 pedidos detectados en Google Sheets antes de importarlos al CRM.';
|
|
require_once __DIR__ . '/layout_header.php';
|
|
|
|
if (($_SESSION['user_role'] ?? '') !== 'Administrador' && ($_SESSION['user_role'] ?? '') !== 'admin') {
|
|
echo "<div class='alert alert-danger'>Acceso denegado.</div>";
|
|
require_once __DIR__ . '/layout_footer.php';
|
|
exit();
|
|
}
|
|
|
|
require_once __DIR__ . '/vendor/autoload.php';
|
|
|
|
$credentialsPath = __DIR__ . '/google_credentials.json';
|
|
$spreadsheetId = '1SSmQuR9quxeQbMKNMDkRe8-n1gU7WuEfsFaJ3WKFO-c';
|
|
$range = 'A:Z';
|
|
$headers = [];
|
|
$previewRows = [];
|
|
$errorMessage = null;
|
|
$totalDataRows = 0;
|
|
|
|
try {
|
|
if (!file_exists($credentialsPath)) {
|
|
throw new RuntimeException('No se encontró el archivo de credenciales de Google.');
|
|
}
|
|
|
|
$client = new Google\Client();
|
|
$client->setAuthConfig($credentialsPath);
|
|
$client->addScope(Google\Service\Sheets::SPREADSHEETS_READONLY);
|
|
|
|
$service = new Google\Service\Sheets($client);
|
|
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
|
|
$values = $response->getValues();
|
|
|
|
if (empty($values)) {
|
|
throw new RuntimeException('La hoja está vacía o no devolvió datos.');
|
|
}
|
|
|
|
$headers = $values[0] ?? [];
|
|
$dataRows = array_slice($values, 1);
|
|
$totalDataRows = count($dataRows);
|
|
$previewRows = array_reverse(array_slice($dataRows, -10));
|
|
} catch (Throwable $exception) {
|
|
$errorMessage = $exception->getMessage();
|
|
}
|
|
?>
|
|
|
|
<div class="container py-4">
|
|
<div class="row g-4 align-items-stretch">
|
|
<div class="col-12 col-xl-8">
|
|
<div class="card shadow-sm border-0 h-100">
|
|
<div class="card-body p-4">
|
|
<div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center gap-3 mb-3">
|
|
<div>
|
|
<h2 class="h4 mb-1">Vista previa de pedidos desde Drive</h2>
|
|
<p class="text-muted mb-0">Aquí verás solo los últimos 10 registros encontrados en la hoja, sin guardar nada todavía en la base de datos.</p>
|
|
</div>
|
|
<span class="badge bg-primary-subtle text-primary-emphasis px-3 py-2">
|
|
<?php echo (int) count($previewRows); ?> visibles
|
|
</span>
|
|
</div>
|
|
|
|
<?php if ($errorMessage !== null): ?>
|
|
<div class="alert alert-danger mb-0" role="alert">
|
|
<strong>No se pudo cargar la vista previa.</strong><br>
|
|
<?php echo htmlspecialchars($errorMessage); ?>
|
|
</div>
|
|
<?php elseif (empty($previewRows)): ?>
|
|
<div class="alert alert-warning mb-0" role="alert">
|
|
La hoja respondió correctamente, pero todavía no hay pedidos suficientes para mostrar una vista previa.
|
|
</div>
|
|
<?php else: ?>
|
|
<div class="table-responsive">
|
|
<table class="table table-hover align-middle">
|
|
<thead class="table-dark">
|
|
<tr>
|
|
<?php foreach ($headers as $header): ?>
|
|
<th scope="col"><?php echo htmlspecialchars((string) $header); ?></th>
|
|
<?php endforeach; ?>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($previewRows as $row): ?>
|
|
<tr>
|
|
<?php for ($i = 0; $i < count($headers); $i++): ?>
|
|
<td><?php echo htmlspecialchars((string) ($row[$i] ?? '')); ?></td>
|
|
<?php endfor; ?>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-12 col-xl-4">
|
|
<div class="card shadow-sm border-0 mb-4">
|
|
<div class="card-body p-4">
|
|
<h2 class="h5">Cómo funciona esta prueba</h2>
|
|
<ol class="mb-0 ps-3">
|
|
<li class="mb-2">Lee la hoja de Google Sheets en tiempo real.</li>
|
|
<li class="mb-2">Toma solo los últimos 10 pedidos detectados.</li>
|
|
<li class="mb-0">Muestra la información en pantalla sin importar nada al CRM.</li>
|
|
</ol>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="card shadow-sm border-0">
|
|
<div class="card-body p-4">
|
|
<h2 class="h5">Resumen rápido</h2>
|
|
<ul class="list-unstyled mb-0">
|
|
<li class="mb-2"><strong>Modo:</strong> Vista previa segura</li>
|
|
<li class="mb-2"><strong>Pedidos totales detectados:</strong> <?php echo (int) $totalDataRows; ?></li>
|
|
<li class="mb-2"><strong>Pedidos visibles:</strong> <?php echo (int) count($previewRows); ?></li>
|
|
<li class="mb-0"><strong>Base de datos:</strong> Sin cambios</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once __DIR__ . '/layout_footer.php'; ?>
|