Private
Public Access
hecha funcionalidad de reportes y exportacion de reportes
Signed-off-by: fede <federico.nicolas.polidoro@gmail.com>
This commit is contained in:
1 parent
61baaf14f8
commit
298f3c699c
7 files changed
+338
-7
No files matched your search
@@ -0,0 +1,62 @@
|
||||
import { API_URL } from '$lib/env';
|
||||
import { getToken } from '../token';
|
||||
|
||||
export async function exportarReporteStock(
|
||||
formato: 'csv' | 'xlsx',
|
||||
filtros?: {
|
||||
repuestoId?: number;
|
||||
almacenId?: number;
|
||||
tipo?: string;
|
||||
desde?: string;
|
||||
hasta?: string;
|
||||
pagina?: number;
|
||||
porPagina?: number;
|
||||
}
|
||||
): Promise<string | null> {
|
||||
try {
|
||||
const params = new URLSearchParams();
|
||||
params.set('formato', formato);
|
||||
if (filtros?.repuestoId) params.set('repuestoId', String(filtros.repuestoId));
|
||||
if (filtros?.almacenId) params.set('almacenId', String(filtros.almacenId));
|
||||
if (filtros?.tipo) params.set('tipo', filtros.tipo);
|
||||
if (filtros?.desde) params.set('desde', filtros.desde);
|
||||
if (filtros?.hasta) params.set('hasta', filtros.hasta);
|
||||
if (filtros?.pagina) params.set('pagina', String(filtros.pagina));
|
||||
if (filtros?.porPagina) params.set('porPagina', String(filtros.porPagina));
|
||||
|
||||
const res = await fetch(`${API_URL}/stock/reportes/export?${params.toString()}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
}
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
let mensaje = 'No se pudo exportar el reporte';
|
||||
try {
|
||||
const data = await res.json();
|
||||
if (data?.error) mensaje = data.error;
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
return mensaje;
|
||||
}
|
||||
|
||||
const blob = await res.blob();
|
||||
const disposition = res.headers.get('Content-Disposition') || '';
|
||||
const match = disposition.match(/filename="?([^"]+)"?/);
|
||||
const filename = match?.[1] ?? `reporte-stock.${formato}`;
|
||||
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement('a');
|
||||
link.href = url;
|
||||
link.download = filename;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(url);
|
||||
return null;
|
||||
} catch {
|
||||
return 'no se pudo alcanzar el servidor';
|
||||
}
|
||||
}
|
||||
@@ -11,10 +11,14 @@
|
||||
SelectItem
|
||||
} from 'carbon-components-svelte';
|
||||
import { goto } from '$app/navigation';
|
||||
import { exportarReporteStock } from '$lib/hooks/stock/exportarReporteStock';
|
||||
|
||||
let { data } = $props();
|
||||
|
||||
let error = $state('');
|
||||
let exportError = $state('');
|
||||
let exportando = $state(false);
|
||||
|
||||
$effect(() => {
|
||||
if (data.error) error = data.error;
|
||||
});
|
||||
@@ -24,6 +28,7 @@
|
||||
let resumen = $derived(Array.isArray(reporte.resumen) ? reporte.resumen : []);
|
||||
|
||||
let filtros = $state({
|
||||
repuestoId: 0,
|
||||
almacenId: 0,
|
||||
tipo: '',
|
||||
desde: '',
|
||||
@@ -31,6 +36,7 @@
|
||||
});
|
||||
|
||||
$effect(() => {
|
||||
filtros.repuestoId = data.filtros?.repuestoId ?? 0;
|
||||
filtros.almacenId = data.filtros?.almacenId ?? 0;
|
||||
filtros.tipo = data.filtros?.tipo ?? '';
|
||||
filtros.desde = data.filtros?.desde ?? '';
|
||||
@@ -42,22 +48,25 @@
|
||||
{ key: 'tipo', value: 'Tipo' },
|
||||
{ key: 'repuesto', value: 'Repuesto' },
|
||||
{ key: 'almacen', value: 'Almacén' },
|
||||
{ key: 'cantidad', value: 'Cantidad' }
|
||||
{ key: 'cantidad', value: 'Cantidad' },
|
||||
{ key: 'saldo', value: 'Saldo posterior' }
|
||||
];
|
||||
|
||||
const rows = $derived(
|
||||
movimientos.map((m: any, i: number) => ({
|
||||
id: m.id ?? i,
|
||||
fecha: m.fecha,
|
||||
fecha: m.fecha ? new Date(m.fecha).toLocaleString() : '',
|
||||
tipo: m.tipo,
|
||||
repuesto: m.repuesto?.nombre ?? m.repuestoId,
|
||||
almacen: m.almacenId,
|
||||
cantidad: m.cantidad
|
||||
cantidad: m.cantidad,
|
||||
saldo: m.saldoPosterior
|
||||
}))
|
||||
);
|
||||
|
||||
function aplicarFiltros() {
|
||||
const params = new URLSearchParams();
|
||||
if (filtros.repuestoId) params.set('repuestoId', String(filtros.repuestoId));
|
||||
if (filtros.almacenId) params.set('almacenId', String(filtros.almacenId));
|
||||
if (filtros.tipo) params.set('tipo', filtros.tipo);
|
||||
if (filtros.desde) params.set('desde', filtros.desde);
|
||||
@@ -66,9 +75,24 @@
|
||||
}
|
||||
|
||||
function limpiarFiltros() {
|
||||
filtros = { almacenId: 0, tipo: '', desde: '', hasta: '' };
|
||||
filtros = { repuestoId: 0, almacenId: 0, tipo: '', desde: '', hasta: '' };
|
||||
void goto('/stock/reportes', { replaceState: true });
|
||||
}
|
||||
|
||||
async function exportar(formato: 'csv' | 'xlsx') {
|
||||
exportError = '';
|
||||
exportando = true;
|
||||
const filtrosExport = {
|
||||
repuestoId: filtros.repuestoId || undefined,
|
||||
almacenId: filtros.almacenId || undefined,
|
||||
tipo: filtros.tipo || undefined,
|
||||
desde: filtros.desde || undefined,
|
||||
hasta: filtros.hasta || undefined
|
||||
};
|
||||
const resultado = await exportarReporteStock(formato, filtrosExport);
|
||||
exportando = false;
|
||||
if (resultado) exportError = resultado;
|
||||
}
|
||||
</script>
|
||||
|
||||
<Header />
|
||||
@@ -86,6 +110,13 @@
|
||||
}}
|
||||
>
|
||||
<div class="flex flex-wrap gap-4 items-end mb-4!">
|
||||
<Select labelText="Repuesto" bind:selected={filtros.repuestoId}>
|
||||
<SelectItem value={0} text="Todos" />
|
||||
{#each data.repuestos as r (r.ID)}
|
||||
<SelectItem value={r.ID} text={r.nombre ?? r.ID} />
|
||||
{/each}
|
||||
</Select>
|
||||
|
||||
<Select labelText="Almacén" bind:selected={filtros.almacenId}>
|
||||
<SelectItem value={0} text="Todos" />
|
||||
{#each data.almacenes as a (a.id)}
|
||||
@@ -123,7 +154,37 @@
|
||||
<InlineNotification kind="error" title="Error" subtitle={error} hideCloseButton class="mb-4" />
|
||||
{/if}
|
||||
|
||||
{#if exportError}
|
||||
<InlineNotification kind="error" title="Exportación" subtitle={exportError} hideCloseButton class="mb-4" />
|
||||
{/if}
|
||||
|
||||
<div class="flex gap-2 mb-4!">
|
||||
<Button kind="secondary" disabled={exportando} onclick={() => exportar('csv')}>
|
||||
{exportando ? 'Exportando...' : 'Exportar CSV'}
|
||||
</Button>
|
||||
<Button kind="secondary" disabled={exportando} onclick={() => exportar('xlsx')}>
|
||||
{exportando ? 'Exportando...' : 'Exportar Excel'}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<DataTable {headers} {rows} />
|
||||
</div>
|
||||
{#if movimientos.length === 0}
|
||||
<InlineNotification kind="info" title="Sin datos" subtitle="No hay movimientos para los filtros seleccionados." hideCloseButton class="mb-4" />
|
||||
{:else}
|
||||
<DataTable {headers} {rows} />
|
||||
{/if}
|
||||
|
||||
{#if resumen.length > 0}
|
||||
<h2 class="mt-6 mb-2">Totales por tipo</h2>
|
||||
<DataTable
|
||||
headers={[
|
||||
{ key: 'tipo', value: 'Tipo' },
|
||||
{ key: 'cantidad', value: 'Cantidad' }
|
||||
]}
|
||||
rows={resumen.map((r: any, i: number) => ({
|
||||
id: i,
|
||||
tipo: r.tipo,
|
||||
cantidad: r.cantidad
|
||||
}))}
|
||||
/>
|
||||
{/if}
|
||||
</div>
|
||||
Reference in new issue
Block a user