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
|
<?php
namespace YnabLedger\Command;
use Generator;
use SplFileObject;
use LimitIterator;
use SeekableIterator;
use ArrayIterator;
use DateTimeImmutable;
use NumberFormatter;
use UnexpectedValueException;
use CLIFramework\Command;
use YnabLedger\Item\RegisterTransaction;
use YnabLedger\Item\BudgetTransaction;
use YnabLedger\Item\LedgerTransaction;
use YnabLedger\Item\LedgerPosting;
function convertDate ($currencySymbol) {
$dateFormat;
return function ($dateString) use ($currencySymbol, &$dateFormat) {
if ($dateFormat === null) {
if (is_numeric(substr($dateString, -4))) {
if (ord($currencySymbol) == ord('£')) {
$dateFormat = 'd/m/Y';
} else {
$dateFormat = 'm/d/Y';
}
} else {
$dateFormat = false;
}
}
if ($dateFormat === false) {
return new DateTimeImmutable($dateString);
} else {
return DateTimeImmutable::createFromFormat(
$dateFormat,
$dateString
);
}
};
}
function convertCleared ($val) {
switch ($val) {
case 'U': case null:
return '';
case 'R': case 'C':
return '*';
default:
throw new UnexpectedValueException(
sprintf("Found a cleared flag with value '%s', but I don't know what it means", $val)
);
}
}
function getAccount ($name, $txn) {
static $accounts = [];
if (!isset($accounts[$name])) {
if ($txn->in >= 0.01 || stripos($name, 'credit') === false) {
$accounts[$name] = ['Assets', $name];
} else {
$accounts[$name] = ['Liabilities', $name ?: end($txn->category)];
}
}
return $accounts[$name];
}
function toLedger (Generator $transactions) {
$transactions->rewind();
$accounts = [];
while ($transactions->valid()) {
$inc = true;
try {
$txn = $transactions->current();
$lTxn = new LedgerTransaction;
$lTxn->date = $txn->date;
$lTxn->state = convertCleared($txn->cleared);
$lTxn->payee = $txn->payee;
$lTxn->note = $txn->memo;
if ($txn instanceof BudgetTransaction) {
$startDate = $txn->date;
do {
$posting = new LedgerPosting;
$posting->currency = $txn->currency;
$posting->amount = $txn->in - $txn->out;
$posting->account = array_merge(['Expenses'], $txn->category);
if ($posting->amount !== 0.00) {
$lTxn->postings[] = $posting;
}
$transactions->next();
$txn = $transactions->current();
$inc = false;
} while (
$txn instanceof BudgetTransaction &&
$txn->date == $startDate
);
$posting = new LedgerPosting;
$posting->account = ['Assets'];
$lTxn->postings[] = $posting;
} elseif ($txn->payee == 'Starting Balance') {
$startDate = $txn->date;
do {
$posting = new LedgerPosting;
$posting->currency = $txn->currency;
$posting->account = getAccount($txn->account, $txn);
$posting->amount = $txn->in - $txn->out;
$lTxn->postings[] = $posting;
$transactions->next();
$txn = $transactions->current();
$inc = false;
} while (
$txn->payee == 'Starting Balance' &&
$txn->date == $startDate
);
$posting = new LedgerPosting;
$posting->account = ['Equity', 'Opening Balances'];
$lTxn->postings[] = $posting;
} elseif (substr($txn->payee, 0, 8) === 'Transfer') {
if ($txn->out <= 0.00) {
goto next;
}
$target = new LedgerPosting;
$target->account = getAccount(explode(' : ', $txn->payee)[1], $txn);
$target->currency = $txn->currency;
$target->amount = $txn->out - $txn->in;
$lTxn->postings[] = $target;
$source = new LedgerPosting;
$source->account = getAccount($txn->account, $txn);
$lTxn->postings[] = $source;
$lTxn->payee = 'Transfer';
} elseif (substr($lTxn->note, 0, 6) == '(Split') {
$payee = $txn->payee;
$startDate = $txn->date;
$lTxn->note = '';
$source = new LedgerPosting;
$source->account = getAccount($txn->account, $txn);
do {
$target = new LedgerPosting;
if (empty($txn->category)) {
$target->account = getAccount(explode(' : ', explode(' / ', $txn->payee, 2)[1])[1], $txn);
} elseif ($txn->category[0] == 'Income') {
$target->account = $txn->category;
} else {
$target->account = array_merge(['Expenses'], $txn->category);
}
$target->currency = $txn->currency;
$target->amount = $txn->out - $txn->in;
sscanf($txn->memo, "(Split %d/%d) %[^\r]", $i, $k, $target->note);
$lTxn->postings[] = $target;
$transactions->next();
$txn = $transactions->current();
$inc = false;
} while (
$txn->date == $startDate &&
$txn->payee === $payee &&
substr($txn->memo, 0, 6) == '(Split'
);
$lTxn->postings[] = $source;
} else {
$target = new LedgerPosting;
if ($txn->category[0] == 'Income') {
$target->account = $txn->category;
} else {
$target->account = array_merge(['Expenses'], $txn->category);
}
$target->currency = $txn->currency;
$target->amount = $txn->out - $txn->in;
$lTxn->postings[] = $target;
$source = new LedgerPosting;
$source->account = getAccount($txn->account, $txn);
$lTxn->postings[] = $source;
}
yield $lTxn;
next:
if ($inc) {
$transactions->next();
}
if (!$transactions->valid()) {
return;
}
} catch (UnexpectedValueException $e) {
var_dump($txn);
throw $e;
}
}
}
function readBudget (SplFileObject $budgetFile) {
$budgetFile->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE);
$file = new LimitIterator($budgetFile, 1);
$fmt = new NumberFormatter($locale, NumberFormatter::CURRENCY);
foreach ($file as $row) {
$txn = new BudgetTransaction;
$txn->date = DateTimeImmutable::createFromFormat('d F Y', '1 ' . $row[0])->setTime(0, 0, 0);
$txn->category = explode(':', $row[1], 2);
$txn->in = $fmt->parseCurrency($row[4], $txn->currency);
$txn->out = $fmt->parseCurrency($row[5], $txn->currency);
$txn->balance = $row[6];
// if (!in_array($row[2], $txn->category, true)) {
// array_unshift($txn->category, $row[2]);
// }
yield $txn;
}
}
function readRegister (SplFileObject $registerFile, NumberFormatter $fmt) {
$registerFile->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE);
$iterator = new LimitIterator($registerFile, 1);
$iterator->rewind();
$convertDate = convertDate($iterator->current()[9]);
$txns = [];
foreach ($iterator as $i => $row) {
// "Account","Flag","Check Number","Date","Payee","Category","Master Category","Sub Category","Memo","Outflow","Inflow","Cleared","Running Balance"
$txn = new RegisterTransaction();
$txn->account = $row[0];
$txn->date = $convertDate($row[3])->setTime(0, 0, 0);
$txn->payee = $row[4];
$txn->category = array_filter(array_map('trim', explode(':', $row[5])));
// if (!in_array($row[6], $txn->category, true)) {
// array_unshift($txn->category, $row[6]);
// }
$txn->memo = trim($row[8]);
$txn->out = $fmt->parseCurrency($row[9], $txn->currency);
$txn->in = $fmt->parseCurrency($row[10], $txn->currency);
$txn->cleared = $row[11];
$txn->line = $i;
$txns[] = $txn;
}
// Sort by date, promote transfers, group splits, promote income and fall back on line in file
usort($txns,
function ($a, $b) use ($convertDate) {
return strcmp($a->date->format('U'), $b->date->format('U'))
?: ($a->out < 0.01 ? -1
: (strpos($b->payee, 'Transfer : ') !== false ? 1
: ((in_array('Split', [substr($a->memo, 1, 5), substr($b->memo, 1, 5)]) ?
strcmp($a->memo, $b->memo)
: $a->line - $b->line))));
}
);
foreach ($txns as $txn) yield $txn;
}
function multiRead (SplFileObject $budgetFile, SplFileObject $registerFile, NumberFormatter $fmt) {
$budget = readBudget($budgetFile);
$register = readRegister($registerFile, $fmt);
$budget->rewind();
$openDate;
foreach ($register as $rTxn) {
if ($openDate !== null) {
if ($rTxn->payee !== 'Starting Balance' &&
$rTxn->category[1] !== 'Available this month') {
if ($budget->valid()) {
$bTxn = $budget->current();
$bDate = $bTxn->date;
if ($bTxn->date < $rTxn->date) {
do {
$bTxn->date = $openDate;
yield $bTxn;
$budget->next();
$bTxn = $budget->current();
} while ($bTxn->date == $bDate);
}
}
}
} elseif ($rTxn->payee === 'Starting Balance') {
$openDate = $rTxn->date;
}
yield $rTxn;
}
}
class ConvertCommand extends Command {
public function brief () {
return 'Convert budget & register exports';
}
public function arguments ($args) {
$args->add('budget')
->desc('Budget export file')
->isa('file')
->glob('*-Budget.csv');
$args->add('register')
->desc('Register export file')
->isa('file')
->glob('*-Register.csv');
}
public function execute ($budgetFile, $registerFile) {
$fmt = new NumberFormatter('en_GB', NumberFormatter::CURRENCY);
$export = toLedger(multiRead(new SplFileObject($budgetFile), new SplFileObject($registerFile), $fmt));
foreach ($export as $txn) {
echo "{$txn->date->format('Y-m-d')} "
, $txn->state ? "$txn->state " : ""
, $txn->payee
, !empty($txn->note) ? " ; $txn->note" : ""
, PHP_EOL;
foreach ($txn->postings as $posting) {
echo " " . implode(':', $posting->account);
if ($posting->currency !== null) {
echo " {$fmt->formatCurrency($posting->amount, $posting->currency)}";
}
echo !empty($posting->note) ? " ; $posting->note" : "", PHP_EOL;
}
echo PHP_EOL;
}
}
}
|