about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2015-04-07 15:59:53 +0100
committerAlan Pearce2015-04-07 16:38:29 +0100
commit7cbc1585aabee7ce0f2d42cf56ee8c1bef2e6601 (patch)
treeee4a065eb2b8b0c74fb20cbfa17b0b4244485eca
parent20795d700243603c1dc7c378c82189469845e961 (diff)
downloadynab-ledger-7cbc1585aabee7ce0f2d42cf56ee8c1bef2e6601.tar.lz
ynab-ledger-7cbc1585aabee7ce0f2d42cf56ee8c1bef2e6601.tar.zst
ynab-ledger-7cbc1585aabee7ce0f2d42cf56ee8c1bef2e6601.zip
Implement virtualised accounts for budgeting
-rw-r--r--src/Command/ConvertCommand.php30
-rw-r--r--src/Item/LedgerTransaction.php1
2 files changed, 29 insertions, 2 deletions
diff --git a/src/Command/ConvertCommand.php b/src/Command/ConvertCommand.php
index c365a15..1b379d0 100644
--- a/src/Command/ConvertCommand.php
+++ b/src/Command/ConvertCommand.php
@@ -66,6 +66,15 @@ function getAccount ($name, $txn) {
     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 @@ function toLedger (Generator $transactions) {
 
             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 @@ function toLedger (Generator $transactions) {
 
                 $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 @@ function toLedger (Generator $transactions) {
 
                 $source = new LedgerPosting;
                 $source->account = getAccount($txn->account, $txn);
+                $source->currency = $txn->currency;
 
                 do {
                     $target = new LedgerPosting;
@@ -156,8 +169,12 @@ function toLedger (Generator $transactions) {
                     }
                     $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 @@ function toLedger (Generator $transactions) {
 
                 $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 @@ class ConvertCommand extends Command {
                 , !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)}";
                 }
diff --git a/src/Item/LedgerTransaction.php b/src/Item/LedgerTransaction.php
index ebe26e8..008c5a1 100644
--- a/src/Item/LedgerTransaction.php
+++ b/src/Item/LedgerTransaction.php
@@ -8,4 +8,5 @@ class LedgerTransaction {
     public $payee;
     public $note;
     public $postings = [];
+    public $isVirtual = false;
 }