all repos — archive/ynab-ledger @ 7cbc1585aabee7ce0f2d42cf56ee8c1bef2e6601

Convert exported YNAB data to ledger-cli

Implement virtualised accounts for budgeting

Alan Pearce
commit

7cbc1585aabee7ce0f2d42cf56ee8c1bef2e6601

parent

20795d700243603c1dc7c378c82189469845e961

2 files changed, 29 insertions(+), 2 deletions(-)

jump to
M src/Command/ConvertCommand.phpsrc/Command/ConvertCommand.php
@@ -66,6 +66,15 @@ }
return $accounts[$name]; } +function createCategoryPosting (RegisterTransaction $txn) { + $category = new LedgerPosting; + $category->isVirtual = true; + $category->currency = $txn->currency; + $category->account = array_merge(['Funds'], $txn->category); + $category->amount = $txn->in - $txn->out; + return $category; +} + function toLedger (Generator $transactions) { $transactions->rewind(); $accounts = [];
@@ -82,11 +91,12 @@ $lTxn->note = $txn->memo;
if ($txn instanceof BudgetTransaction) { $startDate = $txn->date; + $lTxn->isVirtual = true; do { $posting = new LedgerPosting; $posting->currency = $txn->currency; $posting->amount = $txn->in - $txn->out; - $posting->account = array_merge(['Expenses'], $txn->category); + $posting->account = array_merge(['Funds'], $txn->category); if ($posting->amount !== 0.00) { $lTxn->postings[] = $posting; }
@@ -134,6 +144,8 @@ $lTxn->postings[] = $target;
$source = new LedgerPosting; $source->account = getAccount($txn->account, $txn); + $source->amount = $txn->in - $txn->out; + $source->currency = $txn->currency; $lTxn->postings[] = $source; $lTxn->payee = 'Transfer';
@@ -144,6 +156,7 @@ $lTxn->note = '';
$source = new LedgerPosting; $source->account = getAccount($txn->account, $txn); + $source->currency = $txn->currency; do { $target = new LedgerPosting;
@@ -156,8 +169,12 @@ $target->account = array_merge(['Expenses'], $txn->category);
} $target->currency = $txn->currency; $target->amount = $txn->out - $txn->in; + $source->amount += $txn->in - $txn->out; sscanf($txn->memo, "(Split %d/%d) %[^\r]", $i, $k, $target->note); $lTxn->postings[] = $target; + if ($target->account[0] == "Expenses") { + $lTxn->postings[] = createCategoryPosting($txn); + } $transactions->next(); $txn = $transactions->current();
@@ -182,7 +199,13 @@ $lTxn->postings[] = $target;
$source = new LedgerPosting; $source->account = getAccount($txn->account, $txn); + $source->amount = $txn->in - $txn->out; + $source->currenty = $txn->currency; $lTxn->postings[] = $source; + + if ($target->account[0] == "Expenses") { + $lTxn->postings[] = createCategoryPosting($txn); + } } yield $lTxn; next:
@@ -316,7 +339,10 @@ , $txn->payee
, !empty($txn->note) ? " ; $txn->note" : "" , PHP_EOL; foreach ($txn->postings as $posting) { - echo " " . implode(':', $posting->account); + echo " " + , $txn->isVirtual ? "[" : ($posting->isVirtual ? "(" : "") + , implode(':', $posting->account) + , $txn->isVirtual ? "]" : ($posting->isVirtual ? ")" : ""); if ($posting->currency !== null) { echo " {$fmt->formatCurrency($posting->amount, $posting->currency)}"; }
M src/Item/LedgerTransaction.phpsrc/Item/LedgerTransaction.php
@@ -8,4 +8,5 @@ public $state;
public $payee; public $note; public $postings = []; + public $isVirtual = false; }