| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * DateCalculation.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:56:52 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +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. | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +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. | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +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/>. | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Support\Http\Controllers; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Trait DateCalculation | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | trait DateCalculation | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-14 23:22:08 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-08-07 17:34:43 +02:00
										 |  |  |      * Calculate the number of days passed left until end date, as seen from start date. | 
					
						
							|  |  |  |      * If today is between start and end, today will be used instead of end. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * If both are in the past OR both are in the future, simply return the number of days in the period with a minimum | 
					
						
							|  |  |  |      * of 1 | 
					
						
							| 
									
										
										
										
											2018-07-14 23:22:08 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-08-07 17:34:43 +02:00
										 |  |  |     public function activeDaysLeft(Carbon $start, Carbon $end): int | 
					
						
							| 
									
										
										
										
											2018-07-14 23:22:08 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         $difference = (int) ($start->diffInDays($end, true) + 1); | 
					
						
							| 
									
										
										
										
											2023-02-11 07:36:45 +01:00
										 |  |  |         $today      = today(config('app.timezone'))->startOfDay(); | 
					
						
							| 
									
										
										
										
											2018-08-07 17:34:43 +02:00
										 |  |  |         if ($start->lte($today) && $end->gte($today)) { | 
					
						
							| 
									
										
										
										
											2024-08-16 09:39:29 +02:00
										 |  |  |             $difference = $today->diffInDays($end) + 1; | 
					
						
							| 
									
										
										
										
											2018-07-14 23:22:08 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         return (int) (0 === $difference ? 1 : $difference); | 
					
						
							| 
									
										
										
										
											2018-07-14 23:22:08 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-08-07 17:34:43 +02:00
										 |  |  |      * Calculate the number of days passed between two dates. Will take the current moment into consideration. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * If both are in the past OR both are in the future, simply return the period between them with a minimum of 1 | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-08-07 17:34:43 +02:00
										 |  |  |     protected function activeDaysPassed(Carbon $start, Carbon $end): int | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-03-17 12:00:28 +01:00
										 |  |  |         $difference = $start->diffInDays($end, true) + 1; | 
					
						
							| 
									
										
										
										
											2023-02-11 07:36:45 +01:00
										 |  |  |         $today      = today(config('app.timezone'))->startOfDay(); | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 17:34:43 +02:00
										 |  |  |         if ($start->lte($today) && $end->gte($today)) { | 
					
						
							| 
									
										
										
										
											2024-03-17 12:00:28 +01:00
										 |  |  |             $difference = $start->diffInDays($today, true) + 1; | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 08:43:12 +01:00
										 |  |  |         return (int) $difference; | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 17:34:43 +02:00
										 |  |  |     protected function calculateStep(Carbon $start, Carbon $end): string | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-08-07 17:34:43 +02:00
										 |  |  |         $step   = '1D'; | 
					
						
							| 
									
										
										
										
											2024-03-17 12:00:28 +01:00
										 |  |  |         $months = $start->diffInMonths($end, true); | 
					
						
							| 
									
										
										
										
											2018-08-07 17:34:43 +02:00
										 |  |  |         if ($months > 3) { | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |             $step = '1W'; | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-08-07 17:34:43 +02:00
										 |  |  |         if ($months > 24) { | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  |             $step = '1M'; | 
					
						
							| 
									
										
										
										
											2018-08-07 17:34:43 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         if ($months > 100) { | 
					
						
							| 
									
										
										
										
											2025-05-27 17:06:15 +02:00
										 |  |  |             return '1Y'; | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-07 17:34:43 +02:00
										 |  |  |         return $step; | 
					
						
							| 
									
										
										
										
											2018-07-14 16:08:34 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-14 17:23:44 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get a list of the periods that will occur after this date. For example, | 
					
						
							|  |  |  |      * March 2018, April 2018, etc. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function getNextPeriods(Carbon $date, string $range): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // select thing for next 12 periods:
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $loop    = []; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-14 17:23:44 +02:00
										 |  |  |         /** @var Carbon $current */ | 
					
						
							| 
									
										
										
										
											2018-08-08 17:53:40 +02:00
										 |  |  |         $current = app('navigation')->startOfPeriod($date, $range); | 
					
						
							|  |  |  |         $current = app('navigation')->endOfPeriod($current, $range); | 
					
						
							| 
									
										
										
										
											2018-07-14 17:23:44 +02:00
										 |  |  |         $current->addDay(); | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $count   = 0; | 
					
						
							| 
									
										
										
										
											2018-07-14 17:23:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         while ($count < 12) { | 
					
						
							| 
									
										
										
										
											2019-08-31 09:35:35 +02:00
										 |  |  |             $current      = app('navigation')->endOfPeriod($current, $range); | 
					
						
							|  |  |  |             $currentStart = app('navigation')->startOfPeriod($current, $range); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |             $loop[]       = [ | 
					
						
							| 
									
										
										
										
											2019-08-31 09:35:35 +02:00
										 |  |  |                 'label' => $current->format('Y-m-d'), | 
					
						
							|  |  |  |                 'title' => app('navigation')->periodShow($current, $range), | 
					
						
							|  |  |  |                 'start' => clone $currentStart, | 
					
						
							|  |  |  |                 'end'   => clone $current, | 
					
						
							|  |  |  |             ]; | 
					
						
							| 
									
										
										
										
											2018-07-14 17:23:44 +02:00
										 |  |  |             ++$count; | 
					
						
							|  |  |  |             $current->addDay(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $loop; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get a list of the periods that occurred before the start date. For example, | 
					
						
							|  |  |  |      * March 2018, February 2018, etc. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function getPreviousPeriods(Carbon $date, string $range): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // select thing for last 12 periods:
 | 
					
						
							| 
									
										
										
										
											2024-12-22 20:37:54 +01:00
										 |  |  |         $loop    = []; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-14 17:23:44 +02:00
										 |  |  |         /** @var Carbon $current */ | 
					
						
							| 
									
										
										
										
											2018-08-08 17:53:40 +02:00
										 |  |  |         $current = app('navigation')->startOfPeriod($date, $range); | 
					
						
							| 
									
										
										
										
											2018-07-14 17:23:44 +02:00
										 |  |  |         $count   = 0; | 
					
						
							|  |  |  |         while ($count < 12) { | 
					
						
							|  |  |  |             $current->subDay(); | 
					
						
							| 
									
										
										
										
											2019-08-31 09:35:35 +02:00
										 |  |  |             $current    = app('navigation')->startOfPeriod($current, $range); | 
					
						
							|  |  |  |             $currentEnd = app('navigation')->endOfPeriod($current, $range); | 
					
						
							|  |  |  |             $loop[]     = [ | 
					
						
							|  |  |  |                 'label' => $current->format('Y-m-d'), | 
					
						
							|  |  |  |                 'title' => app('navigation')->periodShow($current, $range), | 
					
						
							|  |  |  |                 'start' => clone $current, | 
					
						
							|  |  |  |                 'end'   => clone $currentEnd, | 
					
						
							|  |  |  |             ]; | 
					
						
							| 
									
										
										
										
											2018-07-14 17:23:44 +02:00
										 |  |  |             ++$count; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $loop; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-22 20:32:02 +02:00
										 |  |  | } |