Fix to make sure that the index respects the fiscal year.

This commit is contained in:
James Cole 2018-12-31 13:44:24 +01:00
parent f80de95bb0
commit aeab5f0333
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
2 changed files with 28 additions and 3 deletions

View File

@ -52,7 +52,7 @@ class FiscalHelper implements FiscalHelperInterface
*/ */
public function endOfFiscalYear(Carbon $date): Carbon 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); $endDate = $this->startOfFiscalYear($date);
if (true === $this->useCustomFiscalYear) { if (true === $this->useCustomFiscalYear) {
// add 1 year and sub 1 day // add 1 year and sub 1 day
@ -62,6 +62,7 @@ class FiscalHelper implements FiscalHelperInterface
if (false === $this->useCustomFiscalYear) { if (false === $this->useCustomFiscalYear) {
$endDate->endOfYear(); $endDate->endOfYear();
} }
Log::debug(sprintf('Result of endOfFiscalYear(%s) = %s', $date->format('Y-m-d'), $endDate->format('Y-m-d')));
return $endDate; return $endDate;
} }
@ -73,6 +74,12 @@ class FiscalHelper implements FiscalHelperInterface
*/ */
public function startOfFiscalYear(Carbon $date): Carbon 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. // get start mm-dd. Then create a start date in the year passed.
$startDate = clone $date; $startDate = clone $date;
if (true === $this->useCustomFiscalYear) { if (true === $this->useCustomFiscalYear) {
@ -89,6 +96,8 @@ class FiscalHelper implements FiscalHelperInterface
$startDate->startOfYear(); $startDate->startOfYear();
} }
Log::debug(sprintf('Result of startOfFiscalYear(%s) = %s', $date->format('Y-m-d'), $startDate->format('Y-m-d')));
return $startDate; return $startDate;
} }
} }

View File

@ -24,6 +24,7 @@ namespace FireflyIII\Support;
use Carbon\Carbon; use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\FiscalHelperInterface;
use Log; use Log;
/** /**
@ -583,7 +584,6 @@ class Navigation
'1W' => 'endOfWeek', '1W' => 'endOfWeek',
'1M' => 'endOfMonth', '1M' => 'endOfMonth',
'3M' => 'lastOfQuarter', '3M' => 'lastOfQuarter',
'1Y' => 'endOfYear',
'custom' => 'startOfMonth', // this only happens in test situations. 'custom' => 'startOfMonth', // this only happens in test situations.
]; ];
$end = clone $start; $end = clone $start;
@ -604,6 +604,15 @@ class Navigation
return $end; 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)); throw new FireflyException(sprintf('updateEndDate cannot handle range "%s"', $range));
} }
@ -622,7 +631,6 @@ class Navigation
'1W' => 'startOfWeek', '1W' => 'startOfWeek',
'1M' => 'startOfMonth', '1M' => 'startOfMonth',
'3M' => 'firstOfQuarter', '3M' => 'firstOfQuarter',
'1Y' => 'startOfYear',
'custom' => 'startOfMonth', // this only happens in test situations. 'custom' => 'startOfMonth', // this only happens in test situations.
]; ];
if (isset($functionMap[$range])) { if (isset($functionMap[$range])) {
@ -641,6 +649,14 @@ class Navigation
return $start; 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)); throw new FireflyException(sprintf('updateStartDate cannot handle range "%s"', $range));
} }
} }