Compare commits
4 Commits
core/route
...
features/p
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8092ca05e6 | ||
|
|
170cb74650 | ||
|
|
bdff1f9028 | ||
|
|
e597b4d161 |
11
jest.config.js
Normal file
11
jest.config.js
Normal file
@@ -0,0 +1,11 @@
|
||||
const { createDefaultPreset } = require("ts-jest");
|
||||
|
||||
const tsJestTransformCfg = createDefaultPreset().transform;
|
||||
|
||||
/** @type {import("jest").Config} **/
|
||||
module.exports = {
|
||||
testEnvironment: "node",
|
||||
transform: {
|
||||
...tsJestTransformCfg,
|
||||
},
|
||||
};
|
||||
4120
package-lock.json
generated
4120
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -11,7 +11,7 @@
|
||||
"type": "commonjs",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"test": "jest",
|
||||
"server": "nodemon ./src/server.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
@@ -22,8 +22,11 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/express": "^5.0.6",
|
||||
"@types/jest": "^30.0.0",
|
||||
"@types/node": "^25.9.3",
|
||||
"jest": "^30.4.2",
|
||||
"nodemon": "^3.1.14",
|
||||
"ts-jest": "^29.4.11",
|
||||
"ts-node": "^10.9.2",
|
||||
"typescript": "^6.0.3"
|
||||
}
|
||||
|
||||
@@ -2,127 +2,137 @@ 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 lignes pour l'analyse et cration d'un tableau de toutes les lignes
|
||||
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];
|
||||
|
||||
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
|
||||
};
|
||||
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];
|
||||
|
||||
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é"
|
||||
date = "non trouvé";
|
||||
}
|
||||
|
||||
|
||||
// Découpage en lignes pour l'analyse et cration d'un tableau de toutes les lignes
|
||||
const lines = text.split('\n').map(l => l.trim()).filter(l => l.length > 0);
|
||||
|
||||
// Algorithme de recherche verticale pour metro
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
// Si la ligne correspond exactement à un code-barres à 13 chiffres
|
||||
if (/^\d{13}$/.test(lines[i])) {
|
||||
const product_name = lines[i + 1];
|
||||
const qte = lines[i + 4];
|
||||
const price = lines[i + 5];
|
||||
const tva = lines[i + 6];
|
||||
|
||||
let tva_final = "";
|
||||
|
||||
if (tva == "B"){
|
||||
tva_final = "5.5%";
|
||||
}
|
||||
|
||||
else {
|
||||
tva_final = "20%";
|
||||
}
|
||||
|
||||
|
||||
product_list[product_name] = {
|
||||
quantity: parseInt(qte, 10),
|
||||
price_ht: parseFloat(price.replace(',', '.')),
|
||||
tva: tva_final
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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 +140,4 @@ async function pdfParse(travel: string) {
|
||||
}
|
||||
|
||||
|
||||
export default pdfParse;
|
||||
export default pdf_parse;
|
||||
25
tests/test_parse.test.ts
Normal file
25
tests/test_parse.test.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import pdf_parse from "../src/modules/pdf-parse";
|
||||
// utilisation de jest pour faire les tests
|
||||
|
||||
describe('Test du parser de factures (pdf_parse)', () => {
|
||||
|
||||
it('doit correctement parser la facture Metro et extraire les produits', async () => {
|
||||
// test pour les fichier metro
|
||||
const cheminPDF = "00(014)0057010856.pdf";
|
||||
const resultat = await pdf_parse(cheminPDF);
|
||||
|
||||
expect(resultat!.brand).toBe("METRO");
|
||||
expect(resultat!.purchase_date).toBe("22-04-2026");
|
||||
expect(resultat!.products).toHaveProperty("2015675 SCHWEPPES POMME SLIM 33CL");
|
||||
});
|
||||
|
||||
it('doit correctement parser une facture Auchan', async () => {
|
||||
// test pour les fichier auchan
|
||||
const cheminPDF = "2026-02-25 _ Facture AUCHAN _ Soupe.pdf";
|
||||
const resultat = await pdf_parse(cheminPDF);
|
||||
|
||||
expect(resultat!.brand).toBe("Auchan");
|
||||
expect(resultat!.purchase_date).toBe("25/02/2026");
|
||||
expect(resultat!.products).toHaveProperty("PURSOUP VELOUTE LEGUMES SOLEIL");
|
||||
});
|
||||
});
|
||||
@@ -7,7 +7,8 @@
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"strict": true,
|
||||
"skipLibCheck": true
|
||||
"skipLibCheck": true,
|
||||
"types": ["jest", "node"]
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["dist", "node_modules"]
|
||||
|
||||
Reference in New Issue
Block a user