Implement virtualised accounts for budgeting
2 files changed, 29 insertions(+), 2 deletions(-)
M src/Command/ConvertCommand.php → src/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.php → src/Item/LedgerTransaction.php
@@ -8,4 +8,5 @@ public $state; public $payee; public $note; public $postings = []; + public $isVirtual = false; }