mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-01-03 12:47:17 -06:00
Add a new helper to handle fiscal issues.
Used initially to provide fiscal year support to the reports.
This commit is contained in:
parent
000f86d318
commit
eb31934fb7
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);
|
||||
|
||||
}
|
@ -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');
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user