-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrb2fb.php
executable file
·340 lines (290 loc) · 14 KB
/
rb2fb.php
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/php
<?php
/*
Tool for converting Raiffeisenbank eKonto payments to FlexiBee XML
Author: Maxim Krušina, [email protected], Massimo Filippi, s.r.o.
Homepage: https://github.com/massimo-filippi/flexibee-tools
Notes:
Source encoding: Windows 1250
Destinantion encoding: UTF-8
Other resources:
http://www.abclinuxu.cz/blog/doli/2012/4/konverze-bankovnich-vypisu-rb-do-formatu-abo
ToDo:
- Add UI - ie https://www.sveinbjorn.org/platypus
- Remove first empty line in output file
Usage:
======
- Set all parameters in section bellow (User specific settings)
- Export file from RB / eKonto via: Historie účtu / Pohyby na účtu / Formát CSV (button under the listing)
- Rename CSV file to import.csv and put it into same solder as this script
- Run: ./rb2fb.php > output.xml
- In FlexiBee
-- First setup
--- Menu: Peníze / Seznam bankovních účtů, vybrat účet, Změnit (tlačítko)
--- Elektronická banka (záložka)
---- Formát: Gemnini XML
---- Název klienta: RB2FB (třeba)
---- výpisy: nastavit adresář, kam budeme dávat výpisy (generované tímto konvertorem)
---- přípona: xml
---- Kontrolovat duplicitu výpisů: Ignorovat již načtené položky
---- Uložit a zavřít (tlačítko)
-- Import
--- Menu: Peníze / Banka
--- Button: Služby / Načíst výpisy
*/
// User specific settings
$BankCode = "5500";
$BankCountryID = "CZ";
$BankName = "Raiffeisenbank a. s.";
$AccNoID = "1234567890"; // Number of your bank account
$AccNoCC = "";
$AccName = "Name of account"; // Name of the account
$AccCcy = "CZK"; // Currency code (ie CZK)
$AccCcyText = "Koruna česká"; // Name of currency (ie Koruna česká)
$ChargesCcy = "CZK"; // DOnt remebmer :) (ie CZK)
// Include values from config file, if exists
if(file_exists('config.php'))
include 'config.php';
// Options parsing
$shortopts = "";
$shortopts .= "d"; // Display output
$shortopts .= "h"; // Help, do not accept values
$shortopts .= "i:"; // Input file, value required
$shortopts .= "o:"; // Output file, value required
$shortopts .= "x"; // Disable writing to output file
$options = getopt($shortopts);
// var_dump($options);
// Write Help
if (array_key_exists("h", $options)) {
echo "Use ./" . basename(__FILE__) . " -h for help\n\n";
echo "rb2fb\n";
echo "=====\n";
echo "\n";
echo "This tool will convert Raiffeisenbank eKonto payments (exportable as CVS) to FlexiBee XML file format, which can be imported into XML.\n";
echo "Without this tool, you're forced to install and use Raiffeisenbank's tool eKomunikátor, which is both pricey and very cumbersome.\n";
echo "Homepage: https://github.com/massimo-filippi/flexibee-tools\n";
echo "More info about FlexiBee: https://www.flexibee.eu/\n";
echo "\n";
echo "Usage\n";
echo "-----\n";
echo "./rb2fb.php [-options] [-i file] [-o file]\n";
echo "-d send output to stdout instead of file (if used, there will be no sucess info in output)\n";
echo "-h show this help\n";
echo "-i specify input file, CSV file downloaded from eKonka. Default = import.csv\n";
echo "-o specify output file, XML suitable for importing into FlexiBee. Default = output.xml\n";
echo "-x disable writing to output file\n";
echo "\n";
exit();
}
// Deine constants (CSV offsets)
const DATUM_PROVEDENI = 0; // Dříve DATUM
const DATUM_ZAUCTOVANI = 1;
const CISLO_UCTU = 2; // Číslo našeho účtu
const NAZEV_UCTU = 3;
const KATEGORIE_TRANSAKCE = 4; // Dříve nebylo
const CISLO_PROTIUCTU = 5; // Dříve CISLO_UCTU
const NAZEV_PRPOTIUCTU = 6; // Dříve NAZEV_UCTU
const TYP_TRANSAKCE = 7; // Dříve TYP
const ZPRAVA = 8; // Dříve poznámka
const POZNAMKA = 9; // Vypada v CSV identicky s polem ZPRAVA
const VARIABILNI_SYMBOL = 10;
const KONSTANTNI_SYMBOL = 11;
const SPECIFICKY_SYMBOL = 12;
const ZAUCTOVANA_CASTKA = 13; // Dříve CASTKA
const MENA_UCTU = 14;
const PUVODNI_CASTKA_A_MENA_1 = 15;
const PUVODNI_CASTKA_A_MENA_2 = 16;
const POPLATEK = 17;
const ID_TRANSAKCE = 18; // Dříve KOD_TRANSAKCE
/* Old values = eKonto blue
const DATUM = 0;
const CAS = 1;
const POZNAMKA = 2;
const NAZEV_UCTU = 3;
const CISLO_UCTU = 4;
const DATUM_ODEPSANI = 5;
const VALUTA = 6;
const TYP = 7;
const KOD_TRANSAKCE = 8;
const VARIABILNI_SYMBOL = 9;
const KONSTANTNI_SYMBOL = 10;
const SPECIFICKY_SYMBOL = 11;
const CASTKA = 12;
const POPLATEK = 13;
const SMENA = 14;
const ZPRAVA = 15;
*/
// Convert Win 1250 > UTF-8
function w1250_to_utf8($text) {
// map based on:
// http://konfiguracja.c0.pl/iso02vscp1250en.html
// http://konfiguracja.c0.pl/webpl/index_en.html#examp
// http://www.htmlentities.com/html/entities/
$map = array(
chr(0x8A) => chr(0xA9),
chr(0x8C) => chr(0xA6),
chr(0x8D) => chr(0xAB),
chr(0x8E) => chr(0xAE),
chr(0x8F) => chr(0xAC),
chr(0x9C) => chr(0xB6),
chr(0x9D) => chr(0xBB),
chr(0xA1) => chr(0xB7),
chr(0xA5) => chr(0xA1),
chr(0xBC) => chr(0xA5),
chr(0x9F) => chr(0xBC),
chr(0xB9) => chr(0xB1),
chr(0x9A) => chr(0xB9),
chr(0xBE) => chr(0xB5),
chr(0x9E) => chr(0xBE),
chr(0x80) => '€',
chr(0x82) => '‚',
chr(0x84) => '„',
chr(0x85) => '…',
chr(0x86) => '†',
chr(0x87) => '‡',
chr(0x89) => '‰',
chr(0x8B) => '‹',
chr(0x91) => '‘',
chr(0x92) => '’',
chr(0x93) => '“',
chr(0x94) => '”',
chr(0x95) => '•',
chr(0x96) => '–',
chr(0x97) => '—',
chr(0x99) => '™',
chr(0x9B) => '’',
chr(0xA6) => '¦',
chr(0xA9) => '©',
chr(0xAB) => '«',
chr(0xAE) => '®',
chr(0xB1) => '±',
chr(0xB5) => 'µ',
chr(0xB6) => '¶',
chr(0xB7) => '·',
chr(0xBB) => '»',
);
return htmlspecialchars(html_entity_decode(mb_convert_encoding(strtr($text, $map), 'UTF-8', 'ISO-8859-2'), ENT_QUOTES, 'UTF-8'));
}
// Main code
// Set input file
if (array_key_exists("i", $options)) {
// Use file specifiled in command parameter -i
$filename_input = $options["i"];
} else {
// Use default filename
$filename_input = "import.csv";
}
// Set output file
if (array_key_exists("o", $options)) {
// Use file specifiled in command parameter -o
$filename_output = $options["o"];
} else {
// Use default filename
$filename_output = "output.xml";
}
if (($handle = @fopen($filename_input, "r")) == FALSE) {
// Handle file open error
echo "Error, cannot open input file: " . $filename_input . "\n";
echo "Use ./" . basename(__FILE__) . " -h for help\n\n";
exit();
} else {
$out = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$out .= "<!-- This file stores exported account movements from Gemini/CS 5 application. -->\n";
$out .= "<AccountMovements version='1.0'\n";
$out .= " xmlns='urn:schemas-bscpraha-cz:gemini5:export:movements'\n";
$out .= " Official='N'\n";
// $out .= " StatemDebitTotal='36 104,15'\n";
// $out .= " StatemCreditTotal='38 345,00'\n";
// $out .= " StatemTransactionCount='13'\n";
// $out .= " StatemDebitCount='10'\n";
// $out .= " StatemCreditCount='3'\n";
$out .= ">";
// skip the first line of csv (header row)
$data = fgetcsv($handle, 1000, ";");
for ($row = 1; ($data = fgetcsv($handle, 1000, ";")) !== FALSE; $row++) {
// Convert Win1250 to UTF 8
foreach ($data as &$text) {
$text = w1250_to_utf8($text);
}
// Date string refarmating
$date = date_parse_from_format("j.n.Y",$data[DATUM_PROVEDENI]);
$date_formated = $date['year'].sprintf("%02d", $date['month']).sprintf("%02d", $date['day']);
$date_valute = date_parse_from_format("j.n.Y",$data[DATUM_PROVEDENI]);
$date_valute_formated = $date_valute['year'].sprintf("%02d", $date_valute['month']).sprintf("%02d", $date_valute['day']);
// XML construction loop
$out .= " <Movement ItemNo='".$data[ID_TRANSAKCE]."'\n";
$out .= " Amount='".ltrim($data[ZAUCTOVANA_CASTKA],"-")."'\n"; // Strip minus sign
$out .= " Direction='" . (substr($data[ZAUCTOVANA_CASTKA],0,1) == "-" ? "D" : "C") . "'\n"; // D = Debet C = Credit
$out .= " PostingDate='".$date_formated."'>\n"; // Date formated as: 20170217
$out .= " <PartnerAccNo>".strstr($data[CISLO_PROTIUCTU],"/",true)."</PartnerAccNo>\n"; // Parse part before slash: 190842040287
$out .= " <PartnerAccBank>".substr($data[CISLO_PROTIUCTU],strpos($data[CISLO_PROTIUCTU], "/")+1)."</PartnerAccBank>\n"; // Parse part after slash: 0100
$out .= " <PartnerAccName>".$data[NAZEV_PRPOTIUCTU]."</PartnerAccName>\n";
$out .= " <ValueDate>".$date_valute_formated."</ValueDate>\n";
// $out .= " <PartnerValueDate></PartnerValueDate>\n";
// $out .= " <PayAmount>9,99</PayAmount>\n";
// $out .= " <ExcRate></ExcRate>\n";
$out .= " <ChargesAmount>".ltrim($data[POPLATEK],"-")."</ChargesAmount>\n";
$out .= " <ChargesCcy>".$ChargesCcy."</ChargesCcy>\n";
// $out .= " <CancelIndicator>0</CancelIndicator>\n";
// $out .= " <GeminiRef></GeminiRef>\n";
$out .= " <BankRef>".$data[ID_TRANSAKCE]."</BankRef>\n";
// $out .= " <ClientRef></ClientRef>\n";
$out .= " <MovementTypeText>".$data[TYP_TRANSAKCE] ."</MovementTypeText>\n";
$out .= " <BankCode>". substr($data[CISLO_UCTU],strpos($data[CISLO_UCTU], "/")+1) ."</BankCode>\n"; // Parse part after slash: 0100
$out .= " <BankCountryID>". $BankCountryID ."</BankCountryID>\n";
$out .= " <BankName>". $BankName ."</BankName>\n";
$out .= " <AccNoID>". strstr($data[CISLO_UCTU],"/",true) ."</AccNoID>\n"; // Parse part before slash: 190842040287
$out .= " <AccNoCC>". $AccNoCC ."</AccNoCC>\n";
$out .= " <AccName>". $data[NAZEV_UCTU] ."</AccName>\n";
$out .= " <AccCcy>". $data[MENA_UCTU] ."</AccCcy>\n";
$out .= " <AccCcyText>". $AccCcyText ."</AccCcyText>\n";
// $out .= " <AccTypeID>1</AccTypeID>\n";
// $out .= " <AccTypeCode>001</AccTypeCode>\n";
// $out .= " <AccTypeText>Běžný účet</AccTypeText>\n";
$out .= " <Statistics1>". sprintf("%010d", $data[KONSTANTNI_SYMBOL]) ."</Statistics1>\n"; // konstatní symbol, 10 znaků
$out .= " <Statistics2>". sprintf("%010d", $data[VARIABILNI_SYMBOL]) ."</Statistics2>\n"; // variabilní symbol, 10 znaků
$out .= " <Statistics3>". sprintf("%010d", $data[SPECIFICKY_SYMBOL]) ."</Statistics3>\n"; // specifický symbol, 10 znaků
$out .= " <Statistics4>". "</Statistics4>\n"; // platební titul, 10 znaků
$out .= " <Description1>". $data[POZNAMKA] ."</Description1>\n";
// $out .= " <Description2></Description2>\n";
// $out .= " <Description3></Description3>\n";
// $out .= " <Description4></Description4>\n";
// $out .= " <Description5></Description5>\n";
// $out .= " <Description6></Description6>\n";
// $out .= " <Description7></Description7>\n";
// $out .= " <Description8></Description8>\n";
$out .= " </Movement>\n";
}
$out .= "</AccountMovements>\n";
// Close input file
fclose($handle);
// Set output file (only when -x is not used)
if (!array_key_exists("x", $options)) {
if (array_key_exists("o", $options)) {
// Use filename specifiled in command parameter -o
$filename_output = $options["o"];
} else {
// Use default filename
$filename_output = "output.xml";
}
// Handle file output
if (@file_put_contents($filename_output,$out)) {
// File written succesfully
if (!array_key_exists("d", $options)) {
// Write success info, but only when -d is not used
echo "Succesfully converted into file: " . $filename_output . "\n\n";
}
} else {
// File write failed
echo "Error, cannot write to output file: " . $filename_input . "\n";
echo "Use ./" . basename(__FILE__) . " -d for help\n\n";
exit();
}
}
// Handle display output
if (array_key_exists("d", $options)) {
// Display output IS turned on
echo $out;
}
}
?>