mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-12-26 00:41:34 -06:00
Merge pull request #149 from webenhanced/feature/custom-fiscal-years
Feature - custom fiscal years
This commit is contained in:
commit
e8776d44c5
78
app/Helpers/FiscalHelper.php
Normal file
78
app/Helpers/FiscalHelper.php
Normal file
@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace FireflyIII\Helpers;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Preferences;
|
||||
|
||||
/**
|
||||
* Class FiscalHelper
|
||||
*
|
||||
* @package FireflyIII\Helpers
|
||||
*/
|
||||
class FiscalHelper implements FiscalHelperInterface
|
||||
{
|
||||
|
||||
/** @var bool */
|
||||
protected $useCustomFiscalYear;
|
||||
|
||||
/**
|
||||
* FiscalHelper constructor.
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
if (Preferences::get('customFiscalYear', 0)->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;
|
||||
}
|
||||
}
|
35
app/Helpers/FiscalHelperInterface.php
Normal file
35
app/Helpers/FiscalHelperInterface.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace FireflyIII\Helpers;
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
/**
|
||||
* Interface FiscalHelperInterface
|
||||
*
|
||||
* @package FireflyIII\Helpers
|
||||
*/
|
||||
interface FiscalHelperInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* This method produces a clone of the Carbon date object passed, checks preferences
|
||||
* and calculates the first day of the fiscal year.
|
||||
*
|
||||
* @param Carbon $start
|
||||
*
|
||||
* @return Carbon date object
|
||||
*/
|
||||
public function startOfFiscalYear(Carbon $date);
|
||||
|
||||
/**
|
||||
* This method produces a clone of the Carbon date object passed, checks preferences
|
||||
* and calculates the last day of the fiscal year.
|
||||
*
|
||||
* @param Carbon $date
|
||||
*
|
||||
* @return Carbon date object
|
||||
*/
|
||||
public function endOfFiscalYear(Carbon $start);
|
||||
|
||||
}
|
@ -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' => [],
|
||||
];
|
||||
}
|
||||
|
@ -39,10 +39,13 @@ 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;
|
||||
$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', 'showIncomplete'));
|
||||
return view('preferences.index', compact('budgetMaximum', 'language', 'accounts', 'frontPageAccounts', 'viewRange', 'customFiscalYear', 'fiscalYearStart', 'showIncomplete'));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -70,6 +73,12 @@ 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);
|
||||
$fiscalYearStart = date('m-d', strtotime(Input::get('fiscalYearStart')));
|
||||
Preferences::set('fiscalYearStart', $fiscalYearStart);
|
||||
|
||||
// language:
|
||||
$lang = Input::get('language');
|
||||
if (in_array($lang, array_keys(Config::get('firefly.languages')))) {
|
||||
|
@ -113,6 +113,7 @@ class FireflyServiceProvider extends ServiceProvider
|
||||
$this->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');
|
||||
|
||||
|
||||
}
|
||||
|
@ -169,6 +169,10 @@ 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_fiscal_year_start_label' => 'Fiscal year start date',
|
||||
'pref_save_settings' => 'Save settings',
|
||||
|
||||
// profile:
|
||||
|
@ -45,6 +45,19 @@
|
||||
{{ ExpandedForm.amount('budgetMaximum',budgetMaximum,{'label' : 'Budget maximum'}) }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="box">
|
||||
<div class="box-header with-border">
|
||||
<h3 class="box-title">{{ 'pref_custom_fiscal_year'|_ }}</h3>
|
||||
</div>
|
||||
<div class="box-body">
|
||||
<p class="text-info">
|
||||
{{ 'pref_custom_fiscal_year_help'|_ }}
|
||||
</p>
|
||||
{% 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'|_ }) }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
<div class="col-lg-6 col-md-6 col-sm-6">
|
||||
|
Loading…
Reference in New Issue
Block a user