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

493 lines
20 KiB
PHP
Raw Normal View History

2016-10-21 12:20:03 -05:00
<?php
/**
* SingleController.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2016-10-21 12:20:03 -05:00
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
2016-10-21 12:20:03 -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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2016-10-21 12:20:03 -05:00
*/
declare(strict_types=1);
2016-10-21 12:20:03 -05:00
namespace FireflyIII\Http\Controllers\Transaction;
2017-08-21 10:11:18 -05:00
use Carbon\Carbon;
2016-10-21 12:20:03 -05:00
use ExpandedForm;
2016-10-22 02:31:27 -05:00
use FireflyIII\Events\StoredTransactionJournal;
use FireflyIII\Events\UpdatedTransactionJournal;
2016-10-21 12:20:03 -05:00
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\JournalFormRequest;
2017-09-03 03:51:02 -05:00
use FireflyIII\Models\Account;
2016-10-21 12:20:03 -05:00
use FireflyIII\Models\AccountType;
2017-10-06 08:41:21 -05:00
use FireflyIII\Models\Note;
2017-07-07 10:51:14 -05:00
use FireflyIII\Models\Transaction;
2016-10-21 12:20:03 -05:00
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2016-10-21 12:20:03 -05:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use Log;
use Preferences;
use Session;
use Steam;
use View;
/**
* Class SingleController
*
* @package FireflyIII\Http\Controllers\Transaction
*/
class SingleController extends Controller
{
/** @var AccountRepositoryInterface */
private $accounts;
/** @var AttachmentHelperInterface */
private $attachments;
/** @var BudgetRepositoryInterface */
private $budgets;
/** @var CurrencyRepositoryInterface */
private $currency;
2016-10-21 12:20:03 -05:00
/** @var PiggyBankRepositoryInterface */
private $piggyBanks;
2017-07-04 09:31:16 -05:00
/** @var JournalRepositoryInterface */
private $repository;
2016-10-21 12:20:03 -05:00
/**
*
*/
public function __construct()
{
parent::__construct();
$maxFileSize = Steam::phpBytes(ini_get('upload_max_filesize'));
$maxPostSize = Steam::phpBytes(ini_get('post_max_size'));
$uploadSize = min($maxFileSize, $maxPostSize);
View::share('uploadSize', $uploadSize);
// some useful repositories:
$this->middleware(
function ($request, $next) {
$this->accounts = app(AccountRepositoryInterface::class);
$this->budgets = app(BudgetRepositoryInterface::class);
$this->piggyBanks = app(PiggyBankRepositoryInterface::class);
$this->attachments = app(AttachmentHelperInterface::class);
$this->currency = app(CurrencyRepositoryInterface::class);
2017-07-04 09:31:16 -05:00
$this->repository = app(JournalRepositoryInterface::class);
2016-10-21 12:20:03 -05:00
2016-10-29 00:44:46 -05:00
View::share('title', trans('firefly.transactions'));
View::share('mainTitleIcon', 'fa-repeat');
2016-10-21 12:20:03 -05:00
return $next($request);
}
);
}
public function cloneTransaction(TransactionJournal $journal)
{
2017-07-07 10:51:14 -05:00
$source = $journal->sourceAccountList()->first();
$destination = $journal->destinationAccountList()->first();
$budget = $journal->budgets()->first();
$budgetId = is_null($budget) ? 0 : $budget->id;
$category = $journal->categories()->first();
$categoryName = is_null($category) ? '' : $category->name;
$tags = join(',', $journal->tags()->get()->pluck('tag')->toArray());
/** @var Transaction $transaction */
2017-06-12 10:21:31 -05:00
$transaction = $journal->transactions()->first();
$amount = Steam::positive($transaction->amount);
$foreignAmount = is_null($transaction->foreign_amount) ? null : Steam::positive($transaction->foreign_amount);
$preFilled = [
2017-07-07 10:51:14 -05:00
'description' => $journal->description,
'source_account_id' => $source->id,
'source_account_name' => $source->name,
'destination_account_id' => $destination->id,
'destination_account_name' => $destination->name,
'amount' => $amount,
'source_amount' => $amount,
'destination_amount' => $foreignAmount,
'foreign_amount' => $foreignAmount,
2017-07-07 23:09:17 -05:00
'native_amount' => $foreignAmount,
2017-07-07 10:51:14 -05:00
'amount_currency_id_amount' => $transaction->foreign_currency_id ?? 0,
2017-08-21 10:11:18 -05:00
'date' => (new Carbon())->format('Y-m-d'),
2017-07-07 10:51:14 -05:00
'budget_id' => $budgetId,
'category' => $categoryName,
'tags' => $tags,
'interest_date' => $journal->getMeta('interest_date'),
'book_date' => $journal->getMeta('book_date'),
'process_date' => $journal->getMeta('process_date'),
'due_date' => $journal->getMeta('due_date'),
'payment_date' => $journal->getMeta('payment_date'),
'invoice_date' => $journal->getMeta('invoice_date'),
'internal_reference' => $journal->getMeta('internal_reference'),
2017-10-06 08:41:21 -05:00
'notes' => '',
];
2017-10-06 08:41:21 -05:00
/** @var Note $note */
$note = $journal->notes()->first();
if (!is_null($note)) {
$preFilled['notes'] = $note->text;
}
Session::flash('preFilled', $preFilled);
return redirect(route('transactions.create', [strtolower($journal->transactionType->type)]));
}
2016-10-21 12:20:03 -05:00
/**
* @param string $what
*
* @return View
*/
public function create(string $what = TransactionType::DEPOSIT)
{
$what = strtolower($what);
$uploadSize = min(Steam::phpBytes(ini_get('upload_max_filesize')), Steam::phpBytes(ini_get('post_max_size')));
2017-09-03 09:06:37 -05:00
$assetAccounts = $this->groupedActiveAccountList();
2016-10-21 12:20:03 -05:00
$budgets = ExpandedForm::makeSelectListWithEmpty($this->budgets->getActiveBudgets());
$piggyBanks = $this->piggyBanks->getPiggyBanksWithAmount();
$piggies = ExpandedForm::makeSelectListWithEmpty($piggyBanks);
$preFilled = Session::has('preFilled') ? session('preFilled') : [];
$subTitle = trans('form.add_new_' . $what);
$subTitleIcon = 'fa-plus';
$optionalFields = Preferences::get('transaction_journal_optional_fields', [])->data;
Session::put('preFilled', $preFilled);
// put previous url in session if not redirect from store (not "create another").
if (session('transactions.create.fromStore') !== true) {
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('transactions.create.uri');
2016-10-21 12:20:03 -05:00
}
Session::forget('transactions.create.fromStore');
Session::flash('gaEventCategory', 'transactions');
Session::flash('gaEventAction', 'create-' . $what);
asort($piggies);
return view(
'transactions.single.create',
compact('assetAccounts', 'subTitleIcon', 'uploadSize', 'budgets', 'what', 'piggies', 'subTitle', 'optionalFields', 'preFilled')
);
2016-10-21 12:20:03 -05:00
}
/**
* Shows the form that allows a user to delete a transaction journal.
*
* @param TransactionJournal $journal
*
2017-07-23 01:16:11 -05:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
2016-10-21 12:20:03 -05:00
*/
public function delete(TransactionJournal $journal)
{
2017-03-03 11:19:25 -06:00
// Covered by another controller's tests
// @codeCoverageIgnoreStart
2016-11-22 12:10:17 -06:00
if ($this->isOpeningBalance($journal)) {
return $this->redirectToAccount($journal);
}
2017-03-03 11:19:25 -06:00
// @codeCoverageIgnoreEnd
2016-11-22 12:10:17 -06:00
2016-10-21 12:20:03 -05:00
$what = strtolower($journal->transaction_type_type ?? $journal->transactionType->type);
$subTitle = trans('firefly.delete_' . $what, ['description' => $journal->description]);
// put previous url in session
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('transactions.delete.uri');
2016-10-21 12:20:03 -05:00
Session::flash('gaEventCategory', 'transactions');
Session::flash('gaEventAction', 'delete-' . $what);
return view('transactions.single.delete', compact('journal', 'subTitle', 'what'));
2016-10-21 12:20:03 -05:00
}
/**
2017-08-10 22:42:15 -05:00
* @param TransactionJournal $transactionJournal
2016-10-21 12:20:03 -05:00
*
* @return \Illuminate\Http\RedirectResponse
2017-08-10 22:42:15 -05:00
* @internal param JournalRepositoryInterface $repository
2016-10-21 12:20:03 -05:00
*/
2017-07-04 09:31:16 -05:00
public function destroy(TransactionJournal $transactionJournal)
2016-10-21 12:20:03 -05:00
{
2017-03-03 11:19:25 -06:00
// @codeCoverageIgnoreStart
2016-11-22 12:10:17 -06:00
if ($this->isOpeningBalance($transactionJournal)) {
return $this->redirectToAccount($transactionJournal);
}
2017-03-03 11:19:25 -06:00
// @codeCoverageIgnoreEnd
$type = $transactionJournal->transactionTypeStr();
2017-10-19 11:08:50 -05:00
Session::flash('success', strval(trans('firefly.deleted_' . strtolower($type), ['description' => $transactionJournal->description])));
2016-10-21 12:20:03 -05:00
2017-07-04 09:31:16 -05:00
$this->repository->delete($transactionJournal);
2016-10-21 12:20:03 -05:00
Preferences::mark();
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('transactions.delete.uri'));
2016-10-21 12:20:03 -05:00
}
/**
* @param TransactionJournal $journal
*
* @return mixed
*/
public function edit(TransactionJournal $journal)
{
2017-03-03 11:19:25 -06:00
// @codeCoverageIgnoreStart
2016-11-22 12:10:17 -06:00
if ($this->isOpeningBalance($journal)) {
return $this->redirectToAccount($journal);
}
2017-03-03 11:19:25 -06:00
// @codeCoverageIgnoreEnd
2017-07-04 09:31:16 -05:00
if ($this->isSplitJournal($journal)) {
2016-12-06 01:59:08 -06:00
return redirect(route('transactions.split.edit', [$journal->id]));
2016-10-21 12:20:03 -05:00
}
$what = strtolower($journal->transactionTypeStr());
2017-09-03 03:51:02 -05:00
$assetAccounts = $this->groupedAccountList();
2016-12-12 12:39:54 -06:00
$budgetList = ExpandedForm::makeSelectListWithEmpty($this->budgets->getBudgets());
2016-10-21 12:20:03 -05:00
// view related code
$subTitle = trans('breadcrumbs.edit_journal', ['description' => $journal->description]);
2016-11-22 12:10:17 -06:00
2016-10-21 12:20:03 -05:00
// journal related code
$sourceAccounts = $journal->sourceAccountList();
$destinationAccounts = $journal->destinationAccountList();
2016-10-21 12:20:03 -05:00
$optionalFields = Preferences::get('transaction_journal_optional_fields', [])->data;
$pTransaction = $journal->positiveTransaction();
2017-07-04 09:31:16 -05:00
$foreignCurrency = !is_null($pTransaction->foreignCurrency) ? $pTransaction->foreignCurrency : $pTransaction->transactionCurrency;
2016-10-21 12:20:03 -05:00
$preFilled = [
'date' => $journal->dateAsString(),
'interest_date' => $journal->dateAsString('interest_date'),
'book_date' => $journal->dateAsString('book_date'),
'process_date' => $journal->dateAsString('process_date'),
'category' => $journal->categoryAsString(),
'budget_id' => $journal->budgetId(),
2016-10-21 12:20:03 -05:00
'tags' => join(',', $journal->tags->pluck('tag')->toArray()),
'source_account_id' => $sourceAccounts->first()->id,
'source_account_name' => $sourceAccounts->first()->edit_name,
2016-10-21 12:20:03 -05:00
'destination_account_id' => $destinationAccounts->first()->id,
'destination_account_name' => $destinationAccounts->first()->edit_name,
2016-10-21 12:20:03 -05:00
// new custom fields:
'due_date' => $journal->dateAsString('due_date'),
'payment_date' => $journal->dateAsString('payment_date'),
'invoice_date' => $journal->dateAsString('invoice_date'),
2016-10-21 12:20:03 -05:00
'interal_reference' => $journal->getMeta('internal_reference'),
2017-10-06 08:41:21 -05:00
'notes' => '',
// amount fields
'amount' => $pTransaction->amount,
'source_amount' => $pTransaction->amount,
'native_amount' => $pTransaction->amount,
'destination_amount' => $pTransaction->foreign_amount,
'currency' => $pTransaction->transactionCurrency,
'source_currency' => $pTransaction->transactionCurrency,
'native_currency' => $pTransaction->transactionCurrency,
2017-07-04 09:31:16 -05:00
'foreign_currency' => $foreignCurrency,
'destination_currency' => $foreignCurrency,
2016-10-21 12:20:03 -05:00
];
2017-10-06 08:41:21 -05:00
/** @var Note $note */
$note = $journal->notes()->first();
if (!is_null($note)) {
$preFilled['notes'] = $note->text;
}
2016-10-21 12:20:03 -05:00
// amounts for withdrawals and deposits:
2017-06-05 04:12:50 -05:00
// amount, native_amount, source_amount, destination_amount
if (($journal->isWithdrawal() || $journal->isDeposit()) && !is_null($pTransaction->foreign_amount)) {
$preFilled['amount'] = $pTransaction->foreign_amount;
$preFilled['currency'] = $pTransaction->foreignCurrency;
}
2016-10-21 12:20:03 -05:00
Session::flash('preFilled', $preFilled);
Session::flash('gaEventCategory', 'transactions');
Session::flash('gaEventAction', 'edit-' . $what);
// put previous url in session if not redirect from store (not "return_to_edit").
if (session('transactions.edit.fromUpdate') !== true) {
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('transactions.edit.uri');
2016-10-21 12:20:03 -05:00
}
Session::forget('transactions.edit.fromUpdate');
return view(
'transactions.single.edit',
2016-11-04 10:04:36 -05:00
compact('journal', 'optionalFields', 'assetAccounts', 'what', 'budgetList', 'subTitle')
2016-10-21 12:20:03 -05:00
)->with('data', $preFilled);
}
/**
* @param JournalFormRequest $request
* @param JournalRepositoryInterface $repository
*
* @return \Illuminate\Http\RedirectResponse
*/
public function store(JournalFormRequest $request, JournalRepositoryInterface $repository)
{
$doSplit = intval($request->get('split_journal')) === 1;
$createAnother = intval($request->get('create_another')) === 1;
$data = $request->getJournalData();
$journal = $repository->store($data);
if (is_null($journal->id)) {
// error!
Log::error('Could not store transaction journal: ', $journal->getErrors()->toArray());
Session::flash('error', $journal->getErrors()->first());
return redirect(route('transactions.create', [$request->input('what')]))->withInput();
}
2017-01-14 12:43:33 -06:00
/** @var array $files */
2016-12-28 06:02:56 -06:00
$files = $request->hasFile('attachments') ? $request->file('attachments') : null;
$this->attachments->saveAttachmentsForModel($journal, $files);
2016-10-21 12:20:03 -05:00
// store the journal only, flash the rest.
2017-10-27 05:59:43 -05:00
Log::debug(sprintf('Count of error messages is %d', $this->attachments->getErrors()->count()));
2016-10-21 12:20:03 -05:00
if (count($this->attachments->getErrors()->get('attachments')) > 0) {
Session::flash('error', $this->attachments->getErrors()->get('attachments'));
}
// flash messages
if (count($this->attachments->getMessages()->get('attachments')) > 0) {
Session::flash('info', $this->attachments->getMessages()->get('attachments'));
}
2016-10-22 02:31:27 -05:00
event(new StoredTransactionJournal($journal, $data['piggy_bank_id']));
2016-10-21 12:20:03 -05:00
2017-10-19 11:08:50 -05:00
Session::flash('success', strval(trans('firefly.stored_journal', ['description' => $journal->description])));
2016-10-21 12:20:03 -05:00
Preferences::mark();
2017-03-03 11:19:25 -06:00
// @codeCoverageIgnoreStart
2016-10-21 12:20:03 -05:00
if ($createAnother === true) {
Session::put('transactions.create.fromStore', true);
return redirect(route('transactions.create', [$request->input('what')]))->withInput();
}
if ($doSplit === true) {
2016-12-06 01:59:08 -06:00
return redirect(route('transactions.split.edit', [$journal->id]));
2016-10-21 12:20:03 -05:00
}
2017-03-03 11:19:25 -06:00
// @codeCoverageIgnoreEnd
2016-10-21 12:20:03 -05:00
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('transactions.create.uri'));
2016-10-21 12:20:03 -05:00
}
/**
2017-06-07 01:18:42 -05:00
* @param JournalFormRequest $request
* @param JournalRepositoryInterface $repository
* @param TransactionJournal $journal
2016-10-21 12:20:03 -05:00
*
2016-10-22 02:39:31 -05:00
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2016-10-21 12:20:03 -05:00
*/
2017-06-07 01:18:42 -05:00
public function update(JournalFormRequest $request, JournalRepositoryInterface $repository, TransactionJournal $journal)
2016-10-21 12:20:03 -05:00
{
2017-03-03 11:19:25 -06:00
// @codeCoverageIgnoreStart
2016-11-22 12:10:17 -06:00
if ($this->isOpeningBalance($journal)) {
return $this->redirectToAccount($journal);
}
2017-03-03 11:19:25 -06:00
// @codeCoverageIgnoreEnd
2016-11-22 12:10:17 -06:00
2016-10-21 12:20:03 -05:00
$data = $request->getJournalData();
2017-06-07 01:18:42 -05:00
$journal = $repository->update($journal, $data);
2017-01-14 12:43:33 -06:00
/** @var array $files */
$files = $request->hasFile('attachments') ? $request->file('attachments') : null;
2016-12-28 06:02:56 -06:00
$this->attachments->saveAttachmentsForModel($journal, $files);
2016-10-21 12:20:03 -05:00
2017-03-03 11:19:25 -06:00
// @codeCoverageIgnoreStart
2016-10-21 12:20:03 -05:00
if (count($this->attachments->getErrors()->get('attachments')) > 0) {
Session::flash('error', $this->attachments->getErrors()->get('attachments'));
}
if (count($this->attachments->getMessages()->get('attachments')) > 0) {
Session::flash('info', $this->attachments->getMessages()->get('attachments'));
}
2017-03-03 11:19:25 -06:00
// @codeCoverageIgnoreEnd
2016-10-21 12:20:03 -05:00
2016-10-22 02:31:27 -05:00
event(new UpdatedTransactionJournal($journal));
2016-10-21 12:20:03 -05:00
// update, get events by date and sort DESC
$type = strtolower($journal->transactionTypeStr());
2017-10-19 11:08:50 -05:00
Session::flash('success', strval(trans('firefly.updated_' . $type, ['description' => $data['description']])));
2016-10-21 12:20:03 -05:00
Preferences::mark();
2017-03-03 11:19:25 -06:00
// @codeCoverageIgnoreStart
2016-10-21 12:20:03 -05:00
if (intval($request->get('return_to_edit')) === 1) {
Session::put('transactions.edit.fromUpdate', true);
return redirect(route('transactions.edit', [$journal->id]))->withInput(['return_to_edit' => 1]);
}
2017-03-03 11:19:25 -06:00
// @codeCoverageIgnoreEnd
2016-10-21 12:20:03 -05:00
// redirect to previous URL.
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('transactions.edit.uri'));
2016-10-21 12:20:03 -05:00
}
2017-07-04 09:31:16 -05:00
2017-09-03 03:51:02 -05:00
/**
* @return array
*/
2017-09-08 23:41:45 -05:00
private function groupedAccountList(): array
2017-09-03 03:51:02 -05:00
{
2017-09-08 23:41:45 -05:00
$accounts = $this->accounts->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
2017-09-03 03:51:02 -05:00
$return = [];
/** @var Account $account */
foreach ($accounts as $account) {
$type = $account->getMeta('accountRole');
if (strlen($type) === 0) {
$type = 'no_account_type';
}
$key = strval(trans('firefly.opt_group_' . $type));
$return[$key][$account->id] = $account->name;
}
return $return;
}
2017-09-03 09:06:37 -05:00
/**
* @return array
*/
2017-09-08 23:41:45 -05:00
private function groupedActiveAccountList(): array
2017-09-03 09:06:37 -05:00
{
2017-09-08 23:41:45 -05:00
$accounts = $this->accounts->getActiveAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
2017-09-03 09:06:37 -05:00
$return = [];
/** @var Account $account */
foreach ($accounts as $account) {
$type = $account->getMeta('accountRole');
if (strlen($type) === 0) {
$type = 'no_account_type';
}
$key = strval(trans('firefly.opt_group_' . $type));
$return[$key][$account->id] = $account->name;
}
return $return;
}
2017-07-04 09:31:16 -05:00
/**
* @param TransactionJournal $journal
*
* @return bool
*/
private function isSplitJournal(TransactionJournal $journal): bool
{
$count = $this->repository->countTransactions($journal);
if ($count > 2) {
return true;
}
return false;
}
2016-10-23 05:42:44 -05:00
}