From d9b3ebc82f6c3898d49968851799e39e47d22bb7 Mon Sep 17 00:00:00 2001 From: Graham Miller Date: Fri, 22 Jan 2016 21:09:02 +1000 Subject: [PATCH 1/4] Add preferences option to enable or disable the custom fiscal year handling. Stored in DB as 0 or 1 and converted:- - twig expression in view (expandedform needs true boolean) - checkbox true/false converted to integer in set function in controller Oh and I worked out how to localize the label ... took me a while but ended up so simple. This is stage 1 of the overall custom fiscal year extension for Firefly. --- app/Http/Controllers/PreferencesController.php | 7 ++++++- resources/lang/en_US/firefly.php | 3 +++ resources/views/preferences/index.twig | 12 ++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/PreferencesController.php b/app/Http/Controllers/PreferencesController.php index 9cc7d3e861..641a817fe0 100644 --- a/app/Http/Controllers/PreferencesController.php +++ b/app/Http/Controllers/PreferencesController.php @@ -39,10 +39,11 @@ class PreferencesController extends Controller $budgetMax = Preferences::get('budgetMaximum', 1000); $language = Preferences::get('language', env('DEFAULT_LANGUAGE', 'en_US'))->data; $budgetMaximum = $budgetMax->data; + $customFiscalYear = Preferences::get('customFiscalYear', 0)->data; $showIncomplete = env('SHOW_INCOMPLETE_TRANSLATIONS', 'false') == 'true'; - return view('preferences.index', compact('budgetMaximum', 'language', 'accounts', 'frontPageAccounts', 'viewRange', 'showIncomplete')); + return view('preferences.index', compact('budgetMaximum', 'language', 'accounts', 'frontPageAccounts', 'viewRange', 'customFiscalYear', 'showIncomplete')); } /** @@ -70,6 +71,10 @@ class PreferencesController extends Controller $budgetMaximum = intval(Input::get('budgetMaximum')); Preferences::set('budgetMaximum', $budgetMaximum); + // custom fiscal year + $customFiscalYear = (int) Input::get('customFiscalYear'); + Preferences::set('customFiscalYear', $customFiscalYear); + // language: $lang = Input::get('language'); if (in_array($lang, array_keys(Config::get('firefly.languages')))) { diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index 6143ad0d6d..2aaa1dbe18 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -169,6 +169,9 @@ return [ 'pref_6M' => 'Six months', 'pref_languages' => 'Languages', 'pref_languages_help' => 'Firefly III supports several languages. Which one do you prefer?', + 'pref_custom_fiscal_year' => 'Fiscal year settings', + 'pref_custom_fiscal_year_label' => 'Enabled', + 'pref_custom_fiscal_year_help' => 'In countries that use a financial year other than January 1 to December 31, you can switch this on and specify start / end days of the fiscal year', 'pref_save_settings' => 'Save settings', // profile: diff --git a/resources/views/preferences/index.twig b/resources/views/preferences/index.twig index f847fa1f6d..42aaba1292 100644 --- a/resources/views/preferences/index.twig +++ b/resources/views/preferences/index.twig @@ -45,6 +45,18 @@ {{ ExpandedForm.amount('budgetMaximum',budgetMaximum,{'label' : 'Budget maximum'}) }} +
+
+

{{ 'pref_custom_fiscal_year'|_ }}

+
+
+

+ {{ 'pref_custom_fiscal_year_help'|_ }} +

+ {% set isCustomFiscalYear = customFiscalYear == 1 ? true : false %} + {{ ExpandedForm.checkbox('customFiscalYear','1',isCustomFiscalYear,{ 'label' : 'pref_custom_fiscal_year_label'|_ }) }} +
+
From 000f86d31872f93757ef9ad647756510eeb8ad9f Mon Sep 17 00:00:00 2001 From: Graham Miller Date: Sun, 24 Jan 2016 16:47:39 +1000 Subject: [PATCH 2/4] Added fiscal year start date stored in 'm-d' format to preferences. Displays YYYY-MM-DD for current year to get input. --- app/Http/Controllers/PreferencesController.php | 6 +++++- resources/lang/en_US/firefly.php | 1 + resources/views/preferences/index.twig | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/PreferencesController.php b/app/Http/Controllers/PreferencesController.php index 641a817fe0..461937a273 100644 --- a/app/Http/Controllers/PreferencesController.php +++ b/app/Http/Controllers/PreferencesController.php @@ -40,10 +40,12 @@ class PreferencesController extends Controller $language = Preferences::get('language', env('DEFAULT_LANGUAGE', 'en_US'))->data; $budgetMaximum = $budgetMax->data; $customFiscalYear = Preferences::get('customFiscalYear', 0)->data; + $fiscalYearStartStr = Preferences::get('fiscalYearStart', '01-01')->data; + $fiscalYearStart = date('Y') . '-' . $fiscalYearStartStr; $showIncomplete = env('SHOW_INCOMPLETE_TRANSLATIONS', 'false') == 'true'; - return view('preferences.index', compact('budgetMaximum', 'language', 'accounts', 'frontPageAccounts', 'viewRange', 'customFiscalYear', 'showIncomplete')); + return view('preferences.index', compact('budgetMaximum', 'language', 'accounts', 'frontPageAccounts', 'viewRange', 'customFiscalYear', 'fiscalYearStart', 'showIncomplete')); } /** @@ -74,6 +76,8 @@ class PreferencesController extends Controller // custom fiscal year $customFiscalYear = (int) Input::get('customFiscalYear'); Preferences::set('customFiscalYear', $customFiscalYear); + $fiscalYearStart = date('m-d', strtotime(Input::get('fiscalYearStart'))); + Preferences::set('fiscalYearStart', $fiscalYearStart); // language: $lang = Input::get('language'); diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index 2aaa1dbe18..fb8aaa0b34 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -172,6 +172,7 @@ return [ 'pref_custom_fiscal_year' => 'Fiscal year settings', 'pref_custom_fiscal_year_label' => 'Enabled', 'pref_custom_fiscal_year_help' => 'In countries that use a financial year other than January 1 to December 31, you can switch this on and specify start / end days of the fiscal year', + 'pref_fiscal_year_start_label' => 'Fiscal year start date', 'pref_save_settings' => 'Save settings', // profile: diff --git a/resources/views/preferences/index.twig b/resources/views/preferences/index.twig index 42aaba1292..30416d2918 100644 --- a/resources/views/preferences/index.twig +++ b/resources/views/preferences/index.twig @@ -55,6 +55,7 @@

{% set isCustomFiscalYear = customFiscalYear == 1 ? true : false %} {{ ExpandedForm.checkbox('customFiscalYear','1',isCustomFiscalYear,{ 'label' : 'pref_custom_fiscal_year_label'|_ }) }} + {{ ExpandedForm.date('fiscalYearStart',fiscalYearStart,{ 'label' : 'pref_fiscal_year_start_label'|_ }) }}
From eb31934fb79c56d4d259794e7a29bba0be22f772 Mon Sep 17 00:00:00 2001 From: Graham Miller Date: Wed, 27 Jan 2016 11:54:04 +1000 Subject: [PATCH 3/4] 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'); } From 5ee8d04800a3ac96e16e7df9d3601d2021a50637 Mon Sep 17 00:00:00 2001 From: Graham Miller Date: Wed, 27 Jan 2016 13:38:34 +1000 Subject: [PATCH 4/4] Added modifications to reports enabling the custom fiscal year changes. --- app/Helpers/Report/ReportHelper.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/Helpers/Report/ReportHelper.php b/app/Helpers/Report/ReportHelper.php index 9cfccfd6f7..bedafc34ca 100644 --- a/app/Helpers/Report/ReportHelper.php +++ b/app/Helpers/Report/ReportHelper.php @@ -4,6 +4,7 @@ namespace FireflyIII\Helpers\Report; use Carbon\Carbon; use DB; +use FireflyIII\Helpers\FiscalHelper; use FireflyIII\Helpers\Collection\Account as AccountCollection; use FireflyIII\Helpers\Collection\Balance; use FireflyIII\Helpers\Collection\BalanceEntry; @@ -388,13 +389,14 @@ class ReportHelper implements ReportHelperInterface $start = clone $date; $end = Carbon::now(); $months = []; + $fiscalHelper = new FiscalHelper; while ($start <= $end) { - $year = $start->year; + $year = $fiscalHelper->endOfFiscalYear($start)->year; if (!isset($months[$year])) { $months[$year] = [ - 'start' => Carbon::createFromDate($year, 1, 1)->format('Y-m-d'), - 'end' => Carbon::createFromDate($year, 12, 31)->format('Y-m-d'), + 'start' => $fiscalHelper->startOfFiscalYear($start)->format('Y-m-d'), + 'end' => $fiscalHelper->endOfFiscalYear($start)->format('Y-m-d'), 'months' => [], ]; }