Automatically fill in credit note amounts from invoices

Add functionality to automatically fill in credit note amounts based on verified invoice data. Also implements loading of verification results from cache on component mount for improved performance.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 9fcf4b21-eb0c-4e53-a567-e2ce4a6ad869
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/9fcf4b21-eb0c-4e53-a567-e2ce4a6ad869/chkBbcP
This commit is contained in:
michaelschal committed 2025-09-08 18:37:58 +00:00
1 parent e307b43ed4
commit e75a1fb8f3
2 files changed
+73

No files matched your search

Binary file not shown.

After

Width:  |  Height:  |  Size: 193 KiB

+73
View File
@@ -421,6 +421,26 @@ export default function Avoirs() {
...prev,
[variables.avoirId]: result
}));
// Auto-remplissage si facture trouvée et montant disponible
if (result.exists && result.invoiceAmount) {
// Auto-remplir le montant dans l'avoir via API
const avoir = avoirs.find(a => a.id === variables.avoirId);
if (avoir) {
editAvoirMutation.mutate({
id: variables.avoirId,
data: {
supplierId: avoir.supplierId,
groupId: avoir.groupId,
invoiceReference: avoir.invoiceReference || "",
amount: result.invoiceAmount, // Auto-remplissage du montant
comment: avoir.comment || "",
commercialProcessed: avoir.commercialProcessed,
status: avoir.status as "En attente de demande" | "Demandé" | "Reçu",
}
});
}
}
setVerifyingAvoirs(prev => {
const newSet = new Set(prev);
@@ -519,6 +539,59 @@ export default function Avoirs() {
});
};
// Charger les résultats de vérification depuis le cache au démarrage
useEffect(() => {
const loadVerificationResults = async () => {
if (!avoirs || avoirs.length === 0) return;
const avoirsWithReferences = avoirs.filter(avoir =>
avoir.invoiceReference?.trim() &&
(avoir.group?.nocodbTableName || avoir.group?.nocodbConfigId)
);
if (avoirsWithReferences.length === 0) return;
console.log(`🔍 Chargement des résultats de vérification pour ${avoirsWithReferences.length} avoirs`);
// Charger les résultats depuis le cache en parallèle
const results = await Promise.allSettled(
avoirsWithReferences.map(async (avoir) => {
try {
const result = await apiRequest(`/api/avoirs/${avoir.id}/verify-invoice`, 'POST', {
invoiceReference: avoir.invoiceReference,
forceRefresh: false // Utiliser le cache si disponible
});
return { avoirId: avoir.id, result };
} catch (error) {
console.error(`Erreur chargement cache avoir ${avoir.id}:`, error);
return {
avoirId: avoir.id,
result: {
exists: false,
matchType: 'none',
errorMessage: 'Erreur de chargement'
}
};
}
})
);
// Appliquer les résultats chargés
const newResults: Record<number, any> = {};
results.forEach((result) => {
if (result.status === 'fulfilled') {
const { avoirId, result: verificationResult } = result.value;
newResults[avoirId] = verificationResult;
}
});
setAvoirVerificationResults(newResults);
console.log(`✅ Résultats de vérification chargés pour ${Object.keys(newResults).length} avoirs`);
};
loadVerificationResults();
}, [avoirs]); // Se déclenche quand les avoirs sont chargés
// 🔥 FONCTIONS WEBHOOK MODAL (comme rapprochement)
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0];