import React, { useState } from 'react'; import { useHubStore } from '../store/useHubStore.js'; import { Invoice } from '../types/index.js'; import { hubApi } from '../api/hubApi.js'; import { Search, Eye, FileCode, FileDown, ExternalLink, CheckCircle2, AlertCircle, Clock, Sparkles, } from 'lucide-react'; export const InvoicesTable: React.FC = () => { const { invoices, setSelectedInvoice, tenants } = useHubStore(); const [searchTerm, setSearchTerm] = useState(''); const [statusFilter, setStatusFilter] = useState<'ALL' | 'ACEPTADA' | 'RECHAZADA'>('ALL'); const getTenantName = (tenantId: string) => { const t = tenants.find((tenant) => tenant.id === tenantId); return t ? t.name : 'Innovación & Desarrollo'; }; const getTenantBadge = (tenantId: string) => { const t = tenants.find((tenant) => tenant.id === tenantId); if (t?.slug === 'dripzone') { return ( 👟 DripZone ); } if (t?.slug === 'pacifictrans') { return ( 🚛 PacificTrans ); } return ( 🏢 Emisor ); }; const filteredInvoices = invoices.filter((inv) => { const matchesSearch = inv.fullNumber.toLowerCase().includes(searchTerm.toLowerCase()) || inv.customer.legalOrFullName.toLowerCase().includes(searchTerm.toLowerCase()) || inv.customer.identificationNumber.includes(searchTerm) || (inv.cufe && inv.cufe.toLowerCase().includes(searchTerm.toLowerCase())); const matchesStatus = statusFilter === 'ALL' || inv.dianStatus === statusFilter; return matchesSearch && matchesStatus; }); const formatCop = (val: number) => { return new Intl.NumberFormat('es-CO', { style: 'currency', currency: 'COP', minimumFractionDigits: 0, }).format(val); }; const handleDownloadXml = async (inv: Invoice, e: React.MouseEvent) => { e.stopPropagation(); try { const xml = await hubApi.getXml(inv.id); const blob = new Blob([xml], { type: 'application/xml' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `face_${inv.fullNumber}.xml`; a.click(); URL.revokeObjectURL(url); } catch (err) { alert('Error al descargar el archivo XML.'); } }; const handleDownloadPdf = (inv: Invoice, e: React.MouseEvent) => { e.stopPropagation(); window.open(hubApi.getPdfUrl(inv.id), '_blank'); }; return (
{/* Header & Controls */}

Comprobantes Electrónicos UBL 2.1 ({filteredInvoices.length} facturas)

Historial de facturas firmadas con XAdES-BES y transmitidas a la DIAN.

{/* Search Input */}
setSearchTerm(e.target.value)} className="w-full rounded-lg border border-slate-800 bg-slate-950/80 py-2 pl-9 pr-3 text-xs text-slate-200 placeholder-slate-500 outline-none focus:border-emerald-500 focus:ring-1 focus:ring-emerald-500" />
{/* Status Filter */}
{/* Invoices Table */}
{filteredInvoices.length === 0 ? ( ) : ( filteredInvoices.map((inv) => ( setSelectedInvoice(inv)} className="cursor-pointer transition-colors hover:bg-slate-800/40" > {/* Comprobante */} {/* Tenant */} {/* Cliente */} {/* Fecha */} {/* Total */} {/* Estado DIAN */} {/* Acciones */} )) )}
Comprobante Tenant Cliente / Adquiriente Fecha Emisión Total (COP) Estado DIAN Acciones
No se encontraron comprobantes fiscales que coincidan con la búsqueda.
{inv.fullNumber}
CUFE: {inv.cufe?.slice(0, 16)}...
{getTenantBadge(inv.tenantId)}
{inv.customer.legalOrFullName}
NIT/CC: {inv.customer.identificationNumber}
{inv.issueDate}
{inv.issueTime}
{formatCop(inv.grandTotal)}
IVA: {formatCop(inv.taxTotal)}
{inv.dianStatus === 'ACEPTADA' ? ( Aceptada ) : inv.dianStatus === 'RECHAZADA' ? ( Rechazada ) : ( {inv.dianStatus} )}
e.stopPropagation()}>
); };