| 
									
										
										
										
											2015-02-06 21:23:14 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Navigation.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:56:52 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2016-10-05 06:52:15 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2016-05-20 12:41:23 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-04-09 07:44:22 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							| 
									
										
										
										
											2015-02-06 21:23:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Support; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2015-02-22 09:46:21 +01:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2019-06-21 19:10:02 +02:00
										 |  |  | use FireflyIII\Helpers\Fiscal\FiscalHelperInterface; | 
					
						
							| 
									
										
										
										
											2023-04-01 07:04:42 +02:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2023-02-22 18:03:31 +01:00
										 |  |  | use Psr\Container\ContainerExceptionInterface; | 
					
						
							|  |  |  | use Psr\Container\NotFoundExceptionInterface; | 
					
						
							| 
									
										
										
										
											2015-02-06 21:23:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |  * Class Navigation. | 
					
						
							| 
									
										
										
										
											2015-02-06 21:23:14 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | class Navigation | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Carbon  $theDate | 
					
						
							|  |  |  |      * @param  string  $repeatFreq | 
					
						
							|  |  |  |      * @param  int  $skip | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-05-24 08:57:02 +02:00
										 |  |  |      * @return Carbon | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-05 22:00:03 +02:00
										 |  |  |     public function addPeriod(Carbon $theDate, string $repeatFreq, int $skip): Carbon | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |         $date = clone $theDate; | 
					
						
							|  |  |  |         $add  = ($skip + 1); | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |         $functionMap = [ | 
					
						
							| 
									
										
										
										
											2019-03-02 08:05:15 +01: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', | 
					
						
							|  |  |  |             '1Y'        => 'addYears', | 
					
						
							|  |  |  |             'custom'    => 'addMonths', // custom? just add one month.
 | 
					
						
							| 
									
										
										
										
											2023-05-19 05:43:50 +02:00
										 |  |  |             // last X periods? Jump the relevant month / quarter / year
 | 
					
						
							|  |  |  |             'last7'     => 'addDays', | 
					
						
							|  |  |  |             'last30'    => 'addMonths', | 
					
						
							|  |  |  |             'last90'    => 'addMonths', | 
					
						
							|  |  |  |             'last365'   => 'addYears', | 
					
						
							|  |  |  |             'MTD'       => 'addMonths', | 
					
						
							|  |  |  |             'QTD'       => 'addMonths', | 
					
						
							|  |  |  |             'YTD'       => 'addYears', | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |         $modifierMap = [ | 
					
						
							|  |  |  |             'quarter'   => 3, | 
					
						
							| 
									
										
										
										
											2015-03-04 09:42:47 +01:00
										 |  |  |             '3M'        => 3, | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |             'quarterly' => 3, | 
					
						
							| 
									
										
										
										
											2015-03-04 09:42:47 +01:00
										 |  |  |             '6M'        => 6, | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |             'half-year' => 6, | 
					
						
							| 
									
										
										
										
											2023-05-19 05:43:50 +02:00
										 |  |  |             'last7'     => 7, | 
					
						
							|  |  |  |             'last90'    => 3, | 
					
						
							|  |  |  |             'QTD'       => 3, | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2015-06-06 23:09:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 17:00:16 +02:00
										 |  |  |         if (!array_key_exists($repeatFreq, $functionMap)) { | 
					
						
							| 
									
										
										
										
											2019-08-12 18:19:06 +02:00
										 |  |  |             Log::error(sprintf('Cannot do addPeriod for $repeat_freq "%s"', $repeatFreq)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $theDate; | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-06 17:00:16 +02:00
										 |  |  |         if (array_key_exists($repeatFreq, $modifierMap)) { | 
					
						
							| 
									
										
										
										
											2018-03-29 19:01:47 +02:00
										 |  |  |             $add *= $modifierMap[$repeatFreq]; | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         $function = $functionMap[$repeatFreq]; | 
					
						
							|  |  |  |         $date->$function($add); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-02 08:05:15 +01:00
										 |  |  |         // if period is 1M and diff in month is 2 and new DOM > 1, sub a number of days:
 | 
					
						
							| 
									
										
										
										
											2019-11-09 07:55:59 +01:00
										 |  |  |         // AND skip is 1
 | 
					
						
							| 
									
										
										
										
											2019-03-02 08:05:15 +01:00
										 |  |  |         // result is:
 | 
					
						
							|  |  |  |         // '2019-01-29', '2019-02-28'
 | 
					
						
							|  |  |  |         // '2019-01-30', '2019-02-28'
 | 
					
						
							|  |  |  |         // '2019-01-31', '2019-02-28'
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-25 07:32:29 +02:00
										 |  |  |         $months     = ['1M', 'month', 'monthly']; | 
					
						
							|  |  |  |         $difference = $date->month - $theDate->month; | 
					
						
							| 
									
										
										
										
											2019-11-09 07:55:59 +01:00
										 |  |  |         if (1 === $add && 2 === $difference && $date->day > 0 && in_array($repeatFreq, $months, true)) { | 
					
						
							| 
									
										
										
										
											2019-03-02 08:05:15 +01:00
										 |  |  |             $date->subDays($date->day); | 
					
						
							| 
									
										
										
										
											2017-09-25 07:32:29 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |         return $date; | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-14 10:48:17 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Carbon  $start | 
					
						
							|  |  |  |      * @param  Carbon  $end | 
					
						
							|  |  |  |      * @param  string  $range | 
					
						
							| 
									
										
										
										
											2018-01-14 10:48:17 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2019-08-17 10:47:29 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-01-14 10:48:17 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-05-24 08:57:02 +02:00
										 |  |  |     public function blockPeriods(Carbon $start, Carbon $end, string $range): array | 
					
						
							| 
									
										
										
										
											2018-01-14 10:48:17 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-02-09 16:47:01 +01:00
										 |  |  |         if ($end < $start) { | 
					
						
							| 
									
										
										
										
											2018-04-02 14:50:17 +02:00
										 |  |  |             [$start, $end] = [$end, $start]; | 
					
						
							| 
									
										
										
										
											2018-02-09 16:47:01 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-01-14 10:48:17 +01:00
										 |  |  |         $periods = []; | 
					
						
							| 
									
										
										
										
											2020-01-26 07:15:47 +01:00
										 |  |  |         // first, 13 periods of [range]
 | 
					
						
							|  |  |  |         $loopCount = 0; | 
					
						
							|  |  |  |         $loopDate  = clone $end; | 
					
						
							|  |  |  |         $workStart = clone $loopDate; | 
					
						
							|  |  |  |         $workEnd   = clone $loopDate; | 
					
						
							|  |  |  |         while ($loopCount < 13) { | 
					
						
							|  |  |  |             // make range:
 | 
					
						
							|  |  |  |             $workStart = \Navigation::startOfPeriod($workStart, $range); | 
					
						
							|  |  |  |             $workEnd   = \Navigation::endOfPeriod($workStart, $range); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // make sure we don't go overboard
 | 
					
						
							|  |  |  |             if ($workEnd->gt($start)) { | 
					
						
							| 
									
										
										
										
											2018-01-14 10:48:17 +01:00
										 |  |  |                 $periods[] = [ | 
					
						
							| 
									
										
										
										
											2020-01-26 07:15:47 +01:00
										 |  |  |                     'start'  => clone $workStart, | 
					
						
							|  |  |  |                     'end'    => clone $workEnd, | 
					
						
							| 
									
										
										
										
											2018-01-14 10:48:17 +01:00
										 |  |  |                     'period' => $range, | 
					
						
							|  |  |  |                 ]; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-01-26 07:15:47 +01:00
										 |  |  |             // skip to the next period:
 | 
					
						
							|  |  |  |             $workStart->subDay()->startOfDay(); | 
					
						
							|  |  |  |             $loopCount++; | 
					
						
							| 
									
										
										
										
											2018-01-14 10:48:17 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-26 07:15:47 +01:00
										 |  |  |         // if $workEnd is still before $start, continue on a yearly basis:
 | 
					
						
							|  |  |  |         $loopCount = 0; | 
					
						
							|  |  |  |         if ($workEnd->gt($start)) { | 
					
						
							|  |  |  |             while ($workEnd->gt($start) && $loopCount < 20) { | 
					
						
							|  |  |  |                 // make range:
 | 
					
						
							|  |  |  |                 $workStart = app('navigation')->startOfPeriod($workStart, '1Y'); | 
					
						
							|  |  |  |                 $workEnd   = app('navigation')->endOfPeriod($workStart, '1Y'); | 
					
						
							| 
									
										
										
										
											2018-01-14 10:48:17 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-26 07:15:47 +01:00
										 |  |  |                 // make sure we don't go overboard
 | 
					
						
							|  |  |  |                 if ($workEnd->gt($start)) { | 
					
						
							|  |  |  |                     $periods[] = [ | 
					
						
							|  |  |  |                         'start'  => clone $workStart, | 
					
						
							|  |  |  |                         'end'    => clone $workEnd, | 
					
						
							|  |  |  |                         'period' => '1Y', | 
					
						
							|  |  |  |                     ]; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 // skip to the next period:
 | 
					
						
							|  |  |  |                 $workStart->subDay()->startOfDay(); | 
					
						
							|  |  |  |                 $loopCount++; | 
					
						
							| 
									
										
										
										
											2018-01-14 10:48:17 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-06 17:00:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-14 10:48:17 +01:00
										 |  |  |         return $periods; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Carbon  $end | 
					
						
							|  |  |  |      * @param  string  $repeatFreq | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-05-24 08:57:02 +02:00
										 |  |  |      * @return Carbon | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-05-24 08:57:02 +02:00
										 |  |  |     public function endOfPeriod(Carbon $end, string $repeatFreq): Carbon | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-04-24 20:23:17 +02:00
										 |  |  |         $currentEnd = clone $end; | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $functionMap = [ | 
					
						
							| 
									
										
										
										
											2019-03-02 08:05:15 +01:00
										 |  |  |             '1D'        => 'endOfDay', | 
					
						
							|  |  |  |             'daily'     => 'endOfDay', | 
					
						
							|  |  |  |             '1W'        => 'addWeek', | 
					
						
							|  |  |  |             'week'      => 'addWeek', | 
					
						
							|  |  |  |             'weekly'    => 'addWeek', | 
					
						
							|  |  |  |             '1M'        => 'addMonth', | 
					
						
							|  |  |  |             'month'     => 'addMonth', | 
					
						
							|  |  |  |             'monthly'   => 'addMonth', | 
					
						
							|  |  |  |             '3M'        => 'addMonths', | 
					
						
							|  |  |  |             'quarter'   => 'addMonths', | 
					
						
							|  |  |  |             'quarterly' => 'addMonths', | 
					
						
							|  |  |  |             '6M'        => 'addMonths', | 
					
						
							|  |  |  |             'half-year' => 'addMonths', | 
					
						
							| 
									
										
										
										
											2020-03-14 10:25:12 +01:00
										 |  |  |             'half_year' => 'addMonths', | 
					
						
							| 
									
										
										
										
											2019-03-02 08:05:15 +01:00
										 |  |  |             'year'      => 'addYear', | 
					
						
							|  |  |  |             'yearly'    => 'addYear', | 
					
						
							|  |  |  |             '1Y'        => 'addYear', | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |         ]; | 
					
						
							|  |  |  |         $modifierMap = [ | 
					
						
							|  |  |  |             'quarter'   => 3, | 
					
						
							| 
									
										
										
										
											2015-03-04 09:42:47 +01:00
										 |  |  |             '3M'        => 3, | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |             'quarterly' => 3, | 
					
						
							|  |  |  |             'half-year' => 6, | 
					
						
							| 
									
										
										
										
											2020-03-14 10:25:12 +01:00
										 |  |  |             'half_year' => 6, | 
					
						
							| 
									
										
										
										
											2015-03-04 09:42:47 +01:00
										 |  |  |             '6M'        => 6, | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-14 10:25:12 +01:00
										 |  |  |         $subDay = ['week', 'weekly', '1W', 'month', 'monthly', '1M', '3M', 'quarter', 'quarterly', '6M', 'half-year', 'half_year', '1Y', 'year', 'yearly']; | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-05 07:16:55 +01: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
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if ('custom' === $repeatFreq) { | 
					
						
							| 
									
										
										
										
											2016-02-05 07:16:55 +01:00
										 |  |  |             /** @var Carbon $tStart */ | 
					
						
							| 
									
										
										
										
											2023-02-11 07:36:45 +01:00
										 |  |  |             $tStart = session('start', today(config('app.timezone'))->startOfMonth()); | 
					
						
							| 
									
										
										
										
											2016-02-05 07:16:55 +01:00
										 |  |  |             /** @var Carbon $tEnd */ | 
					
						
							| 
									
										
										
										
											2023-02-11 07:36:45 +01:00
										 |  |  |             $tEnd       = session('end', today(config('app.timezone'))->endOfMonth()); | 
					
						
							| 
									
										
										
										
											2016-02-05 07:16:55 +01:00
										 |  |  |             $diffInDays = $tStart->diffInDays($tEnd); | 
					
						
							|  |  |  |             $currentEnd->addDays($diffInDays); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $currentEnd; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-05-06 15:29:29 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $result = match ($repeatFreq) { | 
					
						
							|  |  |  |             'last7' => $currentEnd->addDays(7)->startOfDay(), | 
					
						
							|  |  |  |             'last30' => $currentEnd->addDays(30)->startOfDay(), | 
					
						
							|  |  |  |             'last90' => $currentEnd->addDays(90)->startOfDay(), | 
					
						
							|  |  |  |             'last365' => $currentEnd->addDays(365)->startOfDay(), | 
					
						
							|  |  |  |             'MTD' => $currentEnd->startOfMonth()->startOfDay(), | 
					
						
							|  |  |  |             'QTD' => $currentEnd->firstOfQuarter()->startOfDay(), | 
					
						
							|  |  |  |             'YTD' => $currentEnd->startOfYear()->startOfDay(), | 
					
						
							|  |  |  |             default => null, | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |         if (null !== $result) { | 
					
						
							|  |  |  |             return $result; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         unset($result); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 17:00:16 +02:00
										 |  |  |         if (!array_key_exists($repeatFreq, $functionMap)) { | 
					
						
							| 
									
										
										
										
											2019-08-12 18:19:06 +02:00
										 |  |  |             Log::error(sprintf('Cannot do endOfPeriod for $repeat_freq "%s"', $repeatFreq)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $end; | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         $function = $functionMap[$repeatFreq]; | 
					
						
							| 
									
										
										
										
											2018-06-02 18:19:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 17:00:16 +02:00
										 |  |  |         if (array_key_exists($repeatFreq, $modifierMap)) { | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |             $currentEnd->$function($modifierMap[$repeatFreq]); | 
					
						
							| 
									
										
										
										
											2019-06-21 19:10:02 +02:00
										 |  |  |             if (in_array($repeatFreq, $subDay, true)) { | 
					
						
							| 
									
										
										
										
											2016-05-20 17:53:03 +02:00
										 |  |  |                 $currentEnd->subDay(); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-03-02 08:05:15 +01:00
										 |  |  |             $currentEnd->endOfDay(); | 
					
						
							| 
									
										
										
										
											2016-05-20 17:53:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return $currentEnd; | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-05-20 17:53:03 +02:00
										 |  |  |         $currentEnd->$function(); | 
					
						
							| 
									
										
										
										
											2019-03-02 08:05:15 +01:00
										 |  |  |         $currentEnd->endOfDay(); | 
					
						
							| 
									
										
										
										
											2019-06-21 19:10:02 +02:00
										 |  |  |         if (in_array($repeatFreq, $subDay, true)) { | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |             $currentEnd->subDay(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $currentEnd; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-02-22 15:40:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-27 11:02:08 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Carbon  $theCurrentEnd | 
					
						
							|  |  |  |      * @param  string  $repeatFreq | 
					
						
							|  |  |  |      * @param  Carbon|null  $maxDate | 
					
						
							| 
									
										
										
										
											2015-02-27 11:02:08 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-05-24 08:57:02 +02:00
										 |  |  |      * @return Carbon | 
					
						
							| 
									
										
										
										
											2015-02-27 11:02:08 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2017-07-23 19:06:24 +02:00
										 |  |  |     public function endOfX(Carbon $theCurrentEnd, string $repeatFreq, ?Carbon $maxDate): Carbon | 
					
						
							| 
									
										
										
										
											2015-02-27 11:02:08 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         $functionMap = [ | 
					
						
							| 
									
										
										
										
											2015-09-25 17:28:42 +02:00
										 |  |  |             '1D'        => 'endOfDay', | 
					
						
							| 
									
										
										
										
											2015-02-27 11:02:08 +01:00
										 |  |  |             'daily'     => 'endOfDay', | 
					
						
							| 
									
										
										
										
											2015-09-25 17:28:42 +02:00
										 |  |  |             '1W'        => 'endOfWeek', | 
					
						
							| 
									
										
										
										
											2015-02-27 11:02:08 +01:00
										 |  |  |             'week'      => 'endOfWeek', | 
					
						
							|  |  |  |             'weekly'    => 'endOfWeek', | 
					
						
							|  |  |  |             'month'     => 'endOfMonth', | 
					
						
							| 
									
										
										
										
											2015-09-25 16:00:14 +02:00
										 |  |  |             '1M'        => 'endOfMonth', | 
					
						
							| 
									
										
										
										
											2015-02-27 11:02:08 +01:00
										 |  |  |             'monthly'   => 'endOfMonth', | 
					
						
							| 
									
										
										
										
											2015-09-25 17:28:42 +02:00
										 |  |  |             '3M'        => 'lastOfQuarter', | 
					
						
							| 
									
										
										
										
											2015-02-27 11:02:08 +01:00
										 |  |  |             'quarter'   => 'lastOfQuarter', | 
					
						
							|  |  |  |             'quarterly' => 'lastOfQuarter', | 
					
						
							| 
									
										
										
										
											2015-09-25 17:28:42 +02:00
										 |  |  |             '1Y'        => 'endOfYear', | 
					
						
							| 
									
										
										
										
											2015-02-27 11:02:08 +01:00
										 |  |  |             'year'      => 'endOfYear', | 
					
						
							|  |  |  |             'yearly'    => 'endOfYear', | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $currentEnd = clone $theCurrentEnd; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 17:00:16 +02:00
										 |  |  |         if (array_key_exists($repeatFreq, $functionMap)) { | 
					
						
							| 
									
										
										
										
											2015-02-27 11:02:08 +01:00
										 |  |  |             $function = $functionMap[$repeatFreq]; | 
					
						
							|  |  |  |             $currentEnd->$function(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-06-29 09:14:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (null !== $maxDate && $currentEnd > $maxDate) { | 
					
						
							| 
									
										
										
										
											2015-02-27 11:02:08 +01:00
										 |  |  |             return clone $maxDate; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $currentEnd; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 18:14:14 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Returns the user's view range and if necessary, corrects the dynamic view | 
					
						
							|  |  |  |      * range to a normal range. | 
					
						
							|  |  |  |      * @param  bool  $correct | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      * @throws ContainerExceptionInterface | 
					
						
							|  |  |  |      * @throws NotFoundExceptionInterface | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getViewRange(bool $correct): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $range = (string)app('preferences')->get('viewRange', '1M')?->data ?? '1M'; | 
					
						
							|  |  |  |         if (!$correct) { | 
					
						
							|  |  |  |             return $range; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         switch ($range) { | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 return $range; | 
					
						
							|  |  |  |             case 'last7': | 
					
						
							|  |  |  |                 return '1W'; | 
					
						
							|  |  |  |             case 'last30': | 
					
						
							|  |  |  |             case 'MTD': | 
					
						
							|  |  |  |                 return '1M'; | 
					
						
							|  |  |  |             case 'last90': | 
					
						
							|  |  |  |             case 'QTD': | 
					
						
							|  |  |  |                 return '3M'; | 
					
						
							|  |  |  |             case 'last365': | 
					
						
							|  |  |  |             case 'YTD': | 
					
						
							|  |  |  |                 return '1Y'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-19 13:37:44 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Carbon  $start | 
					
						
							|  |  |  |      * @param  Carbon  $end | 
					
						
							| 
									
										
										
										
											2016-11-19 13:37:44 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							| 
									
										
										
										
											2022-03-29 15:10:05 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2016-11-19 13:37:44 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function listOfPeriods(Carbon $start, Carbon $end): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-04-19 06:51:40 +02:00
										 |  |  |         $locale = app('steam')->getLocale(); | 
					
						
							| 
									
										
										
										
											2016-11-19 13:37:44 +01:00
										 |  |  |         // define period to increment
 | 
					
						
							|  |  |  |         $increment     = 'addDay'; | 
					
						
							| 
									
										
										
										
											2018-01-25 18:41:27 +01:00
										 |  |  |         $format        = $this->preferredCarbonFormat($start, $end); | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |         $displayFormat = (string)trans('config.month_and_day_js', [], $locale); | 
					
						
							| 
									
										
										
										
											2016-11-19 13:37:44 +01:00
										 |  |  |         // increment by month (for year)
 | 
					
						
							|  |  |  |         if ($start->diffInMonths($end) > 1) { | 
					
						
							|  |  |  |             $increment     = 'addMonth'; | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |             $displayFormat = (string)trans('config.month_js'); | 
					
						
							| 
									
										
										
										
											2016-11-19 13:37:44 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // increment by year (for multi year)
 | 
					
						
							|  |  |  |         if ($start->diffInMonths($end) > 12) { | 
					
						
							|  |  |  |             $increment     = 'addYear'; | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |             $displayFormat = (string)trans('config.year_js'); | 
					
						
							| 
									
										
										
										
											2016-11-19 13:37:44 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         $begin   = clone $start; | 
					
						
							|  |  |  |         $entries = []; | 
					
						
							|  |  |  |         while ($begin < $end) { | 
					
						
							|  |  |  |             $formatted           = $begin->format($format); | 
					
						
							| 
									
										
										
										
											2022-03-27 20:24:13 +02:00
										 |  |  |             $displayed           = $begin->isoFormat($displayFormat); | 
					
						
							| 
									
										
										
										
											2016-11-19 13:37:44 +01:00
										 |  |  |             $entries[$formatted] = $displayed; | 
					
						
							|  |  |  |             $begin->$increment(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-25 23:29:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-19 13:37:44 +01:00
										 |  |  |         return $entries; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Carbon  $theDate | 
					
						
							|  |  |  |      * @param  string  $repeatFrequency | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-05-24 08:57:02 +02:00
										 |  |  |     public function periodShow(Carbon $theDate, string $repeatFrequency): string | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-11-01 15:34:08 +01:00
										 |  |  |         $date      = clone $theDate; | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |         $formatMap = [ | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |             '1D'      => (string)trans('config.specific_day_js'), | 
					
						
							|  |  |  |             'daily'   => (string)trans('config.specific_day_js'), | 
					
						
							|  |  |  |             'custom'  => (string)trans('config.specific_day_js'), | 
					
						
							|  |  |  |             '1W'      => (string)trans('config.week_in_year_js'), | 
					
						
							|  |  |  |             'week'    => (string)trans('config.week_in_year_js'), | 
					
						
							|  |  |  |             'weekly'  => (string)trans('config.week_in_year_js'), | 
					
						
							|  |  |  |             '1M'      => (string)trans('config.month_js'), | 
					
						
							|  |  |  |             'month'   => (string)trans('config.month_js'), | 
					
						
							|  |  |  |             'monthly' => (string)trans('config.month_js'), | 
					
						
							|  |  |  |             '1Y'      => (string)trans('config.year_js'), | 
					
						
							|  |  |  |             'year'    => (string)trans('config.year_js'), | 
					
						
							|  |  |  |             'yearly'  => (string)trans('config.year_js'), | 
					
						
							|  |  |  |             '6M'      => (string)trans('config.half_year_js'), | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2015-05-25 21:17:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 17:00:16 +02:00
										 |  |  |         if (array_key_exists($repeatFrequency, $formatMap)) { | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |             return $date->isoFormat((string)$formatMap[$repeatFrequency]); | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if ('3M' === $repeatFrequency || 'quarter' === $repeatFrequency) { | 
					
						
							| 
									
										
										
										
											2017-11-01 15:34:08 +01:00
										 |  |  |             $quarter = ceil($theDate->month / 3); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return sprintf('Q%d %d', $quarter, $theDate->year); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // special formatter for quarter of year
 | 
					
						
							| 
									
										
										
										
											2019-08-12 18:19:06 +02:00
										 |  |  |         Log::error(sprintf('No date formats for frequency "%s"!', $repeatFrequency)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $date->format('Y-m-d'); | 
					
						
							| 
									
										
										
										
											2015-02-25 15:19:14 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * If the date difference between start and end is less than a month, method returns "Y-m-d". If the difference is less than a year, | 
					
						
							|  |  |  |      * method returns "Y-m". If the date difference is larger, method returns "Y". | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param  Carbon  $start | 
					
						
							|  |  |  |      * @param  Carbon  $end | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function preferredCarbonFormat(Carbon $start, Carbon $end): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $format = 'Y-m-d'; | 
					
						
							|  |  |  |         if ($start->diffInMonths($end) > 1) { | 
					
						
							|  |  |  |             $format = 'Y-m'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($start->diffInMonths($end) > 12) { | 
					
						
							|  |  |  |             $format = 'Y'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $format; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-26 08:41:15 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-12-30 13:47:23 +01:00
										 |  |  |      * If the date difference between start and end is less than a month, method returns trans(config.month_and_day). If the difference is less than a year, | 
					
						
							|  |  |  |      * method returns "config.month". If the date difference is larger, method returns "config.year". | 
					
						
							| 
									
										
										
										
											2016-11-26 08:41:15 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Carbon  $start | 
					
						
							|  |  |  |      * @param  Carbon  $end | 
					
						
							| 
									
										
										
										
											2016-11-26 08:41:15 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-12-30 13:47:23 +01:00
										 |  |  |     public function preferredCarbonLocalizedFormat(Carbon $start, Carbon $end): string | 
					
						
							| 
									
										
										
										
											2016-11-26 08:41:15 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-04-19 06:51:40 +02:00
										 |  |  |         $locale = app('steam')->getLocale(); | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |         $format = (string)trans('config.month_and_day_js', [], $locale); | 
					
						
							| 
									
										
										
										
											2016-11-26 08:41:15 +01:00
										 |  |  |         if ($start->diffInMonths($end) > 1) { | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |             $format = (string)trans('config.month_js', [], $locale); | 
					
						
							| 
									
										
										
										
											2016-11-26 08:41:15 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($start->diffInMonths($end) > 12) { | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |             $format = (string)trans('config.year_js', [], $locale); | 
					
						
							| 
									
										
										
										
											2016-11-26 08:41:15 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-12-30 13:47:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-15 13:47:28 +01:00
										 |  |  |         return $format; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-12-30 13:47:23 +01:00
										 |  |  |      * If the date difference between start and end is less than a month, method returns "endOfDay". If the difference is less than a year, | 
					
						
							|  |  |  |      * method returns "endOfMonth". If the date difference is larger, method returns "endOfYear". | 
					
						
							| 
									
										
										
										
											2016-12-15 13:47:28 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Carbon  $start | 
					
						
							|  |  |  |      * @param  Carbon  $end | 
					
						
							| 
									
										
										
										
											2016-12-15 13:47:28 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-12-30 13:47:23 +01:00
										 |  |  |     public function preferredEndOfPeriod(Carbon $start, Carbon $end): string | 
					
						
							| 
									
										
										
										
											2016-12-15 13:47:28 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-12-30 13:47:23 +01:00
										 |  |  |         $format = 'endOfDay'; | 
					
						
							| 
									
										
										
										
											2016-12-15 13:47:28 +01:00
										 |  |  |         if ($start->diffInMonths($end) > 1) { | 
					
						
							| 
									
										
										
										
											2016-12-30 13:47:23 +01:00
										 |  |  |             $format = 'endOfMonth'; | 
					
						
							| 
									
										
										
										
											2016-12-15 13:47:28 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($start->diffInMonths($end) > 12) { | 
					
						
							| 
									
										
										
										
											2016-12-30 13:47:23 +01:00
										 |  |  |             $format = 'endOfYear'; | 
					
						
							| 
									
										
										
										
											2016-12-15 13:47:28 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-11-26 08:41:15 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $format; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * If the date difference between start and end is less than a month, method returns "1D". If the difference is less than a year, | 
					
						
							|  |  |  |      * method returns "1M". If the date difference is larger, method returns "1Y". | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Carbon  $start | 
					
						
							|  |  |  |      * @param  Carbon  $end | 
					
						
							| 
									
										
										
										
											2016-11-26 08:41:15 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function preferredRangeFormat(Carbon $start, Carbon $end): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $format = '1D'; | 
					
						
							|  |  |  |         if ($start->diffInMonths($end) > 1) { | 
					
						
							|  |  |  |             $format = '1M'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($start->diffInMonths($end) > 12) { | 
					
						
							|  |  |  |             $format = '1Y'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $format; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * If the date difference between start and end is less than a month, method returns "%Y-%m-%d". If the difference is less than a year, | 
					
						
							|  |  |  |      * method returns "%Y-%m". If the date difference is larger, method returns "%Y". | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Carbon  $start | 
					
						
							|  |  |  |      * @param  Carbon  $end | 
					
						
							| 
									
										
										
										
											2016-11-26 08:41:15 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function preferredSqlFormat(Carbon $start, Carbon $end): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $format = '%Y-%m-%d'; | 
					
						
							|  |  |  |         if ($start->diffInMonths($end) > 1) { | 
					
						
							|  |  |  |             $format = '%Y-%m'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($start->diffInMonths($end) > 12) { | 
					
						
							|  |  |  |             $format = '%Y'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $format; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param  Carbon  $theDate | 
					
						
							|  |  |  |      * @param  string  $repeatFreq | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return Carbon | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function startOfPeriod(Carbon $theDate, string $repeatFreq): Carbon | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $date = clone $theDate; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $functionMap = [ | 
					
						
							|  |  |  |             '1D'        => 'startOfDay', | 
					
						
							|  |  |  |             'daily'     => 'startOfDay', | 
					
						
							|  |  |  |             '1W'        => 'startOfWeek', | 
					
						
							|  |  |  |             'week'      => 'startOfWeek', | 
					
						
							|  |  |  |             'weekly'    => 'startOfWeek', | 
					
						
							|  |  |  |             'month'     => 'startOfMonth', | 
					
						
							|  |  |  |             '1M'        => 'startOfMonth', | 
					
						
							|  |  |  |             'monthly'   => 'startOfMonth', | 
					
						
							|  |  |  |             '3M'        => 'firstOfQuarter', | 
					
						
							|  |  |  |             'quarter'   => 'firstOfQuarter', | 
					
						
							|  |  |  |             'quarterly' => 'firstOfQuarter', | 
					
						
							|  |  |  |             'year'      => 'startOfYear', | 
					
						
							|  |  |  |             'yearly'    => 'startOfYear', | 
					
						
							|  |  |  |             '1Y'        => 'startOfYear', | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         if (array_key_exists($repeatFreq, $functionMap)) { | 
					
						
							|  |  |  |             $function = $functionMap[$repeatFreq]; | 
					
						
							|  |  |  |             $date->$function(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $date; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ('half-year' === $repeatFreq || '6M' === $repeatFreq) { | 
					
						
							|  |  |  |             $month = $date->month; | 
					
						
							|  |  |  |             $date->startOfYear(); | 
					
						
							|  |  |  |             if ($month >= 7) { | 
					
						
							|  |  |  |                 $date->addMonths(6); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $date; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $result = match ($repeatFreq) { | 
					
						
							|  |  |  |             'last7' => $date->subDays(7)->startOfDay(), | 
					
						
							|  |  |  |             'last30' => $date->subDays(30)->startOfDay(), | 
					
						
							|  |  |  |             'last90' => $date->subDays(90)->startOfDay(), | 
					
						
							|  |  |  |             'last365' => $date->subDays(365)->startOfDay(), | 
					
						
							|  |  |  |             'MTD' => $date->startOfMonth()->startOfDay(), | 
					
						
							|  |  |  |             'QTD' => $date->firstOfQuarter()->startOfDay(), | 
					
						
							|  |  |  |             'YTD' => $date->startOfYear()->startOfDay(), | 
					
						
							|  |  |  |             default => null, | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |         if (null !== $result) { | 
					
						
							|  |  |  |             return $result; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ('custom' === $repeatFreq) { | 
					
						
							|  |  |  |             return $date; // the date is already at the start.
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Log::error(sprintf('Cannot do startOfPeriod for $repeat_freq "%s"', $repeatFreq)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $theDate; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-29 21:27:51 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Carbon  $theDate | 
					
						
							|  |  |  |      * @param  string  $repeatFreq | 
					
						
							|  |  |  |      * @param  int|null  $subtract | 
					
						
							| 
									
										
										
										
											2015-03-29 21:27:51 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-05-24 08:57:02 +02:00
										 |  |  |      * @return Carbon | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-05-24 08:57:02 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2015-03-29 21:27:51 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-07-15 10:00:08 +02:00
										 |  |  |     public function subtractPeriod(Carbon $theDate, string $repeatFreq, int $subtract = null): Carbon | 
					
						
							| 
									
										
										
										
											2015-03-29 21:27:51 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-07-15 10:00:08 +02:00
										 |  |  |         $subtract = $subtract ?? 1; | 
					
						
							|  |  |  |         $date     = clone $theDate; | 
					
						
							| 
									
										
										
										
											2015-09-25 17:28:42 +02:00
										 |  |  |         // 1D 1W 1M 3M 6M 1Y
 | 
					
						
							| 
									
										
										
										
											2015-03-29 21:27:51 +02:00
										 |  |  |         $functionMap = [ | 
					
						
							| 
									
										
										
										
											2015-09-25 17:28:42 +02:00
										 |  |  |             '1D'      => 'subDays', | 
					
						
							| 
									
										
										
										
											2015-03-29 21:27:51 +02:00
										 |  |  |             'daily'   => 'subDays', | 
					
						
							|  |  |  |             'week'    => 'subWeeks', | 
					
						
							| 
									
										
										
										
											2015-09-25 17:28:42 +02:00
										 |  |  |             '1W'      => 'subWeeks', | 
					
						
							| 
									
										
										
										
											2015-03-29 21:27:51 +02:00
										 |  |  |             'weekly'  => 'subWeeks', | 
					
						
							|  |  |  |             'month'   => 'subMonths', | 
					
						
							| 
									
										
										
										
											2015-09-25 16:00:14 +02:00
										 |  |  |             '1M'      => 'subMonths', | 
					
						
							| 
									
										
										
										
											2015-03-29 21:27:51 +02:00
										 |  |  |             'monthly' => 'subMonths', | 
					
						
							|  |  |  |             'year'    => 'subYears', | 
					
						
							| 
									
										
										
										
											2016-02-05 06:56:10 +01:00
										 |  |  |             '1Y'      => 'subYears', | 
					
						
							| 
									
										
										
										
											2015-03-29 21:27:51 +02:00
										 |  |  |             'yearly'  => 'subYears', | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         $modifierMap = [ | 
					
						
							|  |  |  |             'quarter'   => 3, | 
					
						
							| 
									
										
										
										
											2016-02-05 06:56:10 +01:00
										 |  |  |             '3M'        => 3, | 
					
						
							| 
									
										
										
										
											2015-03-29 21:27:51 +02:00
										 |  |  |             'quarterly' => 3, | 
					
						
							|  |  |  |             'half-year' => 6, | 
					
						
							| 
									
										
										
										
											2016-02-05 06:56:10 +01:00
										 |  |  |             '6M'        => 6, | 
					
						
							| 
									
										
										
										
											2015-03-29 21:27:51 +02:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2021-04-06 17:00:16 +02:00
										 |  |  |         if (array_key_exists($repeatFreq, $functionMap)) { | 
					
						
							| 
									
										
										
										
											2015-03-29 21:27:51 +02:00
										 |  |  |             $function = $functionMap[$repeatFreq]; | 
					
						
							|  |  |  |             $date->$function($subtract); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $date; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-04-06 17:00:16 +02:00
										 |  |  |         if (array_key_exists($repeatFreq, $modifierMap)) { | 
					
						
							| 
									
										
										
										
											2018-03-29 19:01:47 +02:00
										 |  |  |             $subtract *= $modifierMap[$repeatFreq]; | 
					
						
							| 
									
										
										
										
											2015-03-29 21:27:51 +02:00
										 |  |  |             $date->subMonths($subtract); | 
					
						
							| 
									
										
										
										
											2017-12-29 09:05:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-29 21:27:51 +02:00
										 |  |  |             return $date; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-02-05 07:21:51 +01:00
										 |  |  |         // a custom range requires the session start
 | 
					
						
							|  |  |  |         // and session end to calculate the difference in days.
 | 
					
						
							|  |  |  |         // this is then subtracted from $theDate (* $subtract).
 | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if ('custom' === $repeatFreq) { | 
					
						
							| 
									
										
										
										
											2016-02-05 07:21:51 +01:00
										 |  |  |             /** @var Carbon $tStart */ | 
					
						
							| 
									
										
										
										
											2023-02-11 07:36:45 +01:00
										 |  |  |             $tStart = session('start', today(config('app.timezone'))->startOfMonth()); | 
					
						
							| 
									
										
										
										
											2016-02-05 07:21:51 +01:00
										 |  |  |             /** @var Carbon $tEnd */ | 
					
						
							| 
									
										
										
										
											2023-02-11 07:36:45 +01:00
										 |  |  |             $tEnd       = session('end', today(config('app.timezone'))->endOfMonth()); | 
					
						
							| 
									
										
										
										
											2016-02-05 07:21:51 +01:00
										 |  |  |             $diffInDays = $tStart->diffInDays($tEnd); | 
					
						
							|  |  |  |             $date->subDays($diffInDays * $subtract); | 
					
						
							| 
									
										
										
										
											2021-04-06 17:00:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-05 07:21:51 +01:00
										 |  |  |             return $date; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-04-03 16:23:49 +02:00
										 |  |  |         switch ($repeatFreq) { | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |             case 'last7': | 
					
						
							| 
									
										
										
										
											2022-11-02 05:02:09 +01:00
										 |  |  |                 $date->subDays(7); | 
					
						
							|  |  |  |                 return $date; | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |             case 'last30': | 
					
						
							| 
									
										
										
										
											2022-11-02 05:02:09 +01:00
										 |  |  |                 $date->subDays(30); | 
					
						
							|  |  |  |                 return $date; | 
					
						
							| 
									
										
										
										
											2022-04-03 16:23:49 +02:00
										 |  |  |             case 'last90': | 
					
						
							|  |  |  |                 $date->subDays(90); | 
					
						
							|  |  |  |                 return $date; | 
					
						
							|  |  |  |             case 'last365': | 
					
						
							|  |  |  |                 $date->subDays(365); | 
					
						
							|  |  |  |                 return $date; | 
					
						
							|  |  |  |             case 'YTD': | 
					
						
							|  |  |  |                 $date->subYear(); | 
					
						
							|  |  |  |                 return $date; | 
					
						
							|  |  |  |             case 'QTD': | 
					
						
							|  |  |  |                 $date->subQuarter(); | 
					
						
							|  |  |  |                 return $date; | 
					
						
							|  |  |  |             case 'MTD': | 
					
						
							|  |  |  |                 $date->subMonth(); | 
					
						
							|  |  |  |                 return $date; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-29 21:27:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-03 21:54:07 +01:00
										 |  |  |         throw new FireflyException(sprintf('Cannot do subtractPeriod for $repeat_freq "%s"', $repeatFreq)); | 
					
						
							| 
									
										
										
										
											2015-03-29 21:27:51 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 07:35:10 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  string  $range | 
					
						
							|  |  |  |      * @param  Carbon  $start | 
					
						
							| 
									
										
										
										
											2015-02-11 07:35:10 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-05-24 08:57:02 +02:00
										 |  |  |      * @return Carbon | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-05-24 08:57:02 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2015-02-11 07:35:10 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-05 22:00:03 +02:00
										 |  |  |     public function updateEndDate(string $range, Carbon $start): Carbon | 
					
						
							| 
									
										
										
										
											2015-02-06 21:23:14 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-02-11 07:36:45 +01:00
										 |  |  |         Log::debug(sprintf('updateEndDate("%s", "%s")', $range, $start->format('Y-m-d'))); | 
					
						
							| 
									
										
										
										
											2015-02-06 21:23:14 +01:00
										 |  |  |         $functionMap = [ | 
					
						
							| 
									
										
										
										
											2016-11-20 07:24:18 +01:00
										 |  |  |             '1D'     => 'endOfDay', | 
					
						
							|  |  |  |             '1W'     => 'endOfWeek', | 
					
						
							|  |  |  |             '1M'     => 'endOfMonth', | 
					
						
							|  |  |  |             '3M'     => 'lastOfQuarter', | 
					
						
							|  |  |  |             'custom' => 'startOfMonth', // this only happens in test situations.
 | 
					
						
							| 
									
										
										
										
											2015-02-06 21:23:14 +01:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2015-06-05 16:49:16 +02:00
										 |  |  |         $end         = clone $start; | 
					
						
							| 
									
										
										
										
											2015-02-06 21:23:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 17:00:16 +02:00
										 |  |  |         if (array_key_exists($range, $functionMap)) { | 
					
						
							| 
									
										
										
										
											2015-02-06 21:23:14 +01:00
										 |  |  |             $function = $functionMap[$range]; | 
					
						
							|  |  |  |             $end->$function(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $end; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if ('6M' === $range) { | 
					
						
							| 
									
										
										
										
											2015-05-05 10:30:39 +02:00
										 |  |  |             if ($start->month >= 7) { | 
					
						
							| 
									
										
										
										
											2015-02-06 21:23:14 +01:00
										 |  |  |                 $end->endOfYear(); | 
					
						
							| 
									
										
										
										
											2016-05-20 17:58:10 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 return $end; | 
					
						
							| 
									
										
										
										
											2015-02-06 21:23:14 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-05-20 17:58:10 +02:00
										 |  |  |             $end->startOfYear()->addMonths(6); | 
					
						
							| 
									
										
										
										
											2015-02-06 21:23:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return $end; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-12-31 13:44:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // make sure 1Y takes the fiscal year into account.
 | 
					
						
							|  |  |  |         if ('1Y' === $range) { | 
					
						
							|  |  |  |             /** @var FiscalHelperInterface $fiscalHelper */ | 
					
						
							|  |  |  |             $fiscalHelper = app(FiscalHelperInterface::class); | 
					
						
							| 
									
										
										
										
											2019-01-04 17:10:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-31 13:44:24 +01:00
										 |  |  |             return $fiscalHelper->endOfFiscalYear($end); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-12-30 20:25:04 +01:00
										 |  |  |         $list = [ | 
					
						
							|  |  |  |             'last7', | 
					
						
							|  |  |  |             'last30', | 
					
						
							|  |  |  |             'last90', | 
					
						
							|  |  |  |             'last365', | 
					
						
							|  |  |  |             'YTD', | 
					
						
							|  |  |  |             'QTD', | 
					
						
							|  |  |  |             'MTD', | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  |         if (in_array($range, $list, true)) { | 
					
						
							| 
									
										
										
										
											2023-02-11 07:37:05 +01:00
										 |  |  |             $end = today(config('app.timezone')); | 
					
						
							| 
									
										
										
										
											2023-02-11 07:36:45 +01:00
										 |  |  |             $end->endOfDay(); | 
					
						
							|  |  |  |             Log::debug(sprintf('updateEndDate returns "%s"', $end->format('Y-m-d'))); | 
					
						
							| 
									
										
										
										
											2022-12-30 20:25:04 +01:00
										 |  |  |             return $end; | 
					
						
							| 
									
										
										
										
											2022-04-03 14:48:22 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-03 21:54:07 +01:00
										 |  |  |         throw new FireflyException(sprintf('updateEndDate cannot handle range "%s"', $range)); | 
					
						
							| 
									
										
										
										
											2015-02-06 21:23:14 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-11 07:35:10 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  string  $range | 
					
						
							|  |  |  |      * @param  Carbon  $start | 
					
						
							| 
									
										
										
										
											2015-02-11 07:35:10 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-05-24 08:57:02 +02:00
										 |  |  |      * @return Carbon | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-05-24 08:57:02 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2015-02-11 07:35:10 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-04-05 22:00:03 +02:00
										 |  |  |     public function updateStartDate(string $range, Carbon $start): Carbon | 
					
						
							| 
									
										
										
										
											2015-02-06 21:23:14 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-02-11 07:36:45 +01:00
										 |  |  |         Log::debug(sprintf('updateStartDate("%s", "%s")', $range, $start->format('Y-m-d'))); | 
					
						
							| 
									
										
										
										
											2015-02-06 21:23:14 +01:00
										 |  |  |         $functionMap = [ | 
					
						
							| 
									
										
										
										
											2016-11-20 07:24:18 +01:00
										 |  |  |             '1D'     => 'startOfDay', | 
					
						
							|  |  |  |             '1W'     => 'startOfWeek', | 
					
						
							|  |  |  |             '1M'     => 'startOfMonth', | 
					
						
							|  |  |  |             '3M'     => 'firstOfQuarter', | 
					
						
							|  |  |  |             'custom' => 'startOfMonth', // this only happens in test situations.
 | 
					
						
							| 
									
										
										
										
											2015-02-06 21:23:14 +01:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2021-04-06 17:00:16 +02:00
										 |  |  |         if (array_key_exists($range, $functionMap)) { | 
					
						
							| 
									
										
										
										
											2015-02-06 21:23:14 +01:00
										 |  |  |             $function = $functionMap[$range]; | 
					
						
							|  |  |  |             $start->$function(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $start; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if ('6M' === $range) { | 
					
						
							| 
									
										
										
										
											2015-05-05 10:30:39 +02:00
										 |  |  |             if ($start->month >= 7) { | 
					
						
							| 
									
										
										
										
											2015-02-06 21:23:14 +01:00
										 |  |  |                 $start->startOfYear()->addMonths(6); | 
					
						
							| 
									
										
										
										
											2016-05-20 17:53:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 return $start; | 
					
						
							| 
									
										
										
										
											2015-02-06 21:23:14 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-05-20 17:53:03 +02:00
										 |  |  |             $start->startOfYear(); | 
					
						
							| 
									
										
										
										
											2015-02-06 21:23:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return $start; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-12-31 13:44:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // make sure 1Y takes the fiscal year into account.
 | 
					
						
							|  |  |  |         if ('1Y' === $range) { | 
					
						
							|  |  |  |             /** @var FiscalHelperInterface $fiscalHelper */ | 
					
						
							|  |  |  |             $fiscalHelper = app(FiscalHelperInterface::class); | 
					
						
							| 
									
										
										
										
											2019-01-04 17:10:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-31 13:44:24 +01:00
										 |  |  |             return $fiscalHelper->startOfFiscalYear($start); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-04-03 14:48:22 +02:00
										 |  |  |         switch ($range) { | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |             case 'last7': | 
					
						
							| 
									
										
										
										
											2022-11-02 05:02:09 +01:00
										 |  |  |                 $start->subDays(7); | 
					
						
							|  |  |  |                 return $start; | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |             case 'last30': | 
					
						
							| 
									
										
										
										
											2022-11-02 05:02:09 +01:00
										 |  |  |                 $start->subDays(30); | 
					
						
							|  |  |  |                 return $start; | 
					
						
							| 
									
										
										
										
											2022-04-03 14:48:22 +02:00
										 |  |  |             case 'last90': | 
					
						
							|  |  |  |                 $start->subDays(90); | 
					
						
							|  |  |  |                 return $start; | 
					
						
							|  |  |  |             case 'last365': | 
					
						
							|  |  |  |                 $start->subDays(365); | 
					
						
							|  |  |  |                 return $start; | 
					
						
							|  |  |  |             case 'YTD': | 
					
						
							|  |  |  |                 $start->startOfYear(); | 
					
						
							|  |  |  |                 return $start; | 
					
						
							|  |  |  |             case 'QTD': | 
					
						
							|  |  |  |                 $start->startOfQuarter(); | 
					
						
							|  |  |  |                 return $start; | 
					
						
							|  |  |  |             case 'MTD': | 
					
						
							|  |  |  |                 $start->startOfMonth(); | 
					
						
							|  |  |  |                 return $start; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-11-03 21:54:07 +01:00
										 |  |  |         throw new FireflyException(sprintf('updateStartDate cannot handle range "%s"', $range)); | 
					
						
							| 
									
										
										
										
											2015-02-06 21:23:14 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-03-29 08:14:32 +02:00
										 |  |  | } |