chore : correction de la fonction #5
This commit is contained in:
@@ -2,112 +2,92 @@ import * as fs from 'fs';
|
||||
const pdfjsLib = require('pdfjs-dist');
|
||||
|
||||
|
||||
async function pdfParse(travel: string) {
|
||||
interface DetailProduit {
|
||||
quantité: number;
|
||||
prixHT: number;
|
||||
TVA: string;
|
||||
async function pdf_parse(travel: string) {
|
||||
interface product_details {
|
||||
quantity: number;
|
||||
price_ht: number;
|
||||
tva: string;
|
||||
}
|
||||
|
||||
interface res {
|
||||
marque: string;
|
||||
date_achats: string;
|
||||
brand: string;
|
||||
purchase_date: string;
|
||||
products: {
|
||||
[nomProduit: string]: DetailProduit;
|
||||
[product_name: string]: product_details;
|
||||
};
|
||||
}
|
||||
|
||||
const productsList: { [key: string]: DetailProduit } = {};
|
||||
let fournisseur = '';
|
||||
const product_list: { [key: string]: product_details } = {};
|
||||
let supplier = '';
|
||||
let date = '';
|
||||
|
||||
const cheminPDF = travel;
|
||||
const pdf_path = travel;
|
||||
|
||||
try {
|
||||
// Lecture du fichier binaire du PDF et conversion pour PDF.js
|
||||
const dataBuffer = new Uint8Array(fs.readFileSync(cheminPDF));
|
||||
const data_buffer = new Uint8Array(fs.readFileSync(pdf_path));
|
||||
|
||||
// Chargement du document par PDF.js
|
||||
const loadingTask = pdfjsLib.getDocument({ data: dataBuffer });
|
||||
const pdf = await loadingTask.promise;
|
||||
const loading_task = pdfjsLib.getDocument({ data: data_buffer });
|
||||
const pdf = await loading_task.promise;
|
||||
|
||||
let texte = '';
|
||||
let text = '';
|
||||
|
||||
// Boucle pour extraire le texte de chaque page du PDF
|
||||
// Boucle pour extraire le text de chaque page du PDF
|
||||
for (let i = 1; i <= pdf.numPages; i++) {
|
||||
const page = await pdf.getPage(i);
|
||||
const textContent = await page.getTextContent();
|
||||
const text_content = await page.getTextContent();
|
||||
|
||||
// On récupère chaque fragment de texte trouvé graphiquement sur la page et on les sépare par un saut de ligne
|
||||
const textePage = textContent.items.map((item: any) => item.str).join('\n');
|
||||
texte += textePage + '\n';
|
||||
// On récupère chaque fragment de text trouvé graphiquement sur la page et on les sépare par un saut de ligne
|
||||
const text_page = text_content.items.map((item: any) => item.str).join('\n');
|
||||
text += text_page + '\n';
|
||||
}
|
||||
|
||||
const regexAuchan = /auchan/i.test(texte);
|
||||
const regex_auchan = /auchan/i.test(text);
|
||||
|
||||
// on regarde si c'est auchan ou metro
|
||||
|
||||
if (regexAuchan) {
|
||||
fournisseur = "Auchan";
|
||||
if (regex_auchan) {
|
||||
supplier = "Auchan";
|
||||
|
||||
// on regarde la date d'achat avec une expression reguliere pour auchan
|
||||
const regexDate = /(\d{2})\/(\d{2})\/(\d{4})/;
|
||||
const correspondance = texte.match(regexDate);
|
||||
if (correspondance) {
|
||||
date = correspondance[0]
|
||||
const regex_date = /(\d{2})\/(\d{2})\/(\d{4})/;
|
||||
const corres = text.match(regex_date);
|
||||
if (corres) {
|
||||
date = corres[0]
|
||||
} else {
|
||||
date = "non trouvé"
|
||||
}
|
||||
|
||||
// Découpage en lignes pour l'analyse
|
||||
const lignes = texte.split('\n').map(l => l.trim()).filter(l => l.length > 0);
|
||||
// Découpage en lines pour l'analyse
|
||||
const lines = text.split('\n').map(l => l.trim()).filter(l => l.length > 0);
|
||||
|
||||
// Algorithme de recherche verticale pour Auchan
|
||||
for (let i = 0; i < lignes.length; i++) {
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
// Si la ligne correspond exactement à un code-barres à 13 chiffres
|
||||
if (/^\d{13}$/.test(lignes[i])) {
|
||||
const nomProduit = lignes[i + 1];
|
||||
if (/^\d{13}$/.test(lines[i])) {
|
||||
const product_name = lines[i + 1];
|
||||
const qte = lines[i + 3];
|
||||
const price = lines[i + 4];
|
||||
const tva = lines[i + 5];
|
||||
|
||||
let idxQuantite = i + 3;
|
||||
let idxPrixHT = i + 4;
|
||||
let idxTva = i + 5;
|
||||
|
||||
// Gestion de la ligne parasite Eco-participation qui décale le tableau
|
||||
if (lignes[i + 2] && lignes[i + 2].includes("Eco-participation")) {
|
||||
idxQuantite += 2;
|
||||
idxPrixHT += 2;
|
||||
idxTva += 2;
|
||||
}
|
||||
|
||||
const qte = lignes[idxQuantite];
|
||||
const prix = lignes[idxPrixHT];
|
||||
const tva = lignes[idxTva];
|
||||
let tvaFinal: string;
|
||||
|
||||
if (tva) {
|
||||
tvaFinal = tva.replace(',', '.').trim() + "%";
|
||||
} else {
|
||||
tvaFinal = "non trouvé";
|
||||
}
|
||||
|
||||
|
||||
productsList[nomProduit] = {
|
||||
quantité: parseInt(qte, 10),
|
||||
prixHT: parseFloat(prix.replace(',', '.')),
|
||||
TVA: tvaFinal
|
||||
product_list[product_name] = {
|
||||
quantity: parseInt(qte, 10),
|
||||
price_ht: parseFloat(price.replace(',', '.')),
|
||||
tva: tva.replace(',', '.').trim() + "%"
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
fournisseur = "Metro";
|
||||
supplier = "Metro";
|
||||
|
||||
// on regarde la date d'achat avec une expression reguliere pour metro
|
||||
const regexDate = /(\d{2})-(\d{2})-(\d{4})/;
|
||||
const correspondance = texte.match(regexDate);
|
||||
if (correspondance) {
|
||||
date = correspondance[0]
|
||||
const regex_date = /(\d{2})-(\d{2})-(\d{4})/;
|
||||
const corres = text.match(regex_date);
|
||||
if (corres) {
|
||||
date = corres[0]
|
||||
} else {
|
||||
date = "non trouvé"
|
||||
}
|
||||
@@ -116,13 +96,15 @@ async function pdfParse(travel: string) {
|
||||
}
|
||||
|
||||
// Construction de l'objet final
|
||||
const resultat: res = {
|
||||
marque: fournisseur,
|
||||
date_achats: date,
|
||||
products: productsList
|
||||
const result: res = {
|
||||
brand: supplier,
|
||||
purchase_date: date,
|
||||
products: product_list
|
||||
};
|
||||
|
||||
console.log(resultat);
|
||||
console.log(result);
|
||||
return result;
|
||||
|
||||
|
||||
} catch (e) {
|
||||
console.error("Erreur :", e);
|
||||
@@ -130,4 +112,4 @@ async function pdfParse(travel: string) {
|
||||
}
|
||||
|
||||
|
||||
export default pdfParse;
|
||||
export default pdf_parse;
|
||||
Reference in New Issue
Block a user