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

291 lines
13 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\Http\Controllers\Controller;
use FireflyIII\Http\Requests\MassDeleteJournalRequest;
use FireflyIII\Http\Requests\MassEditBulkJournalRequest;
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;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
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;
use Illuminate\Support\Collection;
use Preferences;
use Session;
use View;
/**
2017-11-15 05:25:49 -06:00
* Class MassController.
2016-05-20 04:09:02 -05:00
*/
class MassController extends Controller
{
/** @var JournalRepositoryInterface */
private $repository;
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) {
2017-12-16 12:46:36 -06:00
app('view')->share('title', trans('firefly.transactions'));
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
}
/**
* @param Collection $journals
*
* @return View
*/
2016-12-11 03:38:06 -06:00
public function delete(Collection $journals)
2016-05-20 04:09:02 -05:00
{
$subTitle = trans('firefly.mass_delete_journals');
// 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
}
/**
* @param MassDeleteJournalRequest $request
2016-05-20 04:09:02 -05:00
*
* @return mixed
*/
public function destroy(MassDeleteJournalRequest $request)
2016-05-20 04:09:02 -05:00
{
$ids = $request->get('confirm_mass_delete');
$set = new Collection;
if (is_array($ids)) {
/** @var int $journalId */
foreach ($ids as $journalId) {
/** @var TransactionJournal $journal */
2018-04-02 08:10:40 -05:00
$journal = $this->repository->find((int)$journalId);
if (null !== $journal->id && (int)$journalId === $journal->id) {
2016-05-20 04:09:02 -05:00
$set->push($journal);
}
}
}
unset($journal);
$count = 0;
/** @var TransactionJournal $journal */
foreach ($set as $journal) {
2018-03-09 13:58:46 -06:00
$this->repository->destroy($journal);
2017-11-15 05:25:49 -06:00
++$count;
2016-05-20 04:09:02 -05:00
}
Preferences::mark();
Session::flash('success', trans('firefly.mass_deleted_transactions_success', ['amount' => $count]));
// 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
}
/**
* @param Collection $journals
*
* @return View
*/
2016-12-11 03:38:06 -06:00
public function edit(Collection $journals)
2016-05-20 04:09:02 -05:00
{
2016-10-10 00:49:39 -05:00
$subTitle = trans('firefly.mass_edit_journals');
/** @var AccountRepositoryInterface $repository */
2017-03-09 01:19:05 -06:00
$repository = app(AccountRepositoryInterface::class);
$accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
// get budgets
/** @var BudgetRepositoryInterface $budgetRepository */
$budgetRepository = app(BudgetRepositoryInterface::class);
$budgets = $budgetRepository->getBudgets();
2016-05-20 04:09:02 -05:00
// skip transactions that have multiple destinations, multiple sources or are an opening balance.
2016-07-17 01:50:22 -05:00
$filtered = new Collection;
$messages = [];
/** @var TransactionJournal $journal */
2017-08-12 03:27:45 -05:00
foreach ($journals as $journal) {
$sources = $this->repository->getJournalSourceAccounts($journal);
$destinations = $this->repository->getJournalDestinationAccounts($journal);
2016-07-17 01:50:22 -05:00
if ($sources->count() > 1) {
$messages[] = trans('firefly.cannot_edit_multiple_source', ['description' => $journal->description, 'id' => $journal->id]);
continue;
}
if ($destinations->count() > 1) {
$messages[] = trans('firefly.cannot_edit_multiple_dest', ['description' => $journal->description, 'id' => $journal->id]);
continue;
}
if (TransactionType::OPENING_BALANCE === $this->repository->getTransactionType($journal)) {
$messages[] = trans('firefly.cannot_edit_opening_balance');
continue;
}
// cannot edit reconciled transactions / journals:
if ($this->repository->isJournalReconciled($journal)) {
$messages[] = trans('firefly.cannot_edit_reconciled', ['description' => $journal->description, 'id' => $journal->id]);
continue;
}
2016-07-17 01:50:22 -05:00
$filtered->push($journal);
}
2017-03-04 08:29:20 -06:00
if (count($messages) > 0) {
2016-07-17 01:50:22 -05:00
Session::flash('info', $messages);
}
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-edit.uri');
2016-05-20 04:09:02 -05:00
// collect some useful meta data for the mass edit:
2016-07-17 01:50:22 -05:00
$filtered->each(
2016-05-20 04:27:41 -05:00
function (TransactionJournal $journal) {
$transaction = $this->repository->getFirstPosTransaction($journal);
$currency = $transaction->transactionCurrency;
2018-04-02 08:10:40 -05:00
$journal->amount = (float)$transaction->amount;
$sources = $this->repository->getJournalSourceAccounts($journal);
$destinations = $this->repository->getJournalDestinationAccounts($journal);
$journal->transaction_count = $journal->transactions()->count();
$journal->currency_symbol = $currency->symbol;
$journal->transaction_type_type = $journal->transactionType->type;
2018-04-02 08:10:40 -05:00
$journal->foreign_amount = (float)$transaction->foreign_amount;
$journal->foreign_currency = $transaction->foreignCurrency;
2017-11-15 05:25:49 -06:00
if (null !== $sources->first()) {
2016-05-20 04:27:41 -05:00
$journal->source_account_id = $sources->first()->id;
2017-01-15 12:28:54 -06:00
$journal->source_account_name = $sources->first()->editname;
2016-05-20 04:27:41 -05:00
}
2017-11-15 05:25:49 -06:00
if (null !== $destinations->first()) {
2016-05-20 04:27:41 -05:00
$journal->destination_account_id = $destinations->first()->id;
2017-01-15 12:28:54 -06:00
$journal->destination_account_name = $destinations->first()->editname;
2016-05-20 04:27:41 -05:00
}
}
);
2017-11-15 05:25:49 -06:00
if (0 === $filtered->count()) {
2016-07-17 01:50:22 -05:00
Session::flash('error', trans('firefly.no_edit_multiple_left'));
}
$journals = $filtered;
2017-03-09 01:19:05 -06:00
return view('transactions.mass.edit', compact('journals', 'subTitle', 'accounts', 'budgets'));
2016-05-20 04:09:02 -05:00
}
/**
* @param MassEditJournalRequest $request
* @param JournalRepositoryInterface $repository
*
* @return mixed
*/
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;
if (is_array($journalIds)) {
foreach ($journalIds as $journalId) {
2018-04-02 08:10:40 -05:00
$journal = $repository->find((int)$journalId);
if (null !== $journal) {
2016-05-20 04:09:02 -05:00
// get optional fields:
$what = strtolower($this->repository->getTransactionType($journal));
$sourceAccountId = $request->get('source_account_id')[$journal->id] ?? null;
$currencyId = $request->get('transaction_currency_id')[$journal->id] ?? 1;
$sourceAccountName = $request->get('source_account_name')[$journal->id] ?? null;
$destAccountId = $request->get('destination_account_id')[$journal->id] ?? null;
$destAccountName = $request->get('destination_account_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-04-02 08:10:40 -05:00
'budget_id' => (int)$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,
//'native_amount' => $amount,
//'source_amount' => $amount,
//'foreign_amount' => $foreignAmount,
//'destination_amount' => $foreignAmount,
//'amount' => $foreignAmount,
]],
'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
}
}
}
Preferences::mark();
Session::flash('success', trans('firefly.mass_edited_transactions_success', ['amount' => $count]));
// 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
}