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

281 lines
12 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;
2018-03-11 09:54:33 -05:00
use FireflyIII\Exceptions\FireflyException;
2016-05-13 02:55:06 -05:00
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
2018-03-11 09:54:33 -05:00
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
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;
2018-03-11 09:54:33 -05:00
use FireflyIII\Models\Transaction;
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;
2018-03-11 09:54:33 -05:00
use FireflyIII\Transformers\TransactionTransformer;
use Illuminate\Http\Request;
2018-03-11 09:54:33 -05:00
use Illuminate\Support\Collection;
use Preferences;
use Session;
use Steam;
2018-03-11 09:54:33 -05:00
use Symfony\Component\HttpFoundation\ParameterBag;
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;
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);
2016-10-21 14:41:31 -05:00
$this->attachments = app(AttachmentHelperInterface::class);
$this->currencies = app(CurrencyRepositoryInterface::class);
2017-12-29 02:05:35 -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
2018-03-11 09:54:33 -05:00
* @throws FireflyException
*/
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)) {
2018-03-03 07:24:06 -06:00
return $this->redirectToAccount($journal); // @codeCoverageIgnore
2016-11-22 12:10:17 -06:00
}
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();
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';
2018-03-07 03:18:22 -06:00
$accountList = $this->accounts->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]);
$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;
2018-04-02 08:10:40 -05:00
$accountArray[$account->id]['currency_id'] = (int)$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(
2018-02-25 09:04:25 -06:00
'transactions.split.edit', compact(
2018-03-26 12:19:11 -05:00
'subTitleIcon', 'currencies', 'optionalFields', 'preFilled', 'subTitle', 'uploadSize', 'budgets',
2018-04-02 08:10:40 -05:00
'journal', 'accountArray'
)
2016-05-12 03:38:44 -05:00
);
2016-05-11 16:03:13 -05:00
}
/**
2017-12-29 02:05:35 -06:00
* @param SplitJournalFormRequest $request
* @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)) {
2018-03-03 07:24:06 -06:00
return $this->redirectToAccount($journal); // @codeCoverageIgnore
2016-11-22 12:10:17 -06:00
}
2018-02-25 09:04:25 -06:00
$data = $request->getAll();
$journal = $this->repository->update($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
2018-02-25 09:04:25 -06:00
$type = strtolower($this->repository->getTransactionType($journal));
2018-04-02 08:10:40 -05:00
Session::flash('success', (string)trans('firefly.updated_' . $type, ['description' => $journal->description]));
Preferences::mark();
2017-03-03 11:19:25 -06:00
// @codeCoverageIgnoreStart
2018-04-02 08:10:40 -05:00
if (1 === (int)$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'));
}
/**
2017-09-08 23:41:45 -05:00
* @param SplitJournalFormRequest|Request $request
* @param TransactionJournal $journal
*
* @return array
2018-03-11 09:54:33 -05:00
* @throws FireflyException
*/
private function arrayFromJournal(Request $request, TransactionJournal $journal): array
{
2018-02-25 09:04:25 -06:00
$sourceAccounts = $this->repository->getJournalSourceAccounts($journal);
$destinationAccounts = $this->repository->getJournalDestinationAccounts($journal);
$array = [
2016-05-15 05:08:41 -05:00
'journal_description' => $request->old('journal_description', $journal->description),
'journal_amount' => $this->repository->getJournalTotal($journal),
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,
2018-02-25 09:04:25 -06:00
'what' => strtolower($this->repository->getTransactionType($journal)),
'date' => $request->old('date', $this->repository->getJournalDate($journal, null)),
2018-03-26 12:19:11 -05:00
'tags' => implode(',', $journal->tags->pluck('tag')->toArray()),
2016-10-21 14:41:31 -05:00
// all custom fields:
2018-02-25 09:04:25 -06:00
'interest_date' => $request->old('interest_date', $this->repository->getMetaField($journal, 'interest_date')),
'book_date' => $request->old('book_date', $this->repository->getMetaField($journal, 'book_date')),
'process_date' => $request->old('process_date', $this->repository->getMetaField($journal, 'process_date')),
'due_date' => $request->old('due_date', $this->repository->getMetaField($journal, 'due_date')),
'payment_date' => $request->old('payment_date', $this->repository->getMetaField($journal, 'payment_date')),
'invoice_date' => $request->old('invoice_date', $this->repository->getMetaField($journal, 'invoice_date')),
'internal_reference' => $request->old('internal_reference', $this->repository->getMetaField($journal, 'internal_reference')),
'notes' => $request->old('notes', $this->repository->getNoteText($journal)),
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
2018-03-11 09:54:33 -05:00
* @throws FireflyException
*/
2016-10-21 14:41:31 -05:00
private function getTransactionDataFromJournal(TransactionJournal $journal): array
{
2018-03-11 09:54:33 -05:00
// use collector to collect transactions.
$collector = app(JournalCollectorInterface::class);
$collector->setUser(auth()->user());
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
// filter on specific journals.
$collector->setJournals(new Collection([$journal]));
$set = $collector->getJournals();
$transactions = [];
$transformer = new TransactionTransformer(new ParameterBag);
/** @var Transaction $transaction */
foreach ($set as $transaction) {
if ($transaction->transaction_amount > 0) {
$transactions[] = $transformer->transform($transaction);
2018-02-25 09:04:25 -06:00
}
}
2018-03-11 09:54:33 -05:00
return $transactions;
}
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.
2018-03-11 09:54:33 -05:00
$array[$index] = $row;
$array[$index]['currency_id'] = $array[0]['transaction_currency_id'];
2018-04-04 12:15:05 -05:00
$array[$index]['currency_code'] = $array[0]['transaction_currency_code'] ?? '';
$array[$index]['currency_symbol'] = $array[0]['transaction_currency_symbol'] ?? '';
2018-03-11 09:54:33 -05:00
$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'];
2017-09-08 23:25:20 -05:00
}
return $array;
}
2016-08-12 08:10:03 -05:00
}