| 
									
										
										
										
											2019-01-27 11:00:28 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2019-02-09 10:36:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * CalculateXOccurrences.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:56:52 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2019-02-09 10:36:59 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2019-02-09 10:36:59 +01: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. | 
					
						
							| 
									
										
										
										
											2019-02-09 10:36:59 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2019-02-09 10:36:59 +01: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. | 
					
						
							| 
									
										
										
										
											2019-02-09 10:36:59 +01: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/>. | 
					
						
							| 
									
										
										
										
											2019-02-09 10:36:59 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 11:00:28 +01:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Support\Repositories\Recurring; | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 11:00:28 +01:00
										 |  |  | use Carbon\Carbon; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class CalculateXOccurrences | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | trait CalculateXOccurrences | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Calculates the number of daily occurrences for a recurring transaction, starting at the date, until $count is reached. It will skip | 
					
						
							|  |  |  |      * over $skipMod -1 recurrences. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Carbon  $date | 
					
						
							|  |  |  |      * @param  int  $count | 
					
						
							|  |  |  |      * @param  int  $skipMod | 
					
						
							| 
									
										
										
										
											2019-01-27 11:00:28 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function getXDailyOccurrences(Carbon $date, int $count, int $skipMod): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $return   = []; | 
					
						
							|  |  |  |         $mutator  = clone $date; | 
					
						
							|  |  |  |         $total    = 0; | 
					
						
							|  |  |  |         $attempts = 0; | 
					
						
							|  |  |  |         while ($total < $count) { | 
					
						
							|  |  |  |             $mutator->addDay(); | 
					
						
							|  |  |  |             if (0 === $attempts % $skipMod) { | 
					
						
							|  |  |  |                 $return[] = clone $mutator; | 
					
						
							|  |  |  |                 $total++; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $attempts++; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 11:00:28 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Calculates the number of monthly occurrences for a recurring transaction, starting at the date, until $count is reached. It will skip | 
					
						
							|  |  |  |      * over $skipMod -1 recurrences. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Carbon  $date | 
					
						
							|  |  |  |      * @param  int  $count | 
					
						
							|  |  |  |      * @param  int  $skipMod | 
					
						
							|  |  |  |      * @param  string  $moment | 
					
						
							| 
									
										
										
										
											2019-01-27 11:00:28 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function getXMonthlyOccurrences(Carbon $date, int $count, int $skipMod, string $moment): array | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-02-12 21:49:28 +01:00
										 |  |  |         $return     = []; | 
					
						
							|  |  |  |         $mutator    = clone $date; | 
					
						
							|  |  |  |         $total      = 0; | 
					
						
							|  |  |  |         $attempts   = 0; | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |         $dayOfMonth = (int)$moment; | 
					
						
							| 
									
										
										
										
											2019-01-27 11:00:28 +01:00
										 |  |  |         if ($mutator->day > $dayOfMonth) { | 
					
						
							|  |  |  |             // day has passed already, add a month.
 | 
					
						
							|  |  |  |             $mutator->addMonth(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         while ($total < $count) { | 
					
						
							|  |  |  |             $domCorrected = min($dayOfMonth, $mutator->daysInMonth); | 
					
						
							|  |  |  |             $mutator->day = $domCorrected; | 
					
						
							|  |  |  |             if (0 === $attempts % $skipMod) { | 
					
						
							|  |  |  |                 $return[] = clone $mutator; | 
					
						
							|  |  |  |                 $total++; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $attempts++; | 
					
						
							|  |  |  |             $mutator->endOfMonth()->addDay(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 11:00:28 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Calculates the number of NDOM occurrences for a recurring transaction, starting at the date, until $count is reached. It will skip | 
					
						
							|  |  |  |      * over $skipMod -1 recurrences. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Carbon  $date | 
					
						
							|  |  |  |      * @param  int  $count | 
					
						
							|  |  |  |      * @param  int  $skipMod | 
					
						
							|  |  |  |      * @param  string  $moment | 
					
						
							| 
									
										
										
										
											2019-01-27 11:00:28 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function getXNDomOccurrences(Carbon $date, int $count, int $skipMod, string $moment): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $return   = []; | 
					
						
							|  |  |  |         $total    = 0; | 
					
						
							|  |  |  |         $attempts = 0; | 
					
						
							|  |  |  |         $mutator  = clone $date; | 
					
						
							|  |  |  |         $mutator->addDay(); // always assume today has passed.
 | 
					
						
							|  |  |  |         $mutator->startOfMonth(); | 
					
						
							|  |  |  |         // this feels a bit like a cop out but why reinvent the wheel?
 | 
					
						
							|  |  |  |         $counters   = [1 => 'first', 2 => 'second', 3 => 'third', 4 => 'fourth', 5 => 'fifth',]; | 
					
						
							|  |  |  |         $daysOfWeek = [1 => 'Monday', 2 => 'Tuesday', 3 => 'Wednesday', 4 => 'Thursday', 5 => 'Friday', 6 => 'Saturday', 7 => 'Sunday',]; | 
					
						
							|  |  |  |         $parts      = explode(',', $moment); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         while ($total < $count) { | 
					
						
							|  |  |  |             $string    = sprintf('%s %s of %s %s', $counters[$parts[0]], $daysOfWeek[$parts[1]], $mutator->format('F'), $mutator->format('Y')); | 
					
						
							|  |  |  |             $newCarbon = new Carbon($string); | 
					
						
							|  |  |  |             if (0 === $attempts % $skipMod) { | 
					
						
							|  |  |  |                 $return[] = clone $newCarbon; | 
					
						
							|  |  |  |                 $total++; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $attempts++; | 
					
						
							|  |  |  |             $mutator->endOfMonth()->addDay(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 11:00:28 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Calculates the number of weekly occurrences for a recurring transaction, starting at the date, until $count is reached. It will skip | 
					
						
							|  |  |  |      * over $skipMod -1 recurrences. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Carbon  $date | 
					
						
							|  |  |  |      * @param  int  $count | 
					
						
							|  |  |  |      * @param  int  $skipMod | 
					
						
							|  |  |  |      * @param  string  $moment | 
					
						
							| 
									
										
										
										
											2019-01-27 11:00:28 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function getXWeeklyOccurrences(Carbon $date, int $count, int $skipMod, string $moment): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $return   = []; | 
					
						
							|  |  |  |         $total    = 0; | 
					
						
							|  |  |  |         $attempts = 0; | 
					
						
							|  |  |  |         $mutator  = clone $date; | 
					
						
							|  |  |  |         // monday = 1
 | 
					
						
							|  |  |  |         // sunday = 7
 | 
					
						
							|  |  |  |         $mutator->addDay(); // always assume today has passed.
 | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |         $dayOfWeek = (int)$moment; | 
					
						
							| 
									
										
										
										
											2019-01-27 11:00:28 +01:00
										 |  |  |         if ($mutator->dayOfWeekIso > $dayOfWeek) { | 
					
						
							|  |  |  |             // day has already passed this week, add one week:
 | 
					
						
							|  |  |  |             $mutator->addWeek(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // today is wednesday (3), expected is friday (5): add two days.
 | 
					
						
							|  |  |  |         // today is friday (5), expected is monday (1), subtract four days.
 | 
					
						
							|  |  |  |         $dayDifference = $dayOfWeek - $mutator->dayOfWeekIso; | 
					
						
							|  |  |  |         $mutator->addDays($dayDifference); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         while ($total < $count) { | 
					
						
							|  |  |  |             if (0 === $attempts % $skipMod) { | 
					
						
							|  |  |  |                 $return[] = clone $mutator; | 
					
						
							|  |  |  |                 $total++; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $attempts++; | 
					
						
							|  |  |  |             $mutator->addWeek(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-27 11:00:28 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Calculates the number of yearly occurrences for a recurring transaction, starting at the date, until $count is reached. It will skip | 
					
						
							|  |  |  |      * over $skipMod -1 recurrences. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |      * @param  Carbon  $date | 
					
						
							|  |  |  |      * @param  int  $count | 
					
						
							|  |  |  |      * @param  int  $skipMod | 
					
						
							|  |  |  |      * @param  string  $moment | 
					
						
							| 
									
										
										
										
											2019-01-27 11:00:28 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function getXYearlyOccurrences(Carbon $date, int $count, int $skipMod, string $moment): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $return     = []; | 
					
						
							|  |  |  |         $mutator    = clone $date; | 
					
						
							|  |  |  |         $total      = 0; | 
					
						
							|  |  |  |         $attempts   = 0; | 
					
						
							|  |  |  |         $date       = new Carbon($moment); | 
					
						
							|  |  |  |         $date->year = $mutator->year; | 
					
						
							|  |  |  |         if ($mutator > $date) { | 
					
						
							|  |  |  |             $date->addYear(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $obj = clone $date; | 
					
						
							|  |  |  |         while ($total < $count) { | 
					
						
							|  |  |  |             if (0 === $attempts % $skipMod) { | 
					
						
							|  |  |  |                 $return[] = clone $obj; | 
					
						
							|  |  |  |                 $total++; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-05-24 08:54:58 +02:00
										 |  |  |             $obj->addYears(); | 
					
						
							| 
									
										
										
										
											2019-01-27 11:00:28 +01:00
										 |  |  |             $attempts++; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-09 10:36:59 +01:00
										 |  |  | } |