espace-paie-odentas/components/staff/PayslipPdfVerificationModal.tsx
odentas 897af4b23a feat: Ajout fonctionnalités virements, facturation, signatures et emails
- Ajout sous-header total net à payer sur page virements-salaires
- Migration transfer_done_at pour tracking précis des virements
- Nouvelle page saisie tableau pour création factures en masse
- APIs bulk pour mise à jour dates signature et jours technicien
- API demande mandat SEPA avec email template
- Webhook DocuSeal pour signature contrats (mode TEST)
- Composants modaux détails et vérification PDF fiches de paie
- Upload/suppression/remplacement PDFs dans PayslipsGrid
- Amélioration affichage colonnes et filtres grilles contrats/paies
- Template email mandat SEPA avec sous-texte CTA
- APIs bulk facturation (création, update statut/date paiement)
- API clients sans facture pour période donnée
- Corrections calculs dates et montants avec auto-remplissage
2025-11-02 23:26:19 +01:00

304 lines
12 KiB
TypeScript

// components/staff/PayslipPdfVerificationModal.tsx
"use client";
import React, { useState, useEffect, useCallback } from "react";
import { X, ChevronLeft, ChevronRight, FileText, AlertCircle, CheckCircle, Download, ExternalLink } from "lucide-react";
type PayslipPdf = {
id: string;
contractNumber?: string;
employeeName?: string;
pdfUrl?: string;
hasError: boolean;
errorMessage?: string;
};
type PayslipPdfVerificationModalProps = {
isOpen: boolean;
onClose: () => void;
payslips: PayslipPdf[];
isLoading: boolean;
};
export default function PayslipPdfVerificationModal({
isOpen,
onClose,
payslips,
isLoading
}: PayslipPdfVerificationModalProps) {
const [currentIndex, setCurrentIndex] = useState(0);
const [pdfError, setPdfError] = useState(false);
const currentPayslip = payslips[currentIndex];
const hasPayslips = payslips.length > 0;
// Reset index when payslips change
useEffect(() => {
setCurrentIndex(0);
setPdfError(false);
}, [payslips]);
// Keyboard navigation
useEffect(() => {
if (!isOpen) return;
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
e.preventDefault();
goToPrevious();
} else if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
e.preventDefault();
goToNext();
} else if (e.key === 'Escape') {
e.preventDefault();
onClose();
}
};
document.addEventListener('keydown', handleKeyDown);
return () => document.removeEventListener('keydown', handleKeyDown);
}, [isOpen, currentIndex, payslips.length]);
const goToPrevious = useCallback(() => {
if (payslips.length === 0) return;
setCurrentIndex(prev => prev > 0 ? prev - 1 : payslips.length - 1);
setPdfError(false);
}, [payslips.length]);
const goToNext = useCallback(() => {
if (payslips.length === 0) return;
setCurrentIndex(prev => prev < payslips.length - 1 ? prev + 1 : 0);
setPdfError(false);
}, [payslips.length]);
const goToIndex = useCallback((index: number) => {
setCurrentIndex(index);
setPdfError(false);
}, []);
const handlePdfError = useCallback(() => {
setPdfError(true);
}, []);
const openPdfInNewTab = useCallback(() => {
if (currentPayslip?.pdfUrl) {
window.open(currentPayslip.pdfUrl, '_blank');
}
}, [currentPayslip?.pdfUrl]);
const downloadPdf = useCallback(() => {
if (currentPayslip?.pdfUrl) {
const link = document.createElement('a');
link.href = currentPayslip.pdfUrl;
link.download = `fiche_paie_${currentPayslip.contractNumber || currentPayslip.id}.pdf`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}, [currentPayslip]);
if (!isOpen) return null;
return (
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
<div className="bg-white rounded-lg w-full h-full max-w-7xl max-h-[95vh] flex flex-col">
{/* Header */}
<div className="flex items-center justify-between p-4 border-b bg-gray-50">
<div className="flex items-center gap-3">
<FileText className="size-5 text-blue-600" />
<div>
<h2 className="text-lg font-semibold">Vérification des Fiches de Paie</h2>
{hasPayslips && (
<p className="text-sm text-gray-600">
{currentIndex + 1} sur {payslips.length} fiches de paie
</p>
)}
</div>
</div>
<div className="flex items-center gap-2">
{currentPayslip && !currentPayslip.hasError && currentPayslip.pdfUrl && (
<>
<button
onClick={downloadPdf}
className="p-2 hover:bg-gray-200 rounded-lg transition-colors"
title="Télécharger le PDF"
>
<Download className="size-4" />
</button>
<button
onClick={openPdfInNewTab}
className="p-2 hover:bg-gray-200 rounded-lg transition-colors"
title="Ouvrir dans un nouvel onglet"
>
<ExternalLink className="size-4" />
</button>
</>
)}
<button
onClick={onClose}
className="p-2 hover:bg-gray-200 rounded-lg transition-colors"
>
<X className="size-5" />
</button>
</div>
</div>
<div className="flex flex-1 overflow-hidden">
{/* Sidebar with payslip list */}
<div className="w-80 border-r bg-gray-50 flex flex-col">
<div className="p-3 border-b bg-white">
<h3 className="font-medium text-sm text-gray-700">Liste des fiches de paie</h3>
</div>
<div className="flex-1 overflow-y-auto">
{isLoading ? (
<div className="p-4 text-center text-gray-500">
<div className="animate-spin rounded-full h-6 w-6 border-b-2 border-blue-600 mx-auto mb-2"></div>
Chargement des PDFs...
</div>
) : payslips.length === 0 ? (
<div className="p-4 text-center text-gray-500">
<AlertCircle className="size-8 mx-auto mb-2 text-gray-400" />
<p className="text-sm">Aucune fiche de paie sélectionnée</p>
</div>
) : (
<div className="space-y-1 p-2">
{payslips.map((payslip, index) => (
<button
key={payslip.id}
onClick={() => goToIndex(index)}
className={`w-full text-left p-3 rounded-lg transition-colors ${
index === currentIndex
? 'bg-blue-100 border border-blue-200'
: 'hover:bg-gray-100'
}`}
>
<div className="flex items-center gap-2">
<div className="flex-shrink-0">
{payslip.hasError ? (
<AlertCircle className="size-4 text-red-500" />
) : payslip.pdfUrl ? (
<CheckCircle className="size-4 text-green-500" />
) : (
<div className="size-4 rounded-full border-2 border-gray-300"></div>
)}
</div>
<div className="flex-1 min-w-0">
<div className="font-medium text-sm truncate">
{payslip.contractNumber || `Paie ${index + 1}`}
</div>
<div className="text-xs text-gray-500 truncate">
{payslip.employeeName || 'Nom non disponible'}
</div>
{payslip.hasError && (
<div className="text-xs text-red-500 truncate">
{payslip.errorMessage || 'Erreur PDF'}
</div>
)}
</div>
</div>
</button>
))}
</div>
)}
</div>
</div>
{/* PDF Viewer */}
<div className="flex-1 flex flex-col">
{isLoading ? (
<div className="flex-1 flex items-center justify-center">
<div className="text-center">
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600 mx-auto mb-4"></div>
<p className="text-gray-600">Chargement des PDFs...</p>
</div>
</div>
) : !hasPayslips ? (
<div className="flex-1 flex items-center justify-center">
<div className="text-center text-gray-500">
<FileText className="size-16 mx-auto mb-4 text-gray-300" />
<p className="text-lg font-medium mb-2">Aucune fiche de paie sélectionnée</p>
<p className="text-sm">Sélectionnez des fiches de paie pour vérifier leurs PDFs</p>
</div>
</div>
) : currentPayslip?.hasError ? (
<div className="flex-1 flex items-center justify-center">
<div className="text-center text-red-500">
<AlertCircle className="size-16 mx-auto mb-4" />
<p className="text-lg font-medium mb-2">PDF non disponible</p>
<p className="text-sm">{currentPayslip.errorMessage || 'Le PDF de cette fiche de paie n\'a pas pu être chargé'}</p>
</div>
</div>
) : pdfError ? (
<div className="flex-1 flex items-center justify-center">
<div className="text-center text-red-500">
<AlertCircle className="size-16 mx-auto mb-4" />
<p className="text-lg font-medium mb-2">Erreur de chargement</p>
<p className="text-sm">Impossible d'afficher ce PDF</p>
<button
onClick={openPdfInNewTab}
className="mt-3 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors"
>
Ouvrir dans un nouvel onglet
</button>
</div>
</div>
) : currentPayslip?.pdfUrl ? (
<div className="flex-1 relative">
<iframe
src={currentPayslip.pdfUrl}
className="w-full h-full border-0"
onError={handlePdfError}
title={`PDF de la fiche de paie ${currentPayslip.contractNumber || currentPayslip.id}`}
/>
</div>
) : (
<div className="flex-1 flex items-center justify-center">
<div className="text-center text-gray-500">
<FileText className="size-16 mx-auto mb-4 text-gray-300" />
<p className="text-lg font-medium mb-2">PDF non uploadé</p>
<p className="text-sm">Cette fiche de paie n'a pas encore de PDF</p>
</div>
</div>
)}
{/* Navigation controls */}
{hasPayslips && (
<div className="border-t bg-gray-50 p-3">
<div className="flex items-center justify-between">
<button
onClick={goToPrevious}
disabled={payslips.length <= 1}
className="flex items-center gap-2 px-3 py-2 bg-white border rounded-lg hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
>
<ChevronLeft className="size-4" />
Précédent
</button>
<div className="flex items-center gap-2">
<span className="text-sm text-gray-600">
{currentIndex + 1} / {payslips.length}
</span>
<div className="text-xs text-gray-500">
(Utilisez pour naviguer)
</div>
</div>
<button
onClick={goToNext}
disabled={payslips.length <= 1}
className="flex items-center gap-2 px-3 py-2 bg-white border rounded-lg hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
>
Suivant
<ChevronRight className="size-4" />
</button>
</div>
</div>
)}
</div>
</div>
</div>
</div>
);
}