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

185 lines
5.9 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
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;
use Illuminate\View\View;
2017-12-30 14:25:11 +01:00
use Log;
/**
* 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.
*
2019-07-04 17:31:47 +02:00
* @codeCoverageIgnore
2017-12-30 12:44:10 +01:00
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
$this->repository = app(JournalRepositoryInterface::class);
2022-03-29 14:58:06 +02: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.
*
* See reference nr. 47
*
* @param array $journals
*
* @return Factory|View
*/
2019-07-04 17:31:47 +02:00
public function edit(array $journals)
{
2022-03-29 14:58:06 +02: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.
*
2018-06-06 21:23:00 +02:00
* @param BulkEditJournalRequest $request
*
2021-09-18 10:20:19 +02:00
* @return Application|RedirectResponse|Redirector
*/
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 : [];
$ignoreCategory = 1 === (int) $request->get('ignore_category');
$ignoreBudget = 1 === (int) $request->get('ignore_budget');
$tagsAction = $request->get('tags_action');
2020-08-01 15:52:28 +02:00
$count = 0;
2018-04-24 19:48:42 +02:00
foreach ($journalIds as $journalId) {
2022-03-29 14:58:06 +02: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'));
2020-08-01 15:52:28 +02:00
$resultB = $this->updateJournalTags($journal, $tagsAction, explode(',', $request->string('tags')));
2019-07-04 17:31:47 +02:00
$resultC = $this->updateJournalCategory($journal, $ignoreCategory, $request->string('category'));
if ($resultA || $resultB || $resultC) {
$count++;
}
2018-04-24 19:48:42 +02:00
}
2019-07-04 17:31:47 +02:00
}
app('preferences')->mark();
2022-03-29 14:58:06 +02:00
$request->session()->flash('success', (string) 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
2019-07-04 17:31:47 +02:00
/**
* @param TransactionJournal $journal
* @param bool $ignoreUpdate
* @param int $budgetId
*
2019-07-04 17:31:47 +02:00
* @return bool
*/
private function updateJournalBudget(TransactionJournal $journal, bool $ignoreUpdate, int $budgetId): bool
2019-07-04 17:31:47 +02:00
{
if (true === $ignoreUpdate) {
return false;
}
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
2019-07-04 17:31:47 +02:00
/**
* @param TransactionJournal $journal
2020-08-01 15:52:28 +02:00
* @param string $action
* @param array $tags
*
2019-07-04 17:31:47 +02:00
* @return bool
*/
2020-08-01 15:52:28 +02:00
private function updateJournalTags(TransactionJournal $journal, string $action, array $tags): bool
2019-07-04 17:31:47 +02:00
{
2020-08-01 15:52:28 +02:00
if ('do_replace' === $action) {
Log::debug(sprintf('Set tags to %s', implode(',', $tags)));
$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;
}
/**
* @param TransactionJournal $journal
* @param bool $ignoreUpdate
* @param string $category
*
* @return bool
*/
private function updateJournalCategory(TransactionJournal $journal, bool $ignoreUpdate, string $category): bool
{
if (true === $ignoreUpdate) {
return false;
}
Log::debug(sprintf('Set budget to %s', $category));
$this->repository->updateCategory($journal, $category);
2019-07-04 17:31:47 +02:00
return true;
}
2018-03-05 19:35:58 +01:00
}