40097-vm/includes/drive_test_orders.php
2026-05-26 15:51:40 +00:00

177 lines
7.1 KiB
PHP

<?php
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/callcenter_test_helpers.php';
function drive_test_normalize_header(string $header): string
{
$header = trim($header);
$header = preg_replace('/\s+/', ' ', $header ?? '');
$header = strtoupper((string) $header);
return $header;
}
function drive_test_get_cell(array $row, array $indexes, array $aliases, string $default = ''): string
{
foreach ($aliases as $alias) {
$normalized = drive_test_normalize_header($alias);
if (array_key_exists($normalized, $indexes)) {
return trim((string) ($row[$indexes[$normalized]] ?? $default));
}
}
return $default;
}
function drive_test_fetch_orders(int $limit = 10, int $startRow = 5552): array
{
$credentialsPath = __DIR__ . '/../google_credentials.json';
$spreadsheetId = '1SSmQuR9quxeQbMKNMDkRe8-n1gU7WuEfsFaJ3WKFO-c';
$range = 'A:Z';
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)) {
return [
'headers' => [],
'orders' => [],
'total_rows' => 0,
];
}
$headers = $values[0] ?? [];
$headerIndexes = [];
foreach ($headers as $index => $header) {
$normalized = drive_test_normalize_header((string) $header);
if ($normalized === '') {
$normalized = $index === 0 ? 'CODIGO' : 'COL_' . $index;
}
$headerIndexes[$normalized] = $index;
}
$dataRows = array_slice($values, 1);
$startIndex = max(0, $startRow - 2);
if ($startIndex > 0) {
$dataRows = array_slice($dataRows, $startIndex);
}
if ($limit > 0) {
$previewRows = array_reverse(array_slice($dataRows, -$limit));
} else {
$previewRows = array_reverse($dataRows);
}
$orders = [];
foreach ($previewRows as $row) {
$codigo = trim((string) ($row[0] ?? ''));
$importId = drive_test_get_cell($row, $headerIndexes, ['ID']);
$nombre = drive_test_get_cell($row, $headerIndexes, ['NOMBRE']);
$celular = preg_replace('/\D+/', '', drive_test_get_cell($row, $headerIndexes, ['CELULAR']));
$producto = drive_test_get_cell($row, $headerIndexes, ['PRODUCTO']);
$cantidad = drive_test_get_cell($row, $headerIndexes, ['CANTIDAD']);
$precio = drive_test_get_cell($row, $headerIndexes, ['PRECIO']);
$pais = drive_test_get_cell($row, $headerIndexes, ['PAIS']);
$coordenadas = drive_test_get_cell($row, $headerIndexes, ['COORDENADAS']);
$ciudad = drive_test_get_cell($row, $headerIndexes, ['CIUDAD']);
$metodo = drive_test_get_cell($row, $headerIndexes, ['METODO']);
$sede = drive_test_get_cell($row, $headerIndexes, ['SEDE / ID', 'SEDE/ID']);
$dni = drive_test_get_cell($row, $headerIndexes, ['N° DNI', 'N° DNI ', 'NRO DNI', 'DNI']);
$observaciones = drive_test_get_cell($row, $headerIndexes, ['OBSERVACIONES', 'OBSERVACIONES ']);
$direccion = drive_test_get_cell($row, $headerIndexes, ['DIRECION', 'DIRECCION']);
$referencia = drive_test_get_cell($row, $headerIndexes, ['REFERENCIA']);
$distrito = drive_test_get_cell($row, $headerIndexes, ['DISTRITO']);
$sourceKey = sha1(implode('|', [$codigo, $importId, $nombre, $celular, $producto]));
$orders[] = [
'source_key' => $sourceKey,
'codigo' => $codigo,
'import_id' => $importId,
'nombre' => $nombre,
'direccion' => $direccion,
'referencia' => $referencia,
'agencia' => '',
'sede_agencia' => '',
'distrito' => $distrito,
'celular' => $celular,
'producto' => $producto,
'cantidad' => $cantidad,
'precio' => $precio,
'pais' => $pais,
'coordenadas' => $coordenadas,
'ciudad' => $ciudad,
'metodo' => $metodo,
'sede' => $sede,
'dni' => $dni,
'observaciones' => $observaciones,
];
}
return [
'headers' => $headers,
'orders' => $orders,
'total_rows' => count($dataRows),
];
}
function drive_test_fetch_tracking(PDO $pdo, array $sourceKeys): array
{
if (empty($sourceKeys)) {
return [];
}
cc_test_ensure_tracking_table($pdo);
$placeholders = implode(',', array_fill(0, count($sourceKeys), '?'));
$stmt = $pdo->prepare("SELECT source_key, estado, nota_seguimiento, user_id, direccion, referencia, agencia, sede_agencia, sede, ciudad, distrito, dni, observaciones, producto, cantidad, precio, confirmacion_producto, confirmacion_cantidad, confirmacion_precio, proxima_llamada_at, fecha_entrega_programada, numero_cuenta_enviado_at, ultima_gestion_at, updated_at FROM callcenter_test_tracking WHERE source_key IN ($placeholders)");
$stmt->execute($sourceKeys);
$tracking = [];
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
$tracking[$row['source_key']] = $row;
}
return $tracking;
}
function drive_test_merge_tracking(array $orders, array $tracking): array
{
$editableFields = ['direccion', 'referencia', 'agencia', 'sede_agencia', 'sede', 'ciudad', 'distrito', 'dni', 'observaciones', 'producto', 'cantidad', 'precio', 'confirmacion_producto', 'confirmacion_cantidad', 'confirmacion_precio'];
foreach ($orders as &$order) {
$current = $tracking[$order['source_key']] ?? null;
$order['estado'] = $current['estado'] ?? 'POR LLAMAR';
$order['nota_seguimiento'] = $current['nota_seguimiento'] ?? '';
$order['user_id'] = array_key_exists('user_id', (array) $current) ? ($current['user_id'] !== null ? (int) $current['user_id'] : null) : null;
$order['seguimiento_actualizado'] = $current['updated_at'] ?? null;
$order['proxima_llamada_at'] = $current['proxima_llamada_at'] ?? null;
$order['fecha_entrega_programada'] = $current['fecha_entrega_programada'] ?? null;
$order['numero_cuenta_enviado_at'] = $current['numero_cuenta_enviado_at'] ?? null;
$order['ultima_gestion_at'] = $current['ultima_gestion_at'] ?? ($current['updated_at'] ?? null);
foreach ($editableFields as $field) {
$order[$field . '_drive'] = $order[$field] ?? '';
$trackedValue = $current[$field] ?? null;
$order[$field . '_editado'] = $trackedValue;
if ($trackedValue !== null && trim((string) $trackedValue) !== '') {
$order[$field] = $trackedValue;
}
}
$digits = preg_replace('/\D+/', '', $order['celular'] ?? '');
$order['whatsapp_url'] = $digits !== '' ? 'https://wa.me/' . $digits : '';
$order['telefono_url'] = $digits !== '' ? 'tel:' . $digits : '';
}
unset($order);
return $orders;
}