diff --git a/call_center_pro.php b/call_center_pro.php index 3d88053d..3da06047 100644 --- a/call_center_pro.php +++ b/call_center_pro.php @@ -73,6 +73,10 @@ function cc_test_display_value(?string $value, string $fallback = 'No registrado function cc_test_order_label(array $order): string { + if (!empty($order['is_agregado'])) { + return 'AGREGADOS'; + } + $codigo = trim((string) ($order['codigo'] ?? '')); if ($codigo !== '') { return '#' . ltrim($codigo, '#'); @@ -98,16 +102,40 @@ function cc_test_badge_class(string $estado): string function cc_test_order_time(array $order): int { - foreach (['proxima_llamada_at', 'numero_cuenta_enviado_at', 'ultima_gestion_at', 'seguimiento_actualizado', 'import_id'] as $field) { + foreach (['proxima_llamada_at', 'numero_cuenta_enviado_at', 'ultima_gestion_at', 'seguimiento_actualizado'] as $field) { $date = cc_test_parse_datetime($order[$field] ?? null); if ($date) { return $date->getTimestamp(); } } - return 0; + // Fallback recency for Drive imports where import_id is not a datetime (e.g. "#35898"). + return cc_test_import_time($order); } +function cc_test_import_time(array $order): int +{ + if (!empty($order['is_agregado'])) { + return (int) ($order['id'] ?? 0); + } + + $importRaw = trim((string) ($order['import_id'] ?? '')); + if ($importRaw !== '') { + $date = cc_test_parse_datetime($importRaw); + if ($date) { + return $date->getTimestamp(); + } + + $digits = preg_replace('/\D+/', '', $importRaw); + if ($digits !== '') { + return (int) $digits; + } + } + + $codigoRaw = trim((string) ($order['codigo'] ?? '')); + $digits = preg_replace('/\D+/', '', $codigoRaw); + return $digits !== '' ? (int) $digits : 0; +} function cc_test_followup_semaforo(array $order): ?array { if (cc_test_normalize_state((string) ($order['estado'] ?? '')) !== 'SE ENVIO NUMERO DE CUENTA') { @@ -146,6 +174,8 @@ $orders = []; $visibleOrders = []; $modalsHtml = []; $totalRows = 0; +$agregadosCount = 0; +$tuaniLastProcessedRow = null; $loadLimit = $storeKey === 'otra_tienda' ? 0 : 100; $startRow = (int) ($storeConfig['startRow'] ?? 5552); $catalogoProductos = []; @@ -172,9 +202,31 @@ try { $catalogoProductos = []; } - $preview = drive_test_fetch_orders($loadLimit, $startRow, $storeKey); - $totalRows = (int) ($preview['total_rows'] ?? 0); - $orders = $preview['orders'] ?? []; + if ($storeKey === 'otra_tienda') { + $syncInfo = drive_test_sync_orders_incremental($pdo, $storeKey, 0); + $totalRows = (int) ($syncInfo['drive_rows_total'] ?? 0); + $tuaniLastProcessedRow = (int) ($syncInfo['last_processed_row'] ?? 0); + $startRow = (int) ($syncInfo['next_start_row'] ?? $startRow); + + $orders = drive_test_fetch_orders_from_db($pdo, $storeKey); + + $agregadosCount = 0; + foreach ($orders as $o) { + if (!empty($o['is_agregado'])) { + $agregadosCount++; + } + } + } else { + $preview = drive_test_fetch_orders($loadLimit, $startRow, $storeKey); + $totalRows = (int) ($preview['total_rows'] ?? 0); + $orders = $preview['orders'] ?? []; + + $agregadosOrders = drive_test_fetch_orders_from_db($pdo, $storeKey, true); + $agregadosCount = count($agregadosOrders); + if (!empty($agregadosOrders)) { + $orders = array_merge($orders, $agregadosOrders); + } + } $tracking = drive_test_fetch_tracking($pdo, array_column($orders, 'source_key')); $orders = drive_test_merge_tracking($orders, $tracking); @@ -289,9 +341,22 @@ try { })); usort($visibleOrders, static function (array $a, array $b) use ($view): int { - if ($view === 'pendientes_hoy') { - $aDue = cc_test_parse_datetime($a['proxima_llamada_at'] ?? null); - $bDue = cc_test_parse_datetime($b['proxima_llamada_at'] ?? null); + $aAgregado = !empty($a['is_agregado']); + $bAgregado = !empty($b['is_agregado']); + if ($aAgregado !== $bAgregado) { + return $aAgregado ? 1 : -1; + } + + $aImport = cc_test_import_time($a); + $bImport = cc_test_import_time($b); + + if ($aImport !== $bImport) { + return $bImport <=> $aImport; + } + + if ($view === "pendientes_hoy") { + $aDue = cc_test_parse_datetime($a["proxima_llamada_at"] ?? null); + $bDue = cc_test_parse_datetime($b["proxima_llamada_at"] ?? null); if ($aDue && $bDue && $aDue != $bDue) { return $aDue <=> $bDue; } @@ -334,7 +399,8 @@ require_once 'layout_header.php';