1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* BulkController.php |
4
|
|
|
* Copyright (c) 2017 [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\Http\Controllers\Transaction; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
use ExpandedForm; |
28
|
|
|
use FireflyIII\Http\Controllers\Controller; |
29
|
|
|
use FireflyIII\Http\Requests\BulkEditJournalRequest; |
30
|
|
|
use FireflyIII\Models\TransactionJournal; |
31
|
|
|
use FireflyIII\Models\TransactionType; |
32
|
|
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; |
33
|
|
|
use FireflyIII\Repositories\Journal\JournalRepositoryInterface; |
34
|
|
|
use Illuminate\Http\Request; |
35
|
|
|
use Illuminate\Support\Collection; |
36
|
|
|
use Log; |
37
|
|
|
use Preferences; |
38
|
|
|
use Session; |
39
|
|
|
use View; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Class BulkController |
43
|
|
|
*/ |
44
|
|
|
class BulkController extends Controller |
45
|
|
|
{ |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* |
50
|
|
|
*/ |
51
|
|
|
public function __construct() |
52
|
|
|
{ |
53
|
|
|
parent::__construct(); |
54
|
|
|
|
55
|
|
|
$this->middleware( |
56
|
|
|
function ($request, $next) { |
57
|
|
|
app('view')->share('title', trans('firefly.transactions')); |
58
|
|
|
app('view')->share('mainTitleIcon', 'fa-repeat'); |
59
|
|
|
|
60
|
|
|
return $next($request); |
61
|
|
|
} |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param Collection $journals |
67
|
|
|
* |
68
|
|
|
* @return View |
69
|
|
|
*/ |
70
|
|
|
public function edit(Request $request, Collection $journals) |
71
|
|
|
{ |
72
|
|
|
|
73
|
|
|
$subTitle = trans('firefly.mass_bulk_journals'); |
74
|
|
|
|
75
|
|
|
// skip transactions that have multiple destinations, multiple sources or are an opening balance. |
76
|
|
|
$filtered = new Collection; |
77
|
|
|
$messages = []; |
78
|
|
|
/** @var TransactionJournal $journal */ |
79
|
|
|
foreach ($journals as $journal) { |
80
|
|
|
$sources = $journal->sourceAccountList(); |
81
|
|
|
$destinations = $journal->destinationAccountList(); |
82
|
|
|
if ($sources->count() > 1) { |
83
|
|
|
$messages[] = trans('firefly.cannot_edit_multiple_source', ['description' => $journal->description, 'id' => $journal->id]); |
84
|
|
|
continue; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ($destinations->count() > 1) { |
88
|
|
|
$messages[] = trans('firefly.cannot_edit_multiple_dest', ['description' => $journal->description, 'id' => $journal->id]); |
89
|
|
|
continue; |
90
|
|
|
} |
91
|
|
|
if (TransactionType::OPENING_BALANCE === $journal->transactionType->type) { |
92
|
|
|
$messages[] = trans('firefly.cannot_edit_opening_balance'); |
93
|
|
|
continue; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// cannot edit reconciled transactions / journals: |
97
|
|
|
if ($journal->transactions->first()->reconciled) { |
98
|
|
|
$messages[] = trans('firefly.cannot_edit_reconciled', ['description' => $journal->description, 'id' => $journal->id]); |
99
|
|
|
continue; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$filtered->push($journal); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (count($messages) > 0) { |
106
|
|
|
$request->session()->flash('info', $messages); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// put previous url in session |
110
|
|
|
$this->rememberPreviousUri('transactions.bulk-edit.uri'); |
111
|
|
|
|
112
|
|
|
// get list of budgets: |
113
|
|
|
/** @var BudgetRepositoryInterface $repository */ |
114
|
|
|
$repository = app(BudgetRepositoryInterface::class); |
115
|
|
|
$budgetList = ExpandedForm::makeSelectListWithEmpty($repository->getActiveBudgets()); |
116
|
|
|
// collect some useful meta data for the mass edit: |
117
|
|
|
$filtered->each( |
118
|
|
|
function (TransactionJournal $journal) { |
119
|
|
|
$journal->transaction_count = $journal->transactions()->count(); |
120
|
|
|
} |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
if (0 === $filtered->count()) { |
124
|
|
|
$request->session()->flash('error', trans('firefly.no_edit_multiple_left')); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$journals = $filtered; |
128
|
|
|
|
129
|
|
|
return view('transactions.bulk.edit', compact('journals', 'subTitle', 'budgetList')); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param BulkEditJournalRequest $request |
135
|
|
|
* @param JournalRepositoryInterface $repository |
136
|
|
|
* |
137
|
|
|
* @return mixed |
138
|
|
|
*/ |
139
|
|
|
public function update(BulkEditJournalRequest $request, JournalRepositoryInterface $repository) |
140
|
|
|
{ |
141
|
|
|
$journalIds = $request->get('journals'); |
142
|
|
|
$ignoreCategory = intval($request->get('ignore_category')) === 1; |
143
|
|
|
$ignoreBudget = intval($request->get('ignore_budget')) === 1; |
144
|
|
|
$ignoreTags = intval($request->get('ignore_tags')) === 1; |
145
|
|
|
$count = 0; |
146
|
|
|
if (is_array($journalIds)) { |
147
|
|
|
foreach ($journalIds as $journalId) { |
148
|
|
|
$journal = $repository->find(intval($journalId)); |
149
|
|
|
if (!is_null($journal)) { |
150
|
|
|
$count++; |
151
|
|
|
Log::debug(sprintf('Found journal #%d', $journal->id)); |
152
|
|
|
// update category if not told to ignore |
153
|
|
|
if ($ignoreCategory === false) { |
154
|
|
|
Log::debug(sprintf('Set category to %s', $request->string('category'))); |
155
|
|
|
$repository->updateCategory($journal, $request->string('category')); |
156
|
|
|
} |
157
|
|
|
// update budget if not told to ignore (and is withdrawal) |
158
|
|
|
if ($ignoreBudget === false) { |
159
|
|
|
Log::debug(sprintf('Set budget to %d', $request->integer('budget_id'))); |
160
|
|
|
$repository->updateBudget($journal, $request->integer('budget_id')); |
161
|
|
|
} |
162
|
|
|
if ($ignoreTags === false) { |
163
|
|
|
Log::debug(sprintf('Set tags to %s', $request->string('budget_id'))); |
164
|
|
|
$repository->updateTags($journal, explode(',', $request->string('tags'))); |
165
|
|
|
} |
166
|
|
|
// update tags if not told to ignore (and is withdrawal) |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
Preferences::mark(); |
172
|
|
|
$request->session()->flash('success', trans('firefly.mass_edited_transactions_success', ['amount' => $count])); |
173
|
|
|
|
174
|
|
|
// redirect to previous URL: |
175
|
|
|
return redirect($this->getPreviousUri('transactions.bulk-edit.uri')); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
} |