mirror of
https://github.com/R0m1k3/LogiFlow.git
synced 2026-10-11 17:27:31 +02:00
Improve delivery reconciliation by fetching missing invoice data
Enhance the delivery reconciliation process by implementing fallback mechanisms to fetch and update missing `dueDate` and `invoiceAmountTTC` from external invoice verification services when these details are absent in the existing delivery records. This involves modifications to `server/routes.ts` to handle these updates asynchronously and log the process. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 1e4433b5-47ab-464c-b663-fea2e53367dd Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: 5af17562-f143-442a-a620-6e8a4b02ac1d Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/1e4433b5-47ab-464c-b663-fea2e53367dd/vfXE6SO
This commit is contained in:
1 parent
0be8df7f07
commit
55beb1973d
1 file changed
+44
-6
+44
-6
@@ -399,10 +399,11 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
|||||||
|
|
||||||
console.log(`📅 Livraisons avec échéance: ${deliveriesWithDueDate.length}, sans échéance: ${deliveriesWithoutDueDate.length}`);
|
console.log(`📅 Livraisons avec échéance: ${deliveriesWithDueDate.length}, sans échéance: ${deliveriesWithoutDueDate.length}`);
|
||||||
|
|
||||||
// FALLBACK : Pour les livraisons sans dueDate, interroger NocoDB
|
// FALLBACK : Pour les livraisons sans dueDate ou sans TTC, interroger NocoDB
|
||||||
const { InvoiceVerificationService } = await import('./invoiceVerification.js');
|
const { InvoiceVerificationService } = await import('./invoiceVerification.js');
|
||||||
const verificationService = new InvoiceVerificationService();
|
const verificationService = new InvoiceVerificationService();
|
||||||
|
|
||||||
|
// Traiter les livraisons sans dueDate
|
||||||
for (const delivery of deliveriesWithoutDueDate) {
|
for (const delivery of deliveriesWithoutDueDate) {
|
||||||
try {
|
try {
|
||||||
const result = await verificationService.verifyInvoice(
|
const result = await verificationService.verifyInvoice(
|
||||||
@@ -412,25 +413,62 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
|||||||
delivery.reconciled || false
|
delivery.reconciled || false
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const updateData: any = {};
|
||||||
|
|
||||||
if (result.exists && result.dueDate) {
|
if (result.exists && result.dueDate) {
|
||||||
// Normaliser la date avant de la stocker
|
// Normaliser la date avant de la stocker
|
||||||
const normalizedDateString = normalizeDateString(result.dueDate);
|
const normalizedDateString = normalizeDateString(result.dueDate);
|
||||||
if (normalizedDateString) {
|
if (normalizedDateString) {
|
||||||
// Convertir la string normalisée en objet Date pour Drizzle
|
|
||||||
const normalizedDate = new Date(normalizedDateString);
|
const normalizedDate = new Date(normalizedDateString);
|
||||||
// Mettre à jour deliveries avec la date normalisée (caching)
|
updateData.dueDate = normalizedDate;
|
||||||
await storage.updateDelivery(delivery.id, { dueDate: normalizedDate });
|
|
||||||
delivery.dueDate = normalizedDate;
|
delivery.dueDate = normalizedDate;
|
||||||
console.log(`📅 Fallback: échéance récupérée et normalisée pour livraison #${delivery.id}`);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Récupérer aussi le TTC si manquant
|
||||||
|
if (result.exists && result.invoiceAmountTTC && !delivery.invoiceAmountTTC) {
|
||||||
|
updateData.invoiceAmountTTC = result.invoiceAmountTTC.toString();
|
||||||
|
delivery.invoiceAmountTTC = result.invoiceAmountTTC.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mettre à jour si nécessaire
|
||||||
|
if (Object.keys(updateData).length > 0) {
|
||||||
|
await storage.updateDelivery(delivery.id, updateData);
|
||||||
|
console.log(`📅 Fallback: données récupérées pour livraison #${delivery.id}`, updateData);
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`❌ Fallback échéance échoué pour livraison #${delivery.id}:`, error);
|
console.error(`❌ Fallback échoué pour livraison #${delivery.id}:`, error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Combiner toutes les livraisons qui ont maintenant une dueDate
|
// Combiner toutes les livraisons qui ont maintenant une dueDate
|
||||||
const allDeliveriesWithDueDate = [...deliveriesWithDueDate, ...deliveriesWithoutDueDate.filter((d: any) => d.dueDate)];
|
const allDeliveriesWithDueDate = [...deliveriesWithDueDate, ...deliveriesWithoutDueDate.filter((d: any) => d.dueDate)];
|
||||||
|
|
||||||
|
// FALLBACK TTC : Pour les livraisons qui ont une dueDate mais pas de TTC
|
||||||
|
const deliveriesNeedingTTC = allDeliveriesWithDueDate.filter((d: any) => !d.invoiceAmountTTC || parseFloat(d.invoiceAmountTTC) === 0);
|
||||||
|
|
||||||
|
console.log(`💰 Livraisons nécessitant récupération TTC: ${deliveriesNeedingTTC.length}`);
|
||||||
|
|
||||||
|
for (const delivery of deliveriesNeedingTTC) {
|
||||||
|
try {
|
||||||
|
const result = await verificationService.verifyInvoice(
|
||||||
|
delivery.invoiceReference!,
|
||||||
|
delivery.groupId,
|
||||||
|
false,
|
||||||
|
delivery.reconciled || false
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result.exists && result.invoiceAmountTTC) {
|
||||||
|
await storage.updateDelivery(delivery.id, {
|
||||||
|
invoiceAmountTTC: result.invoiceAmountTTC.toString()
|
||||||
|
});
|
||||||
|
delivery.invoiceAmountTTC = result.invoiceAmountTTC.toString();
|
||||||
|
console.log(`💰 TTC récupéré pour livraison #${delivery.id}: ${result.invoiceAmountTTC}€`);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`❌ Fallback TTC échoué pour livraison #${delivery.id}:`, error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Récupérer tous les fournisseurs pour le mapping du mode de paiement
|
// Récupérer tous les fournisseurs pour le mapping du mode de paiement
|
||||||
const allSuppliers = await storage.getSuppliers();
|
const allSuppliers = await storage.getSuppliers();
|
||||||
|
|||||||
Reference in new issue
Block a user