| 
									
										
										
										
											2020-02-07 20:51:25 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * GroupCloneService.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:56:35 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2020-02-07 20:51:25 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU Affero General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Services\Internal\Update; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-17 07:18:50 +02:00
										 |  |  | use FireflyIII\Factory\PiggyBankEventFactory; | 
					
						
							| 
									
										
										
										
											2020-02-07 20:51:25 +01:00
										 |  |  | use FireflyIII\Models\Budget; | 
					
						
							|  |  |  | use FireflyIII\Models\Category; | 
					
						
							|  |  |  | use FireflyIII\Models\Note; | 
					
						
							| 
									
										
										
										
											2022-07-17 07:18:50 +02:00
										 |  |  | use FireflyIII\Models\PiggyBankEvent; | 
					
						
							| 
									
										
										
										
											2020-02-07 20:51:25 +01:00
										 |  |  | use FireflyIII\Models\Tag; | 
					
						
							|  |  |  | use FireflyIII\Models\Transaction; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionGroup; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionJournal; | 
					
						
							|  |  |  | use FireflyIII\Models\TransactionJournalMeta; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class GroupCloneService | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class GroupCloneService | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public function cloneGroup(TransactionGroup $group): TransactionGroup | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $newGroup = $group->replicate(); | 
					
						
							|  |  |  |         $newGroup->save(); | 
					
						
							|  |  |  |         foreach ($group->transactionJournals as $journal) { | 
					
						
							| 
									
										
										
										
											2023-11-05 19:41:37 +01:00
										 |  |  |             $this->cloneJournal($journal, $newGroup, $group->id); | 
					
						
							| 
									
										
										
										
											2020-02-07 20:51:25 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-09-18 10:26:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 20:51:25 +01:00
										 |  |  |         return $newGroup; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function cloneJournal(TransactionJournal $journal, TransactionGroup $newGroup, int $originalGroup): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $newJournal                       = $journal->replicate(); | 
					
						
							|  |  |  |         $newJournal->transaction_group_id = $newGroup->id; | 
					
						
							| 
									
										
										
										
											2024-04-02 23:44:45 +08:00
										 |  |  |         $newJournal->date                 = now(); | 
					
						
							| 
									
										
										
										
											2020-02-07 20:51:25 +01:00
										 |  |  |         $newJournal->save(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($journal->transactions as $transaction) { | 
					
						
							|  |  |  |             $this->cloneTransaction($transaction, $newJournal); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // clone notes
 | 
					
						
							|  |  |  |         /** @var Note $note */ | 
					
						
							|  |  |  |         foreach ($journal->notes as $note) { | 
					
						
							|  |  |  |             $this->cloneNote($note, $newJournal, $originalGroup); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // clone location (not yet available)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // clone meta
 | 
					
						
							|  |  |  |         /** @var TransactionJournalMeta $meta */ | 
					
						
							|  |  |  |         foreach ($journal->transactionJournalMeta as $meta) { | 
					
						
							|  |  |  |             $this->cloneMeta($meta, $newJournal); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 20:51:25 +01:00
										 |  |  |         // clone category
 | 
					
						
							|  |  |  |         /** @var Category $category */ | 
					
						
							|  |  |  |         foreach ($journal->categories as $category) { | 
					
						
							|  |  |  |             $newJournal->categories()->save($category); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // clone budget
 | 
					
						
							|  |  |  |         /** @var Budget $budget */ | 
					
						
							|  |  |  |         foreach ($journal->budgets as $budget) { | 
					
						
							|  |  |  |             $newJournal->budgets()->save($budget); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // clone links (ongoing).
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // clone tags
 | 
					
						
							|  |  |  |         /** @var Tag $tag */ | 
					
						
							|  |  |  |         foreach ($journal->tags as $tag) { | 
					
						
							|  |  |  |             $newJournal->tags()->save($tag); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         // add note saying "cloned".
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // add relation.
 | 
					
						
							| 
									
										
										
										
											2022-12-11 07:17:59 +01:00
										 |  |  |         // TODO clone ALL linked piggy banks
 | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         /** @var null|PiggyBankEvent $event */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $event                            = $journal->piggyBankEvents()->first(); | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |         if (null !== $event) { | 
					
						
							| 
									
										
										
										
											2022-07-17 07:18:50 +02:00
										 |  |  |             $piggyBank = $event->piggyBank; | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |             $factory   = app(PiggyBankEventFactory::class); | 
					
						
							| 
									
										
										
										
											2022-07-17 07:18:50 +02:00
										 |  |  |             $factory->create($newJournal, $piggyBank); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-02-07 20:51:25 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function cloneTransaction(Transaction $transaction, TransactionJournal $newJournal): void | 
					
						
							| 
									
										
										
										
											2020-02-07 20:51:25 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $newTransaction                         = $transaction->replicate(); | 
					
						
							|  |  |  |         $newTransaction->transaction_journal_id = $newJournal->id; | 
					
						
							|  |  |  |         $newTransaction->reconciled             = false; | 
					
						
							|  |  |  |         $newTransaction->save(); | 
					
						
							| 
									
										
										
										
											2020-02-07 20:51:25 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private function cloneNote(Note $note, TransactionJournal $newJournal, int $oldGroupId): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $newNote              = $note->replicate(); | 
					
						
							|  |  |  |         $newNote->text        .= sprintf( | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |             "\n\n%s", | 
					
						
							|  |  |  |             trans('firefly.clones_journal_x', ['description' => $newJournal->description, 'id' => $oldGroupId]) | 
					
						
							| 
									
										
										
										
											2020-02-07 20:51:25 +01:00
										 |  |  |         ); | 
					
						
							|  |  |  |         $newNote->noteable_id = $newJournal->id; | 
					
						
							|  |  |  |         $newNote->save(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |     private function cloneMeta(TransactionJournalMeta $meta, TransactionJournal $newJournal): void | 
					
						
							| 
									
										
										
										
											2020-02-07 20:51:25 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         $newMeta                         = $meta->replicate(); | 
					
						
							|  |  |  |         $newMeta->transaction_journal_id = $newJournal->id; | 
					
						
							|  |  |  |         if ('recurrence_id' !== $newMeta->name) { | 
					
						
							|  |  |  |             $newMeta->save(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-02-07 20:51:25 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | } |