Correction ajout paie staff/contrats/id

This commit is contained in:
Renaud 2025-10-17 17:36:36 +02:00
parent a0eb421f4b
commit e5382f6491
2 changed files with 63 additions and 4 deletions

View file

@ -51,11 +51,12 @@ export async function POST(req: NextRequest) {
// Récupérer les informations du contrat pour construire le chemin S3
const { data: contract, error: contractError } = await sb
.from("cddu_contracts")
.select("contract_number, org_id, employee_name")
.select("contract_number, org_id, organization_id, client_organization_id, employee_name")
.eq("id", contractId)
.single();
if (contractError || !contract) {
console.error('❌ [Payslip Upload] Erreur chargement contrat:', contractError);
return NextResponse.json({ error: "Contrat introuvable" }, { status: 404 });
}
@ -67,17 +68,27 @@ export async function POST(req: NextRequest) {
.single();
if (payslipError || !payslip) {
console.error('❌ [Payslip Upload] Erreur chargement payslip:', payslipError);
return NextResponse.json({ error: "Paie introuvable" }, { status: 404 });
}
// Essayer plusieurs champs possibles pour l'ID d'organisation
const orgId = contract.organization_id || contract.client_organization_id || contract.org_id;
if (!orgId) {
console.error('❌ [Payslip Upload] Aucun ID d\'organisation trouvé sur le contrat:', contract);
return NextResponse.json({ error: "Organisation introuvable sur le contrat" }, { status: 404 });
}
// Récupérer l'organization pour avoir l'org_key
const { data: org, error: orgError } = await sb
.from("organizations")
.select("api_name")
.eq("id", contract.org_id)
.eq("id", orgId)
.single();
if (orgError || !org?.api_name) {
console.error('❌ [Payslip Upload] Erreur chargement organisation:', orgError, 'orgId:', orgId);
return NextResponse.json({ error: "Organisation introuvable" }, { status: 404 });
}

View file

@ -1,6 +1,6 @@
"use client";
import { useState, useEffect, useRef } from "react";
import { useState, useEffect, useRef, DragEvent } from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { X, Save, Plus, Upload, FileText } from "lucide-react";
@ -27,6 +27,7 @@ export function PayslipModal({
const [isUploading, setIsUploading] = useState(false);
const [uploadedFile, setUploadedFile] = useState<File | null>(null);
const [isGeneratingUrl, setIsGeneratingUrl] = useState(false);
const [isDragging, setIsDragging] = useState(false);
const fileInputRef = useRef<HTMLInputElement>(null);
const [formData, setFormData] = useState({
@ -147,6 +148,43 @@ export function PayslipModal({
}
};
// Gestion du drag & drop
const handleDragEnter = (e: DragEvent<HTMLDivElement>) => {
e.preventDefault();
e.stopPropagation();
setIsDragging(true);
};
const handleDragLeave = (e: DragEvent<HTMLDivElement>) => {
e.preventDefault();
e.stopPropagation();
setIsDragging(false);
};
const handleDragOver = (e: DragEvent<HTMLDivElement>) => {
e.preventDefault();
e.stopPropagation();
};
const handleDrop = async (e: DragEvent<HTMLDivElement>) => {
e.preventDefault();
e.stopPropagation();
setIsDragging(false);
const files = Array.from(e.dataTransfer.files);
if (files.length === 0) return;
const file = files[0];
// Vérifier que c'est un PDF
if (file.type !== 'application/pdf') {
toast.error("Seuls les fichiers PDF sont acceptés");
return;
}
await handleFileUpload(file);
};
const handleViewFile = async () => {
if (!formData.storage_path) {
toast.error("Aucun fichier disponible");
@ -439,7 +477,17 @@ export function PayslipModal({
<label className="text-sm font-medium text-gray-700 mb-2 block">
Bulletin de paie (PDF)
</label>
<div className="border-2 border-dashed border-gray-300 rounded-lg p-6 text-center hover:border-gray-400 transition-colors">
<div
className={`border-2 border-dashed rounded-lg p-6 text-center transition-colors ${
isDragging
? 'border-blue-500 bg-blue-50'
: 'border-gray-300 hover:border-gray-400'
}`}
onDragEnter={handleDragEnter}
onDragLeave={handleDragLeave}
onDragOver={handleDragOver}
onDrop={handleDrop}
>
{formData.storage_url || uploadedFile ? (
<div className="space-y-3">
<FileText className="mx-auto h-12 w-12 text-green-500" />