Private
Public Access
190 lines
5.5 KiB
Svelte
190 lines
5.5 KiB
Svelte
<script lang="ts">
|
|
import Header from '$lib/components/Header.svelte';
|
|
import {
|
|
Button,
|
|
DataTable,
|
|
DatePicker,
|
|
DatePickerInput,
|
|
Form,
|
|
InlineNotification,
|
|
Select,
|
|
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;
|
|
});
|
|
|
|
let reporte = $derived(data.reporte ?? {});
|
|
let movimientos = $derived(Array.isArray(reporte.movimientos) ? reporte.movimientos : []);
|
|
let resumen = $derived(Array.isArray(reporte.resumen) ? reporte.resumen : []);
|
|
|
|
let filtros = $state({
|
|
repuestoId: 0,
|
|
almacenId: 0,
|
|
tipo: '',
|
|
desde: '',
|
|
hasta: ''
|
|
});
|
|
|
|
$effect(() => {
|
|
filtros.repuestoId = data.filtros?.repuestoId ?? 0;
|
|
filtros.almacenId = data.filtros?.almacenId ?? 0;
|
|
filtros.tipo = data.filtros?.tipo ?? '';
|
|
filtros.desde = data.filtros?.desde ?? '';
|
|
filtros.hasta = data.filtros?.hasta ?? '';
|
|
});
|
|
|
|
const headers = [
|
|
{ key: 'fecha', value: 'Fecha' },
|
|
{ key: 'tipo', value: 'Tipo' },
|
|
{ key: 'repuesto', value: 'Repuesto' },
|
|
{ key: 'almacen', value: 'Almacén' },
|
|
{ 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 ? new Date(m.fecha).toLocaleString() : '',
|
|
tipo: m.tipo,
|
|
repuesto: m.repuesto?.nombre ?? m.repuestoId,
|
|
almacen: m.almacenId,
|
|
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);
|
|
if (filtros.hasta) params.set('hasta', filtros.hasta);
|
|
void goto(`/stock/reportes${params.toString() ? `?${params.toString()}` : ''}`);
|
|
}
|
|
|
|
function limpiarFiltros() {
|
|
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 />
|
|
|
|
<div class="page-container">
|
|
<div class="flex justify-between items-center mb-4!">
|
|
<h1 class="page-title">Reporte de stock</h1>
|
|
<Button kind="tertiary" onclick={() => goto('/stock')}>Volver</Button>
|
|
</div>
|
|
|
|
<Form
|
|
onsubmit={(e) => {
|
|
e.preventDefault();
|
|
aplicarFiltros();
|
|
}}
|
|
>
|
|
<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)}
|
|
<SelectItem value={a.id} text={a.codigo} />
|
|
{/each}
|
|
</Select>
|
|
|
|
<Select labelText="Tipo" bind:selected={filtros.tipo}>
|
|
<SelectItem value="" text="Todos" />
|
|
{#each (reporte.tipos ?? []) as t}
|
|
<SelectItem value={t} text={t} />
|
|
{/each}
|
|
</Select>
|
|
|
|
<DatePicker
|
|
datePickerType="range"
|
|
on:change={(e) => {
|
|
const dates = (e as CustomEvent).detail?.selectedDates ?? [];
|
|
filtros.desde = dates[0]?.toISOString().slice(0, 10) ?? '';
|
|
filtros.hasta = dates[1]?.toISOString().slice(0, 10) ?? '';
|
|
}}
|
|
>
|
|
<DatePickerInput labelText="Desde" placeholder="mm/dd/yyyy" value={filtros.desde} />
|
|
<DatePickerInput labelText="Hasta" placeholder="mm/dd/yyyy" value={filtros.hasta} />
|
|
</DatePicker>
|
|
|
|
<div class="flex gap-2">
|
|
<Button type="submit">Generar</Button>
|
|
<Button kind="ghost" onclick={limpiarFiltros}>Limpiar</Button>
|
|
</div>
|
|
</div>
|
|
</Form>
|
|
|
|
{#if error}
|
|
<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>
|
|
|
|
{#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> |