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

271 lines
11 KiB
PHP
Raw Normal View History

2016-10-29 08:14:33 -05:00
<?php
/**
* ConvertController.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2016-10-29 08:14:33 -05:00
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
2016-10-29 08:14:33 -05:00
*
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-10-29 08:14:33 -05:00
*/
declare(strict_types=1);
2016-10-29 08:14:33 -05:00
namespace FireflyIII\Http\Controllers\Transaction;
2016-10-30 00:14:07 -05:00
use FireflyIII\Exceptions\FireflyException;
2016-10-29 08:14:33 -05:00
use FireflyIII\Http\Controllers\Controller;
2016-10-30 00:14:07 -05:00
use FireflyIII\Models\Account;
2016-10-29 08:14:33 -05:00
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2016-10-30 00:14:07 -05:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2016-10-29 08:14:33 -05:00
use Illuminate\Http\Request;
2018-07-01 02:27:22 -05:00
use Log;
2016-10-29 08:14:33 -05:00
use View;
/**
2017-11-15 05:25:49 -06:00
* Class ConvertController.
2016-10-29 08:14:33 -05:00
*/
class ConvertController extends Controller
{
/** @var JournalRepositoryInterface */
private $repository;
2016-10-29 08:14:33 -05:00
/**
* ConvertController constructor.
*/
public function __construct()
{
parent::__construct();
// some useful repositories:
$this->middleware(
function ($request, $next) {
$this->repository = app(JournalRepositoryInterface::class);
2016-10-29 08:14:33 -05:00
2017-12-16 12:46:36 -06:00
app('view')->share('title', trans('firefly.transactions'));
app('view')->share('mainTitleIcon', 'fa-exchange');
2016-10-29 08:14:33 -05:00
return $next($request);
}
);
}
/**
* @param TransactionType $destinationType
* @param TransactionJournal $journal
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
*/
2016-12-06 01:59:08 -06:00
public function index(TransactionType $destinationType, TransactionJournal $journal)
2016-10-29 08:14:33 -05:00
{
// @codeCoverageIgnoreStart
2016-11-22 12:10:17 -06:00
if ($this->isOpeningBalance($journal)) {
2018-07-01 02:27:22 -05:00
Log::debug('This is an opening balance.');
2016-11-22 12:10:17 -06:00
return $this->redirectToAccount($journal);
}
// @codeCoverageIgnoreEnd
$positiveAmount = $this->repository->getJournalTotal($journal);
2016-10-29 08:14:33 -05:00
$sourceType = $journal->transactionType;
2016-10-29 10:30:55 -05:00
$subTitle = trans('firefly.convert_to_' . $destinationType->type, ['description' => $journal->description]);
$subTitleIcon = 'fa-exchange';
2016-10-29 08:14:33 -05:00
2016-10-29 10:30:55 -05:00
// cannot convert to its own type.
2016-10-29 08:14:33 -05:00
if ($sourceType->type === $destinationType->type) {
2018-07-01 02:27:22 -05:00
Log::debug('This is already a transaction of the expected type..');
2018-04-22 10:10:11 -05:00
session()->flash('info', trans('firefly.convert_is_already_type_' . $destinationType->type));
2016-10-29 08:14:33 -05:00
return redirect(route('transactions.show', [$journal->id]));
}
2016-10-29 10:30:55 -05:00
// cannot convert split.
2016-10-29 08:14:33 -05:00
if ($journal->transactions()->count() > 2) {
2018-07-01 02:27:22 -05:00
Log::info('This journal has more than two transactions.');
2018-04-22 10:10:11 -05:00
session()->flash('error', trans('firefly.cannot_convert_split_journal'));
2016-10-29 08:14:33 -05:00
return redirect(route('transactions.show', [$journal->id]));
}
2016-10-29 10:30:55 -05:00
// get source and destination account:
$sourceAccount = $this->repository->getJournalSourceAccounts($journal)->first();
$destinationAccount = $this->repository->getJournalDestinationAccounts($journal)->first();
2016-10-29 08:14:33 -05:00
return view(
2018-04-24 12:48:42 -05:00
'transactions.convert', compact(
2018-06-06 14:23:00 -05:00
'sourceType', 'destinationType', 'journal', 'positiveAmount', 'sourceAccount', 'destinationAccount', 'sourceType',
'subTitle', 'subTitleIcon'
)
2016-10-29 08:14:33 -05:00
);
}
2016-10-29 10:30:55 -05:00
/**
2018-04-24 12:48:42 -05:00
* @param Request $request
* @param TransactionType $destinationType
* @param TransactionJournal $journal
2016-10-30 00:14:07 -05:00
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws FireflyException
* @throws FireflyException
2016-10-29 10:30:55 -05:00
*/
2018-04-24 12:48:42 -05:00
public function postIndex(Request $request, TransactionType $destinationType, TransactionJournal $journal)
2016-10-29 08:14:33 -05:00
{
2017-03-04 04:19:44 -06:00
// @codeCoverageIgnoreStart
2016-11-22 12:10:17 -06:00
if ($this->isOpeningBalance($journal)) {
2018-07-01 02:27:22 -05:00
Log::debug('Journal is opening balance, return to account.');
2016-11-22 12:10:17 -06:00
return $this->redirectToAccount($journal);
}
2017-03-04 04:19:44 -06:00
// @codeCoverageIgnoreEnd
2016-11-22 12:10:17 -06:00
2016-10-30 00:14:07 -05:00
$data = $request->all();
2016-10-29 10:30:55 -05:00
2016-10-30 00:14:07 -05:00
if ($journal->transactionType->type === $destinationType->type) {
2018-07-01 02:27:22 -05:00
Log::info('Journal is already of the desired type.');
2018-04-22 10:10:11 -05:00
session()->flash('error', trans('firefly.convert_is_already_type_' . $destinationType->type));
2016-10-29 10:30:55 -05:00
return redirect(route('transactions.show', [$journal->id]));
}
if ($journal->transactions()->count() > 2) {
2018-07-01 02:27:22 -05:00
Log::info('Journal has more than two transactions.');
2018-04-22 10:10:11 -05:00
session()->flash('error', trans('firefly.cannot_convert_split_journal'));
2016-10-29 10:30:55 -05:00
return redirect(route('transactions.show', [$journal->id]));
}
2016-10-30 00:14:07 -05:00
// get the new source and destination account:
$source = $this->getSourceAccount($journal, $destinationType, $data);
$destination = $this->getDestinationAccount($journal, $destinationType, $data);
// update the journal:
2018-04-24 12:48:42 -05:00
$errors = $this->repository->convert($journal, $destinationType, $source, $destination);
2016-10-29 10:30:55 -05:00
2016-10-30 00:14:07 -05:00
if ($errors->count() > 0) {
2017-02-15 08:12:46 -06:00
return redirect(route('transactions.convert.index', [strtolower($destinationType->type), $journal->id]))->withErrors($errors)->withInput();
2016-10-30 00:14:07 -05:00
}
2016-10-29 10:30:55 -05:00
2018-04-22 10:10:11 -05:00
session()->flash('success', trans('firefly.converted_to_' . $destinationType->type));
2016-10-29 10:30:55 -05:00
2016-10-30 00:14:07 -05:00
return redirect(route('transactions.show', [$journal->id]));
}
2016-10-29 10:30:55 -05:00
2016-10-30 00:14:07 -05:00
/**
* @param TransactionJournal $journal
* @param TransactionType $destinationType
* @param array $data
*
* @return Account
2017-11-15 05:25:49 -06:00
*
2016-10-30 00:14:07 -05:00
* @throws FireflyException
*/
private function getDestinationAccount(TransactionJournal $journal, TransactionType $destinationType, array $data): Account
{
/** @var AccountRepositoryInterface $accountRepository */
$accountRepository = app(AccountRepositoryInterface::class);
$sourceAccount = $this->repository->getJournalSourceAccounts($journal)->first();
$destinationAccount = $this->repository->getJournalDestinationAccounts($journal)->first();
2016-10-30 00:14:07 -05:00
$sourceType = $journal->transactionType;
$joined = $sourceType->type . '-' . $destinationType->type;
switch ($joined) {
default:
2017-03-04 04:19:44 -06:00
throw new FireflyException('Cannot handle ' . $joined); // @codeCoverageIgnore
2017-06-07 00:38:58 -05:00
case TransactionType::WITHDRAWAL . '-' . TransactionType::DEPOSIT:
// one
2016-10-30 00:14:07 -05:00
$destination = $sourceAccount;
break;
2017-06-07 00:38:58 -05:00
case TransactionType::WITHDRAWAL . '-' . TransactionType::TRANSFER:
// two
2018-04-02 08:10:40 -05:00
$destination = $accountRepository->findNull((int)$data['destination_account_asset']);
2016-10-30 00:14:07 -05:00
break;
2017-06-07 00:38:58 -05:00
case TransactionType::DEPOSIT . '-' . TransactionType::WITHDRAWAL:
case TransactionType::TRANSFER . '-' . TransactionType::WITHDRAWAL:
// three and five
2017-11-15 05:25:49 -06:00
if ('' === $data['destination_account_expense'] || null === $data['destination_account_expense']) {
2017-05-07 12:45:40 -05:00
// destination is a cash account.
2018-03-26 13:48:47 -05:00
return $accountRepository->getCashAccount();
2017-05-07 12:45:40 -05:00
}
2016-10-30 00:14:07 -05:00
$data = [
2018-03-26 13:48:47 -05:00
'name' => $data['destination_account_expense'],
'accountType' => 'expense',
'account_type_id' => null,
'virtualBalance' => 0,
'active' => true,
'iban' => null,
2016-10-30 00:14:07 -05:00
];
$destination = $accountRepository->store($data);
break;
2017-06-07 00:38:58 -05:00
case TransactionType::DEPOSIT . '-' . TransactionType::TRANSFER:
case TransactionType::TRANSFER . '-' . TransactionType::DEPOSIT:
// four and six
2016-10-30 00:14:07 -05:00
$destination = $destinationAccount;
break;
}
2016-10-29 10:30:55 -05:00
2016-10-30 00:14:07 -05:00
return $destination;
}
2016-10-29 08:14:33 -05:00
2016-10-30 00:14:07 -05:00
/**
* @param TransactionJournal $journal
* @param TransactionType $destinationType
* @param array $data
*
* @return Account
2017-11-15 05:25:49 -06:00
*
2016-10-30 00:14:07 -05:00
* @throws FireflyException
*/
private function getSourceAccount(TransactionJournal $journal, TransactionType $destinationType, array $data): Account
{
/** @var AccountRepositoryInterface $accountRepository */
$accountRepository = app(AccountRepositoryInterface::class);
$sourceAccount = $this->repository->getJournalSourceAccounts($journal)->first();
$destinationAccount = $this->repository->getJournalDestinationAccounts($journal)->first();
2016-10-30 00:14:07 -05:00
$sourceType = $journal->transactionType;
$joined = $sourceType->type . '-' . $destinationType->type;
switch ($joined) {
default:
2017-03-04 04:19:44 -06:00
throw new FireflyException('Cannot handle ' . $joined); // @codeCoverageIgnore
2017-06-24 06:04:41 -05:00
case TransactionType::WITHDRAWAL . '-' . TransactionType::DEPOSIT:
case TransactionType::TRANSFER . '-' . TransactionType::DEPOSIT:
2017-05-07 12:45:40 -05:00
2017-11-15 05:25:49 -06:00
if ('' === $data['source_account_revenue'] || null === $data['source_account_revenue']) {
2017-05-07 12:45:40 -05:00
// destination is a cash account.
2018-03-26 13:48:47 -05:00
return $accountRepository->getCashAccount();
2017-05-07 12:45:40 -05:00
}
2016-10-30 00:14:07 -05:00
$data = [
2018-03-26 13:48:47 -05:00
'name' => $data['source_account_revenue'],
'accountType' => 'revenue',
'virtualBalance' => 0,
'active' => true,
'account_type_id' => null,
'iban' => null,
2016-10-30 00:14:07 -05:00
];
$source = $accountRepository->store($data);
break;
2017-06-24 06:04:41 -05:00
case TransactionType::WITHDRAWAL . '-' . TransactionType::TRANSFER:
case TransactionType::TRANSFER . '-' . TransactionType::WITHDRAWAL:
2016-10-30 00:14:07 -05:00
$source = $sourceAccount;
break;
2017-06-24 06:04:41 -05:00
case TransactionType::DEPOSIT . '-' . TransactionType::WITHDRAWAL:
2016-10-30 00:14:07 -05:00
$source = $destinationAccount;
break;
2017-06-24 06:04:41 -05:00
case TransactionType::DEPOSIT . '-' . TransactionType::TRANSFER:
2018-04-02 08:10:40 -05:00
$source = $accountRepository->findNull((int)$data['source_account_asset']);
2016-10-30 00:14:07 -05:00
break;
}
2016-10-29 08:14:33 -05:00
2016-10-30 00:14:07 -05:00
return $source;
2016-10-29 08:14:33 -05:00
}
}