-
Notifications
You must be signed in to change notification settings - Fork 1
/
camt2csv.php
93 lines (69 loc) · 2.23 KB
/
camt2csv.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
<?php
require_once 'vendor/autoload.php';
/*
* camt2csv
*
* PHP script to convert camt.053 files to CSV files intended for Firefly III
*
* @link https://github.com/cstuder/camt2csv
*/
use Genkgo\Camt\Config;
use Genkgo\Camt\Reader;
// Configuration
$headers = [
'Date', // Value Date
'Description', // Additional Information / Remittance Information
'Asset Account', // Account IBAN
'Opposing Account', // Related party account IBAN or identification
'Amount', // Amount
'Foreign currency code', // Currency code
'Transaction booking date', // Booking Date
'External ID', // Account Servicer Reference
];
// Read command line arguments
if (!isset($argv[1])) {
echo "Usage: php camt2csv.php <input_file|input_directory>\n";
exit(1);
}
$input = realpath($argv[1]);
// Find files to convert
$files = [];
if (is_dir($input)) {
$files = glob($input . '/*.xml');
} else {
$files[] = $input;
}
// Convert files
$reader = new Reader(Config::getDefault());
foreach ($files as $file) {
echo "Converting {$file}\n";
$outputfilename = str_replace('.xml', '.csv', $file);
$output = fopen($outputfilename, 'w');
fputcsv($output, $headers);
$statements = $reader->readFile($file)->getRecords();
foreach ($statements as $statement) {
foreach ($statement->getEntries() as $entry) {
$record = [];
// Date
$record[] = $entry->getValueDate()->format('Y-m-d');
// Description
$record[] = $entry->getAdditionalInfo();
// Asset Account
$record[] = $statement->getAccount()->getIdentification();
// Opposing Account
$record[] = $entry->getTransactionDetail()->getRelatedParty()->getAccount()->getIdentification();
// Amount
$record[] = $entry->getAmount()->getAmount();
// Foreign currency code
$record[] = $entry->getAmount()->getCurrency();
// Transaction booking date
$record[] = $entry->getBookingDate()->format('Y-m-d');
// External ID
$record[] = $entry->getAccountServicerReference();
fputcsv($output, $record);
}
}
fclose($output);
}
// Done.
echo "Done.\n";