From eb31934fb79c56d4d259794e7a29bba0be22f772 Mon Sep 17 00:00:00 2001 From: Graham Miller Date: Wed, 27 Jan 2016 11:54:04 +1000 Subject: [PATCH] Add a new helper to handle fiscal issues. Used initially to provide fiscal year support to the reports. --- app/Helpers/FiscalHelper.php | 78 ++++++++++++++++++++++++ app/Helpers/FiscalHelperInterface.php | 35 +++++++++++ app/Providers/FireflyServiceProvider.php | 1 + 3 files changed, 114 insertions(+) create mode 100644 app/Helpers/FiscalHelper.php create mode 100644 app/Helpers/FiscalHelperInterface.php diff --git a/app/Helpers/FiscalHelper.php b/app/Helpers/FiscalHelper.php new file mode 100644 index 0000000000..1d33aa5d8f --- /dev/null +++ b/app/Helpers/FiscalHelper.php @@ -0,0 +1,78 @@ +data) { + $this->useCustomFiscalYear = true; + } else { + $this->useCustomFiscalYear = false; + } + } + + /** + * @param Carbon $date + * + * @return Carbon date object + */ + public function startOfFiscalYear(Carbon $date) + { + // get start mm-dd. Then create a start date in the year passed. + $startDate = clone $date; + if ($this->useCustomFiscalYear === true) { + $prefStartStr = Preferences::get('fiscalYearStart', '01-01')->data; + list($mth, $day) = explode('-', $prefStartStr); + $startDate->month(intval($mth))->day(intval($day)); + + // if start date is after passed date, sub 1 year. + if ($startDate > $date) { + $startDate->subYear(); + } + } else { + $startDate->startOfYear(); + } + return $startDate; + } + + /** + * @param Carbon $date + * + * @return Carbon date object + */ + public function endOfFiscalYear(Carbon $date) + { + // get start of fiscal year for passed date + $endDate = $this->startOfFiscalYear($date); + if ($this->useCustomFiscalYear === true) { + // add 1 year and sub 1 day + $endDate->addYear(); + $endDate->subDay(); + } else { + $endDate->endOfYear(); + } + + + return $endDate; + } +} diff --git a/app/Helpers/FiscalHelperInterface.php b/app/Helpers/FiscalHelperInterface.php new file mode 100644 index 0000000000..55fc8b7d5e --- /dev/null +++ b/app/Helpers/FiscalHelperInterface.php @@ -0,0 +1,35 @@ +app->bind('FireflyIII\Helpers\Help\HelpInterface', 'FireflyIII\Helpers\Help\Help'); $this->app->bind('FireflyIII\Helpers\Report\ReportHelperInterface', 'FireflyIII\Helpers\Report\ReportHelper'); $this->app->bind('FireflyIII\Helpers\Report\ReportQueryInterface', 'FireflyIII\Helpers\Report\ReportQuery'); + $this->app->bind('FireflyIII\Helpers\FiscalHelperInterface', 'FireflyIII\Helpers\FiscalHelper'); }