All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 58s
- Crear página principal /informe-comercios con todos los estados (loading, error, initial, main) - Implementar componente TotalesMonetariosComercio con distribución de pagos y gráficas - Implementar componente TotalesPesoComercio con totales por tipo de café - Implementar componente TablaComerciosResumen con paginación y exportación - Agregar navegación en sidebar para Informe de Comercios con icono receipt - Integrar filtros avanzados (fechas, clientes, ubicaciones, tipos) - Incluir sistema de alertas para cambios pendientes y comercios anulados - Agregar funciones de copiar a texto/JSON en todos los componentes
519 lines
19 KiB
Vue
519 lines
19 KiB
Vue
<template>
|
|
<div class="flex flex-col gap-8">
|
|
<!-- Loading State -->
|
|
<UCard v-if="loading && !data" class="brand-card border border-transparent">
|
|
<div class="flex flex-col items-center justify-center gap-4 py-10 text-[var(--brand-text-muted)]">
|
|
<div class="flex items-center gap-3">
|
|
<span class="inline-flex h-8 w-8 animate-spin rounded-full border-2 border-[var(--brand-primary-strong)] border-t-transparent align-middle" aria-hidden="true" />
|
|
<span class="text-sm uppercase tracking-[0.3em]">Cargando datos...</span>
|
|
</div>
|
|
</div>
|
|
</UCard>
|
|
|
|
<!-- Error State -->
|
|
<div v-else-if="error" class="rounded-lg border border-red-500/40 bg-red-500/18 p-4 text-sm text-red-200">
|
|
<p>Error al cargar datos: {{ error }}</p>
|
|
<UButton class="mt-4" :loading="loading" :disabled="loading" @click="loadData" color="primary">
|
|
Reintentar
|
|
</UButton>
|
|
</div>
|
|
|
|
<!-- Initial State - No data loaded yet -->
|
|
<template v-else-if="!data && !loading">
|
|
<!-- Card de Filtros -->
|
|
<UCard class="brand-card border border-transparent">
|
|
<template #header>
|
|
<div class="flex flex-col gap-3">
|
|
<div class="flex flex-wrap items-center justify-between gap-3">
|
|
<div>
|
|
<h2 class="text-xl font-bold brand-section-title">Filtros y Configuraciones</h2>
|
|
<p class="text-xs text-[var(--brand-text-muted)] mt-1">
|
|
Aplicados a <code>created_at</code> de comercios
|
|
</p>
|
|
</div>
|
|
<div class="flex items-center gap-2">
|
|
<UCheckbox v-model="includeAnulados" label="Incluir anulados" @update:model-value="onToggleAnulados" />
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Alerta roja cuando incluye anulados -->
|
|
<UAlert
|
|
v-if="includeAnulados"
|
|
color="error"
|
|
variant="solid"
|
|
icon="i-lucide-alert-triangle"
|
|
title="Incluir anulados activado"
|
|
description="Los cálculos incluyen comercios anulados. Esto puede afectar los resultados financieros."
|
|
/>
|
|
</div>
|
|
</template>
|
|
|
|
<DateRangeSelector
|
|
:selected-preset="selectedPreset"
|
|
:fecha-desde="fechaDesde"
|
|
:fecha-hasta="fechaHasta"
|
|
@update:selected-preset="onUpdatePreset"
|
|
@update:fecha-desde="onUpdateFechaDesde"
|
|
@update:fecha-hasta="onUpdateFechaHasta"
|
|
/>
|
|
|
|
<!-- Filtros Avanzados -->
|
|
<div class="mt-6 space-y-4">
|
|
<h3 class="text-sm font-semibold text-[var(--brand-text)]">Filtros Avanzados</h3>
|
|
|
|
<!-- Selector de Clientes -->
|
|
<div>
|
|
<label class="block text-sm font-medium mb-2 text-[var(--brand-text)]">Clientes</label>
|
|
<ClienteMultiSelector
|
|
:selected-ids="selectedClienteIds"
|
|
@update:selected-ids="selectedClienteIds = $event"
|
|
/>
|
|
</div>
|
|
|
|
<!-- Selector de Ubicaciones -->
|
|
<div>
|
|
<label class="block text-sm font-medium mb-2 text-[var(--brand-text)]">Ubicaciones</label>
|
|
<UbicacionMultiSelector
|
|
:selected-ubicaciones="selectedUbicaciones"
|
|
:ubicaciones="opcionesFiltros.ubicaciones"
|
|
@update:selected-ubicaciones="selectedUbicaciones = $event"
|
|
/>
|
|
</div>
|
|
|
|
<!-- Selector de Tipos de Café -->
|
|
<div>
|
|
<label class="block text-sm font-medium mb-2 text-[var(--brand-text)]">Tipos de Café</label>
|
|
<SimpleMultiSelector
|
|
:selected-items="selectedTipos"
|
|
:items="opcionesFiltros.tipos"
|
|
icon="i-lucide-coffee"
|
|
placeholder="Todos los tipos"
|
|
item-label="tipo"
|
|
items-label="tipos"
|
|
@update:selected-items="selectedTipos = $event"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<template #footer>
|
|
<div class="flex flex-col sm:flex-row items-center justify-between gap-3">
|
|
<div class="text-xs text-[var(--brand-text-muted)]">
|
|
Rango activo: {{ rangoLegible }}
|
|
</div>
|
|
<UButton
|
|
:loading="loading"
|
|
:disabled="loading"
|
|
:ui="{ base: 'bg-[var(--brand-primary-strong)] text-[var(--brand-bg)] border border-[var(--brand-primary)] hover:bg-[var(--brand-primary)] hover:border-[var(--brand-accent)] disabled:opacity-50 disabled:cursor-not-allowed' }"
|
|
size="sm"
|
|
@click="loadData"
|
|
>
|
|
<template #leading>
|
|
<UIcon name="i-lucide-refresh-cw" :class="{ 'animate-spin': loading }" />
|
|
</template>
|
|
Actualizar
|
|
</UButton>
|
|
</div>
|
|
</template>
|
|
</UCard>
|
|
|
|
<!-- Mensaje de bienvenida -->
|
|
<UCard class="brand-card border border-transparent">
|
|
<div class="flex flex-col items-center justify-center gap-4 py-16 text-center">
|
|
<div class="rounded-full bg-[var(--brand-primary-strong)]/10 p-6">
|
|
<UIcon name="i-lucide-receipt" class="w-12 h-12 text-[var(--brand-primary-strong)]" />
|
|
</div>
|
|
<div class="flex flex-col gap-2">
|
|
<h3 class="text-lg font-semibold text-[var(--brand-text)]">
|
|
Informe de Comercios
|
|
</h3>
|
|
<p class="text-sm text-[var(--brand-text-muted)] max-w-md">
|
|
Configura los filtros y haz clic en el botón "Actualizar" para cargar el informe detallado de comercios
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</UCard>
|
|
</template>
|
|
|
|
<!-- Main Content -->
|
|
<template v-else-if="data">
|
|
<!-- Card de Filtros -->
|
|
<UCard
|
|
:class="[
|
|
'brand-card border transition-all duration-300',
|
|
hasPendingChanges
|
|
? 'border-yellow-500/60'
|
|
: 'border-transparent'
|
|
]"
|
|
>
|
|
<template #header>
|
|
<div class="flex flex-col gap-3">
|
|
<div class="flex flex-wrap items-center justify-between gap-3">
|
|
<div>
|
|
<h2 class="text-xl font-bold brand-section-title">Filtros y Configuraciones</h2>
|
|
<p class="text-xs text-[var(--brand-text-muted)] mt-1">
|
|
Aplicados a <code>created_at</code> de comercios
|
|
</p>
|
|
</div>
|
|
<div class="flex items-center gap-2">
|
|
<UCheckbox v-model="includeAnulados" label="Incluir anulados" @update:model-value="onToggleAnulados" />
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Alerta de cambios pendientes -->
|
|
<UAlert
|
|
v-if="hasPendingChanges"
|
|
color="warning"
|
|
variant="soft"
|
|
class="py-2"
|
|
>
|
|
<template #title>
|
|
<div class="flex items-center justify-between gap-3 text-sm">
|
|
<div class="flex items-center gap-2">
|
|
<span class="inline-flex h-2 w-2 rounded-full bg-yellow-500 animate-pulse"></span>
|
|
<span class="font-medium">Cambios pendientes - Haz clic en "Actualizar" para aplicar</span>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
</UAlert>
|
|
|
|
<!-- Alerta roja cuando incluye anulados -->
|
|
<UAlert
|
|
v-if="includeAnulados"
|
|
color="error"
|
|
variant="solid"
|
|
icon="i-lucide-alert-triangle"
|
|
title="Incluir anulados activado"
|
|
description="Los cálculos incluyen comercios anulados. Esto puede afectar los resultados financieros."
|
|
/>
|
|
</div>
|
|
</template>
|
|
|
|
<DateRangeSelector
|
|
:selected-preset="selectedPreset"
|
|
:fecha-desde="fechaDesde"
|
|
:fecha-hasta="fechaHasta"
|
|
@update:selected-preset="onUpdatePreset"
|
|
@update:fecha-desde="onUpdateFechaDesde"
|
|
@update:fecha-hasta="onUpdateFechaHasta"
|
|
/>
|
|
|
|
<!-- Filtros Avanzados -->
|
|
<div class="mt-6 space-y-4">
|
|
<h3 class="text-sm font-semibold text-[var(--brand-text)]">Filtros Avanzados</h3>
|
|
|
|
<!-- Selector de Clientes -->
|
|
<div>
|
|
<label class="block text-sm font-medium mb-2 text-[var(--brand-text)]">Clientes</label>
|
|
<ClienteMultiSelector
|
|
:selected-ids="selectedClienteIds"
|
|
@update:selected-ids="selectedClienteIds = $event"
|
|
/>
|
|
</div>
|
|
|
|
<!-- Selector de Ubicaciones -->
|
|
<div>
|
|
<label class="block text-sm font-medium mb-2 text-[var(--brand-text)]">Ubicaciones</label>
|
|
<UbicacionMultiSelector
|
|
:selected-ubicaciones="selectedUbicaciones"
|
|
:ubicaciones="opcionesFiltros.ubicaciones"
|
|
@update:selected-ubicaciones="selectedUbicaciones = $event"
|
|
/>
|
|
</div>
|
|
|
|
<!-- Selector de Tipos de Café -->
|
|
<div>
|
|
<label class="block text-sm font-medium mb-2 text-[var(--brand-text)]">Tipos de Café</label>
|
|
<SimpleMultiSelector
|
|
:selected-items="selectedTipos"
|
|
:items="opcionesFiltros.tipos"
|
|
icon="i-lucide-coffee"
|
|
placeholder="Todos los tipos"
|
|
item-label="tipo"
|
|
items-label="tipos"
|
|
@update:selected-items="selectedTipos = $event"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
<template #footer>
|
|
<div class="flex flex-col sm:flex-row items-center justify-between gap-3">
|
|
<div class="text-xs text-[var(--brand-text-muted)]">
|
|
Rango activo: {{ rangoLegible }} · Comercios filtrados: {{ data.contadores.comercios_filtrados || 0 }}/{{ data.contadores.total_comercios || 0 }}
|
|
</div>
|
|
<UButton
|
|
:loading="loading"
|
|
:disabled="loading"
|
|
:ui="{
|
|
base: hasPendingChanges
|
|
? 'bg-yellow-500 text-black border border-yellow-600 hover:bg-yellow-400 hover:border-yellow-500 disabled:opacity-50 disabled:cursor-not-allowed'
|
|
: 'bg-[var(--brand-primary-strong)] text-[var(--brand-bg)] border border-[var(--brand-primary)] hover:bg-[var(--brand-primary)] hover:border-[var(--brand-accent)] disabled:opacity-50 disabled:cursor-not-allowed'
|
|
}"
|
|
size="sm"
|
|
@click="loadData"
|
|
>
|
|
<template #leading>
|
|
<UIcon name="i-lucide-refresh-cw" :class="{ 'animate-spin': loading }" />
|
|
</template>
|
|
Actualizar
|
|
</UButton>
|
|
</div>
|
|
</template>
|
|
</UCard>
|
|
|
|
<!-- Contadores -->
|
|
<UCard v-if="data.contadores" class="brand-card border border-transparent">
|
|
<template #header>
|
|
<h2 class="text-xl font-bold brand-section-title">Estadísticas del Filtro</h2>
|
|
</template>
|
|
|
|
<div class="grid grid-cols-2 md:grid-cols-4 gap-4">
|
|
<div class="rounded-lg border border-[var(--brand-border)] bg-[var(--brand-surface)] px-4 py-3">
|
|
<div class="text-xs text-[var(--brand-text-muted)] uppercase tracking-wide mb-1">Comercios Filtrados</div>
|
|
<div class="text-2xl font-bold text-[var(--brand-primary)]">
|
|
{{ data.contadores.comercios_filtrados || 0 }}
|
|
</div>
|
|
</div>
|
|
<div class="rounded-lg border border-[var(--brand-border)] bg-[var(--brand-surface)] px-4 py-3">
|
|
<div class="text-xs text-[var(--brand-text-muted)] uppercase tracking-wide mb-1">Total Comercios</div>
|
|
<div class="text-2xl font-bold text-[var(--brand-text)]">
|
|
{{ data.contadores.total_comercios || 0 }}
|
|
</div>
|
|
</div>
|
|
<div class="rounded-lg border border-[var(--brand-border)] bg-[var(--brand-surface)] px-4 py-3">
|
|
<div class="text-xs text-[var(--brand-text-muted)] uppercase tracking-wide mb-1">Clientes Activos</div>
|
|
<div class="text-2xl font-bold text-[var(--brand-primary)]">
|
|
{{ data.contadores.clientes_con_comercios_filtrados || 0 }}
|
|
</div>
|
|
</div>
|
|
<div class="rounded-lg border border-[var(--brand-border)] bg-[var(--brand-surface)] px-4 py-3">
|
|
<div class="text-xs text-[var(--brand-text-muted)] uppercase tracking-wide mb-1">Total Clientes</div>
|
|
<div class="text-2xl font-bold text-[var(--brand-text)]">
|
|
{{ data.contadores.total_clientes || 0 }}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<template #footer>
|
|
<div class="text-xs text-[var(--brand-text-muted)]">
|
|
Última actualización: {{ lastUpdated }}
|
|
</div>
|
|
</template>
|
|
</UCard>
|
|
|
|
<!-- Secciones de Totales -->
|
|
<TotalesMonetariosComercio
|
|
v-if="data.totalesMonetarios"
|
|
:data="data.totalesMonetarios"
|
|
:contadores="data.contadores"
|
|
:rango-legible="rangoLegible"
|
|
:last-updated="lastUpdated"
|
|
/>
|
|
|
|
<TotalesPesoComercio
|
|
v-if="data.totalesPeso"
|
|
:data="data.totalesPeso"
|
|
:contadores="data.contadores"
|
|
:rango-legible="rangoLegible"
|
|
:last-updated="lastUpdated"
|
|
/>
|
|
|
|
<!-- Lista de Comercios -->
|
|
<TablaComerciosResumen
|
|
v-if="data.listaComercio"
|
|
:comercios="data.listaComercio"
|
|
:contadores="data.contadores"
|
|
:rango-legible="rangoLegible"
|
|
:last-updated="lastUpdated"
|
|
/>
|
|
</template>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { useInformeLayout } from '~/composables/useInformeLayout'
|
|
|
|
// Define page metadata
|
|
definePageMeta({
|
|
layout: 'informe',
|
|
title: 'Informe Comercios'
|
|
})
|
|
|
|
// Get page sections from layout
|
|
const { pageSections } = useInformeLayout()
|
|
|
|
// Reactive state
|
|
const data = ref<any>(null)
|
|
const loading = ref(false)
|
|
const error = ref<string | null>(null)
|
|
const lastUpdated = ref<string>('')
|
|
|
|
// Filtros básicos
|
|
const includeAnulados = ref(false)
|
|
|
|
type PresetValue =
|
|
| '' | 'custom' | 'hoy' | 'semana' | 'mes' | 'ytd'
|
|
| 'cosecha-20-21' | 'cosecha-21-22' | 'cosecha-22-23'
|
|
| 'cosecha-23-24' | 'cosecha-24-25' | 'cosecha-25-26'
|
|
|
|
const selectedPreset = ref<PresetValue>('hoy')
|
|
const fechaDesde = ref<string | null>(null)
|
|
const fechaHasta = ref<string | null>(null)
|
|
|
|
// Filtros avanzados
|
|
const selectedClienteIds = ref<number[]>([])
|
|
const selectedUbicaciones = ref<string[]>([])
|
|
const selectedTipos = ref<string[]>([])
|
|
|
|
// Opciones de filtros disponibles (desde Metabase)
|
|
const opcionesFiltros = ref({
|
|
ubicaciones: [] as string[],
|
|
tipos: [] as string[]
|
|
})
|
|
|
|
// Filtros aplicados (los que se usaron en la última carga de datos)
|
|
const appliedFilters = ref<{
|
|
fechaDesde: string | null
|
|
fechaHasta: string | null
|
|
includeAnulados: boolean
|
|
clienteIds: number[]
|
|
ubicaciones: string[]
|
|
tipos: string[]
|
|
} | null>(null)
|
|
|
|
// Computed
|
|
const rangoLegible = computed(() => {
|
|
if (!fechaDesde.value && !fechaHasta.value) return 'Sin filtro de fecha'
|
|
const f = fechaDesde.value ?? '—'
|
|
const t = fechaHasta.value ?? '—'
|
|
return `${f} → ${t}`
|
|
})
|
|
|
|
// Detectar si hay cambios pendientes sin aplicar
|
|
const hasPendingChanges = computed(() => {
|
|
// Si no hay datos cargados, no hay cambios pendientes
|
|
if (!appliedFilters.value) return false
|
|
|
|
// Comparar filtros actuales con los aplicados
|
|
return (
|
|
fechaDesde.value !== appliedFilters.value.fechaDesde ||
|
|
fechaHasta.value !== appliedFilters.value.fechaHasta ||
|
|
includeAnulados.value !== appliedFilters.value.includeAnulados ||
|
|
JSON.stringify(selectedClienteIds.value) !== JSON.stringify(appliedFilters.value.clienteIds) ||
|
|
JSON.stringify(selectedUbicaciones.value) !== JSON.stringify(appliedFilters.value.ubicaciones) ||
|
|
JSON.stringify(selectedTipos.value) !== JSON.stringify(appliedFilters.value.tipos)
|
|
)
|
|
})
|
|
|
|
// Methods
|
|
async function loadData() {
|
|
// Prevenir múltiples peticiones simultáneas
|
|
if (loading.value) {
|
|
console.warn('[InformeComercio] Ya hay una petición en proceso, ignorando nueva solicitud')
|
|
return
|
|
}
|
|
|
|
loading.value = true
|
|
error.value = null
|
|
|
|
try {
|
|
const payload = {
|
|
fecha_desde: fechaDesde.value,
|
|
fecha_hasta: fechaHasta.value,
|
|
incluir_anulados: includeAnulados.value,
|
|
cliente_ids: selectedClienteIds.value,
|
|
tipos: selectedTipos.value,
|
|
comercio_ids: [],
|
|
granularidad: 'dia' // Default granularity
|
|
}
|
|
|
|
console.log('[InformeComercio] Cargando datos con filtros:', payload)
|
|
const result = await $fetch('/api/metabase/informe-comercios', {
|
|
method: 'POST',
|
|
body: payload
|
|
})
|
|
|
|
data.value = result
|
|
lastUpdated.value = new Date().toLocaleString('es-ES', {
|
|
year: 'numeric',
|
|
month: 'long',
|
|
day: 'numeric',
|
|
hour: '2-digit',
|
|
minute: '2-digit'
|
|
})
|
|
|
|
// Guardar los filtros aplicados
|
|
appliedFilters.value = {
|
|
fechaDesde: fechaDesde.value,
|
|
fechaHasta: fechaHasta.value,
|
|
includeAnulados: includeAnulados.value,
|
|
clienteIds: [...selectedClienteIds.value],
|
|
ubicaciones: [...selectedUbicaciones.value],
|
|
tipos: [...selectedTipos.value]
|
|
}
|
|
|
|
console.log('[InformeComercio] Datos cargados:', result)
|
|
} catch (err: any) {
|
|
error.value = err.message || 'Error al cargar datos del informe de comercios'
|
|
console.error('[InformeComercio] Error loading data:', err)
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
async function onToggleAnulados(newValue: boolean | 'indeterminate') {
|
|
if (newValue === true) {
|
|
// Pedir confirmación al activar
|
|
const confirmed = confirm(
|
|
'⚠️ ADVERTENCIA\n\n' +
|
|
'Está a punto de incluir comercios ANULADOS en los cálculos.\n\n' +
|
|
'Esto puede afectar significativamente los resultados financieros y métricas.\n\n' +
|
|
'¿Está seguro de que desea continuar?'
|
|
)
|
|
|
|
if (!confirmed) {
|
|
// Si cancela, revertir el cambio
|
|
includeAnulados.value = false
|
|
return
|
|
}
|
|
}
|
|
|
|
// NO recargar automáticamente - el usuario debe hacer clic en "Actualizar"
|
|
}
|
|
|
|
function onUpdatePreset(value: PresetValue) {
|
|
selectedPreset.value = value
|
|
}
|
|
|
|
function onUpdateFechaDesde(value: string | null) {
|
|
fechaDesde.value = value
|
|
}
|
|
|
|
function onUpdateFechaHasta(value: string | null) {
|
|
fechaHasta.value = value
|
|
}
|
|
|
|
// Cargar opciones de filtros desde el endpoint
|
|
async function loadOpcionesFiltros() {
|
|
try {
|
|
// Por ahora usamos valores hardcoded, pero se podría hacer un endpoint específico
|
|
opcionesFiltros.value = {
|
|
ubicaciones: [], // Se cargará del endpoint cuando se ejecute la primera query
|
|
tipos: ['uva', 'verde', 'mojado', 'oreado']
|
|
}
|
|
} catch (error) {
|
|
console.error('[InformeComercio] Error loading opciones de filtros:', error)
|
|
}
|
|
}
|
|
|
|
// Inicializar preset por defecto sin cargar datos
|
|
onMounted(async () => {
|
|
// Default preset: hoy
|
|
selectedPreset.value = 'hoy'
|
|
|
|
// Cargar opciones de filtros disponibles
|
|
await loadOpcionesFiltros()
|
|
|
|
// NO cargar datos automáticamente - el usuario debe hacer clic en "Actualizar"
|
|
})
|
|
</script>
|