Actualizar servicios.html
This commit is contained in:
@@ -370,46 +370,52 @@
|
|||||||
if (html.includes(`value="${currentValue}"`)) opSelect.value = currentValue;
|
if (html.includes(`value="${currentValue}"`)) opSelect.value = currentValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==========================================
|
// ==========================================
|
||||||
// 🚀 LÓGICA INTELIGENTE DE ENRUTAMIENTO (ESTADOS)
|
// 🚀 LÓGICA INTELIGENTE DE ENRUTAMIENTO (ESTADOS) - VERSIÓN OPCIÓN 2
|
||||||
// ==========================================
|
// ==========================================
|
||||||
function getServiceStateInfo(s) {
|
function getServiceStateInfo(s) {
|
||||||
const raw = s.raw_data || {};
|
const raw = s.raw_data || {};
|
||||||
const dbStat = raw.status_operativo;
|
const dbStat = raw.status_operativo; // Aquí viene el ID del estado (ej: "42") o texto antiguo
|
||||||
|
|
||||||
// 1. Bolsa de Trabajo (Máxima prioridad visual)
|
// 1. Prioridad Máxima: Bolsa de Trabajo / Robot WhatsApp
|
||||||
if (!s.assigned_name && (s.automation_status === 'in_progress' || s.automation_status === 'failed')) {
|
if (!s.assigned_name && (s.automation_status === 'in_progress' || s.automation_status === 'failed')) {
|
||||||
return { id: 'bolsa', name: s.automation_status === 'in_progress' ? 'Buscando Operario' : 'Fallo en Bolsa', color: s.automation_status === 'in_progress' ? 'amber' : 'red', isBlocked: true, is_final: false };
|
return { id: 'bolsa', name: s.automation_status === 'in_progress' ? 'Buscando Operario' : 'Fallo en Bolsa', color: s.automation_status === 'in_progress' ? 'amber' : 'red', isBlocked: true, is_final: false };
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Si viene limpio del scraper sin estado -> Pendiente de Asignar
|
// 2. Match Directo por ID (La forma más segura para que los filtros de los botones no fallen)
|
||||||
|
// Si dbStat es un ID que existe en nuestra lista de estados, lo usamos directamente.
|
||||||
|
const foundById = systemStatuses.find(st => String(st.id) === String(dbStat));
|
||||||
|
if (foundById) return { ...foundById, isBlocked: false };
|
||||||
|
|
||||||
|
// 3. Lógica de "La pelota en el tejado": Si tiene operario pero NO tiene fecha agendada
|
||||||
|
if (s.assigned_name && (!raw.scheduled_date || raw.scheduled_date === "")) {
|
||||||
|
const asignado = systemStatuses.find(st => st.name.toLowerCase() === 'asignado');
|
||||||
|
const esperando = systemStatuses.find(st => st.name.toLowerCase().includes('esperando'));
|
||||||
|
|
||||||
|
// Si en la base de datos el texto coincide con "esperando" (compatibilidad)
|
||||||
|
if (dbStat === 'esperando_cliente' && esperando) return { ...esperando, isBlocked: false };
|
||||||
|
|
||||||
|
// Por defecto, si tiene técnico asignado pero no hay fecha ni estado específico de "esperando",
|
||||||
|
// lo mandamos a la columna "ASIGNADO" (Oficina debe gestionar).
|
||||||
|
if (asignado) return { ...asignado, isBlocked: false };
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Si viene nuevo del scraper (Sin operario y sin estado)
|
||||||
if (!s.assigned_name || dbStat === 'sin_asignar') {
|
if (!s.assigned_name || dbStat === 'sin_asignar') {
|
||||||
const found = systemStatuses.find(st => st.name.toLowerCase().includes('pendiente de asignar')) || systemStatuses[0];
|
const found = systemStatuses.find(st => st.name.toLowerCase().includes('pendiente de asignar')) || systemStatuses[0];
|
||||||
return { ...found, isBlocked: false };
|
return { ...found, isBlocked: false };
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Match directo por ID Numérico (El nuevo estándar)
|
// 5. Fallbacks históricos de texto (Para que los servicios antiguos no se rompan)
|
||||||
const foundObj = systemStatuses.find(st => String(st.id) === String(dbStat));
|
const stLower = String(dbStat).toLowerCase();
|
||||||
if (foundObj) return { ...foundObj, isBlocked: false };
|
if (stLower === 'citado') return { ...systemStatuses.find(st => st.name.toLowerCase().includes('citado')), isBlocked: false };
|
||||||
|
if (stLower === 'de_camino') return { ...systemStatuses.find(st => st.name.toLowerCase().includes('camino')), isBlocked: false };
|
||||||
// 4. Fallbacks históricos de texto (Para que los servicios viejos no se rompan)
|
if (stLower === 'trabajando') return { ...systemStatuses.find(st => st.name.toLowerCase().includes('trabajando')), isBlocked: false };
|
||||||
if (dbStat === 'asignado_operario') return { ...systemStatuses.find(st => st.name.toLowerCase() === 'asignado'), isBlocked: false };
|
if (stLower === 'incidencia') return { ...systemStatuses.find(st => st.name.toLowerCase().includes('incidencia')), isBlocked: false };
|
||||||
if (dbStat === 'citado') return { ...systemStatuses.find(st => st.name.toLowerCase().includes('citado')), isBlocked: false };
|
if (stLower === 'desasignado') return { ...systemStatuses.find(st => st.name.toLowerCase().includes('desasignado')), isBlocked: false };
|
||||||
if (dbStat === 'de_camino') return { ...systemStatuses.find(st => st.name.toLowerCase().includes('camino')), isBlocked: false };
|
if (stLower === 'terminado' || stLower === 'finalizado') return { ...systemStatuses.find(st => st.is_final), isBlocked: false };
|
||||||
if (dbStat === 'trabajando') return { ...systemStatuses.find(st => st.name.toLowerCase().includes('trabajando')), isBlocked: false };
|
|
||||||
if (dbStat === 'incidencia') return { ...systemStatuses.find(st => st.name.toLowerCase().includes('incidencia')), isBlocked: false };
|
|
||||||
if (dbStat === 'terminado') return { ...systemStatuses.find(st => st.name.toLowerCase().includes('terminado') || st.name.toLowerCase().includes('finalizado')), isBlocked: false };
|
|
||||||
|
|
||||||
// 5. Fallback final para asignados: Si tiene operario pero NO status_operativo
|
|
||||||
if (s.assigned_name && (!raw.scheduled_date || raw.scheduled_date === "")) {
|
|
||||||
const f = systemStatuses.find(st => st.name.toLowerCase() === 'asignado') || systemStatuses[1];
|
|
||||||
return { ...f, isBlocked: false };
|
|
||||||
}
|
|
||||||
if (s.assigned_name && raw.scheduled_date) {
|
|
||||||
const f = systemStatuses.find(st => st.name.toLowerCase().includes('citado')) || systemStatuses[3];
|
|
||||||
return { ...f, isBlocked: false };
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Fallback de seguridad
|
||||||
return { id: 'unknown', name: 'Desconocido', color: 'gray', isBlocked: false, is_final: false };
|
return { id: 'unknown', name: 'Desconocido', color: 'gray', isBlocked: false, is_final: false };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user