Private
Public Access
349 lines
9.6 KiB
Svelte
349 lines
9.6 KiB
Svelte
<script lang="ts">
|
|
import {
|
|
Button,
|
|
InlineNotification,
|
|
Modal,
|
|
Table,
|
|
TableBody,
|
|
TableCell,
|
|
TableContainer,
|
|
TableHead,
|
|
TableHeader,
|
|
TableRow,
|
|
Tag
|
|
} from 'carbon-components-svelte';
|
|
import { Download, Email } from 'carbon-icons-svelte';
|
|
import { obtenerComprobante, enviarEmailComprobante } from '$lib/hooks/facturas/comprobante';
|
|
import type { ComprobanteResponse, PagoResumen } from '../../types';
|
|
|
|
let { open = $bindable(false), facturaId = 0, onEmailEnviado = () => {} } = $props();
|
|
|
|
let comprobante: ComprobanteResponse | null = $state(null);
|
|
let pagos: PagoResumen[] = $state([]);
|
|
let cargando = $state(false);
|
|
let enviandoEmail = $state(false);
|
|
let error = $state('');
|
|
let info = $state('');
|
|
|
|
function formatFecha(iso?: string): string {
|
|
if (!iso) return '-';
|
|
try {
|
|
return new Date(iso).toLocaleDateString('es-AR');
|
|
} catch {
|
|
return iso;
|
|
}
|
|
}
|
|
|
|
function formatFechaHora(iso?: string): string {
|
|
if (!iso) return '-';
|
|
try {
|
|
return new Date(iso).toLocaleString('es-AR', {
|
|
day: '2-digit',
|
|
month: '2-digit',
|
|
year: 'numeric',
|
|
hour: '2-digit',
|
|
minute: '2-digit'
|
|
});
|
|
} catch {
|
|
return iso;
|
|
}
|
|
}
|
|
|
|
function formatMonto(n?: number): string {
|
|
if (n === undefined || n === null) return '-';
|
|
return n.toLocaleString('es-AR', { style: 'currency', currency: 'ARS' });
|
|
}
|
|
|
|
function estadoTagKind(estado: string): 'green' | 'blue' | 'red' | 'gray' | 'warm-gray' {
|
|
switch (estado) {
|
|
case 'PAGADA':
|
|
return 'green';
|
|
case 'PARCIAL':
|
|
return 'blue';
|
|
case 'CANCELADA':
|
|
return 'red';
|
|
case 'EMITIDA':
|
|
return 'warm-gray';
|
|
default:
|
|
return 'gray';
|
|
}
|
|
}
|
|
|
|
function nombreRepuesto(d: { repuestoId: number; nombre?: string }): string {
|
|
return d.nombre ?? `Repuesto ${d.repuestoId}`;
|
|
}
|
|
|
|
$effect(() => {
|
|
if (!open) {
|
|
comprobante = null;
|
|
pagos = [];
|
|
error = '';
|
|
info = '';
|
|
}
|
|
});
|
|
|
|
async function cargarComprobante() {
|
|
if (!facturaId) return;
|
|
cargando = true;
|
|
const res = await obtenerComprobante(fetch, facturaId);
|
|
cargando = false;
|
|
if (res && typeof res === 'object' && !(res as any).error) {
|
|
const c = res as ComprobanteResponse;
|
|
comprobante = c;
|
|
pagos = c.pagos ?? [];
|
|
} else {
|
|
error = typeof res === 'string' ? res : 'no se pudo cargar el comprobante';
|
|
}
|
|
}
|
|
|
|
$effect(() => {
|
|
if (open && facturaId) {
|
|
void cargarComprobante();
|
|
}
|
|
});
|
|
|
|
function descargarComprobante() {
|
|
if (!comprobante) return;
|
|
window.print();
|
|
}
|
|
|
|
async function enviarEmail() {
|
|
if (!comprobante) return;
|
|
enviandoEmail = true;
|
|
const res = await enviarEmailComprobante(fetch, comprobante.ID);
|
|
enviandoEmail = false;
|
|
if (res && typeof res === 'object' && !(res as any).error) {
|
|
info = 'Email enviado al cliente correctamente.';
|
|
void onEmailEnviado?.();
|
|
} else {
|
|
error = typeof res === 'string' ? res : 'no se pudo enviar el email';
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<Modal bind:open={open} modalHeading={`Comprobante ${comprobante?.numero ?? ''}`} passiveModal>
|
|
{#if cargando}
|
|
<div class="p-6 flex flex-col items-center justify-center gap-3">
|
|
<p class="text-gray-500 text-sm">Cargando comprobante…</p>
|
|
</div>
|
|
{:else if comprobante}
|
|
<div class="comprobante-print space-y-6 max-w-3xl bg-white/10 border border-blue-200/20 p-1">
|
|
<!-- Encabezado del comprobante -->
|
|
<div class="p-6 bg-linear-to-r from-blue-600/90 to-blue-800/90 text-white shadow-sm print:shadow-none">
|
|
<p class="text-xs uppercase tracking-wider text-blue-100 opacity-90 mb-1">Repuestera</p>
|
|
<h2 class="text-xl font-bold mb-1">Comprobante de venta</h2>
|
|
<p class="text-sm text-blue-100 opacity-95">Factura <strong>{comprobante.numero ?? `F-${comprobante.ID}`}</strong></p>
|
|
</div>
|
|
|
|
<!-- Datos del cliente usando Carbon Table -->
|
|
<TableContainer>
|
|
<Table size="compact">
|
|
<TableHead>
|
|
<TableRow>
|
|
<TableHeader>Dato</TableHeader>
|
|
<TableHeader>Valor</TableHeader>
|
|
</TableRow>
|
|
</TableHead>
|
|
<TableBody>
|
|
<TableRow>
|
|
<TableCell>Cliente</TableCell>
|
|
<TableCell>{comprobante.cliente?.nombre ?? '-'}</TableCell>
|
|
</TableRow>
|
|
<TableRow>
|
|
<TableCell>CUIT</TableCell>
|
|
<TableCell>{comprobante.cliente?.cuit ?? '-'}</TableCell>
|
|
</TableRow>
|
|
<TableRow>
|
|
<TableCell>Fecha de emisión</TableCell>
|
|
<TableCell>{formatFecha(comprobante.FechaEmision)}</TableCell>
|
|
</TableRow>
|
|
<TableRow>
|
|
<TableCell>Estado</TableCell>
|
|
<TableCell>
|
|
<Tag type={estadoTagKind(comprobante.estado)}>
|
|
{comprobante.estado}
|
|
</Tag>
|
|
</TableCell>
|
|
</TableRow>
|
|
<TableRow>
|
|
<TableCell>Total</TableCell>
|
|
<TableCell class="text-right font-bold text-blue-700">
|
|
{formatMonto(comprobante.total)}
|
|
</TableCell>
|
|
</TableRow>
|
|
{#if (comprobante.saldo ?? 0) > 0}
|
|
<TableRow>
|
|
<TableCell>Saldo a pagar</TableCell>
|
|
<TableCell class="text-right font-bold text-amber-700">
|
|
{formatMonto(comprobante.saldo)}
|
|
</TableCell>
|
|
</TableRow>
|
|
{/if}
|
|
</TableBody>
|
|
</Table>
|
|
</TableContainer>
|
|
|
|
<!-- Ítems usando Carbon Table -->
|
|
{#if comprobante.detalles && comprobante.detalles.length > 0}
|
|
<TableContainer title="Ítems">
|
|
<Table>
|
|
<TableHead>
|
|
<TableRow>
|
|
<TableHeader>Repuesto</TableHeader>
|
|
<TableHeader>Cant.</TableHeader>
|
|
<TableHeader>Precio unit.</TableHeader>
|
|
<TableHeader>Subtotal</TableHeader>
|
|
</TableRow>
|
|
</TableHead>
|
|
<TableBody>
|
|
{#each comprobante.detalles as d}
|
|
<TableRow>
|
|
<TableCell>{nombreRepuesto(d)}</TableCell>
|
|
<TableCell class="text-right">{d.cantidad}</TableCell>
|
|
<TableCell class="text-right">{formatMonto(d.precioUnitario)}</TableCell>
|
|
<TableCell class="text-right font-medium">{formatMonto(d.subtotal)}</TableCell>
|
|
</TableRow>
|
|
{/each}
|
|
</TableBody>
|
|
</Table>
|
|
</TableContainer>
|
|
{/if}
|
|
|
|
<!-- Resumen de importe usando Carbon Table -->
|
|
<TableContainer>
|
|
<Table size="compact">
|
|
<TableHead>
|
|
<TableRow>
|
|
<TableHeader>Concepto</TableHeader>
|
|
<TableHeader>Importe</TableHeader>
|
|
</TableRow>
|
|
</TableHead>
|
|
<TableBody>
|
|
<TableRow>
|
|
<TableCell>Subtotal</TableCell>
|
|
<TableCell class="text-right">{formatMonto(comprobante.subtotal ?? 0)}</TableCell>
|
|
</TableRow>
|
|
{#if (comprobante.descuentoTotal ?? 0) > 0}
|
|
<TableRow>
|
|
<TableCell>Descuento</TableCell>
|
|
<TableCell class="text-right" style="color:#da1e28;">
|
|
-{formatMonto(comprobante.descuentoTotal)}
|
|
</TableCell>
|
|
</TableRow>
|
|
{/if}
|
|
<TableRow>
|
|
<TableCell>IVA (21%)</TableCell>
|
|
<TableCell class="text-right">{formatMonto(comprobante.impuestos ?? 0)}</TableCell>
|
|
</TableRow>
|
|
<TableRow>
|
|
<TableCell class="font-bold">Total</TableCell>
|
|
<TableCell class="text-right font-bold text-blue-700">
|
|
{formatMonto(comprobante.total)}
|
|
</TableCell>
|
|
</TableRow>
|
|
</TableBody>
|
|
</Table>
|
|
</TableContainer>
|
|
|
|
<!-- Pagos realizados usando Carbon Table -->
|
|
<div class="space-y-2">
|
|
<TableContainer>
|
|
<Table size="compact">
|
|
<TableHead>
|
|
<TableRow>
|
|
<TableHeader>Fecha</TableHeader>
|
|
<TableHeader>Forma de pago</TableHeader>
|
|
<TableHeader>Importe</TableHeader>
|
|
<TableHeader>Observación</TableHeader>
|
|
</TableRow>
|
|
</TableHead>
|
|
<TableBody>
|
|
{#if pagos.length > 0}
|
|
{#each pagos as p}
|
|
<TableRow>
|
|
<TableCell>{formatFechaHora(p.fecha)}</TableCell>
|
|
<TableCell>{p.formaDePago ?? '-'}</TableCell>
|
|
<TableCell class="text-right font-medium">{formatMonto(p.importe)}</TableCell>
|
|
<TableCell class="text-gray-500">{p.observacion ?? '-'}</TableCell>
|
|
</TableRow>
|
|
{/each}
|
|
{:else}
|
|
<TableRow>
|
|
<TableCell colspan={4} class="text-center text-gray-500">
|
|
Sin pagos registrados.
|
|
</TableCell>
|
|
</TableRow>
|
|
{/if}
|
|
</TableBody>
|
|
</Table>
|
|
</TableContainer>
|
|
</div>
|
|
|
|
<!-- Botones de acción (ocultos al imprimir) -->
|
|
<div class="mt-6 flex flex-wrap gap-3 no-print">
|
|
<Button kind="ghost" icon={Download} onclick={descargarComprobante} size="small">
|
|
Descargar comprobante
|
|
</Button>
|
|
{#if comprobante.cliente?.email}
|
|
<Button
|
|
kind="ghost"
|
|
icon={Email}
|
|
disabled={enviandoEmail}
|
|
onclick={enviarEmail}
|
|
size="small"
|
|
>
|
|
{enviandoEmail ? 'Enviando…' : 'Enviar email al cliente'}
|
|
</Button>
|
|
{:else}
|
|
<Button kind="ghost" icon={Email} disabled size="small">
|
|
Cliente sin email
|
|
</Button>
|
|
{/if}
|
|
</div>
|
|
|
|
{#if error}
|
|
<InlineNotification kind="error" title="Error" subtitle={error} hideCloseButton class="mt-4" />
|
|
{/if}
|
|
{#if info}
|
|
<InlineNotification kind="success" title="Enviado" subtitle={info} hideCloseButton class="mt-4" />
|
|
{/if}
|
|
</div>
|
|
{:else if error}
|
|
<div class="p-6 flex flex-col items-center justify-center gap-2">
|
|
<InlineNotification kind="error" title="Error" subtitle={error} hideCloseButton />
|
|
</div>
|
|
{:else}
|
|
<div class="p-6 flex flex-col items-center justify-center gap-2 text-gray-500">
|
|
<p>Seleccioná una factura para ver su comprobante.</p>
|
|
</div>
|
|
{/if}
|
|
</Modal>
|
|
|
|
<style>
|
|
.no-print {
|
|
display: flex;
|
|
}
|
|
|
|
@media print {
|
|
:global(body) * {
|
|
visibility: hidden !important;
|
|
}
|
|
.comprobante-print,
|
|
:global(.comprobante-print) * {
|
|
visibility: visible !important;
|
|
}
|
|
.comprobante-print {
|
|
position: absolute !important;
|
|
top: 0;
|
|
left: 0;
|
|
width: 100%;
|
|
max-width: 800px;
|
|
margin: 0 auto;
|
|
background: white !important;
|
|
}
|
|
.no-print {
|
|
display: none !important;
|
|
}
|
|
}
|
|
</style>
|