Merge pull request #149 from webenhanced/feature/custom-fiscal-years

Feature - custom fiscal years
This commit is contained in:
James Cole 2016-01-27 17:57:13 +01:00
commit e8776d44c5
7 changed files with 146 additions and 4 deletions

View 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;
}
}

View 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);
}

View File

@ -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' => [],
];
}

View File

@ -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')))) {

View File

@ -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');
}

View File

@ -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:

View File

@ -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">