feat(cfdi): descarga masiva de XMLs como ZIP, limite 1,000

This commit is contained in:
Horux Dev
2026-05-24 21:19:56 +00:00
parent 80e2c099d9
commit 5c940847af
5 changed files with 131 additions and 1 deletions

View File

@@ -5,7 +5,7 @@ import { useDebounce } from '@horux/shared-ui';
import { Header } from '@/components/layouts/header';
import { Card, CardContent, CardHeader, CardTitle, CardDescription, Button, Input, Label, Select, SelectContent, SelectItem, SelectTrigger, SelectValue, Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter, Popover, PopoverTrigger, PopoverContent } from '@horux/shared-ui';
import { useCfdis, useCreateCfdi, useDeleteCfdi } from '@/lib/hooks/use-cfdi';
import { createManyCfdis, searchEmisores, searchReceptores, getCfdis, getConceptosList, type EmisorReceptor } from '@/lib/api/cfdi';
import { createManyCfdis, searchEmisores, searchReceptores, getCfdis, getConceptosList, downloadXmlsZip, type EmisorReceptor } from '@/lib/api/cfdi';
import { cancelarFactura, downloadPdf } from '@/lib/api/facturacion';
import type { CfdiFilters, TipoCfdi, Cfdi } from '@horux/shared';
import type { CreateCfdiData } from '@/lib/api/cfdi';
@@ -261,6 +261,8 @@ export default function CfdiPage() {
const [loadingEmisor, setLoadingEmisor] = useState(false);
const [loadingReceptor, setLoadingReceptor] = useState(false);
const [showForm, setShowForm] = useState(false);
const [selectedIds, setSelectedIds] = useState<Set<number>>(new Set());
const [downloadingXmls, setDownloadingXmls] = useState(false);
// Debounced values for autocomplete
const debouncedEmisor = useDebounce(columnFilters.emisor, 300);
@@ -1760,6 +1762,54 @@ export default function CfdiPage() {
<FileText className="h-4 w-4" />
CFDIs ({data?.total || 0})
</CardTitle>
<div className="flex items-center gap-2">
{selectedIds.size > 0 && (
<>
<span className="text-xs text-muted-foreground">
{selectedIds.size} seleccionados
</span>
<Button
variant="outline"
size="sm"
onClick={async () => {
if (selectedIds.size > 1000) {
alert('Máximo 1,000 CFDIs por descarga');
return;
}
try {
setDownloadingXmls(true);
const blob = await downloadXmlsZip(Array.from(selectedIds));
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `cfdis-xml-${Date.now()}.zip`;
a.click();
URL.revokeObjectURL(url);
} catch (err: any) {
alert(err?.response?.data?.message || err?.message || 'Error al descargar XMLs');
} finally {
setDownloadingXmls(false);
}
}}
disabled={downloadingXmls}
>
{downloadingXmls ? (
<Loader2 className="h-4 w-4 mr-1 animate-spin" />
) : (
<Download className="h-4 w-4 mr-1" />
)}
Descargar XMLs
</Button>
<Button
variant="ghost"
size="sm"
onClick={() => setSelectedIds(new Set())}
>
Limpiar
</Button>
</>
)}
</div>
{hasActiveColumnFilters && (
<div className="flex items-center gap-2 text-xs text-muted-foreground">
<span>Filtros activos:</span>
@@ -1817,6 +1867,19 @@ export default function CfdiPage() {
<table className="w-full">
<thead>
<tr className="border-b text-center text-sm text-muted-foreground">
<th className="pb-3 w-8">
<input
type="checkbox"
checked={data?.data.length ? selectedIds.size === data.data.length : false}
onChange={() => {
if (selectedIds.size === data?.data.length) {
setSelectedIds(new Set());
} else {
setSelectedIds(new Set(data?.data.map((c: any) => c.id) || []));
}
}}
/>
</th>
<th className="pb-3 font-medium">
<div className="flex items-center gap-1 justify-center">
Fecha
@@ -1992,6 +2055,20 @@ export default function CfdiPage() {
<tbody className="text-sm text-center">
{data?.data.map((cfdi) => (
<tr key={cfdi.id} className="border-b hover:bg-muted/50">
<td className="py-3">
<input
type="checkbox"
checked={selectedIds.has(cfdi.id)}
onChange={() => {
setSelectedIds(prev => {
const next = new Set(prev);
if (next.has(cfdi.id)) next.delete(cfdi.id);
else next.add(cfdi.id);
return next;
});
}}
/>
</td>
<td className="py-3">{formatDate(cfdi.fechaEmision)}</td>
<td className="py-3">
<span className="text-xs" title={formatTipoComprobante(cfdi.tipoComprobante)}>

View File

@@ -91,6 +91,11 @@ export async function getCfdiById(id: string): Promise<Cfdi> {
return response.data;
}
export async function downloadXmlsZip(ids: number[]): Promise<Blob> {
const response = await apiClient.post('/cfdi/download-xmls', { ids }, { responseType: 'blob' });
return response.data;
}
export async function getResumenCfdi(año?: number, mes?: number, contribuyenteId?: string) {
const params = new URLSearchParams();
if (año) params.set('año', año.toString());