1 | <?php |
||
2 | /** |
||
3 | * GroupCloneService.php |
||
4 | * Copyright (c) 2019 [email protected] |
||
5 | * |
||
6 | * This file is part of Firefly III (https://github.com/firefly-iii). |
||
7 | * |
||
8 | * This program is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU Affero General Public License as |
||
10 | * published by the Free Software Foundation, either version 3 of the |
||
11 | * License, or (at your option) any later version. |
||
12 | * |
||
13 | * This program is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | * GNU Affero General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU Affero General Public License |
||
19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
20 | */ |
||
21 | |||
22 | declare(strict_types=1); |
||
23 | |||
24 | namespace FireflyIII\Services\Internal\Update; |
||
25 | |||
26 | use Carbon\Carbon; |
||
27 | use FireflyIII\Models\Budget; |
||
28 | use FireflyIII\Models\Category; |
||
29 | use FireflyIII\Models\Note; |
||
30 | use FireflyIII\Models\Tag; |
||
31 | use FireflyIII\Models\Transaction; |
||
32 | use FireflyIII\Models\TransactionGroup; |
||
33 | use FireflyIII\Models\TransactionJournal; |
||
34 | use FireflyIII\Models\TransactionJournalMeta; |
||
35 | |||
36 | /** |
||
37 | * Class GroupCloneService |
||
38 | * TODO test. |
||
39 | */ |
||
40 | class GroupCloneService |
||
41 | { |
||
42 | /** |
||
43 | * @param TransactionGroup $group |
||
44 | * |
||
45 | * @return TransactionGroup |
||
46 | */ |
||
47 | public function cloneGroup(TransactionGroup $group): TransactionGroup |
||
48 | { |
||
49 | $newGroup = $group->replicate(); |
||
50 | $newGroup->save(); |
||
51 | foreach ($group->transactionJournals as $journal) { |
||
52 | $this->cloneJournal($journal, $newGroup, (int)$group->id); |
||
53 | } |
||
54 | |||
55 | |||
56 | return $newGroup; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param TransactionJournal $journal |
||
61 | * @param TransactionGroup $newGroup |
||
62 | * @param int $originalGroup |
||
63 | */ |
||
64 | private function cloneJournal(TransactionJournal $journal, TransactionGroup $newGroup, int $originalGroup): void |
||
65 | { |
||
66 | $newJournal = $journal->replicate(); |
||
67 | $newJournal->transaction_group_id = $newGroup->id; |
||
68 | $newJournal->date = Carbon::now(); |
||
69 | $newJournal->save(); |
||
70 | |||
71 | foreach ($journal->transactions as $transaction) { |
||
72 | $this->cloneTransaction($transaction, $newJournal); |
||
73 | } |
||
74 | |||
75 | // clone notes |
||
76 | /** @var Note $note */ |
||
77 | foreach ($journal->notes as $note) { |
||
78 | $this->cloneNote($note, $newJournal, $originalGroup); |
||
79 | } |
||
80 | // clone location (not yet available) |
||
81 | |||
82 | // clone meta |
||
83 | /** @var TransactionJournalMeta $meta */ |
||
84 | foreach ($journal->transactionJournalMeta as $meta) { |
||
85 | $this->cloneMeta($meta, $newJournal); |
||
86 | } |
||
87 | // clone category |
||
88 | /** @var Category $category */ |
||
89 | foreach ($journal->categories as $category) { |
||
90 | $newJournal->categories()->save($category); |
||
91 | } |
||
92 | |||
93 | // clone budget |
||
94 | /** @var Budget $budget */ |
||
95 | foreach ($journal->budgets as $budget) { |
||
96 | $newJournal->budgets()->save($budget); |
||
97 | } |
||
98 | // clone links (ongoing). |
||
99 | |||
100 | // clone tags |
||
101 | /** @var Tag $tag */ |
||
102 | foreach ($journal->tags as $tag) { |
||
103 | $newJournal->tags()->save($tag); |
||
104 | } |
||
105 | // add note saying "cloned". |
||
106 | |||
107 | // add relation. |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param TransactionJournalMeta $meta |
||
112 | * @param TransactionJournal $newJournal |
||
113 | */ |
||
114 | private function cloneMeta(TransactionJournalMeta $meta, TransactionJournal $newJournal): void |
||
115 | { |
||
116 | $newMeta = $meta->replicate(); |
||
117 | $newMeta->transaction_journal_id = $newJournal->id; |
||
118 | if ('recurrence_id' !== $newMeta->name) { |
||
119 | $newMeta->save(); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param Note $note |
||
125 | * @param TransactionJournal $newJournal |
||
126 | * @param int $oldGroupId |
||
127 | */ |
||
128 | private function cloneNote(Note $note, TransactionJournal $newJournal, int $oldGroupId): void |
||
129 | { |
||
130 | $newNote = $note->replicate(); |
||
131 | $newNote->text .= sprintf( |
||
132 | "\n\n%s", trans('firefly.clones_journal_x', ['description' => $newJournal->description, 'id' => $oldGroupId]) |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
133 | ); |
||
134 | $newNote->noteable_id = $newJournal->id; |
||
135 | $newNote->save(); |
||
136 | |||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @param Transaction $transaction |
||
141 | * @param TransactionJournal $newJournal |
||
142 | */ |
||
143 | private function cloneTransaction(Transaction $transaction, TransactionJournal $newJournal): void |
||
144 | { |
||
145 | $newTransaction = $transaction->replicate(); |
||
146 | $newTransaction->transaction_journal_id = $newJournal->id; |
||
147 | $newTransaction->reconciled = false; |
||
148 | $newTransaction->save(); |
||
149 | } |
||
150 | |||
151 | |||
152 | } |
||
153 |