| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2018-12-31 07:48:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * CreateRecurringTransactions.php | 
					
						
							| 
									
										
										
										
											2020-02-07 11:16:38 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-12-31 07:48:23 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-12-31 07:48:23 +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. | 
					
						
							| 
									
										
										
										
											2018-12-31 07:48:23 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-12-31 07:48:23 +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. | 
					
						
							| 
									
										
										
										
											2018-12-31 07:48:23 +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/>. | 
					
						
							| 
									
										
										
										
											2018-12-31 07:48:23 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-22 20:32:02 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  | namespace FireflyIII\Jobs; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2018-06-25 16:01:45 +02:00
										 |  |  | use FireflyIII\Events\RequestedReportOnJournals; | 
					
						
							| 
									
										
										
										
											2019-03-30 11:03:39 +01:00
										 |  |  | use FireflyIII\Events\StoredTransactionGroup; | 
					
						
							| 
									
										
										
										
											2021-09-18 10:20:19 +02:00
										 |  |  | use FireflyIII\Exceptions\DuplicateTransactionException; | 
					
						
							|  |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  | use FireflyIII\Models\Recurrence; | 
					
						
							|  |  |  | use FireflyIII\Models\RecurrenceRepetition; | 
					
						
							|  |  |  | use FireflyIII\Models\RecurrenceTransaction; | 
					
						
							| 
									
										
										
										
											2019-06-07 17:57:46 +02:00
										 |  |  | use FireflyIII\Models\TransactionGroup; | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  | use FireflyIII\Repositories\Journal\JournalRepositoryInterface; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2019-06-07 17:57:46 +02:00
										 |  |  | use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  | use Illuminate\Bus\Queueable; | 
					
						
							|  |  |  | use Illuminate\Contracts\Queue\ShouldQueue; | 
					
						
							|  |  |  | use Illuminate\Foundation\Bus\Dispatchable; | 
					
						
							|  |  |  | use Illuminate\Queue\InteractsWithQueue; | 
					
						
							|  |  |  | use Illuminate\Queue\SerializesModels; | 
					
						
							|  |  |  | use Illuminate\Support\Collection; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2018-07-22 15:20:45 +02:00
										 |  |  |  * Class CreateRecurringTransactions. | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | class CreateRecurringTransactions implements ShouldQueue | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:28 +01:00
										 |  |  |     use Dispatchable; | 
					
						
							|  |  |  |     use InteractsWithQueue; | 
					
						
							|  |  |  |     use Queueable; | 
					
						
							|  |  |  |     use SerializesModels; | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-24 17:27:36 +02:00
										 |  |  |     public int                                  $created; | 
					
						
							|  |  |  |     public int                                  $executed; | 
					
						
							|  |  |  |     public int                                  $submitted; | 
					
						
							|  |  |  |     private Carbon                              $date; | 
					
						
							|  |  |  |     private bool                                $force; | 
					
						
							|  |  |  |     private TransactionGroupRepositoryInterface $groupRepository; | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |     private Collection                          $groups; | 
					
						
							| 
									
										
										
										
											2020-10-24 17:27:36 +02:00
										 |  |  |     private JournalRepositoryInterface          $journalRepository; | 
					
						
							| 
									
										
										
										
											2022-12-29 15:42:02 +01:00
										 |  |  |     private Collection                          $recurrences; | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |     private RecurringRepositoryInterface        $repository; | 
					
						
							| 
									
										
										
										
											2019-07-31 16:53:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Create a new job instance. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-07-31 16:53:09 +02:00
										 |  |  |     public function __construct(?Carbon $date) | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $newDate                 = new Carbon(); | 
					
						
							| 
									
										
										
										
											2022-12-29 15:42:02 +01:00
										 |  |  |         $newDate->startOfDay(); | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $this->date              = $newDate; | 
					
						
							| 
									
										
										
										
											2022-12-29 15:42:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-31 16:53:09 +02:00
										 |  |  |         if (null !== $date) { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $newDate    = clone $date; | 
					
						
							| 
									
										
										
										
											2021-03-27 20:01:28 +01:00
										 |  |  |             $newDate->startOfDay(); | 
					
						
							|  |  |  |             $this->date = $newDate; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |         $this->repository        = app(RecurringRepositoryInterface::class); | 
					
						
							|  |  |  |         $this->journalRepository = app(JournalRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2019-06-07 17:57:46 +02:00
										 |  |  |         $this->groupRepository   = app(TransactionGroupRepositoryInterface::class); | 
					
						
							|  |  |  |         $this->force             = false; | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |         $this->submitted         = 0; | 
					
						
							|  |  |  |         $this->executed          = 0; | 
					
						
							|  |  |  |         $this->created           = 0; | 
					
						
							| 
									
										
										
										
											2022-12-29 15:42:02 +01:00
										 |  |  |         $this->recurrences       = new Collection(); | 
					
						
							|  |  |  |         $this->groups            = new Collection(); | 
					
						
							| 
									
										
										
										
											2018-07-02 20:17:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug(sprintf('Created new CreateRecurringTransactions("%s")', $this->date->format('Y-m-d'))); | 
					
						
							| 
									
										
										
										
											2019-06-07 17:57:46 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |     public function getGroups(): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->groups; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Execute the job. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function handle(): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug(sprintf('Now at start of CreateRecurringTransactions() job for %s.', $this->date->format('D d M Y'))); | 
					
						
							| 
									
										
										
										
											2022-12-29 15:42:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // only use recurrences from database if there is no collection submitted.
 | 
					
						
							|  |  |  |         if (0 !== count($this->recurrences)) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |             app('log')->debug('Using predetermined set of recurrences.'); | 
					
						
							| 
									
										
										
										
											2022-12-29 15:42:02 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         if (0 === count($this->recurrences)) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |             app('log')->debug('Grab all recurrences from the database.'); | 
					
						
							| 
									
										
										
										
											2022-12-29 15:42:02 +01:00
										 |  |  |             $this->recurrences = $this->repository->getAll(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |         $result          = []; | 
					
						
							| 
									
										
										
										
											2022-12-29 15:42:02 +01:00
										 |  |  |         $count           = $this->recurrences->count(); | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |         $this->submitted = $count; | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug(sprintf('Count of collection is %d', $count)); | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // filter recurrences:
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $filtered        = $this->filterRecurrences($this->recurrences); | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug(sprintf('Left after filtering is %d', $filtered->count())); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |         /** @var Recurrence $recurrence */ | 
					
						
							|  |  |  |         foreach ($filtered as $recurrence) { | 
					
						
							| 
									
										
										
										
											2021-04-07 07:28:43 +02:00
										 |  |  |             if (!array_key_exists($recurrence->user_id, $result)) { | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:28 +01:00
										 |  |  |                 $result[$recurrence->user_id] = new Collection(); | 
					
						
							| 
									
										
										
										
											2018-06-23 08:19:29 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |             $this->repository->setUser($recurrence->user); | 
					
						
							|  |  |  |             $this->journalRepository->setUser($recurrence->user); | 
					
						
							| 
									
										
										
										
											2019-06-07 17:57:46 +02:00
										 |  |  |             $this->groupRepository->setUser($recurrence->user); | 
					
						
							| 
									
										
										
										
											2019-10-21 18:22:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // clear cache for user
 | 
					
						
							|  |  |  |             app('preferences')->setForUser($recurrence->user, 'lastActivity', microtime()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |             app('log')->debug(sprintf('Now at recurrence #%d of user #%d', $recurrence->id, $recurrence->user_id)); | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $createdReps                  = $this->handleRepetitions($recurrence); | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |             app('log')->debug(sprintf('Done with recurrence #%d', $recurrence->id)); | 
					
						
							| 
									
										
										
										
											2020-10-24 17:27:36 +02:00
										 |  |  |             $result[$recurrence->user_id] = $result[$recurrence->user_id]->merge($createdReps); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |             ++$this->executed; | 
					
						
							| 
									
										
										
										
											2018-06-23 08:19:29 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug('Now running report thing.'); | 
					
						
							| 
									
										
										
										
											2018-06-23 08:19:29 +02:00
										 |  |  |         // will now send email to users.
 | 
					
						
							| 
									
										
										
										
											2018-06-25 16:01:45 +02:00
										 |  |  |         foreach ($result as $userId => $journals) { | 
					
						
							|  |  |  |             event(new RequestedReportOnJournals($userId, $journals)); | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-06-23 08:19:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug('Done with handle()'); | 
					
						
							| 
									
										
										
										
											2018-08-21 18:20:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // clear cache:
 | 
					
						
							|  |  |  |         app('preferences')->mark(); | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |     public function setDate(Carbon $date): void | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $newDate    = clone $date; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         $newDate->startOfDay(); | 
					
						
							|  |  |  |         $this->date = $newDate; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setForce(bool $force): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->force = $force; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function setRecurrences(Collection $recurrences): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->recurrences = $recurrences; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function filterRecurrences(Collection $recurrences): Collection | 
					
						
							| 
									
										
										
										
											2020-03-17 16:46:00 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         return $recurrences->filter( | 
					
						
							|  |  |  |             function (Recurrence $recurrence) { | 
					
						
							|  |  |  |                 return $this->validRecurrence($recurrence); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2020-03-17 16:46:00 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * Is the info in the recurrence valid? | 
					
						
							| 
									
										
										
										
											2020-03-17 16:46:00 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function validRecurrence(Recurrence $recurrence): bool | 
					
						
							| 
									
										
										
										
											2020-03-17 16:46:00 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug(sprintf('Now filtering recurrence #%d, owned by user #%d', $recurrence->id, $recurrence->user_id)); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         // is not active.
 | 
					
						
							|  |  |  |         if (!$this->active($recurrence)) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:27 +01:00
										 |  |  |             app('log')->info(sprintf('Recurrence #%d is not active. Skipped.', $recurrence->id)); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // has repeated X times.
 | 
					
						
							|  |  |  |         $journalCount = $this->repository->getJournalCount($recurrence); | 
					
						
							|  |  |  |         if (0 !== $recurrence->repetitions && $journalCount >= $recurrence->repetitions && false === $this->force) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:27 +01:00
										 |  |  |             app('log')->info(sprintf('Recurrence #%d has run %d times, so will run no longer.', $recurrence->id, $recurrence->repetitions)); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // is no longer running
 | 
					
						
							|  |  |  |         if ($this->repeatUntilHasPassed($recurrence)) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:27 +01:00
										 |  |  |             app('log')->info( | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                 sprintf( | 
					
						
							|  |  |  |                     'Recurrence #%d was set to run until %s, and today\'s date is %s. Skipped.', | 
					
						
							|  |  |  |                     $recurrence->id, | 
					
						
							|  |  |  |                     $recurrence->repeat_until->format('Y-m-d'), | 
					
						
							|  |  |  |                     $this->date->format('Y-m-d') | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // first_date is in the future
 | 
					
						
							|  |  |  |         if ($this->hasNotStartedYet($recurrence)) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:27 +01:00
										 |  |  |             app('log')->info( | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                 sprintf( | 
					
						
							|  |  |  |                     'Recurrence #%d is set to run on %s, and today\'s date is %s. Skipped.', | 
					
						
							|  |  |  |                     $recurrence->id, | 
					
						
							|  |  |  |                     $recurrence->first_date->format('Y-m-d'), | 
					
						
							|  |  |  |                     $this->date->format('Y-m-d') | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // already fired today (with success):
 | 
					
						
							|  |  |  |         if (false === $this->force && $this->hasFiredToday($recurrence)) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:27 +01:00
										 |  |  |             app('log')->info(sprintf('Recurrence #%d has already fired today. Skipped.', $recurrence->id)); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug('Will be included.'); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2020-03-17 16:46:00 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * Return recurring transaction is active. | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function active(Recurrence $recurrence): bool | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         return $recurrence->active; | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-17 16:46:00 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * Return true if the $repeat_until date is in the past. | 
					
						
							| 
									
										
										
										
											2020-03-17 16:46:00 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function repeatUntilHasPassed(Recurrence $recurrence): bool | 
					
						
							| 
									
										
										
										
											2020-03-17 16:46:00 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         // date has passed
 | 
					
						
							|  |  |  |         return null !== $recurrence->repeat_until && $recurrence->repeat_until->lt($this->date); | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * Has the recurrence started yet? | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function hasNotStartedYet(Recurrence $recurrence): bool | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $startDate = $this->getStartDate($recurrence); | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug(sprintf('Start date is %s', $startDate->format('Y-m-d'))); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $startDate->gt($this->date); | 
					
						
							| 
									
										
										
										
											2020-03-17 16:46:00 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-07-22 15:20:45 +02:00
										 |  |  |      * Get the start date of a recurrence. | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     private function getStartDate(Recurrence $recurrence): Carbon | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $startDate = clone $recurrence->first_date; | 
					
						
							|  |  |  |         if (null !== $recurrence->latest_date && $recurrence->latest_date->gte($startDate)) { | 
					
						
							|  |  |  |             $startDate = clone $recurrence->latest_date; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $startDate; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * Has the recurrence fired today. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function hasFiredToday(Recurrence $recurrence): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return null !== $recurrence->latest_date && $recurrence->latest_date->eq($this->date); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Separate method that will loop all repetitions and do something with it. Will return | 
					
						
							|  |  |  |      * all created transaction journals. | 
					
						
							| 
									
										
										
										
											2019-08-17 10:47:29 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @throws DuplicateTransactionException | 
					
						
							|  |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function handleRepetitions(Recurrence $recurrence): Collection | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $collection = new Collection(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         /** @var RecurrenceRepetition $repetition */ | 
					
						
							|  |  |  |         foreach ($recurrence->recurrenceRepetitions as $repetition) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |             app('log')->debug( | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                 sprintf( | 
					
						
							|  |  |  |                     'Now repeating %s with value "%s", skips every %d time(s)', | 
					
						
							|  |  |  |                     $repetition->repetition_type, | 
					
						
							|  |  |  |                     $repetition->repetition_moment, | 
					
						
							|  |  |  |                     $repetition->repetition_skip | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // start looping from $startDate to today perhaps we have a hit?
 | 
					
						
							|  |  |  |             // add two days to $this->date, so we always include the weekend.
 | 
					
						
							|  |  |  |             $includeWeekend = clone $this->date; | 
					
						
							|  |  |  |             $includeWeekend->addDays(2); | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $occurrences    = $this->repository->getOccurrencesInRange($repetition, $recurrence->first_date, $includeWeekend); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             unset($includeWeekend); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |             $result         = $this->handleOccurrences($recurrence, $repetition, $occurrences); | 
					
						
							|  |  |  |             $collection     = $collection->merge($result); | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         return $collection; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2024-01-10 01:12:48 +00:00
										 |  |  |      * Check if the occurrences should be executed. | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @throws DuplicateTransactionException | 
					
						
							|  |  |  |      * @throws FireflyException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private function handleOccurrences(Recurrence $recurrence, RecurrenceRepetition $repetition, array $occurrences): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $collection = new Collection(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         /** @var Carbon $date */ | 
					
						
							|  |  |  |         foreach ($occurrences as $date) { | 
					
						
							|  |  |  |             $result = $this->handleOccurrence($recurrence, $repetition, $date); | 
					
						
							|  |  |  |             if (null !== $result) { | 
					
						
							|  |  |  |                 $collection->push($result); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $collection; | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-09-18 10:20:19 +02:00
										 |  |  |      * @throws DuplicateTransactionException | 
					
						
							|  |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-07-26 07:57:48 +02:00
										 |  |  |     private function handleOccurrence(Recurrence $recurrence, RecurrenceRepetition $repetition, Carbon $date): ?TransactionGroup | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-08-14 20:06:16 +02:00
										 |  |  |         $date->startOfDay(); | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |         if ($date->ne($this->date)) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-10-29 06:33:43 +01:00
										 |  |  |         app('log')->debug(sprintf('%s IS today (%s)', $date->format('Y-m-d'), $this->date->format('Y-m-d'))); | 
					
						
							| 
									
										
										
										
											2019-01-03 18:57:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |         // count created journals on THIS day.
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $journalCount            = $this->repository->getJournalCount($recurrence, $date, $date); | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |         if ($journalCount > 0 && false === $this->force) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:27 +01:00
										 |  |  |             app('log')->info(sprintf('Already created %d journal(s) for date %s', $journalCount, $date->format('Y-m-d'))); | 
					
						
							| 
									
										
										
										
											2018-12-20 22:32:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-06-07 17:57:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 15:42:02 +01:00
										 |  |  |         if ($this->repository->createdPreviously($recurrence, $date) && false === $this->force) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:27 +01:00
										 |  |  |             app('log')->info('There is a transaction already made for this date, so will not be created now'); | 
					
						
							| 
									
										
										
										
											2022-12-29 15:42:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |         if ($journalCount > 0 && true === $this->force) { | 
					
						
							| 
									
										
										
										
											2022-10-30 14:44:49 +01:00
										 |  |  |             app('log')->warning(sprintf('Already created %d groups for date %s but FORCED to continue.', $journalCount, $date->format('Y-m-d'))); | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-06-23 08:19:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |         // create transaction array and send to factory.
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $groupTitle              = null; | 
					
						
							|  |  |  |         $count                   = $recurrence->recurrenceTransactions->count(); | 
					
						
							| 
									
										
										
										
											2022-09-24 07:03:03 +02:00
										 |  |  |         if ($count > 1) { | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |             /** @var RecurrenceTransaction $first */ | 
					
						
							|  |  |  |             $first      = $recurrence->recurrenceTransactions()->first(); | 
					
						
							|  |  |  |             $groupTitle = $first->description; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:28 +01:00
										 |  |  |         if (0 === $count) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error('No transactions to be created in this recurrence. Cannot continue.'); | 
					
						
							| 
									
										
										
										
											2022-09-24 07:03:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-07-26 07:57:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $array                   = [ | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |             'user'         => $recurrence->user_id, | 
					
						
							|  |  |  |             'group_title'  => $groupTitle, | 
					
						
							| 
									
										
										
										
											2020-07-26 07:57:48 +02:00
										 |  |  |             'transactions' => $this->getTransactionData($recurrence, $repetition, $date), | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2021-05-15 12:58:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  |         /** @var TransactionGroup $group */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $group                   = $this->groupRepository->store($array); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         ++$this->created; | 
					
						
							| 
									
										
										
										
											2023-10-29 06:31:27 +01:00
										 |  |  |         app('log')->info(sprintf('Created new transaction group #%d', $group->id)); | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // trigger event:
 | 
					
						
							| 
									
										
										
										
											2022-11-02 06:25:37 +01:00
										 |  |  |         event(new StoredTransactionGroup($group, $recurrence->apply_rules, true)); | 
					
						
							| 
									
										
										
										
											2022-12-29 15:42:02 +01:00
										 |  |  |         $this->groups->push($group); | 
					
						
							| 
									
										
										
										
											2019-07-27 13:54:06 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // update recurring thing:
 | 
					
						
							|  |  |  |         $recurrence->latest_date = $date; | 
					
						
							|  |  |  |         $recurrence->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $group; | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-17 16:46:00 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * Get transaction information from a recurring transaction. | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function getTransactionData(Recurrence $recurrence, RecurrenceRepetition $repetition, Carbon $date): array | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         // total transactions expected for this recurrence:
 | 
					
						
							|  |  |  |         $total        = $this->repository->totalTransactions($recurrence, $repetition); | 
					
						
							|  |  |  |         $count        = $this->repository->getJournalCount($recurrence) + 1; | 
					
						
							|  |  |  |         $transactions = $recurrence->recurrenceTransactions()->get(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-20 06:17:56 +02:00
										 |  |  |         /** @var RecurrenceTransaction $first */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $first        = $transactions->first(); | 
					
						
							|  |  |  |         $return       = []; | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         /** @var RecurrenceTransaction $transaction */ | 
					
						
							|  |  |  |         foreach ($transactions as $index => $transaction) { | 
					
						
							|  |  |  |             $single   = [ | 
					
						
							| 
									
										
										
										
											2023-09-20 06:17:56 +02:00
										 |  |  |                 'type'                  => null === $first->transactionType ? strtolower($recurrence->transactionType->type) : strtolower($first->transactionType->type), | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                 'date'                  => $date, | 
					
						
							|  |  |  |                 'user'                  => $recurrence->user_id, | 
					
						
							| 
									
										
										
										
											2023-11-05 19:41:37 +01:00
										 |  |  |                 'currency_id'           => $transaction->transaction_currency_id, | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                 'currency_code'         => null, | 
					
						
							| 
									
										
										
										
											2023-09-20 06:17:56 +02:00
										 |  |  |                 'description'           => $first->description, | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                 'amount'                => $transaction->amount, | 
					
						
							|  |  |  |                 'budget_id'             => $this->repository->getBudget($transaction), | 
					
						
							|  |  |  |                 'budget_name'           => null, | 
					
						
							|  |  |  |                 'category_id'           => $this->repository->getCategoryId($transaction), | 
					
						
							|  |  |  |                 'category_name'         => $this->repository->getCategoryName($transaction), | 
					
						
							|  |  |  |                 'source_id'             => $transaction->source_id, | 
					
						
							|  |  |  |                 'source_name'           => null, | 
					
						
							|  |  |  |                 'destination_id'        => $transaction->destination_id, | 
					
						
							|  |  |  |                 'destination_name'      => null, | 
					
						
							|  |  |  |                 'foreign_currency_id'   => $transaction->foreign_currency_id, | 
					
						
							|  |  |  |                 'foreign_currency_code' => null, | 
					
						
							|  |  |  |                 'foreign_amount'        => $transaction->foreign_amount, | 
					
						
							|  |  |  |                 'reconciled'            => false, | 
					
						
							|  |  |  |                 'identifier'            => $index, | 
					
						
							| 
									
										
										
										
											2023-11-05 19:41:37 +01:00
										 |  |  |                 'recurrence_id'         => $recurrence->id, | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |                 'order'                 => $index, | 
					
						
							|  |  |  |                 'notes'                 => (string)trans('firefly.created_from_recurrence', ['id' => $recurrence->id, 'title' => $recurrence->title]), | 
					
						
							|  |  |  |                 'tags'                  => $this->repository->getTags($transaction), | 
					
						
							|  |  |  |                 'piggy_bank_id'         => $this->repository->getPiggyBank($transaction), | 
					
						
							|  |  |  |                 'piggy_bank_name'       => null, | 
					
						
							|  |  |  |                 'bill_id'               => $this->repository->getBillId($transaction), | 
					
						
							|  |  |  |                 'bill_name'             => null, | 
					
						
							|  |  |  |                 'recurrence_total'      => $total, | 
					
						
							|  |  |  |                 'recurrence_count'      => $count, | 
					
						
							|  |  |  |                 'recurrence_date'       => $date, | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |             $return[] = $single; | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-06-23 08:19:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         return $return; | 
					
						
							| 
									
										
										
										
											2018-06-22 18:42:23 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | } |