about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2015-04-06 11:10:32 +0100
committerAlan Pearce2015-04-06 11:10:32 +0100
commit5b2846ac67f99d1cc8bc5cd5bec705014196d3c7 (patch)
tree347fc48ffb4080f66fec6074a268c6ae3ae194eb
parent2d371fdd6b9d805e7d7b35f53c3c626fa1408740 (diff)
downloadynab-ledger-5b2846ac67f99d1cc8bc5cd5bec705014196d3c7.tar.lz
ynab-ledger-5b2846ac67f99d1cc8bc5cd5bec705014196d3c7.tar.zst
ynab-ledger-5b2846ac67f99d1cc8bc5cd5bec705014196d3c7.zip
Fix handling of first month’s budget
-rw-r--r--src/Command/ConvertCommand.php31
1 files changed, 18 insertions, 13 deletions
diff --git a/src/Command/ConvertCommand.php b/src/Command/ConvertCommand.php
index 21f0a03..2d87015 100644
--- a/src/Command/ConvertCommand.php
+++ b/src/Command/ConvertCommand.php
@@ -251,22 +251,27 @@ function multiRead (SplFileObject $budgetFile, SplFileObject $registerFile, Numb
     $budget = readBudget($budgetFile);
     $register = readRegister($registerFile, $fmt);
     $budget->rewind();
+    $openDate;
     foreach ($register as $rTxn) {
-        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 = $rTxn->date;
-                        yield $bTxn;
-
-                        $budget->next();
-                        $bTxn = $budget->current();
-                    } while ($bTxn->date == $bDate);
+        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;
     }