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.
|
2016-10-04 23:52:15 -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:44:05 -06:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-04-29 10:29:13 -05:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2016-05-20 05:41:23 -05:00
|
|
|
|
2016-04-29 10:29:13 -05:00
|
|
|
namespace FireflyIII\Http\Controllers\Transaction;
|
|
|
|
|
2019-03-30 05:03:39 -05:00
|
|
|
use FireflyIII\Events\UpdatedTransactionGroup;
|
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;
|
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;
|
2016-05-11 16:03:13 -05:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
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-08-09 09:13:13 -05:00
|
|
|
use FireflyIII\Support\Http\Controllers\ModelInformation;
|
2018-08-09 10:46:14 -05:00
|
|
|
use FireflyIII\Support\Http\Controllers\RequestInformation;
|
2016-05-05 11:59:46 -05:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use View;
|
2016-04-29 10:29:13 -05:00
|
|
|
|
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class SplitController.
|
2018-07-22 01:10:16 -05:00
|
|
|
*
|
2018-07-20 07:34:56 -05:00
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
|
2016-04-29 10:29:13 -05:00
|
|
|
*/
|
|
|
|
class SplitController extends Controller
|
|
|
|
{
|
2018-08-09 10:46:14 -05:00
|
|
|
use ModelInformation, RequestInformation;
|
2018-08-09 09:13:13 -05:00
|
|
|
|
2018-07-22 01:10:16 -05:00
|
|
|
/** @var AttachmentHelperInterface Attachment helper */
|
2016-10-21 14:41:31 -05:00
|
|
|
private $attachments;
|
2016-10-22 02:44:47 -05:00
|
|
|
|
2018-07-21 01:06:24 -05:00
|
|
|
/** @var BudgetRepositoryInterface The budget repository */
|
2016-10-21 14:41:31 -05:00
|
|
|
private $budgets;
|
2016-10-22 02:44:47 -05:00
|
|
|
|
2018-07-20 13:53:48 -05:00
|
|
|
/** @var CurrencyRepositoryInterface The currency repository */
|
2016-10-21 14:41:31 -05:00
|
|
|
private $currencies;
|
2018-07-20 13:53:48 -05:00
|
|
|
/** @var JournalRepositoryInterface Journals and transactions overview */
|
2017-12-24 04:29:16 -06:00
|
|
|
private $repository;
|
|
|
|
|
2016-04-29 10:29:13 -05:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* SplitController constructor.
|
2016-04-29 10:29:13 -05:00
|
|
|
*/
|
2016-05-05 11:59:46 -05:00
|
|
|
public function __construct()
|
2016-04-29 10:29:13 -05:00
|
|
|
{
|
2019-04-19 00:00:19 -05:00
|
|
|
throw new FireflyException('Do not use me.');
|
2016-05-05 11:59:46 -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->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');
|
2018-07-15 02:38:49 -05:00
|
|
|
app('view')->share('title', (string)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
|
|
|
}
|
|
|
|
|
2016-05-12 15:44:31 -05:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Edit a split.
|
|
|
|
*
|
2016-05-12 15:44:31 -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
|
2016-05-12 15:44:31 -05:00
|
|
|
*/
|
|
|
|
public function edit(Request $request, TransactionJournal $journal)
|
2016-05-11 16:03:13 -05:00
|
|
|
{
|
2019-03-30 05:03:39 -05:00
|
|
|
throw new FireflyException('Needs refactoring');
|
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-05-31 14:48:09 -05:00
|
|
|
// basic fields:
|
2018-07-13 08:50:42 -05:00
|
|
|
$uploadSize = min(app('steam')->phpBytes(ini_get('upload_max_filesize')), app('steam')->phpBytes(ini_get('post_max_size')));
|
2018-07-15 02:38:49 -05:00
|
|
|
$subTitle = (string)trans('breadcrumbs.edit_journal', ['description' => $journal->description]);
|
2018-05-31 14:48:09 -05:00
|
|
|
$subTitleIcon = 'fa-pencil';
|
2016-11-22 12:10:17 -06:00
|
|
|
|
2018-05-31 14:48:09 -05:00
|
|
|
// lists and collections
|
|
|
|
$currencies = $this->currencies->get();
|
2018-07-15 08:45:45 -05:00
|
|
|
$budgets = app('expandedform')->makeSelectListWithEmpty($this->budgets->getActiveBudgets());
|
2018-05-31 14:48:09 -05:00
|
|
|
|
|
|
|
// other fields
|
2018-07-14 09:08:34 -05:00
|
|
|
$optionalFields = app('preferences')->get('transaction_journal_optional_fields', [])->data;
|
2016-10-10 00:49:39 -05:00
|
|
|
$preFilled = $this->arrayFromJournal($request, $journal);
|
2017-08-19 11:24:04 -05:00
|
|
|
|
2016-05-13 00:33:04 -05:00
|
|
|
// 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');
|
2016-05-13 00:33:04 -05:00
|
|
|
}
|
2018-04-22 10:12:22 -05:00
|
|
|
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-05-31 14:48:09 -05:00
|
|
|
'journal'
|
2018-02-25 12:09:05 -06:00
|
|
|
)
|
2016-05-12 03:38:44 -05:00
|
|
|
);
|
2016-05-11 16:03:13 -05:00
|
|
|
}
|
2016-05-05 11:59:46 -05:00
|
|
|
|
2016-05-12 15:44:31 -05:00
|
|
|
/**
|
2018-07-22 01:10:16 -05:00
|
|
|
* Store new split journal.
|
|
|
|
*
|
2017-12-29 02:05:35 -06:00
|
|
|
* @param SplitJournalFormRequest $request
|
|
|
|
* @param TransactionJournal $journal
|
2016-05-13 00:33:04 -05:00
|
|
|
*
|
2016-10-21 14:41:31 -05:00
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2018-07-20 07:34:56 -05:00
|
|
|
*
|
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
2016-05-12 15:44:31 -05:00
|
|
|
*/
|
2017-12-24 04:29:16 -06:00
|
|
|
public function update(SplitJournalFormRequest $request, TransactionJournal $journal)
|
2016-05-12 15:44:31 -05:00
|
|
|
{
|
2019-03-30 05:03:39 -05:00
|
|
|
throw new FireflyException('Needs refactoring.');
|
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-04-24 12:26:16 -05:00
|
|
|
$data = $request->getAll();
|
2018-04-22 05:01:18 -05:00
|
|
|
|
|
|
|
// keep current bill:
|
|
|
|
$data['bill_id'] = $journal->bill_id;
|
2018-05-31 14:48:09 -05:00
|
|
|
$journal = $this->repository->update($journal, $data);
|
2018-02-25 09:04:25 -06:00
|
|
|
|
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);
|
2019-03-30 05:03:39 -05:00
|
|
|
event(new UpdatedTransactionGroup($group));
|
2016-05-13 00:33:04 -05:00
|
|
|
|
2016-05-13 02:55:06 -05:00
|
|
|
// flash messages
|
2017-03-03 11:19:25 -06:00
|
|
|
// @codeCoverageIgnoreStart
|
2019-05-30 05:31:19 -05:00
|
|
|
if (count($this->attachments->getMessages()->get('attachments')) > 0) {
|
2018-04-22 10:10:11 -05:00
|
|
|
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-22 10:10:11 -05:00
|
|
|
session()->flash('success', (string)trans('firefly.updated_' . $type, ['description' => $journal->description]));
|
2018-06-24 06:46:34 -05:00
|
|
|
app('preferences')->mark();
|
2016-05-13 00:33:04 -05:00
|
|
|
|
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')) {
|
2016-05-13 00:33:04 -05:00
|
|
|
// set value so edit routine will not overwrite URL:
|
2018-04-22 10:12:22 -05:00
|
|
|
session()->put('transactions.edit-split.fromUpdate', true);
|
2016-05-13 00:33:04 -05:00
|
|
|
|
2016-12-06 01:59:08 -06:00
|
|
|
return redirect(route('transactions.split.edit', [$journal->id]))->withInput(['return_to_edit' => 1]);
|
2016-05-13 00:33:04 -05:00
|
|
|
}
|
2017-03-03 11:19:25 -06:00
|
|
|
// @codeCoverageIgnoreEnd
|
2016-05-13 00:33:04 -05:00
|
|
|
|
|
|
|
// redirect to previous URL.
|
2017-02-05 01:26:54 -06:00
|
|
|
return redirect($this->getPreviousUri('transactions.edit-split.uri'));
|
2016-05-12 15:44:31 -05:00
|
|
|
}
|
|
|
|
|
2016-08-12 08:10:03 -05:00
|
|
|
}
|