Overview
Comment:Add useful scripts
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 7f7d12d84ecc90d66c1ce02a0e3e3643f0af63e2ae20770d0120ac61268eb864
User & Date: bohwaz on 2021-11-08 08:01:02
Other Links: manifest | tags
Context
2021-11-08
10:09
Enhance style of file buttons check-in: 03178b5b25 user: bohwaz tags: trunk, stable
08:01
Add useful scripts check-in: 7f7d12d84e user: bohwaz tags: trunk
2021-11-03
17:37
Fix: config is not required here check-in: 2239ebd2fa user: bohwaz tags: trunk, stable
Changes

Added tools/extract-credit-mutuel-csv.php version [05c87f9eb7].









































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
 * Extracteur de données des relevés de compte du Crédit Mutuel
 * à destination de Garradin (ou autre logiciel de compta)
 *
 * https://garradin.eu/
 *
 * Ce script prend en argument un répertoire contenant des extraits
 * de compte en PDF (ou un seul extrait de compte) et crée un fichier
 * CSV importable directement dans Garradin.
 *
 * Ce script requiert d'avoir installé Tabula (Java) :
 * https://github.com/tabulapdf/tabula-java
 *
 * Copyright (C) 2020 BohwaZ - licence AGPLv3
 */

// Ajuster cette constante en fonction du chemin où vous avez placé
// le JAR de Tabula
const TABULA_PATH = '/home/bohwaz/bin/tabula.jar';

if (empty($argv[1]) || empty($argv[2])) {
	printf("Usage: %s REPERTOIRE_OU_FICHIER FICHIER_SORTIE_CSV\n", $argv[0]);
	exit(1);
}

$path = $argv[1];
$dest = $argv[2];

if (!is_readable($path)) {
	die("Ne peut lire le répertoire\n");
}

$path = rtrim($path, '/');

$header = null;
$sum_header = null;
$out = [];
$i = 0;

if (is_file($path)) {
	$files = [$path];
}
else {
	$files = glob($path . '/*.pdf');
}

// Lecture du CSV
foreach ($files as $file) {
	printf("Lecture de %s…" . PHP_EOL, $file);

	$csv = shell_exec(sprintf('java -jar %s -g -l -p all %s', TABULA_PATH, escapeshellarg($file)));

	$csv = explode("\n", $csv);

	/*
		Date
		Date valeur
		Opération
		Débit EUROS
		Crédit EUROS
	 */
	foreach ($csv as $line) {
		$row = str_getcsv($line);

		if (count($row) < 2) {
			echo "Saut ligne vide\n";
			continue;
		}

		if (preg_match('!^Solde.*(?:AU\s+(\d+/\d+/\d+))!i', trim($row[0]), $match)) {
			if (null === $sum_header) {
				$row = [$match[1], null, $row[0], null, $row[2]];
				$out[$i++] = $row;
				$sum_header = $row;
				continue;
			}

			echo "Saut solde : ";
			echo implode(', ', $row);
			echo PHP_EOL;
			continue;
		}
		elseif (preg_match('!^(?:Solde|Total|Réf\s+:.*SOLDE)!i', trim($row[0]))) {
			echo "Saut solde : ";
			echo implode(', ', $row);
			echo PHP_EOL;
			continue;
		}

		if (null === $header) {
			echo "Saut entête\n";
			$header = $row;
			continue;
		}

		if ($header === $row) {
			echo "Saut répétition entête\n";
			continue;
		}

		if (count($row) !== count($header)) {
			echo "Ligne incohérente\n";
			var_dump($row); exit;
		}

		foreach ($row as &$cell) {
			$cell = preg_replace('/\s\s+/', ' ', $cell);
		}

		unset($cell);

		if (empty($row[0])) {
			$out[$i - 1][2] .= PHP_EOL . $row[2];
			continue;
		}

		$out[$i++] = $row;
	}
}

// Création du CSV de sortie
$fp = fopen($dest, 'w');

fputcsv($fp, ['Numéro d\'écriture', 'Date', 'Libellé', 'Compte de débit', 'Compte de crédit', 'Montant', 'Numéro pièce comptable', 'Référence paiement', 'Notes']);

foreach ($out as $line) {
	$label = $line[2];
	$notes = null;
	$ref = null;

	if (false !== ($pos = strpos($label, "\n"))) {
		$notes = trim(substr($label, $pos));
		$label = trim(substr($label, 0, $pos));
	}

	if (preg_match('/^VRST (REF.*)/', $label, $match)) {
		$label = 'Versement espèces';
		$ref = $match[1];
	}
	elseif (preg_match('/^REM CHQ (REF.*)/', $label, $match)) {
		$label = 'Remise de chèques';
		$ref = $match[1];
	}
	elseif (preg_match('/^FACTURE (SGT.*)/', $label, $match)) {
		$label = 'Frais bancaires Crédit Mutuel';
		$ref = $match[1];
	}
	elseif (preg_match('/^CHEQUE (\d+)/', $label, $match)) {
		$label = 'Chèque ' . $match[1];
		$ref = $match[1];
	}
	elseif (preg_match('/^VIR ROZO/', $label) && preg_match('/(REP\d+-\d+)/s', $notes, $match)) {
		$label = 'Virement Rozo';
		$ref = $match[1];
	}

	$amount = !empty($line[4]) ? $line[4] : '-' . $line[3];
	$amount = str_replace('.', '', $amount);

	fputcsv($fp, ['', $line[0], $label, '', '', $amount, '', $ref, $notes]);
}

fclose($fp);

Added tools/extract-paypal-csv.php version [13eeefbd37].







































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
 * Extracteur de données des relevés de compte Paypal
 * à destination de Garradin (ou autre logiciel de compta)
 *
 * https://garradin.eu/
 *
 * Ce script prend en argument un fichier CSV exporté de Paypal
 * https://business.paypal.com/merchantdata/reportHome?reportType=DLOG
 * et produit un import exploitable dans Garradin
 *
 * Copyright (C) 2020 BohwaZ - licence AGPLv3
 */

if (empty($argv[1]) || empty($argv[2])) {
	printf("Usage: %s FICHIER_PAYPAL_CSV FICHIER_SORTIE_CSV\n", $argv[0]);
	exit(1);
}

$path = $argv[1];
$dest = $argv[2];

if (!is_readable($path)) {
	die("Ne peut lire le répertoire\n");
}

$path = rtrim($path, '/');

$header = null;
$sum_header = null;
$out = [];
$i = 0;

if (!is_file($path)) {
	die("Le fichier Paypal est invalide\n");
}

// Lecture du CSV
printf("Lecture de %s…" . PHP_EOL, $path);

/*
	Date
	Heure
	Fuseau horaire
	Nom
	Type
	État
	Devise
	Avant commission
	Commission
	Net
	De l'adresse email
	À l'adresse email
	Numéro de transaction
	Adresse de livraison
	État de l'adresse
	Titre de l'objet
	Numéro de l'objet
	Montant des frais d'expédition et de traitement
	Montant de l'assurance
	TVA
	Nom de l'option 1
	Valeur de l'option 1
	Nom de l'option 2
	Valeur de l'option 2
	Numéro de la transaction de référence
	Numéro de facture
	Numéro de client
	Quantité
	Numéro de reçu
	Solde
	Adresse
	Adresse (suite)/District/Quartier
	Ville
	État/Province/Région/Comté/Territoire/Préfecture/République
	Code postal
	Pays
	Numéro de téléphone du contact
	Objet
	Remarque
	Indicatif pays
	Impact sur le solde
*/

$header = null;

$bom = "\xef\xbb\xbf";

// Read file from beginning.
$fp = fopen($path, 'r');

// Progress file pointer and get first 3 characters to compare to the BOM string.
if (fgets($fp, 4) !== $bom) {
    // BOM not found - rewind pointer to start of file.
    rewind($fp);
}

$l = 0;

static $required = ['Date', 'Nom', 'Type', 'Commission', 'Net', 'Objet', 'Remarque', 'Numéro de transaction'];

while (!feof($fp)) {
	$l++;
	$row = fgetcsv($fp);

	if (!$row) {
		break;
	}

	if (null === $header) {
		$header = $row;
		continue;
	}

	$c = (object) array_combine($header, $row);

	foreach ($required as $key) {
		if (!isset($c->$key)) {
			printf('Colonne "%s" manquante sur ligne %d' . PHP_EOL, $key, $l);
			continue(2);
		}
	}

	$out[] = $c;
}

fclose($fp);

// Création du CSV de sortie
$fp = fopen($dest, 'w');

fputcsv($fp, ['Numéro d\'écriture', 'Date', 'Libellé', 'Compte de débit', 'Compte de crédit', 'Montant', 'Numéro pièce comptable', 'Référence paiement', 'Notes']);

static $notes_keys = ['Nom', 'Objet', 'Remarque'];

foreach ($out as $c) {
	$label = $c->Type;

	if ($c->Nom) {
		$label = $c->Nom . ' - ' . $label;
	}

	$notes = '';
	$ref = $c->{'Numéro de transaction'};

	foreach ($notes_keys as $k) {
		if ($c->{$k}) {
			$notes .= $c->{$k} . "\n";
		}
	}

	$notes = trim($notes);

	if ($c->Commission != '0,00') {
		$amount = preg_replace('/\s+/U', '', $c->Commission);
		fputcsv($fp, ['', $c->Date, 'Commission PayPal sur transaction', '', '', $amount, '', $ref, $notes]);
	}

	$amount = preg_replace('/[\s ]+/U', '', $c->Net);
	fputcsv($fp, ['', $c->Date, $label, '', '', $amount, '', $ref, $notes]);
}

fclose($fp);