GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Issues (724)

TransactionRules/Actions/ConvertToWithdrawal.php (1 issue)

1
<?php
2
/**
3
 * ConvertToWithdrawal.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\TransactionRules\Actions;
25
26
27
use FireflyIII\Exceptions\FireflyException;
28
use FireflyIII\Factory\AccountFactory;
29
use FireflyIII\Models\Account;
30
use FireflyIII\Models\AccountType;
31
use FireflyIII\Models\RuleAction;
32
use FireflyIII\Models\TransactionJournal;
33
use FireflyIII\Models\TransactionType;
34
use Log;
35
36
37
/**
38
 *
39
 * Class ConvertToWithdrawal
40
 */
41
class ConvertToWithdrawal implements ActionInterface
42
{
43
    /** @var RuleAction The rule action */
44
    private $action;
45
46
    /**
47
     * TriggerInterface constructor.
48
     *
49
     * @param RuleAction $action
50
     */
51
    public function __construct(RuleAction $action)
52
    {
53
        $this->action = $action;
54
    }
55
56
    /**
57
     * Execute the action.
58
     *
59
     * @param TransactionJournal $journal
60
     *
61
     * @return bool
62
     * @throws FireflyException
63
     */
64
    public function act(TransactionJournal $journal): bool
65
    {
66
        $type = $journal->transactionType->type;
67
        if (TransactionType::WITHDRAWAL === $type) {
68
            // @codeCoverageIgnoreStart
69
            Log::error(sprintf('Journal #%d is already a withdrawal (rule "%s").', $journal->id, $this->action->rule->title));
70
71
            return false;
72
            // @codeCoverageIgnoreEnd
73
        }
74
75
        $destTransactions   = $journal->transactions()->where('amount', '>', 0)->get();
76
        $sourceTransactions = $journal->transactions()->where('amount', '<', 0)->get();
77
78
        // break if count is zero:
79
        if (1 !== $sourceTransactions->count()) {
80
            // @codeCoverageIgnoreStart
81
            Log::error(
82
                vsprintf(
83
                    'Journal #%d has %d source transactions. ConvertToWithdrawal failed. (rule "%s").',
84
                    [$journal->id, $sourceTransactions->count(), $this->action->rule->title]
85
                )
86
            );
87
88
            return false;
89
            // @codeCoverageIgnoreEnd
90
        }
91
        if (0 === $destTransactions->count()) {
92
            // @codeCoverageIgnoreStart
93
            Log::error(
94
                vsprintf(
95
                    'Journal #%d has %d dest transactions. ConvertToWithdrawal failed. (rule "%s").',
96
                    [$journal->id, $destTransactions->count(), $this->action->rule->title]
97
                )
98
            );
99
100
            return false;
101
            // @codeCoverageIgnoreEnd
102
        }
103
104
105
        if (TransactionType::DEPOSIT === $type) {
106
            Log::debug('Going to transform a deposit to a withdrawal.');
107
108
            return $this->convertDeposit($journal);
109
        }
110
        if (TransactionType::TRANSFER === $type) {
111
            Log::debug('Going to transform a transfer to a withdrawal.');
112
113
            return $this->convertTransfer($journal);
114
        }
115
116
        return false; // @codeCoverageIgnore
117
    }
118
119
    /**
120
     * Input is a deposit from A to B
121
     * Is converted to a withdrawal from B to C.
122
     *
123
     * @param TransactionJournal $journal
124
     *
125
     * @return bool
126
     * @throws FireflyException
127
     */
128
    private function convertDeposit(TransactionJournal $journal): bool
129
    {
130
        // find or create expense account.
131
        /** @var AccountFactory $factory */
132
        $factory = app(AccountFactory::class);
133
        $factory->setUser($journal->user);
134
135
        $destTransactions   = $journal->transactions()->where('amount', '>', 0)->get();
136
        $sourceTransactions = $journal->transactions()->where('amount', '<', 0)->get();
137
138
        // get the action value, or use the original source revenue name in case the action value is empty:
139
        // this becomes a new or existing expense account.
140
        /** @var Account $source */
141
        $source      = $sourceTransactions->first()->account;
142
        $expenseName = '' === $this->action->action_value ? $source->name : $this->action->action_value;
143
        $expense     = $factory->findOrCreate($expenseName, AccountType::EXPENSE);
144
145
        Log::debug(sprintf('ConvertToWithdrawal. Action value is "%s", expense name is "%s"', $this->action->action_value, $source->name));
146
        unset($source);
147
148
        // get destination asset account from transaction(s).
149
        /** @var Account $destination */
150
        $destination = $destTransactions->first()->account;
151
152
        // update source transaction(s) to be the original destination account
153
        $journal->transactions()
154
                ->where('amount', '<', 0)
155
                ->update(['account_id' => $destination->id]);
156
157
        // update destination transaction(s) to be new expense account.
158
        $journal->transactions()
159
                ->where('amount', '>', 0)
160
                ->update(['account_id' => $expense->id]);
161
162
        // change transaction type of journal:
163
        $newType                      = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
164
        $journal->transaction_type_id = $newType->id;
165
        $journal->save();
166
167
        Log::debug('Converted deposit to withdrawal.');
168
169
        return true;
170
    }
171
172
    /**
173
     * Input is a transfer from A to B.
174
     * Output is a withdrawal from A to C.
175
     *
176
     * @param TransactionJournal $journal
177
     *
178
     * @return bool
179
     * @throws FireflyException
180
     */
181
    private function convertTransfer(TransactionJournal $journal): bool
182
    {
183
        // find or create expense account.
184
        /** @var AccountFactory $factory */
185
        $factory = app(AccountFactory::class);
186
        $factory->setUser($journal->user);
187
188
        $destTransactions = $journal->transactions()->where('amount', '>', 0)->get();
189
190
        // get the action value, or use the original destination name in case the action value is empty:
191
        // this becomes a new or existing expense account.
192
        /** @var Account $destination */
193
        $destination = $destTransactions->first()->account;
194
        $expenseName = '' === $this->action->action_value ? $destination->name : $this->action->action_value;
195
        $expense     = $factory->findOrCreate($expenseName, AccountType::EXPENSE);
196
197
        Log::debug(sprintf('ConvertToWithdrawal. Action value is "%s", revenue name is "%s"', $this->action->action_value, $destination->name));
198
        unset($source);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $source seems to be never defined.
Loading history...
199
200
        // update destination transaction(s) to be the expense account
201
        $journal->transactions()
202
                ->where('amount', '>', 0)
203
                ->update(['account_id' => $expense->id]);
204
205
        // change transaction type of journal:
206
        $newType                      = TransactionType::whereType(TransactionType::WITHDRAWAL)->first();
207
        $journal->transaction_type_id = $newType->id;
208
        $journal->save();
209
        Log::debug('Converted transfer to withdrawal.');
210
211
        return true;
212
    }
213
}
214