Skip to content

Instantly share code, notes, and snippets.

@charlycoste
Last active May 5, 2016 15:09
Show Gist options
  • Save charlycoste/3ffde0d4cc576528ef01fb280bcb832e to your computer and use it in GitHub Desktop.
Save charlycoste/3ffde0d4cc576528ef01fb280bcb832e to your computer and use it in GitHub Desktop.
Analyse de relevés de compte LCL
<?php
$dir = './Relevés/';
$files = scandir($dir);
$regex = [
'/SOLDE INTERMEDIAIRE[^\d]+([\d ]+,\d{2})/'=>'intermediaire',
'/^\s+$/'=>'vide',
'/du \d{2}.\d{2}.\d{4} au \d{2}.\d{2}.\d{4}.*N°\s+\d+/'=>'numérotation',
'/RELEVE DE COMPTE COURANT/'=>'Titre',
'/ANCIEN SOLDE\s+([\d ]+,\d{2})/i'=>'Ancien solde',
'/SOLDE EN EUROS\s+([\d ]+,\d{2})/i'=>'Ligne du solde',
'/\s+\d{2}\.\d{2}\s+.*\d{2}\.\d{2}\.\d{2}([\s\.]+)([\d ]+,\d{2})/'=>'Ligne de transaction',
];
function parseFloat($value)
{
return round(str_replace([' ',','], ['','.'], $value), 2);
}
foreach ($files as $file) {
if (in_array($file, ['.','..'])) {
continue;
}
echo "Traitement du fichier $file\n";
$output ="";
exec("pdftotext -layout {$dir}{$file} - 2>/dev/null", $output);
if (empty($output)) {
exec("tesseract {$dir}{$file} - 2>/dev/null", $output);
}
foreach ($output as $out) {
foreach ($regex as $exp=>$label) {
$matches = [];
if (preg_match($exp, $out, $matches)) {
switch ($label) {
case 'Ancien solde':
$solde = parseFloat($matches[1]);
echo "Ancien solde = {$solde}\n";
$somme = $solde;
break;
case 'Ligne du solde':
$solde = parseFloat($matches[1]);
echo "Nouveau solde = {$solde}\n";
if (abs(($solde - $somme)) < 0.01) {
echo "{$solde} == {$somme} ok\n";
} else {
echo "{$solde} == {$somme} ko\n";
}
//echo ($solde - $somme)."\n";
break;
case 'Ligne de transaction':
$transaction = parseFloat($matches[2]);
if (strlen($matches[1]) < 30) {
$transaction *= -1;
}
echo "Transfert ".(($transaction>0) ? '(crédit)' : '(débit)')."= {$transaction}\n";
$somme += $transaction;
//echo "solde calculé= {$somme}\n";
break;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment