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

374 lines
16 KiB
PHP
Raw Normal View History

2016-04-29 10:29:13 -05:00
<?php
/**
* SplitController.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2016-04-29 10:29:13 -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:44:05 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2016-04-29 10:29:13 -05:00
*/
declare(strict_types=1);
2016-04-29 10:29:13 -05:00
namespace FireflyIII\Http\Controllers\Transaction;
use ExpandedForm;
2016-10-22 02:31:27 -05:00
use FireflyIII\Events\UpdatedTransactionJournal;
2016-05-13 02:55:06 -05:00
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
2016-04-29 10:29:13 -05:00
use FireflyIII\Http\Controllers\Controller;
2017-09-08 23:25:20 -05:00
use FireflyIII\Http\Requests\SplitJournalFormRequest;
use FireflyIII\Models\Account;
2016-10-21 14:41:31 -05:00
use FireflyIII\Models\AccountType;
2017-10-19 13:37:39 -05:00
use FireflyIII\Models\Note;
2016-05-11 16:03:13 -05:00
use FireflyIII\Models\TransactionJournal;
2016-10-10 00:49:39 -05:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2016-10-21 14:41:31 -05:00
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2017-06-07 01:18:42 -05:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2016-10-21 14:41:31 -05:00
use FireflyIII\Repositories\Journal\JournalTaskerInterface;
use Illuminate\Http\Request;
2016-10-23 05:37:12 -05:00
use Log;
use Preferences;
use Session;
use Steam;
use View;
2016-04-29 10:29:13 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class SplitController.
2016-04-29 10:29:13 -05:00
*/
class SplitController extends Controller
{
2017-11-15 05:25:49 -06:00
/** @var AccountRepositoryInterface */
2016-10-21 14:41:31 -05:00
private $accounts;
2016-10-22 02:44:47 -05:00
2016-10-21 14:41:31 -05:00
/** @var AttachmentHelperInterface */
private $attachments;
2016-10-22 02:44:47 -05:00
2017-11-15 05:25:49 -06:00
/** @var BudgetRepositoryInterface */
2016-10-21 14:41:31 -05:00
private $budgets;
2016-10-22 02:44:47 -05:00
2016-10-21 14:41:31 -05:00
/** @var CurrencyRepositoryInterface */
private $currencies;
/** @var JournalTaskerInterface */
private $tasker;
2017-12-24 04:29:16 -06:00
/** @var JournalRepositoryInterface */
private $repository;
2016-04-29 10:29:13 -05:00
/**
*
*/
public function __construct()
2016-04-29 10:29:13 -05:00
{
parent::__construct();
2016-10-29 00:44:46 -05:00
2016-10-21 14:41:31 -05:00
// some useful repositories:
$this->middleware(
function ($request, $next) {
2016-10-22 02:44:47 -05:00
$this->accounts = app(AccountRepositoryInterface::class);
$this->budgets = app(BudgetRepositoryInterface::class);
$this->tasker = app(JournalTaskerInterface::class);
2016-10-21 14:41:31 -05:00
$this->attachments = app(AttachmentHelperInterface::class);
$this->currencies = app(CurrencyRepositoryInterface::class);
2017-12-24 04:29:16 -06:00
$this->repository = app(JournalRepositoryInterface::class);
2017-12-16 12:46:36 -06:00
app('view')->share('mainTitleIcon', 'fa-share-alt');
app('view')->share('title', trans('firefly.split-transactions'));
2016-10-21 14:41:31 -05:00
return $next($request);
}
2016-05-15 05:26:40 -05:00
);
2016-05-15 05:08:41 -05:00
}
/**
* @param Request $request
* @param TransactionJournal $journal
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View
*/
public function edit(Request $request, TransactionJournal $journal)
2016-05-11 16:03:13 -05:00
{
2016-11-22 12:10:17 -06:00
if ($this->isOpeningBalance($journal)) {
return $this->redirectToAccount($journal);
}
2016-10-10 00:49:39 -05:00
$uploadSize = min(Steam::phpBytes(ini_get('upload_max_filesize')), Steam::phpBytes(ini_get('post_max_size')));
$currencies = $this->currencies->get();
$accountList = $this->accounts->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
$assetAccounts = ExpandedForm::makeSelectList($accountList);
2016-10-10 00:49:39 -05:00
$optionalFields = Preferences::get('transaction_journal_optional_fields', [])->data;
2016-10-21 14:41:31 -05:00
$budgets = ExpandedForm::makeSelectListWithEmpty($this->budgets->getActiveBudgets());
2016-10-10 00:49:39 -05:00
$preFilled = $this->arrayFromJournal($request, $journal);
$subTitle = trans('breadcrumbs.edit_journal', ['description' => $journal->description]);
$subTitleIcon = 'fa-pencil';
2016-05-12 03:38:44 -05:00
$accountArray = [];
// account array to display currency info:
/** @var Account $account */
foreach ($accountList as $account) {
2017-08-19 14:59:13 -05:00
$accountArray[$account->id] = $account;
$accountArray[$account->id]['currency_id'] = intval($account->getMeta('currency_id'));
}
// put previous url in session if not redirect from store (not "return_to_edit").
2017-11-15 05:25:49 -06:00
if (true !== session('transactions.edit-split.fromUpdate')) {
2017-02-05 01:26:54 -06:00
$this->rememberPreviousUri('transactions.edit-split.uri');
}
Session::forget('transactions.edit-split.fromUpdate');
2016-05-12 03:38:44 -05:00
return view(
2017-01-15 12:16:46 -06:00
'transactions.split.edit',
2016-05-15 05:26:40 -05:00
compact(
2017-11-15 03:52:29 -06:00
'subTitleIcon',
'currencies',
'optionalFields',
'preFilled',
'subTitle',
'uploadSize',
'assetAccounts',
'budgets',
'journal',
'accountArray',
'previous'
2016-05-15 05:26:40 -05:00
)
2016-05-12 03:38:44 -05:00
);
2016-05-11 16:03:13 -05:00
}
/**
2017-09-08 23:25:20 -05:00
* @param SplitJournalFormRequest $request
2017-06-07 01:18:42 -05:00
* @param TransactionJournal $journal
*
2016-10-21 14:41:31 -05:00
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
2017-12-24 04:29:16 -06:00
public function update(SplitJournalFormRequest $request, TransactionJournal $journal)
{
2016-11-22 12:10:17 -06:00
if ($this->isOpeningBalance($journal)) {
return $this->redirectToAccount($journal);
}
2016-12-05 13:01:01 -06:00
$data = $this->arrayFromInput($request);
2017-12-24 04:29:16 -06:00
$journal = $this->repository->updateSplitJournal($journal, $data);
2017-01-14 12:43:33 -06:00
/** @var array $files */
2017-02-05 01:26:54 -06:00
$files = $request->hasFile('attachments') ? $request->file('attachments') : null;
2016-05-13 02:55:06 -05:00
// save attachments:
2016-12-28 06:02:56 -06:00
$this->attachments->saveAttachmentsForModel($journal, $files);
2016-10-22 02:31:27 -05:00
event(new UpdatedTransactionJournal($journal));
2016-05-13 02:55:06 -05:00
// flash messages
2017-03-03 11:19:25 -06:00
// @codeCoverageIgnoreStart
2016-10-21 14:41:31 -05:00
if (count($this->attachments->getMessages()->get('attachments')) > 0) {
Session::flash('info', $this->attachments->getMessages()->get('attachments'));
2016-05-13 02:55:06 -05:00
}
2017-03-03 11:19:25 -06:00
// @codeCoverageIgnoreEnd
2016-05-13 02:55:06 -05:00
$type = strtolower($journal->transactionTypeStr());
2017-10-19 11:08:50 -05:00
Session::flash('success', strval(trans('firefly.updated_' . $type, ['description' => $data['journal_description']])));
Preferences::mark();
2017-03-03 11:19:25 -06:00
// @codeCoverageIgnoreStart
2017-11-15 05:25:49 -06:00
if (1 === intval($request->get('return_to_edit'))) {
// set value so edit routine will not overwrite URL:
Session::put('transactions.edit-split.fromUpdate', true);
2016-12-06 01:59:08 -06:00
return redirect(route('transactions.split.edit', [$journal->id]))->withInput(['return_to_edit' => 1]);
}
2017-03-03 11:19:25 -06:00
// @codeCoverageIgnoreEnd
// redirect to previous URL.
2017-02-05 01:26:54 -06:00
return redirect($this->getPreviousUri('transactions.edit-split.uri'));
}
2016-10-21 14:41:31 -05:00
/**
2017-09-08 23:25:20 -05:00
* @param SplitJournalFormRequest $request
2016-10-21 14:41:31 -05:00
*
* @return array
*/
2017-09-08 23:25:20 -05:00
private function arrayFromInput(SplitJournalFormRequest $request): array
2016-10-21 14:41:31 -05:00
{
2017-11-15 05:25:49 -06:00
$tags = null === $request->get('tags') ? '' : $request->get('tags');
2016-10-21 14:41:31 -05:00
$array = [
'journal_description' => $request->get('journal_description'),
'journal_source_account_id' => $request->get('journal_source_account_id'),
'journal_source_account_name' => $request->get('journal_source_account_name'),
'journal_destination_account_id' => $request->get('journal_destination_account_id'),
'what' => $request->get('what'),
'date' => $request->get('date'),
// all custom fields:
'interest_date' => $request->get('interest_date'),
'book_date' => $request->get('book_date'),
'process_date' => $request->get('process_date'),
'due_date' => $request->get('due_date'),
'payment_date' => $request->get('payment_date'),
'invoice_date' => $request->get('invoice_date'),
'internal_reference' => $request->get('internal_reference'),
'notes' => $request->get('notes'),
2017-09-12 15:11:45 -05:00
'tags' => explode(',', $tags),
2016-10-21 14:41:31 -05:00
// transactions.
'transactions' => $this->getTransactionDataFromRequest($request),
];
return $array;
}
/**
2017-09-08 23:41:45 -05:00
* @param SplitJournalFormRequest|Request $request
* @param TransactionJournal $journal
*
* @return array
*/
private function arrayFromJournal(Request $request, TransactionJournal $journal): array
{
$sourceAccounts = $journal->sourceAccountList();
$destinationAccounts = $journal->destinationAccountList();
2017-11-05 12:49:20 -06:00
$notes = '';
2017-10-19 13:37:39 -05:00
/** @var Note $note */
2017-12-24 04:29:16 -06:00
$note = $this->repository->getNote($journal);
2017-11-15 05:25:49 -06:00
if (null !== $note) {
2017-10-19 13:37:39 -05:00
$notes = $note->text;
}
2017-11-05 12:49:20 -06:00
$array = [
2016-05-15 05:08:41 -05:00
'journal_description' => $request->old('journal_description', $journal->description),
'journal_amount' => $journal->amountPositive(),
2016-05-15 05:08:41 -05:00
'sourceAccounts' => $sourceAccounts,
2016-10-21 14:41:31 -05:00
'journal_source_account_id' => $request->old('journal_source_account_id', $sourceAccounts->first()->id),
'journal_source_account_name' => $request->old('journal_source_account_name', $sourceAccounts->first()->name),
'journal_destination_account_id' => $request->old('journal_destination_account_id', $destinationAccounts->first()->id),
2016-05-15 05:08:41 -05:00
'destinationAccounts' => $destinationAccounts,
'what' => strtolower($journal->transactionTypeStr()),
2017-06-24 06:04:41 -05:00
'date' => $request->old('date', $journal->date->format('Y-m-d')),
2016-10-21 14:41:31 -05:00
'tags' => join(',', $journal->tags->pluck('tag')->toArray()),
// all custom fields:
'interest_date' => $request->old('interest_date', $journal->getMeta('interest_date')),
'book_date' => $request->old('book_date', $journal->getMeta('book_date')),
'process_date' => $request->old('process_date', $journal->getMeta('process_date')),
'due_date' => $request->old('due_date', $journal->getMeta('due_date')),
'payment_date' => $request->old('payment_date', $journal->getMeta('payment_date')),
'invoice_date' => $request->old('invoice_date', $journal->getMeta('invoice_date')),
'internal_reference' => $request->old('internal_reference', $journal->getMeta('internal_reference')),
2017-10-19 13:37:39 -05:00
'notes' => $request->old('notes', $notes),
2016-10-21 14:41:31 -05:00
// transactions.
'transactions' => $this->getTransactionDataFromJournal($journal),
];
2017-09-08 23:25:20 -05:00
// update transactions array with old request data.
2017-12-24 04:29:16 -06:00
2017-09-08 23:25:20 -05:00
$array['transactions'] = $this->updateWithPrevious($array['transactions'], $request->old());
return $array;
}
/**
* @param TransactionJournal $journal
*
* @return array
*/
2016-10-21 14:41:31 -05:00
private function getTransactionDataFromJournal(TransactionJournal $journal): array
{
2016-10-21 14:41:31 -05:00
$transactions = $this->tasker->getTransactionsOverview($journal);
$return = [];
/** @var array $transaction */
foreach ($transactions as $index => $transaction) {
$set = [
'description' => $transaction['description'],
'source_account_id' => $transaction['source_account_id'],
'source_account_name' => $transaction['source_account_name'],
'destination_account_id' => $transaction['destination_account_id'],
'destination_account_name' => $transaction['destination_account_name'],
'amount' => round($transaction['destination_amount'], 12),
'budget_id' => isset($transaction['budget_id']) ? intval($transaction['budget_id']) : 0,
'category' => $transaction['category'],
'transaction_currency_id' => $transaction['transaction_currency_id'],
'transaction_currency_code' => $transaction['transaction_currency_code'],
'transaction_currency_symbol' => $transaction['transaction_currency_symbol'],
'foreign_amount' => round($transaction['foreign_destination_amount'], 12),
'foreign_currency_id' => $transaction['foreign_currency_id'],
'foreign_currency_code' => $transaction['foreign_currency_code'],
'foreign_currency_symbol' => $transaction['foreign_currency_symbol'],
2016-10-21 14:41:31 -05:00
];
// set initial category and/or budget:
2017-11-15 05:25:49 -06:00
if (1 === count($transactions) && 0 === $index) {
$set['budget_id'] = $journal->budgetId();
$set['category'] = $journal->categoryAsString();
}
$return[] = $set;
}
2016-10-21 14:41:31 -05:00
return $return;
}
2016-10-21 14:41:31 -05:00
/**
2017-09-08 23:41:45 -05:00
* @param SplitJournalFormRequest|Request $request
2016-10-21 14:41:31 -05:00
*
* @return array
*/
2017-09-08 23:25:20 -05:00
private function getTransactionDataFromRequest(SplitJournalFormRequest $request): array
2016-10-21 14:41:31 -05:00
{
$return = [];
$transactions = $request->get('transactions');
foreach ($transactions as $transaction) {
$return[] = [
'description' => $transaction['description'],
'source_account_id' => $transaction['source_account_id'] ?? 0,
'source_account_name' => $transaction['source_account_name'] ?? '',
'destination_account_id' => $transaction['destination_account_id'] ?? 0,
'destination_account_name' => $transaction['destination_account_name'] ?? '',
2016-12-30 06:45:02 -06:00
'amount' => round($transaction['amount'] ?? 0, 12),
'foreign_amount' => !isset($transaction['foreign_amount']) ? null : round($transaction['foreign_amount'] ?? 0, 12),
2016-10-21 15:01:42 -05:00
'budget_id' => isset($transaction['budget_id']) ? intval($transaction['budget_id']) : 0,
2016-10-21 14:41:31 -05:00
'category' => $transaction['category'] ?? '',
'transaction_currency_id' => intval($transaction['transaction_currency_id']),
'foreign_currency_id' => $transaction['foreign_currency_id'] ?? null,
2016-10-21 14:41:31 -05:00
];
}
2016-10-23 05:37:12 -05:00
Log::debug(sprintf('Found %d splits in request data.', count($return)));
return $return;
}
2017-09-08 23:25:20 -05:00
/**
* @param $array
* @param $old
*
* @return array
*/
private function updateWithPrevious($array, $old): array
{
2017-11-15 05:25:49 -06:00
if (0 === count($old) || !isset($old['transactions'])) {
2017-09-08 23:25:20 -05:00
return $array;
}
$old = $old['transactions'];
2017-12-24 04:29:16 -06:00
2017-09-08 23:25:20 -05:00
foreach ($old as $index => $row) {
if (isset($array[$index])) {
$array[$index] = array_merge($array[$index], $row);
continue;
}
// take some info from first transaction, that should at least exist.
$array[$index] = $row;
$array[$index]['transaction_currency_id'] = $array[0]['transaction_currency_id'];
$array[$index]['transaction_currency_code'] = $array[0]['transaction_currency_code'];
$array[$index]['transaction_currency_symbol'] = $array[0]['transaction_currency_symbol'];
$array[$index]['foreign_amount'] = round($array[0]['foreign_destination_amount'] ?? '0', 12);
$array[$index]['foreign_currency_id'] = $array[0]['foreign_currency_id'];
$array[$index]['foreign_currency_code'] = $array[0]['foreign_currency_code'];
$array[$index]['foreign_currency_symbol'] = $array[0]['foreign_currency_symbol'];
}
return $array;
}
2016-08-12 08:10:03 -05:00
}