1 | <?php |
||
2 | /** |
||
3 | * TransactionUpdateService.php |
||
4 | * Copyright (c) 2018 [email protected] |
||
5 | * |
||
6 | * This file is part of Firefly III. |
||
7 | * |
||
8 | * Firefly III is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU General Public License as published by |
||
10 | * the Free Software Foundation, either version 3 of the License, or |
||
11 | * (at your option) any later version. |
||
12 | * |
||
13 | * Firefly III 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 General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU General Public License |
||
19 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||
20 | */ |
||
21 | |||
22 | declare(strict_types=1); |
||
23 | |||
24 | namespace FireflyIII\Services\Internal\Update; |
||
25 | |||
26 | use FireflyIII\Models\Transaction; |
||
27 | use FireflyIII\Services\Internal\Support\TransactionServiceTrait; |
||
28 | use FireflyIII\User; |
||
29 | |||
30 | /** |
||
31 | * Class TransactionUpdateService |
||
32 | */ |
||
33 | class TransactionUpdateService |
||
34 | { |
||
35 | use TransactionServiceTrait; |
||
36 | |||
37 | /** @var User */ |
||
38 | private $user; |
||
39 | |||
40 | /** |
||
41 | * @param int $transactionId |
||
42 | * |
||
43 | * @return Transaction|null |
||
44 | */ |
||
45 | public function reconcile(int $transactionId): ?Transaction |
||
46 | { |
||
47 | $transaction = Transaction::find($transactionId); |
||
48 | if (null !== $transaction) { |
||
49 | $transaction->reconciled = true; |
||
50 | $transaction->save(); |
||
51 | |||
52 | return $transaction; |
||
53 | } |
||
54 | |||
55 | return null; |
||
56 | |||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param User $user |
||
61 | */ |
||
62 | public function setUser(User $user): void |
||
63 | { |
||
64 | $this->user = $user; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param Transaction $transaction |
||
69 | * @param array $data |
||
70 | * |
||
71 | * @return Transaction |
||
72 | */ |
||
73 | public function update(Transaction $transaction, array $data): Transaction |
||
74 | { |
||
75 | $currency = $this->findCurrency($data['currency_id'], $data['currency_code']); |
||
76 | $journal = $transaction->transactionJournal; |
||
77 | $description = $journal->description === $data['description'] ? null : $data['description']; |
||
78 | |||
79 | // update description: |
||
80 | $transaction->description = $description; |
||
81 | $foreignAmount = null; |
||
82 | if ((float)$transaction->amount < 0) { |
||
83 | // this is the source transaction. |
||
84 | $type = $this->accountType($journal, 'source'); |
||
85 | $account = $this->findAccount($type, $data['source_id'], $data['source_name']); |
||
86 | $amount = app('steam')->negative((string)$data['amount']); |
||
87 | $foreignAmount = app('steam')->negative((string)$data['foreign_amount']); |
||
88 | } |
||
89 | |||
90 | if ((float)$transaction->amount > 0) { |
||
91 | // this is the destination transaction. |
||
92 | $type = $this->accountType($journal, 'destination'); |
||
93 | $account = $this->findAccount($type, $data['destination_id'], $data['destination_name']); |
||
94 | $amount = app('steam')->positive((string)$data['amount']); |
||
95 | $foreignAmount = app('steam')->positive((string)$data['foreign_amount']); |
||
96 | } |
||
97 | |||
98 | // update the actual transaction: |
||
99 | $transaction->description = $description; |
||
100 | $transaction->amount = $amount; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
101 | $transaction->foreign_amount = null; |
||
102 | $transaction->transaction_currency_id = $currency->id; |
||
103 | $transaction->account_id = $account->id; |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||
104 | $transaction->reconciled = $data['reconciled']; |
||
105 | $transaction->save(); |
||
106 | |||
107 | // set foreign currency |
||
108 | $foreign = $this->findCurrency($data['foreign_currency_id'], $data['foreign_currency_code']); |
||
109 | // set foreign amount: |
||
110 | if (null !== $data['foreign_amount'] && null !== $foreign) { |
||
111 | $this->setForeignCurrency($transaction, $foreign); |
||
112 | $this->setForeignAmount($transaction, $foreignAmount); |
||
113 | } |
||
114 | if (null === $data['foreign_amount'] || null === $foreign) { |
||
115 | $this->setForeignCurrency($transaction, null); |
||
116 | $this->setForeignAmount($transaction, null); |
||
117 | } |
||
118 | |||
119 | // set budget: |
||
120 | $budget = $this->findBudget($data['budget_id'], $data['budget_name']); |
||
121 | $this->setBudget($transaction, $budget); |
||
122 | |||
123 | // set category |
||
124 | $category = $this->findCategory($data['category_id'], $data['category_name']); |
||
125 | $this->setCategory($transaction, $category); |
||
126 | |||
127 | return $transaction; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Update budget for a journal. |
||
132 | * |
||
133 | * @param Transaction $transaction |
||
134 | * @param int $budgetId |
||
135 | * |
||
136 | * @return Transaction |
||
137 | */ |
||
138 | public function updateBudget(Transaction $transaction, int $budgetId): Transaction |
||
139 | { |
||
140 | $budget = $this->findBudget($budgetId, null); |
||
141 | $this->setBudget($transaction, $budget); |
||
142 | |||
143 | return $transaction; |
||
144 | |||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Update category for a journal. |
||
149 | * |
||
150 | * @param Transaction $transaction |
||
151 | * @param string $category |
||
152 | * |
||
153 | * @return Transaction |
||
154 | */ |
||
155 | public function updateCategory(Transaction $transaction, string $category): Transaction |
||
156 | { |
||
157 | $found = $this->findCategory(0, $category); |
||
158 | $this->setCategory($transaction, $found); |
||
159 | |||
160 | return $transaction; |
||
161 | } |
||
162 | } |
||
163 |