firefly-iii/app/Support/Navigation.php

393 lines
12 KiB
PHP
Raw Normal View History

2015-02-06 14:23:14 -06:00
<?php
/**
* Navigation.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
2015-02-06 14:23:14 -06:00
namespace FireflyIII\Support;
use Carbon\Carbon;
2015-02-22 02:46:21 -06:00
use FireflyIII\Exceptions\FireflyException;
2015-02-06 14:23:14 -06:00
/**
* Class Navigation
*
* @package FireflyIII\Support
*/
class Navigation
{
2015-02-22 08:40:13 -06:00
/**
2015-05-05 03:23:01 -05:00
* @param \Carbon\Carbon $theDate
2015-02-25 08:19:14 -06:00
* @param $repeatFreq
* @param $skip
2015-02-22 08:40:13 -06:00
*
2015-02-25 08:19:14 -06:00
* @return \Carbon\Carbon
2015-02-22 08:40:13 -06:00
* @throws FireflyException
*/
2016-04-05 15:00:03 -05:00
public function addPeriod(Carbon $theDate, string $repeatFreq, int $skip): Carbon
2015-02-22 08:40:13 -06:00
{
2015-02-25 08:19:14 -06:00
$date = clone $theDate;
$add = ($skip + 1);
2015-02-22 08:40:13 -06:00
2015-02-25 08:19:14 -06:00
$functionMap = [
2015-05-17 02:35:49 -05:00
'1D' => 'addDays', 'daily' => 'addDays',
'1W' => 'addWeeks', 'weekly' => 'addWeeks', 'week' => 'addWeeks',
'1M' => 'addMonths', 'month' => 'addMonths', 'monthly' => 'addMonths', '3M' => 'addMonths',
'quarter' => 'addMonths', 'quarterly' => 'addMonths', '6M' => 'addMonths', 'half-year' => 'addMonths',
'year' => 'addYears', 'yearly' => 'addYears',
2015-02-22 08:40:13 -06:00
];
2015-02-25 08:19:14 -06:00
$modifierMap = [
'quarter' => 3,
2015-03-04 02:42:47 -06:00
'3M' => 3,
2015-02-25 08:19:14 -06:00
'quarterly' => 3,
2015-03-04 02:42:47 -06:00
'6M' => 6,
2015-02-25 08:19:14 -06:00
'half-year' => 6,
];
2015-06-06 16:09:12 -05:00
2015-02-25 08:19:14 -06:00
if (!isset($functionMap[$repeatFreq])) {
throw new FireflyException('Cannot do addPeriod for $repeat_freq "' . $repeatFreq . '"');
2015-02-22 08:40:13 -06:00
}
2015-02-25 08:19:14 -06:00
if (isset($modifierMap[$repeatFreq])) {
$add = $add * $modifierMap[$repeatFreq];
}
$function = $functionMap[$repeatFreq];
$date->$function($add);
return $date;
2015-02-22 08:40:13 -06:00
}
2015-02-25 08:19:14 -06:00
/**
2016-04-24 13:23:17 -05:00
* @param \Carbon\Carbon $end
2015-02-25 08:19:14 -06:00
* @param $repeatFreq
*
2015-05-05 03:23:01 -05:00
* @return \Carbon\Carbon
2015-02-25 08:19:14 -06:00
* @throws FireflyException
*/
2016-04-24 13:23:17 -05:00
public function endOfPeriod(Carbon $end, string $repeatFreq): Carbon
2015-02-25 08:19:14 -06:00
{
2016-04-24 13:23:17 -05:00
$currentEnd = clone $end;
2015-02-25 08:19:14 -06:00
$functionMap = [
2016-04-24 13:23:17 -05:00
'1D' => 'endOfDay', 'daily' => 'endOfDay',
2015-05-17 03:30:18 -05:00
'1W' => 'addWeek', 'week' => 'addWeek', 'weekly' => 'addWeek',
'1M' => 'addMonth', 'month' => 'addMonth', 'monthly' => 'addMonth',
'3M' => 'addMonths', 'quarter' => 'addMonths', 'quarterly' => 'addMonths', '6M' => 'addMonths', 'half-year' => 'addMonths',
'year' => 'addYear', 'yearly' => 'addYear', '1Y' => 'addYear',
2015-02-25 08:19:14 -06:00
];
$modifierMap = [
'quarter' => 3,
2015-03-04 02:42:47 -06:00
'3M' => 3,
2015-02-25 08:19:14 -06:00
'quarterly' => 3,
'half-year' => 6,
2015-03-04 02:42:47 -06:00
'6M' => 6,
2015-02-25 08:19:14 -06:00
];
2015-03-04 02:42:47 -06:00
$subDay = ['week', 'weekly', '1W', 'month', 'monthly', '1M', '3M', 'quarter', 'quarterly', '6M', 'half-year', 'year', 'yearly'];
2015-02-25 08:19:14 -06:00
2016-02-05 00:16:55 -06:00
// if the range is custom, the end of the period
// is another X days (x is the difference between start)
// and end added to $theCurrentEnd
if ($repeatFreq == 'custom') {
/** @var Carbon $tStart */
$tStart = session('start', Carbon::now()->startOfMonth());
/** @var Carbon $tEnd */
$tEnd = session('end', Carbon::now()->endOfMonth());
$diffInDays = $tStart->diffInDays($tEnd);
$currentEnd->addDays($diffInDays);
return $currentEnd;
}
2015-02-25 08:19:14 -06:00
if (!isset($functionMap[$repeatFreq])) {
2015-07-06 09:52:18 -05:00
throw new FireflyException('Cannot do endOfPeriod for $repeat_freq "' . $repeatFreq . '"');
2015-02-25 08:19:14 -06:00
}
$function = $functionMap[$repeatFreq];
if (isset($modifierMap[$repeatFreq])) {
$currentEnd->$function($modifierMap[$repeatFreq]);
if (in_array($repeatFreq, $subDay)) {
$currentEnd->subDay();
}
return $currentEnd;
2015-02-25 08:19:14 -06:00
}
$currentEnd->$function();
2015-02-25 08:19:14 -06:00
if (in_array($repeatFreq, $subDay)) {
$currentEnd->subDay();
}
return $currentEnd;
}
2015-02-22 08:40:13 -06:00
2015-02-27 04:02:08 -06:00
/**
*
2015-12-31 10:20:54 -06:00
* @param \Carbon\Carbon $theCurrentEnd
2015-02-27 04:02:08 -06:00
* @param $repeatFreq
2015-12-31 10:20:54 -06:00
* @param \Carbon\Carbon $maxDate
2015-02-27 04:02:08 -06:00
*
2015-12-31 10:20:54 -06:00
* @return \Carbon\Carbon
2015-02-27 04:02:08 -06:00
*/
2016-04-05 15:00:03 -05:00
public function endOfX(Carbon $theCurrentEnd, string $repeatFreq, Carbon $maxDate = null): Carbon
2015-02-27 04:02:08 -06:00
{
$functionMap = [
2015-09-25 10:28:42 -05:00
'1D' => 'endOfDay',
2015-02-27 04:02:08 -06:00
'daily' => 'endOfDay',
2015-09-25 10:28:42 -05:00
'1W' => 'endOfWeek',
2015-02-27 04:02:08 -06:00
'week' => 'endOfWeek',
'weekly' => 'endOfWeek',
'month' => 'endOfMonth',
2015-09-25 09:00:14 -05:00
'1M' => 'endOfMonth',
2015-02-27 04:02:08 -06:00
'monthly' => 'endOfMonth',
2015-09-25 10:28:42 -05:00
'3M' => 'lastOfQuarter',
2015-02-27 04:02:08 -06:00
'quarter' => 'lastOfQuarter',
'quarterly' => 'lastOfQuarter',
2015-09-25 10:28:42 -05:00
'1Y' => 'endOfYear',
2015-02-27 04:02:08 -06:00
'year' => 'endOfYear',
'yearly' => 'endOfYear',
];
$currentEnd = clone $theCurrentEnd;
if (isset($functionMap[$repeatFreq])) {
$function = $functionMap[$repeatFreq];
$currentEnd->$function();
}
2015-06-29 02:14:39 -05:00
2015-09-25 09:00:14 -05:00
if (!is_null($maxDate) && $currentEnd > $maxDate) {
2015-02-27 04:02:08 -06:00
return clone $maxDate;
}
return $currentEnd;
}
2015-02-25 08:19:14 -06:00
/**
2015-06-27 01:06:24 -05:00
* @param \Carbon\Carbon $date
2015-02-25 08:19:14 -06:00
* @param $repeatFrequency
*
* @return string
* @throws FireflyException
*/
2016-04-05 15:00:03 -05:00
public function periodShow(Carbon $date, string $repeatFrequency): string
2015-02-25 08:19:14 -06:00
{
$formatMap = [
'1D' => trans('config.specific_day'),
'daily' => trans('config.specific_day'),
'custom' => trans('config.specific_day'),
'1W' => trans('config.week_in_year'),
'week' => trans('config.week_in_year'),
'weekly' => trans('config.week_in_year'),
'3M' => trans('config.quarter_of_year'),
'quarter' => trans('config.quarter_of_year'),
'1M' => trans('config.month'),
'month' => trans('config.month'),
'monthly' => trans('config.month'),
'1Y' => trans('config.year'),
'year' => trans('config.year'),
'yearly' => trans('config.year'),
'6M' => trans('config.half_year'),
2015-02-25 08:19:14 -06:00
];
2015-05-25 14:17:36 -05:00
2015-02-25 08:19:14 -06:00
if (isset($formatMap[$repeatFrequency])) {
2016-02-10 09:23:37 -06:00
return $date->formatLocalized(strval($formatMap[$repeatFrequency]));
2015-02-25 08:19:14 -06:00
}
throw new FireflyException('No date formats for frequency "' . $repeatFrequency . '"!');
}
/**
2015-05-05 03:23:01 -05:00
* @param \Carbon\Carbon $theDate
2015-02-25 08:19:14 -06:00
* @param $repeatFreq
*
2015-05-05 03:23:01 -05:00
* @return \Carbon\Carbon
2015-02-25 08:19:14 -06:00
* @throws FireflyException
*/
2016-04-05 15:00:03 -05:00
public function startOfPeriod(Carbon $theDate, string $repeatFreq): Carbon
2015-02-25 08:19:14 -06:00
{
$date = clone $theDate;
$functionMap = [
2015-03-04 02:42:47 -06:00
'1D' => 'startOfDay',
2015-03-03 10:40:17 -06:00
'daily' => 'startOfDay',
2015-03-04 02:42:47 -06:00
'1W' => 'startOfWeek',
2015-03-03 10:40:17 -06:00
'week' => 'startOfWeek',
'weekly' => 'startOfWeek',
'month' => 'startOfMonth',
2015-03-04 02:42:47 -06:00
'1M' => 'startOfMonth',
2015-03-03 10:40:17 -06:00
'monthly' => 'startOfMonth',
2015-03-04 02:42:47 -06:00
'3M' => 'firstOfQuarter',
2015-03-03 10:40:17 -06:00
'quarter' => 'firstOfQuarter',
'quarterly' => 'firstOfQuarter',
'year' => 'startOfYear',
'yearly' => 'startOfYear',
'1Y' => 'startOfYear',
2015-02-25 08:19:14 -06:00
];
if (isset($functionMap[$repeatFreq])) {
$function = $functionMap[$repeatFreq];
$date->$function();
return $date;
}
2015-03-04 02:42:47 -06:00
if ($repeatFreq == 'half-year' || $repeatFreq == '6M') {
2015-05-05 03:30:39 -05:00
$month = $date->month;
2015-02-25 08:19:14 -06:00
$date->startOfYear();
if ($month >= 7) {
$date->addMonths(6);
}
return $date;
}
if ($repeatFreq === 'custom') {
return $date; // the date is already at the start.
}
2015-07-06 09:52:18 -05:00
throw new FireflyException('Cannot do startOfPeriod for $repeat_freq "' . $repeatFreq . '"');
2015-02-25 08:19:14 -06:00
}
2015-03-29 14:27:51 -05:00
/**
2015-12-31 10:20:54 -06:00
* @param \Carbon\Carbon $theDate
2015-03-29 14:27:51 -05:00
* @param $repeatFreq
* @param int $subtract
*
2015-12-31 10:20:54 -06:00
* @return \Carbon\Carbon
2015-03-29 14:27:51 -05:00
* @throws FireflyException
*/
2016-04-05 15:00:03 -05:00
public function subtractPeriod(Carbon $theDate, string $repeatFreq, int $subtract = 1): Carbon
2015-03-29 14:27:51 -05:00
{
$date = clone $theDate;
2015-09-25 10:28:42 -05:00
// 1D 1W 1M 3M 6M 1Y
2015-03-29 14:27:51 -05:00
$functionMap = [
2015-09-25 10:28:42 -05:00
'1D' => 'subDays',
2015-03-29 14:27:51 -05:00
'daily' => 'subDays',
'week' => 'subWeeks',
2015-09-25 10:28:42 -05:00
'1W' => 'subWeeks',
2015-03-29 14:27:51 -05:00
'weekly' => 'subWeeks',
'month' => 'subMonths',
2015-09-25 09:00:14 -05:00
'1M' => 'subMonths',
2015-03-29 14:27:51 -05:00
'monthly' => 'subMonths',
'year' => 'subYears',
'1Y' => 'subYears',
2015-03-29 14:27:51 -05:00
'yearly' => 'subYears',
];
$modifierMap = [
'quarter' => 3,
'3M' => 3,
2015-03-29 14:27:51 -05:00
'quarterly' => 3,
'half-year' => 6,
'6M' => 6,
2015-03-29 14:27:51 -05:00
];
if (isset($functionMap[$repeatFreq])) {
$function = $functionMap[$repeatFreq];
$date->$function($subtract);
return $date;
}
if (isset($modifierMap[$repeatFreq])) {
$subtract = $subtract * $modifierMap[$repeatFreq];
$date->subMonths($subtract);
return $date;
}
// a custom range requires the session start
// and session end to calculate the difference in days.
// this is then subtracted from $theDate (* $subtract).
2016-02-05 02:30:06 -06:00
if ($repeatFreq === 'custom') {
/** @var Carbon $tStart */
$tStart = session('start', Carbon::now()->startOfMonth());
/** @var Carbon $tEnd */
$tEnd = session('end', Carbon::now()->endOfMonth());
$diffInDays = $tStart->diffInDays($tEnd);
$date->subDays($diffInDays * $subtract);
2016-02-05 02:30:06 -06:00
return $date;
}
2015-03-29 14:27:51 -05:00
2015-07-06 09:52:18 -05:00
throw new FireflyException('Cannot do subtractPeriod for $repeat_freq "' . $repeatFreq . '"');
2015-03-29 14:27:51 -05:00
}
2015-02-11 00:35:10 -06:00
/**
* @param $range
2015-05-05 03:23:01 -05:00
* @param \Carbon\Carbon $start
2015-02-11 00:35:10 -06:00
*
2015-05-05 03:23:01 -05:00
* @return \Carbon\Carbon
2015-02-11 00:35:10 -06:00
* @throws FireflyException
*/
2016-04-05 15:00:03 -05:00
public function updateEndDate(string $range, Carbon $start): Carbon
2015-02-06 14:23:14 -06:00
{
$functionMap = [
'1D' => 'endOfDay',
'1W' => 'endOfWeek',
'1M' => 'endOfMonth',
'3M' => 'lastOfQuarter',
'1Y' => 'endOfYear',
];
2015-06-05 09:49:16 -05:00
$end = clone $start;
2015-02-06 14:23:14 -06:00
if (isset($functionMap[$range])) {
$function = $functionMap[$range];
$end->$function();
return $end;
}
if ($range == '6M') {
2015-05-05 03:30:39 -05:00
if ($start->month >= 7) {
2015-02-06 14:23:14 -06:00
$end->endOfYear();
2016-05-20 10:58:10 -05:00
return $end;
2015-02-06 14:23:14 -06:00
}
2016-05-20 10:58:10 -05:00
$end->startOfYear()->addMonths(6);
2015-02-06 14:23:14 -06:00
return $end;
}
2015-07-06 09:52:18 -05:00
throw new FireflyException('updateEndDate cannot handle $range "' . $range . '"');
2015-02-06 14:23:14 -06:00
}
2015-02-11 00:35:10 -06:00
/**
2015-05-05 03:23:01 -05:00
* @param $range
* @param \Carbon\Carbon $start
2015-02-11 00:35:10 -06:00
*
2015-05-05 03:23:01 -05:00
* @return \Carbon\Carbon
2015-02-11 00:35:10 -06:00
* @throws FireflyException
*/
2016-04-05 15:00:03 -05:00
public function updateStartDate(string $range, Carbon $start): Carbon
2015-02-06 14:23:14 -06:00
{
$functionMap = [
'1D' => 'startOfDay',
'1W' => 'startOfWeek',
'1M' => 'startOfMonth',
'3M' => 'firstOfQuarter',
'1Y' => 'startOfYear',
];
if (isset($functionMap[$range])) {
$function = $functionMap[$range];
$start->$function();
return $start;
}
if ($range == '6M') {
2015-05-05 03:30:39 -05:00
if ($start->month >= 7) {
2015-02-06 14:23:14 -06:00
$start->startOfYear()->addMonths(6);
return $start;
2015-02-06 14:23:14 -06:00
}
$start->startOfYear();
2015-02-06 14:23:14 -06:00
return $start;
2015-02-06 14:23:14 -06:00
}
2015-07-06 09:52:18 -05:00
throw new FireflyException('updateStartDate cannot handle $range "' . $range . '"');
2015-02-06 14:23:14 -06:00
}
2015-03-29 01:14:32 -05:00
}