firefly-iii/app/Http/Controllers/Transaction/MassController.php

253 lines
11 KiB
PHP
Raw Normal View History

2016-05-20 04:09:02 -05:00
<?php
/**
* MassController.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2016-05-20 04:09:02 -05:00
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2016-05-20 04:09:02 -05:00
*/
declare(strict_types=1);
2016-05-20 04:09:02 -05:00
namespace FireflyIII\Http\Controllers\Transaction;
use Carbon\Carbon;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
2018-09-26 13:44:43 -05:00
use FireflyIII\Helpers\Filter\InternalTransferFilter;
2018-05-29 00:25:04 -05:00
use FireflyIII\Helpers\Filter\TransactionViewFilter;
2018-09-26 13:44:43 -05:00
use FireflyIII\Helpers\Filter\TransferFilter;
2016-05-20 04:09:02 -05:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\MassDeleteJournalRequest;
2018-02-09 12:11:55 -06:00
use FireflyIII\Http\Requests\MassEditJournalRequest;
2016-05-20 04:09:02 -05:00
use FireflyIII\Models\AccountType;
2018-04-24 12:26:16 -05:00
use FireflyIII\Models\Transaction;
2016-05-20 04:09:02 -05:00
use FireflyIII\Models\TransactionJournal;
2016-10-10 00:49:39 -05:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2017-03-09 01:19:05 -06:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
2016-05-20 04:09:02 -05:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2018-04-24 12:26:16 -05:00
use FireflyIII\Transformers\TransactionTransformer;
2018-07-09 12:24:08 -05:00
use FireflyIII\User;
2016-05-20 04:09:02 -05:00
use Illuminate\Support\Collection;
2018-06-06 14:23:00 -05:00
use Illuminate\View\View as IlluminateView;
2018-04-24 12:26:16 -05:00
use Symfony\Component\HttpFoundation\ParameterBag;
2016-05-20 04:09:02 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class MassController.
2018-07-20 07:34:56 -05:00
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2016-05-20 04:09:02 -05:00
*/
class MassController extends Controller
{
/** @var JournalRepositoryInterface Journals and transactions overview */
private $repository;
2016-05-20 04:09:02 -05:00
/**
2018-07-22 01:10:16 -05:00
* MassController constructor.
2016-05-20 04:09:02 -05:00
*/
public function __construct()
{
parent::__construct();
2016-10-29 00:44:46 -05:00
$this->middleware(
function ($request, $next) {
2018-07-15 02:38:49 -05:00
app('view')->share('title', (string)trans('firefly.transactions'));
2017-12-16 12:46:36 -06:00
app('view')->share('mainTitleIcon', 'fa-repeat');
$this->repository = app(JournalRepositoryInterface::class);
2016-10-29 00:44:46 -05:00
return $next($request);
}
);
2016-05-20 04:09:02 -05:00
}
/**
2018-07-22 01:10:16 -05:00
* Mass delete transactions.
*
2016-05-20 04:09:02 -05:00
* @param Collection $journals
*
2018-04-27 04:29:09 -05:00
* @return IlluminateView
2016-05-20 04:09:02 -05:00
*/
2018-04-27 04:29:09 -05:00
public function delete(Collection $journals): IlluminateView
2016-05-20 04:09:02 -05:00
{
2018-07-15 02:38:49 -05:00
$subTitle = (string)trans('firefly.mass_delete_journals');
2016-05-20 04:09:02 -05:00
// put previous url in session
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('transactions.mass-delete.uri');
2016-05-20 04:09:02 -05:00
2017-12-31 02:01:27 -06:00
return view('transactions.mass.delete', compact('journals', 'subTitle'));
2016-05-20 04:09:02 -05:00
}
/**
2018-07-22 01:10:16 -05:00
* Do the mass delete.
*
* @param MassDeleteJournalRequest $request
2016-05-20 04:09:02 -05:00
*
* @return mixed
2018-07-20 07:34:56 -05:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2016-05-20 04:09:02 -05:00
*/
public function destroy(MassDeleteJournalRequest $request)
2016-05-20 04:09:02 -05:00
{
2018-07-20 07:34:56 -05:00
$ids = $request->get('confirm_mass_delete');
$count = 0;
2018-04-24 12:26:16 -05:00
if (\is_array($ids)) {
/** @var string $journalId */
2016-05-20 04:09:02 -05:00
foreach ($ids as $journalId) {
/** @var TransactionJournal $journal */
2018-04-24 12:26:16 -05:00
$journal = $this->repository->findNull((int)$journalId);
if (null !== $journal && (int)$journalId === $journal->id) {
2018-07-20 07:34:56 -05:00
$this->repository->destroy($journal);
++$count;
2016-05-20 04:09:02 -05:00
}
}
}
2018-07-08 05:08:53 -05:00
app('preferences')->mark();
2018-07-15 02:38:49 -05:00
session()->flash('success', (string)trans('firefly.mass_deleted_transactions_success', ['amount' => $count]));
2016-05-20 04:09:02 -05:00
// redirect to previous URL:
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('transactions.mass-delete.uri'));
2016-05-20 04:09:02 -05:00
}
/**
2018-07-22 01:10:16 -05:00
* Mass edit of journals.
*
2016-05-20 04:09:02 -05:00
* @param Collection $journals
*
2018-04-27 23:23:13 -05:00
* @return IlluminateView
2016-05-20 04:09:02 -05:00
*/
2018-04-27 04:29:09 -05:00
public function edit(Collection $journals): IlluminateView
2016-05-20 04:09:02 -05:00
{
2018-07-09 12:24:08 -05:00
/** @var User $user */
2018-07-13 08:50:42 -05:00
$user = auth()->user();
2018-07-15 02:38:49 -05:00
$subTitle = (string)trans('firefly.mass_edit_journals');
2016-10-10 00:49:39 -05:00
/** @var AccountRepositoryInterface $repository */
2017-03-09 01:19:05 -06:00
$repository = app(AccountRepositoryInterface::class);
$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
/** @var BudgetRepositoryInterface $budgetRepository */
$budgetRepository = app(BudgetRepositoryInterface::class);
$budgets = $budgetRepository->getBudgets();
2016-05-20 04:09:02 -05:00
2018-04-24 12:26:16 -05:00
$this->rememberPreviousUri('transactions.mass-edit.uri');
2016-07-17 01:50:22 -05:00
2018-04-24 12:26:16 -05:00
$transformer = new TransactionTransformer(new ParameterBag);
/** @var TransactionCollectorInterface $collector */
$collector = app(TransactionCollectorInterface::class);
2018-07-09 12:24:08 -05:00
$collector->setUser($user);
2018-04-24 12:26:16 -05:00
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
$collector->setJournals($journals);
2018-05-29 00:25:04 -05:00
$collector->addFilter(TransactionViewFilter::class);
2018-09-26 13:44:43 -05:00
$collector->addFilter(TransferFilter::class);
$collection = $collector->getTransactions();
2018-05-29 00:25:04 -05:00
$transactions = $collection->map(
2018-04-24 12:26:16 -05:00
function (Transaction $transaction) use ($transformer) {
2018-07-08 00:59:58 -05:00
$transformed = $transformer->transform($transaction);
2018-05-29 00:25:04 -05:00
// make sure amount is positive:
2018-07-08 00:59:58 -05:00
$transformed['amount'] = app('steam')->positive((string)$transformed['amount']);
$transformed['foreign_amount'] = app('steam')->positive((string)$transformed['foreign_amount']);
2018-06-06 14:23:00 -05:00
2018-07-08 00:59:58 -05:00
return $transformed;
2016-05-20 04:27:41 -05:00
}
);
2018-05-29 00:25:04 -05:00
return view('transactions.mass.edit', compact('transactions', 'subTitle', 'accounts', 'budgets'));
2016-05-20 04:09:02 -05:00
}
/**
2018-07-22 01:10:16 -05:00
* Mass update of journals.
*
2016-05-20 04:09:02 -05:00
* @param MassEditJournalRequest $request
* @param JournalRepositoryInterface $repository
*
* @return mixed
2018-07-20 07:34:56 -05:00
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2016-05-20 04:09:02 -05:00
*/
2017-06-07 01:18:42 -05:00
public function update(MassEditJournalRequest $request, JournalRepositoryInterface $repository)
2016-05-20 04:09:02 -05:00
{
$journalIds = $request->get('journals');
$count = 0;
2018-04-27 23:23:13 -05:00
if (\is_array($journalIds)) {
2016-05-20 04:09:02 -05:00
foreach ($journalIds as $journalId) {
2018-07-08 00:59:58 -05:00
$journal = $repository->findNull((int)$journalId);
2018-04-02 08:10:40 -05:00
if (null !== $journal) {
2016-05-20 04:09:02 -05:00
// get optional fields:
$what = strtolower($this->repository->getTransactionType($journal));
2018-06-29 22:21:21 -05:00
$sourceAccountId = $request->get('source_id')[$journal->id] ?? null;
$currencyId = $request->get('transaction_currency_id')[$journal->id] ?? 1;
2018-06-29 22:21:21 -05:00
$sourceAccountName = $request->get('source_name')[$journal->id] ?? null;
$destAccountId = $request->get('destination_id')[$journal->id] ?? null;
$destAccountName = $request->get('destination_name')[$journal->id] ?? null;
2018-04-02 08:10:40 -05:00
$budgetId = (int)($request->get('budget_id')[$journal->id] ?? 0.0);
2016-11-11 23:34:54 -06:00
$category = $request->get('category')[$journal->id];
$tags = $journal->tags->pluck('tag')->toArray();
$amount = round($request->get('amount')[$journal->id], 12);
$foreignAmount = isset($request->get('foreign_amount')[$journal->id]) ? round($request->get('foreign_amount')[$journal->id], 12) : null;
$foreignCurrencyId = isset($request->get('foreign_currency_id')[$journal->id]) ?
2018-04-02 08:10:40 -05:00
(int)$request->get('foreign_currency_id')[$journal->id] : null;
2016-05-20 04:09:02 -05:00
// build data array
$data = [
'id' => $journal->id,
'what' => $what,
'description' => $request->get('description')[$journal->id],
'date' => new Carbon($request->get('date')[$journal->id]),
'bill_id' => null,
'bill_name' => null,
2018-03-25 02:01:43 -05:00
'notes' => $repository->getNoteText($journal),
'transactions' => [[
'category_id' => null,
'category_name' => $category,
2018-07-08 00:59:58 -05:00
'budget_id' => $budgetId,
'budget_name' => null,
2018-04-02 08:10:40 -05:00
'source_id' => (int)$sourceAccountId,
'source_name' => $sourceAccountName,
2018-04-02 08:10:40 -05:00
'destination_id' => (int)$destAccountId,
'destination_name' => $destAccountName,
'amount' => $amount,
'identifier' => 0,
'reconciled' => false,
2018-04-02 08:10:40 -05:00
'currency_id' => (int)$currencyId,
'currency_code' => null,
'description' => null,
2018-03-25 02:01:43 -05:00
'foreign_amount' => $foreignAmount,
'foreign_currency_id' => $foreignCurrencyId,
'foreign_currency_code' => null,
]],
'currency_id' => $foreignCurrencyId,
'tags' => $tags,
'interest_date' => $journal->interest_date,
'book_date' => $journal->book_date,
'process_date' => $journal->process_date,
2016-05-20 04:09:02 -05:00
];
// call repository update function.
2017-06-07 01:18:42 -05:00
$repository->update($journal, $data);
2016-05-20 04:09:02 -05:00
2017-11-15 05:25:49 -06:00
++$count;
2016-05-20 04:09:02 -05:00
}
}
}
2018-07-08 05:08:53 -05:00
app('preferences')->mark();
2018-07-15 02:38:49 -05:00
session()->flash('success', (string)trans('firefly.mass_edited_transactions_success', ['amount' => $count]));
2016-05-20 04:09:02 -05:00
// redirect to previous URL:
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('transactions.mass-edit.uri'));
2016-05-20 04:09:02 -05:00
}
2016-08-12 08:10:03 -05:00
}