2016-01-26 19:54:04 -06:00
|
|
|
<?php
|
2016-05-20 05:27:31 -05:00
|
|
|
/**
|
|
|
|
* FiscalHelper.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-04 23:52:15 -05:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-05-20 05:27:31 -05:00
|
|
|
*/
|
|
|
|
|
2016-02-05 05:08:25 -06:00
|
|
|
declare(strict_types = 1);
|
2016-01-26 19:54:04 -06:00
|
|
|
|
|
|
|
namespace FireflyIII\Helpers;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Preferences;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class FiscalHelper
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Helpers
|
|
|
|
*/
|
|
|
|
class FiscalHelper implements FiscalHelperInterface
|
|
|
|
{
|
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
protected $useCustomFiscalYear;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* FiscalHelper constructor.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2016-04-27 03:38:51 -05:00
|
|
|
$this->useCustomFiscalYear = Preferences::get('customFiscalYear', false)->data;
|
2016-01-26 19:54:04 -06:00
|
|
|
}
|
|
|
|
|
2016-02-04 00:23:14 -06:00
|
|
|
/**
|
|
|
|
* @param Carbon $date
|
|
|
|
*
|
|
|
|
* @return Carbon date object
|
|
|
|
*/
|
2016-04-06 09:37:28 -05:00
|
|
|
public function endOfFiscalYear(Carbon $date): Carbon
|
2016-02-04 00:23:14 -06:00
|
|
|
{
|
|
|
|
// 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();
|
2016-04-27 03:38:51 -05:00
|
|
|
|
|
|
|
return $endDate;
|
2016-02-04 00:23:14 -06:00
|
|
|
}
|
2016-04-27 03:38:51 -05:00
|
|
|
$endDate->endOfYear();
|
2016-02-04 00:23:14 -06:00
|
|
|
|
|
|
|
|
|
|
|
return $endDate;
|
|
|
|
}
|
|
|
|
|
2016-01-26 19:54:04 -06:00
|
|
|
/**
|
|
|
|
* @param Carbon $date
|
|
|
|
*
|
|
|
|
* @return Carbon date object
|
|
|
|
*/
|
2016-04-06 09:37:28 -05:00
|
|
|
public function startOfFiscalYear(Carbon $date): Carbon
|
2016-01-26 19:54:04 -06:00
|
|
|
{
|
|
|
|
// 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();
|
|
|
|
}
|
2016-04-27 03:38:51 -05:00
|
|
|
|
|
|
|
return $startDate;
|
2016-01-26 19:54:04 -06:00
|
|
|
}
|
2016-04-27 03:38:51 -05:00
|
|
|
$startDate->startOfYear();
|
2016-01-26 19:54:04 -06:00
|
|
|
|
2016-02-04 00:23:14 -06:00
|
|
|
return $startDate;
|
2016-01-26 19:54:04 -06:00
|
|
|
}
|
|
|
|
}
|