2016-04-29 10:29:13 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* SplitController.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
|
|
|
* This software may be modified and distributed under the terms
|
|
|
|
* of the MIT license. See the LICENSE file for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers\Transaction;
|
|
|
|
|
|
|
|
|
|
|
|
use ExpandedForm;
|
2016-04-29 13:59:28 -05:00
|
|
|
use FireflyIII\Crud\Split\JournalInterface;
|
2016-05-13 00:33:04 -05:00
|
|
|
use FireflyIII\Events\TransactionJournalUpdated;
|
2016-04-29 10:29:13 -05:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
2016-04-29 13:59:28 -05:00
|
|
|
use FireflyIII\Http\Requests\SplitJournalFormRequest;
|
2016-05-12 15:44:31 -05:00
|
|
|
use FireflyIII\Models\Transaction;
|
2016-05-11 16:03:13 -05:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2016-04-29 10:29:13 -05:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
|
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
2016-05-05 11:59:46 -05:00
|
|
|
use Illuminate\Http\Request;
|
2016-05-13 00:33:04 -05:00
|
|
|
use Input;
|
2016-04-30 02:48:39 -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;
|
|
|
|
use URL;
|
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
|
|
|
|
*/
|
|
|
|
class SplitController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
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();
|
|
|
|
View::share('mainTitleIcon', 'fa-share-alt');
|
|
|
|
View::share('title', trans('firefly.split-transactions'));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
$count = $journal->transactions()->count();
|
|
|
|
if ($count === 2) {
|
|
|
|
return redirect(route('transactions.edit', [$journal->id]));
|
|
|
|
}
|
2016-05-12 03:38:44 -05:00
|
|
|
|
|
|
|
/** @var CurrencyRepositoryInterface $currencyRepository */
|
|
|
|
$currencyRepository = app(CurrencyRepositoryInterface::class);
|
|
|
|
/** @var AccountRepositoryInterface $accountRepository */
|
|
|
|
$accountRepository = app(AccountRepositoryInterface::class);
|
|
|
|
|
|
|
|
/** @var BudgetRepositoryInterface $budgetRepository */
|
|
|
|
$budgetRepository = app(BudgetRepositoryInterface::class);
|
|
|
|
|
2016-05-13 00:33:04 -05:00
|
|
|
$uploadSize = min(Steam::phpBytes(ini_get('upload_max_filesize')), Steam::phpBytes(ini_get('post_max_size')));
|
2016-05-12 03:38:44 -05:00
|
|
|
$currencies = ExpandedForm::makeSelectList($currencyRepository->get());
|
|
|
|
$assetAccounts = ExpandedForm::makeSelectList($accountRepository->getAccounts(['Default account', 'Asset account']));
|
|
|
|
$budgets = ExpandedForm::makeSelectListWithEmpty($budgetRepository->getActiveBudgets());
|
2016-05-12 15:44:31 -05:00
|
|
|
$preFilled = $this->arrayFromJournal($request, $journal);
|
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) {
|
|
|
|
Session::put('transactions.edit-split.url', URL::previous());
|
|
|
|
}
|
|
|
|
Session::forget('transactions.edit-split.fromUpdate');
|
2016-05-12 03:38:44 -05:00
|
|
|
|
|
|
|
return view(
|
|
|
|
'split.journals.edit',
|
2016-05-13 00:33:04 -05:00
|
|
|
compact('currencies', 'preFilled', 'amount', 'sourceAccounts', 'uploadSize', 'destinationAccounts', 'assetAccounts', '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
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function journalFromStore(Request $request)
|
|
|
|
{
|
|
|
|
if ($request->old('journal_currency_id')) {
|
|
|
|
$preFilled = $this->arrayFromOldData($request->old());
|
|
|
|
} else {
|
|
|
|
$preFilled = $this->arrayFromSession();
|
|
|
|
}
|
|
|
|
|
|
|
|
Session::flash('preFilled', $preFilled);
|
|
|
|
View::share('subTitle', trans('firefly.split-new-transaction'));
|
|
|
|
|
2016-04-29 10:29:13 -05:00
|
|
|
/** @var CurrencyRepositoryInterface $currencyRepository */
|
|
|
|
$currencyRepository = app(CurrencyRepositoryInterface::class);
|
|
|
|
/** @var AccountRepositoryInterface $accountRepository */
|
|
|
|
$accountRepository = app(AccountRepositoryInterface::class);
|
|
|
|
|
|
|
|
/** @var BudgetRepositoryInterface $budgetRepository */
|
|
|
|
$budgetRepository = app(BudgetRepositoryInterface::class);
|
|
|
|
|
2016-04-29 13:59:28 -05:00
|
|
|
$currencies = ExpandedForm::makeSelectList($currencyRepository->get());
|
2016-04-29 10:29:13 -05:00
|
|
|
$assetAccounts = ExpandedForm::makeSelectList($accountRepository->getAccounts(['Default account', 'Asset account']));
|
|
|
|
$budgets = ExpandedForm::makeSelectListWithEmpty($budgetRepository->getActiveBudgets());
|
2016-04-30 02:48:39 -05:00
|
|
|
|
2016-05-12 15:44:31 -05:00
|
|
|
return view('split.journals.from-store', compact('currencies', 'assetAccounts', 'budgets', 'preFilled'));
|
2016-04-29 10:29:13 -05:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-04-29 13:59:28 -05:00
|
|
|
/**
|
|
|
|
* @param SplitJournalFormRequest $request
|
|
|
|
* @param JournalInterface $repository
|
|
|
|
*
|
2016-05-11 16:03:13 -05:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
* @throws FireflyException
|
2016-04-29 13:59:28 -05:00
|
|
|
*/
|
|
|
|
public function postJournalFromStore(SplitJournalFormRequest $request, JournalInterface $repository)
|
2016-04-29 10:29:13 -05:00
|
|
|
{
|
2016-04-29 13:59:28 -05:00
|
|
|
$data = $request->getSplitData();
|
|
|
|
|
|
|
|
// store an empty journal first. This will be the place holder.
|
|
|
|
$journal = $repository->storeJournal($data);
|
|
|
|
// Then, store each transaction individually.
|
|
|
|
|
2016-05-05 11:59:46 -05:00
|
|
|
if (is_null($journal->id)) {
|
|
|
|
throw new FireflyException('Could not store transaction.');
|
|
|
|
}
|
2016-04-29 13:59:28 -05:00
|
|
|
|
2016-04-29 10:29:13 -05:00
|
|
|
// forget temp journal data
|
2016-04-29 13:59:28 -05:00
|
|
|
Session::forget('temporary_split_data');
|
|
|
|
|
|
|
|
// this is where we originally came from.
|
|
|
|
return redirect(session('transactions.create.url'));
|
2016-04-29 10:29:13 -05:00
|
|
|
}
|
|
|
|
|
2016-05-12 15:44:31 -05:00
|
|
|
/**
|
2016-05-13 00:33:04 -05:00
|
|
|
* @param TransactionJournal $journal
|
2016-05-12 15:44:31 -05:00
|
|
|
* @param SplitJournalFormRequest $request
|
|
|
|
* @param JournalInterface $repository
|
2016-05-13 00:33:04 -05:00
|
|
|
*
|
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2016-05-12 15:44:31 -05:00
|
|
|
*/
|
2016-05-13 00:33:04 -05:00
|
|
|
public function update(TransactionJournal $journal, SplitJournalFormRequest $request, JournalInterface $repository)
|
2016-05-12 15:44:31 -05:00
|
|
|
{
|
2016-05-13 00:33:04 -05:00
|
|
|
|
|
|
|
$data = $request->getSplitData();
|
|
|
|
$journal = $repository->updateJournal($journal, $data);
|
|
|
|
|
|
|
|
event(new TransactionJournalUpdated($journal));
|
|
|
|
// update, get events by date and sort DESC
|
|
|
|
|
|
|
|
$type = strtolower($journal->transaction_type_type ?? TransactionJournal::transactionTypeStr($journal));
|
|
|
|
Session::flash('success', strval(trans('firefly.updated_' . $type, ['description' => e($data['journal_description'])])));
|
|
|
|
Preferences::mark();
|
|
|
|
|
|
|
|
if (intval(Input::get('return_to_edit')) === 1) {
|
|
|
|
// set value so edit routine will not overwrite URL:
|
|
|
|
Session::put('transactions.edit-split.fromUpdate', true);
|
|
|
|
|
|
|
|
return redirect(route('split.journal.edit', [$journal->id]))->withInput(['return_to_edit' => 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// redirect to previous URL.
|
|
|
|
return redirect(session('transactions.edit-split.url'));
|
|
|
|
|
|
|
|
|
|
|
|
// update all:
|
|
|
|
|
2016-05-12 15:44:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function arrayFromJournal(Request $request, TransactionJournal $journal): array
|
|
|
|
{
|
|
|
|
if (Session::has('_old_input')) {
|
|
|
|
Log::debug('Old input: ', session('_old_input'));
|
|
|
|
}
|
|
|
|
$sourceAccounts = TransactionJournal::sourceAccountList($journal);
|
|
|
|
$firstSourceId = $sourceAccounts->first()->id;
|
|
|
|
$array = [
|
|
|
|
'journal_description' => $request->old('journal_description', $journal->description),
|
|
|
|
'journal_amount' => TransactionJournal::amountPositive($journal),
|
|
|
|
'sourceAccounts' => $sourceAccounts,
|
|
|
|
'transaction_currency_id' => $request->old('transaction_currency_id', $journal->transaction_currency_id),
|
|
|
|
'destinationAccounts' => TransactionJournal::destinationAccountList($journal),
|
|
|
|
'what' => strtolower(TransactionJournal::transactionTypeStr($journal)),
|
|
|
|
'date' => $request->old('date', $journal->date),
|
|
|
|
'interest_date' => $request->old('interest_date', $journal->interest_date),
|
|
|
|
'book_date' => $request->old('book_date', $journal->book_date),
|
|
|
|
'process_date' => $request->old('process_date', $journal->process_date),
|
|
|
|
'description' => [],
|
|
|
|
'destination_account_id' => [],
|
|
|
|
'destination_account_name' => [],
|
|
|
|
'amount' => [],
|
|
|
|
'budget_id' => [],
|
|
|
|
'category' => [],
|
|
|
|
];
|
2016-05-13 00:33:04 -05:00
|
|
|
$index = 0;
|
2016-05-12 15:44:31 -05:00
|
|
|
/** @var Transaction $transaction */
|
|
|
|
foreach ($journal->transactions()->get() as $transaction) {
|
2016-05-13 00:33:04 -05:00
|
|
|
$budget = $transaction->budgets()->first();
|
|
|
|
$category = $transaction->categories()->first();
|
|
|
|
$budgetId = 0;
|
|
|
|
$categoryName = '';
|
2016-05-12 15:44:31 -05:00
|
|
|
if (!is_null($budget)) {
|
|
|
|
$budgetId = $budget->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_null($category)) {
|
|
|
|
$categoryName = $category->name;
|
|
|
|
}
|
2016-05-13 00:33:04 -05:00
|
|
|
|
|
|
|
$budgetId = $request->old('budget_id')[$index] ?? $budgetId;
|
|
|
|
$categoryName = $request->old('category')[$index] ?? $categoryName;
|
|
|
|
$amount = $request->old('amount')[$index] ?? $transaction->amount;
|
|
|
|
$description = $request->old('description')[$index] ?? $transaction->description;
|
|
|
|
$destinationName = $request->old('destination_account_name')[$index] ??$transaction->account->name;
|
|
|
|
|
|
|
|
|
|
|
|
if ($journal->isWithdrawal() && $transaction->account_id !== $firstSourceId) {
|
|
|
|
$array['description'][] = $description;
|
|
|
|
$array['destination_account_id'][] = $transaction->account_id;
|
|
|
|
$array['destination_account_name'][] = $destinationName;
|
|
|
|
$array['amount'][] = $amount;
|
|
|
|
$array['budget_id'][] = intval($budgetId);
|
|
|
|
$array['category'][] = $categoryName;
|
|
|
|
// only add one when "valid" transaction
|
|
|
|
$index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-12 15:44:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2016-05-05 11:59:46 -05:00
|
|
|
/**
|
|
|
|
* @param array $old
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function arrayFromOldData(array $old): array
|
|
|
|
{
|
|
|
|
// this array is pretty much equal to what we expect it to be.
|
|
|
|
Log::debug('Prefilled', $old);
|
|
|
|
|
|
|
|
return $old;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
private function arrayFromSession(): array
|
|
|
|
{
|
|
|
|
// expect data to be in session or in post?
|
|
|
|
$data = session('temporary_split_data');
|
|
|
|
|
|
|
|
if (!is_array($data)) {
|
|
|
|
Log::error('Could not find transaction data in your session. Please go back and try again.', ['data' => $data]); // translate me.
|
|
|
|
throw new FireflyException('Could not find transaction data in your session. Please go back and try again.'); // translate me.
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::debug('Journal data', $data);
|
|
|
|
|
|
|
|
$preFilled = [
|
|
|
|
'what' => $data['what'],
|
|
|
|
'journal_description' => $data['description'],
|
|
|
|
'journal_source_account_id' => $data['source_account_id'],
|
|
|
|
'journal_source_account_name' => $data['source_account_name'],
|
|
|
|
'journal_destination_account_id' => $data['destination_account_id'],
|
|
|
|
'journal_destination_account_name' => $data['destination_account_name'],
|
|
|
|
'journal_amount' => $data['amount'],
|
|
|
|
'journal_currency_id' => $data['amount_currency_id_amount'],
|
|
|
|
'date' => $data['date'],
|
|
|
|
'interest_date' => $data['interest_date'],
|
|
|
|
'book_date' => $data['book_date'],
|
|
|
|
'process_date' => $data['process_date'],
|
|
|
|
|
|
|
|
'description' => [],
|
|
|
|
'destination_account_id' => [],
|
|
|
|
'destination_account_name' => [],
|
|
|
|
'amount' => [],
|
|
|
|
'budget_id' => [],
|
|
|
|
'category' => [],
|
|
|
|
];
|
|
|
|
|
|
|
|
// create the first transaction:
|
|
|
|
$preFilled['description'][] = $data['description'];
|
|
|
|
$preFilled['destination_account_id'][] = $data['destination_account_id'];
|
|
|
|
$preFilled['destination_account_name'][] = $data['destination_account_name'];
|
|
|
|
$preFilled['amount'][] = $data['amount'];
|
|
|
|
$preFilled['budget_id'][] = $data['budget_id'];
|
|
|
|
$preFilled['category'][] = $data['category'];
|
|
|
|
|
|
|
|
// echo '<pre>';
|
|
|
|
// var_dump($data);
|
|
|
|
// var_dump($preFilled);
|
|
|
|
// exit;
|
|
|
|
Log::debug('Prefilled', $preFilled);
|
|
|
|
|
|
|
|
return $preFilled;
|
|
|
|
}
|
|
|
|
|
2016-04-29 10:29:13 -05:00
|
|
|
}
|