Files
firefly-iii/app/Http/Controllers/Transaction/BulkController.php

168 lines
5.8 KiB
PHP
Raw Normal View History

<?php
/**
* BulkController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* 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\Http\Controllers\Transaction;
2021-03-28 11:46:23 +02:00
2022-10-18 20:41:48 +02:00
use FireflyIII\Events\UpdatedTransactionGroup;
use FireflyIII\Http\Controllers\Controller;
2017-12-30 14:25:11 +01:00
use FireflyIII\Http\Requests\BulkEditJournalRequest;
use FireflyIII\Models\TransactionJournal;
2017-12-30 13:05:19 +01:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2021-09-18 10:20:19 +02:00
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
2021-09-18 10:20:19 +02:00
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Redirector;
2022-10-18 20:41:48 +02:00
use Illuminate\Support\Collection;
2023-05-29 13:56:55 +02:00
use Illuminate\View\View;
/**
* Class BulkController
*/
class BulkController extends Controller
{
/** @var JournalRepositoryInterface Journals and transactions overview */
private $repository;
2021-03-28 11:46:23 +02:00
2017-12-30 12:44:10 +01:00
/**
2018-07-08 12:08:53 +02:00
* BulkController constructor.
2017-12-30 12:44:10 +01:00
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
$this->repository = app(JournalRepositoryInterface::class);
2024-12-22 08:43:12 +01:00
app('view')->share('title', (string) trans('firefly.transactions'));
2020-05-01 06:24:24 +02:00
app('view')->share('mainTitleIcon', 'fa-exchange');
2017-12-30 12:44:10 +01:00
return $next($request);
}
);
}
/**
2018-07-22 08:10:16 +02:00
* Edit a set of journals in bulk.
*
2022-10-30 11:43:17 +01:00
* TODO user wont be able to tell if the journal is part of a split.
*
* @return Factory|View
*/
2019-07-04 17:31:47 +02:00
public function edit(array $journals)
{
2024-12-22 08:43:12 +01:00
$subTitle = (string) trans('firefly.mass_bulk_journals');
2022-04-12 18:19:30 +02:00
$this->rememberPreviousUrl('transactions.bulk-edit.url');
2019-08-03 06:27:56 +02:00
// make amounts positive.
2017-12-30 13:05:19 +01:00
// get list of budgets:
2020-10-13 06:35:33 +02:00
/** @var BudgetRepositoryInterface $budgetRepos */
$budgetRepos = app(BudgetRepositoryInterface::class);
2021-03-28 11:46:23 +02:00
$budgetList = app('expandedform')->makeSelectListWithEmpty($budgetRepos->getActiveBudgets());
return view('transactions.bulk.edit', compact('journals', 'subTitle', 'budgetList'));
}
2021-03-28 11:46:23 +02:00
/**
2018-07-22 08:10:16 +02:00
* Update all journals.
*
2023-12-20 19:35:52 +01:00
* @return Application|Redirector|RedirectResponse
*/
2018-04-24 19:48:42 +02:00
public function update(BulkEditJournalRequest $request)
{
2022-03-29 14:58:06 +02:00
$journalIds = $request->get('journals');
$journalIds = is_array($journalIds) ? $journalIds : [];
2024-12-22 08:43:12 +01:00
$ignoreCategory = 1 === (int) $request->get('ignore_category');
$ignoreBudget = 1 === (int) $request->get('ignore_budget');
2022-03-29 14:58:06 +02:00
$tagsAction = $request->get('tags_action');
2022-10-30 14:24:28 +01:00
$collection = new Collection();
2022-10-18 20:41:48 +02:00
$count = 0;
2018-04-24 19:48:42 +02:00
foreach ($journalIds as $journalId) {
2024-12-22 08:43:12 +01:00
$journalId = (int) $journalId;
2021-06-30 06:17:38 +02:00
$journal = $this->repository->find($journalId);
2019-07-04 17:31:47 +02:00
if (null !== $journal) {
$resultA = $this->updateJournalBudget($journal, $ignoreBudget, $request->integer('budget_id'));
2022-05-02 19:35:35 +02:00
$resultB = $this->updateJournalTags($journal, $tagsAction, explode(',', $request->convertString('tags')));
$resultC = $this->updateJournalCategory($journal, $ignoreCategory, $request->convertString('category'));
2019-07-04 17:31:47 +02:00
if ($resultA || $resultB || $resultC) {
2023-12-20 19:35:52 +01:00
++$count;
2022-10-18 20:41:48 +02:00
$collection->push($journal);
2019-07-04 17:31:47 +02:00
}
2018-04-24 19:48:42 +02:00
}
2019-07-04 17:31:47 +02:00
}
2022-10-18 20:41:48 +02:00
// run rules on changed journals:
/** @var TransactionJournal $journal */
foreach ($collection as $journal) {
event(new UpdatedTransactionGroup($journal->transactionGroup, true, true));
2022-10-18 20:41:48 +02:00
}
2019-07-04 17:31:47 +02:00
app('preferences')->mark();
2023-11-05 08:15:17 +01:00
$request->session()->flash('success', trans_choice('firefly.mass_edited_transactions_success', $count));
2018-04-24 19:48:42 +02:00
2019-07-04 17:31:47 +02:00
// redirect to previous URL:
2022-04-12 18:19:30 +02:00
return redirect($this->getPreviousUrl('transactions.bulk-edit.url'));
2019-07-04 17:31:47 +02:00
}
2018-04-24 19:48:42 +02:00
private function updateJournalBudget(TransactionJournal $journal, bool $ignoreUpdate, int $budgetId): bool
2019-07-04 17:31:47 +02:00
{
if (true === $ignoreUpdate) {
return false;
}
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Set budget to %d', $budgetId));
$this->repository->updateBudget($journal, $budgetId);
2018-04-24 19:48:42 +02:00
2019-07-04 17:31:47 +02:00
return true;
}
2018-04-24 19:48:42 +02:00
2023-06-21 12:34:58 +02:00
private function updateJournalTags(TransactionJournal $journal, string $action, array $tags): bool
2019-07-04 17:31:47 +02:00
{
2023-06-21 12:34:58 +02:00
if ('do_replace' === $action) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Set tags to %s', implode(',', $tags)));
2023-06-21 12:34:58 +02:00
$this->repository->updateTags($journal, $tags);
}
if ('do_append' === $action) {
$existing = $journal->tags->pluck('tag')->toArray();
$new = array_unique(array_merge($tags, $existing));
$this->repository->updateTags($journal, $new);
2019-07-04 17:31:47 +02:00
}
2021-03-28 11:46:23 +02:00
return true;
}
2023-06-21 12:34:58 +02:00
private function updateJournalCategory(TransactionJournal $journal, bool $ignoreUpdate, string $category): bool
2021-03-28 11:46:23 +02:00
{
2023-06-21 12:34:58 +02:00
if (true === $ignoreUpdate) {
return false;
2021-03-28 11:46:23 +02:00
}
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Set budget to %s', $category));
2023-06-21 12:34:58 +02:00
$this->repository->updateCategory($journal, $category);
2021-03-28 11:46:23 +02:00
2019-07-04 17:31:47 +02:00
return true;
}
2018-03-05 19:35:58 +01:00
}