2016-04-29 10:29:13 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* SplitController.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-04 23:52:15 -05:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2016-10-21 14:41:31 -05:00
|
|
|
use FireflyIII\Models\AccountType;
|
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;
|
|
|
|
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Journal\JournalTaskerInterface;
|
2016-05-05 11:59:46 -05:00
|
|
|
use Illuminate\Http\Request;
|
2016-10-23 05:37:12 -05:00
|
|
|
use Log;
|
2016-05-13 00:33:04 -05:00
|
|
|
use Preferences;
|
2016-04-29 13:59:28 -05:00
|
|
|
use Session;
|
2016-05-13 00:33:04 -05:00
|
|
|
use Steam;
|
2016-05-05 11:59:46 -05:00
|
|
|
use View;
|
2016-04-29 10:29:13 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class SplitController
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Http\Controllers\Transaction
|
2016-05-15 08:24:23 -05:00
|
|
|
*
|
2016-04-29 10:29:13 -05:00
|
|
|
*/
|
|
|
|
class SplitController extends Controller
|
|
|
|
{
|
2016-10-21 14:41:31 -05:00
|
|
|
|
|
|
|
/** @var AccountRepositoryInterface */
|
|
|
|
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
|
|
|
|
2016-10-21 14:41:31 -05:00
|
|
|
/** @var BudgetRepositoryInterface */
|
|
|
|
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;
|
|
|
|
|
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
|
|
|
{
|
2016-05-05 11:59:46 -05:00
|
|
|
parent::__construct();
|
2016-10-29 00:44:46 -05:00
|
|
|
|
2016-05-15 05:08:41 -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);
|
2016-10-29 00:44:46 -05:00
|
|
|
View::share('mainTitleIcon', 'fa-share-alt');
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-05-12 15:44:31 -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')));
|
2016-10-21 14:41:31 -05:00
|
|
|
$currencies = ExpandedForm::makeSelectList($this->currencies->get());
|
|
|
|
$assetAccounts = ExpandedForm::makeSelectList($this->accounts->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]));
|
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
|
|
|
|
2016-05-13 00:33:04 -05:00
|
|
|
Session::flash('gaEventCategory', 'transactions');
|
|
|
|
Session::flash('gaEventAction', 'edit-split-' . $preFilled['what']);
|
|
|
|
|
|
|
|
// put previous url in session if not redirect from store (not "return_to_edit").
|
|
|
|
if (session('transactions.edit-split.fromUpdate') !== true) {
|
2017-02-05 01:26:54 -06:00
|
|
|
$this->rememberPreviousUri('transactions.edit-split.uri');
|
2016-05-13 00:33:04 -05:00
|
|
|
}
|
|
|
|
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(
|
2016-09-04 09:21:51 -05:00
|
|
|
'subTitleIcon', 'currencies', 'optionalFields',
|
|
|
|
'preFilled', 'subTitle', 'amount', 'sourceAccounts', 'uploadSize', 'destinationAccounts', 'assetAccounts',
|
2016-05-15 05:26:40 -05:00
|
|
|
'budgets', 'journal'
|
|
|
|
)
|
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-04-29 10:29:13 -05:00
|
|
|
|
2016-05-12 15:44:31 -05:00
|
|
|
/**
|
2016-10-21 14:41:31 -05:00
|
|
|
* @param Request $request
|
|
|
|
* @param JournalRepositoryInterface $repository
|
|
|
|
* @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
|
2016-05-12 15:44:31 -05:00
|
|
|
*/
|
2016-10-21 14:41:31 -05:00
|
|
|
public function update(Request $request, JournalRepositoryInterface $repository, TransactionJournal $journal)
|
2016-05-12 15:44:31 -05:00
|
|
|
{
|
2016-12-11 04:04:53 -06:00
|
|
|
|
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);
|
2016-10-21 14:41:31 -05:00
|
|
|
$journal = $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 00:33:04 -05:00
|
|
|
|
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
|
|
|
|
2017-03-04 00:26:03 -06:00
|
|
|
$type = strtolower($journal->transactionTypeStr());
|
2016-05-13 00:33:04 -05:00
|
|
|
Session::flash('success', strval(trans('firefly.updated_' . $type, ['description' => e($data['journal_description'])])));
|
|
|
|
Preferences::mark();
|
|
|
|
|
2017-03-03 11:19:25 -06:00
|
|
|
// @codeCoverageIgnoreStart
|
2016-05-15 05:08:41 -05:00
|
|
|
if (intval($request->get('return_to_edit')) === 1) {
|
2016-05-13 00:33:04 -05:00
|
|
|
// 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]);
|
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-10-21 14:41:31 -05:00
|
|
|
/**
|
2016-10-22 02:44:47 -05:00
|
|
|
* @param Request $request
|
2016-10-21 14:41:31 -05:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-10-22 02:39:31 -05:00
|
|
|
private function arrayFromInput(Request $request): array
|
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'),
|
2016-10-22 02:39:31 -05:00
|
|
|
'currency_id' => $request->get('currency_id'),
|
2016-10-21 14:41:31 -05:00
|
|
|
'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'),
|
|
|
|
'tags' => explode(',', $request->get('tags')),
|
|
|
|
|
|
|
|
// transactions.
|
|
|
|
'transactions' => $this->getTransactionDataFromRequest($request),
|
|
|
|
];
|
|
|
|
|
2016-11-19 00:27:54 -06:00
|
|
|
|
2016-10-21 14:41:31 -05:00
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2016-05-12 15:44:31 -05:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function arrayFromJournal(Request $request, TransactionJournal $journal): array
|
|
|
|
{
|
2017-03-04 00:18:35 -06:00
|
|
|
$sourceAccounts = $journal->sourceAccountList();
|
|
|
|
$destinationAccounts = $journal->destinationAccountList();
|
2016-05-15 05:08:41 -05:00
|
|
|
$array = [
|
|
|
|
'journal_description' => $request->old('journal_description', $journal->description),
|
2017-03-04 00:18:35 -06:00
|
|
|
'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-10-22 00:28:31 -05:00
|
|
|
'currency_id' => $request->old('currency_id', $journal->transaction_currency_id),
|
2016-05-15 05:08:41 -05:00
|
|
|
'destinationAccounts' => $destinationAccounts,
|
2017-03-04 00:18:35 -06:00
|
|
|
'what' => strtolower($journal->transactionTypeStr()),
|
2016-05-15 05:08:41 -05:00
|
|
|
'date' => $request->old('date', $journal->date),
|
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')),
|
|
|
|
'notes' => $request->old('notes', $journal->getMeta('notes')),
|
|
|
|
|
|
|
|
// transactions.
|
|
|
|
'transactions' => $this->getTransactionDataFromJournal($journal),
|
2016-05-12 15:44:31 -05:00
|
|
|
];
|
2016-05-22 09:38:32 -05:00
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-10-21 14:41:31 -05:00
|
|
|
private function getTransactionDataFromJournal(TransactionJournal $journal): array
|
2016-05-22 09:38:32 -05:00
|
|
|
{
|
2016-10-21 14:41:31 -05:00
|
|
|
$transactions = $this->tasker->getTransactionsOverview($journal);
|
|
|
|
$return = [];
|
|
|
|
/** @var array $transaction */
|
2016-12-04 10:13:37 -06:00
|
|
|
foreach ($transactions as $index => $transaction) {
|
|
|
|
$set = [
|
2016-10-21 14:41:31 -05:00
|
|
|
'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'],
|
2016-12-30 06:45:02 -06:00
|
|
|
'amount' => round($transaction['destination_amount'], 12),
|
2016-10-21 15:00:45 -05:00
|
|
|
'budget_id' => isset($transaction['budget_id']) ? intval($transaction['budget_id']) : 0,
|
2016-10-21 14:41:31 -05:00
|
|
|
'category' => $transaction['category'],
|
|
|
|
];
|
2016-12-04 10:13:37 -06:00
|
|
|
|
|
|
|
// set initial category and/or budget:
|
|
|
|
if (count($transactions) === 1 && $index === 0) {
|
2017-03-04 00:18:35 -06:00
|
|
|
$set['budget_id'] = $journal->budgetId();
|
|
|
|
$set['category'] = $journal->categoryAsString();
|
2016-12-04 10:13:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
$return[] = $set;
|
|
|
|
|
2016-05-22 09:38:32 -05:00
|
|
|
}
|
|
|
|
|
2016-10-21 14:41:31 -05:00
|
|
|
return $return;
|
|
|
|
}
|
2016-05-13 00:33:04 -05:00
|
|
|
|
2016-10-21 14:41:31 -05:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getTransactionDataFromRequest(Request $request): array
|
|
|
|
{
|
|
|
|
$return = [];
|
|
|
|
$transactions = $request->get('transactions');
|
|
|
|
foreach ($transactions as $transaction) {
|
2016-10-23 05:37:12 -05:00
|
|
|
|
2016-10-21 14:41:31 -05:00
|
|
|
$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),
|
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'] ?? '',
|
|
|
|
];
|
2016-05-05 11:59:46 -05:00
|
|
|
}
|
2016-10-23 05:37:12 -05:00
|
|
|
Log::debug(sprintf('Found %d splits in request data.', count($return)));
|
2016-05-05 11:59:46 -05:00
|
|
|
|
2016-05-22 09:38:32 -05:00
|
|
|
return $return;
|
2016-05-05 11:59:46 -05:00
|
|
|
}
|
|
|
|
|
2016-11-22 12:10:17 -06:00
|
|
|
|
2016-08-12 08:10:03 -05:00
|
|
|
}
|