diff --git a/app/Helpers/FiscalHelper.php b/app/Helpers/FiscalHelper.php index dbc1435db8..cb33d6b46e 100644 --- a/app/Helpers/FiscalHelper.php +++ b/app/Helpers/FiscalHelper.php @@ -52,7 +52,7 @@ class FiscalHelper implements FiscalHelperInterface */ public function endOfFiscalYear(Carbon $date): Carbon { - // get start of fiscal year for passed date + Log::debug(sprintf('Now in endOfFiscalYear(%s).', $date->format('Y-m-d'))); $endDate = $this->startOfFiscalYear($date); if (true === $this->useCustomFiscalYear) { // add 1 year and sub 1 day @@ -62,6 +62,7 @@ class FiscalHelper implements FiscalHelperInterface if (false === $this->useCustomFiscalYear) { $endDate->endOfYear(); } + Log::debug(sprintf('Result of endOfFiscalYear(%s) = %s', $date->format('Y-m-d'), $endDate->format('Y-m-d'))); return $endDate; } @@ -73,6 +74,12 @@ class FiscalHelper implements FiscalHelperInterface */ public function startOfFiscalYear(Carbon $date): Carbon { + // because PHP is stupid: + if ($date->day >= 28) { + $date->day = 28; + Log::info(sprintf('Corrected date to %s', $date->format('Y-m-d'))); + } + // get start mm-dd. Then create a start date in the year passed. $startDate = clone $date; if (true === $this->useCustomFiscalYear) { @@ -89,6 +96,8 @@ class FiscalHelper implements FiscalHelperInterface $startDate->startOfYear(); } + Log::debug(sprintf('Result of startOfFiscalYear(%s) = %s', $date->format('Y-m-d'), $startDate->format('Y-m-d'))); + return $startDate; } } diff --git a/app/Support/Navigation.php b/app/Support/Navigation.php index bef0a248bf..2db02584f0 100644 --- a/app/Support/Navigation.php +++ b/app/Support/Navigation.php @@ -24,6 +24,7 @@ namespace FireflyIII\Support; use Carbon\Carbon; use FireflyIII\Exceptions\FireflyException; +use FireflyIII\Helpers\FiscalHelperInterface; use Log; /** @@ -583,7 +584,6 @@ class Navigation '1W' => 'endOfWeek', '1M' => 'endOfMonth', '3M' => 'lastOfQuarter', - '1Y' => 'endOfYear', 'custom' => 'startOfMonth', // this only happens in test situations. ]; $end = clone $start; @@ -604,6 +604,15 @@ class Navigation return $end; } + + // make sure 1Y takes the fiscal year into account. + if ('1Y' === $range) { + /** @var FiscalHelperInterface $fiscalHelper */ + $fiscalHelper = app(FiscalHelperInterface::class); + return $fiscalHelper->endOfFiscalYear($end); + } + + throw new FireflyException(sprintf('updateEndDate cannot handle range "%s"', $range)); } @@ -622,7 +631,6 @@ class Navigation '1W' => 'startOfWeek', '1M' => 'startOfMonth', '3M' => 'firstOfQuarter', - '1Y' => 'startOfYear', 'custom' => 'startOfMonth', // this only happens in test situations. ]; if (isset($functionMap[$range])) { @@ -641,6 +649,14 @@ class Navigation return $start; } + + // make sure 1Y takes the fiscal year into account. + if ('1Y' === $range) { + /** @var FiscalHelperInterface $fiscalHelper */ + $fiscalHelper = app(FiscalHelperInterface::class); + return $fiscalHelper->startOfFiscalYear($start); + } + throw new FireflyException(sprintf('updateStartDate cannot handle range "%s"', $range)); } }