const authorized = {'ALPHA SERVICES':'TALPHA','BETA FOURNITURES':'TBETA','GAMMA PRESTATIONS':'TGAMMA'};
const seen = new Set();
return $input.all().map(item => {
  const d = item.json, errors=[];
  const vendor=String(d.fournisseur??'').trim().toUpperCase();
  const num=String(d.numero??'').trim().toUpperCase();
  const key=vendor+'|'+num;
  if (!String(d.fichier??'').toLowerCase().endsWith('.pdf')) errors.push('FORMAT_NON_PDF');
  if(!vendor || !num) errors.push('CHAMP_OBLIGATOIRE_MANQUANT');
  if (!authorized[vendor]) errors.push('FOURNISSEUR_NON_AUTORISE');
  if (!/^\d{4}-\d{2}-\d{2}$/.test(String(d.date??'')) || Number.isNaN(Date.parse(d.date))) errors.push('DATE_INVALIDE');
  const ht=Number(d.ht),tva=Number(d.tva),ttc=Number(d.ttc);
  if (![ht,tva,ttc].every(Number.isFinite)) errors.push('MONTANT_INVALIDE');
  else if (Math.abs(Math.round((ht+tva-ttc)*100))>1) errors.push('TTC_INCOHERENT');
  if(seen.has(key)) errors.push('DOUBLON_FOURNISSEUR_NUMERO');
  if(vendor && num) seen.add(key);
  const status=errors.length?'A_VERIFIER':'A_VALIDER';
  return {json:{...d,statut_regles:status,motifs:errors.join('|'),code_tiers_test:authorized[vendor]||'',decision_humaine:'NON_RENSEIGNE',sortie:'BLOQUE_AVANT_APPROBATION'}};
});
