diff --git a/app/Crud/Split/Journal.php b/app/Crud/Split/Journal.php index bd0bfeb380..192a829778 100644 --- a/app/Crud/Split/Journal.php +++ b/app/Crud/Split/Journal.php @@ -64,6 +64,13 @@ class Journal implements JournalInterface ); $journal->save(); + foreach ($data['transactions'] as $transaction) { + $this->storeTransaction($journal, $transaction); + } + + $journal->completed = true; + $journal->save(); + return $journal; } @@ -116,6 +123,38 @@ class Journal implements JournalInterface } + /** + * @param TransactionJournal $journal + * @param array $data + * + * @return TransactionJournal + */ + public function updateJournal(TransactionJournal $journal, array $data): TransactionJournal + { + echo '
'; + print_r($data); + + $journal->description = $data['journal_description']; + $journal->transaction_currency_id = $data['journal_currency_id']; + $journal->date = $data['date']; + $journal->interest_date = $data['interest_date']; + $journal->book_date = $data['book_date']; + $journal->process_date = $data['process_date']; + $journal->save(); + + // delete original transactions, and recreate them. + $journal->transactions()->delete(); + + foreach ($data['transactions'] as $transaction) { + $this->storeTransaction($journal, $transaction); + } + + $journal->completed = true; + $journal->save(); + + return $journal; + } + /** * @param string $type * @param array $transaction diff --git a/app/Crud/Split/JournalInterface.php b/app/Crud/Split/JournalInterface.php index 00c00a5cba..709f0abd45 100644 --- a/app/Crud/Split/JournalInterface.php +++ b/app/Crud/Split/JournalInterface.php @@ -36,4 +36,12 @@ interface JournalInterface * @return Collection */ public function storeTransaction(TransactionJournal $journal, array $transaction): Collection; + + /** + * @param TransactionJournal $journal + * @param array $data + * + * @return TransactionJournal + */ + public function updateJournal(TransactionJournal $journal, array $data): TransactionJournal; } \ No newline at end of file diff --git a/app/Http/Controllers/Transaction/SplitController.php b/app/Http/Controllers/Transaction/SplitController.php index 27ed8387d2..275318fc26 100644 --- a/app/Http/Controllers/Transaction/SplitController.php +++ b/app/Http/Controllers/Transaction/SplitController.php @@ -12,6 +12,7 @@ namespace FireflyIII\Http\Controllers\Transaction; use ExpandedForm; use FireflyIII\Crud\Split\JournalInterface; +use FireflyIII\Events\TransactionJournalUpdated; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Requests\SplitJournalFormRequest; @@ -21,8 +22,12 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use Illuminate\Http\Request; +use Input; use Log; +use Preferences; use Session; +use Steam; +use URL; use View; /** @@ -63,16 +68,24 @@ class SplitController extends Controller /** @var BudgetRepositoryInterface $budgetRepository */ $budgetRepository = app(BudgetRepositoryInterface::class); + $uploadSize = min(Steam::phpBytes(ini_get('upload_max_filesize')), Steam::phpBytes(ini_get('post_max_size'))); $currencies = ExpandedForm::makeSelectList($currencyRepository->get()); $assetAccounts = ExpandedForm::makeSelectList($accountRepository->getAccounts(['Default account', 'Asset account'])); $budgets = ExpandedForm::makeSelectListWithEmpty($budgetRepository->getActiveBudgets()); $preFilled = $this->arrayFromJournal($request, $journal); - // get the transactions: + 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'); return view( 'split.journals.edit', - compact('currencies', 'preFilled', 'amount', 'sourceAccounts', 'destinationAccounts', 'assetAccounts', 'budgets', 'what', 'journal') + compact('currencies', 'preFilled', 'amount', 'sourceAccounts', 'uploadSize', 'destinationAccounts', 'assetAccounts', 'budgets', 'journal') ); } @@ -105,7 +118,6 @@ class SplitController extends Controller $assetAccounts = ExpandedForm::makeSelectList($accountRepository->getAccounts(['Default account', 'Asset account'])); $budgets = ExpandedForm::makeSelectListWithEmpty($budgetRepository->getActiveBudgets()); - return view('split.journals.from-store', compact('currencies', 'assetAccounts', 'budgets', 'preFilled')); @@ -129,13 +141,6 @@ class SplitController extends Controller if (is_null($journal->id)) { throw new FireflyException('Could not store transaction.'); } - foreach ($data['transactions'] as $transaction) { - $transactions = $repository->storeTransaction($journal, $transaction); - } - - // TODO move to repository. - $journal->completed = true; - $journal->save(); // forget temp journal data Session::forget('temporary_split_data'); @@ -145,12 +150,38 @@ class SplitController extends Controller } /** + * @param TransactionJournal $journal * @param SplitJournalFormRequest $request * @param JournalInterface $repository + * + * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ - public function update(SplitJournalFormRequest $request, JournalInterface $repository) + public function update(TransactionJournal $journal, SplitJournalFormRequest $request, JournalInterface $repository) { - echo 'ok'; + + $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: + } /** @@ -184,28 +215,40 @@ class SplitController extends Controller 'budget_id' => [], 'category' => [], ]; - $index = 0; + $index = 0; /** @var Transaction $transaction */ foreach ($journal->transactions()->get() as $transaction) { - //if ($journal->isWithdrawal() && $transaction->account_id !== $firstSourceId) { - $array['description'][] = $transaction->description; - $array['destination_account_id'][] = $transaction->account_id; - $array['destination_account_name'][] = $transaction->account->name; - $array['amount'][] = $transaction->amount; - //} - $budget = $transaction->budgets()->first(); - $budgetId = 0; + $budget = $transaction->budgets()->first(); + $category = $transaction->categories()->first(); + $budgetId = 0; + $categoryName = ''; if (!is_null($budget)) { $budgetId = $budget->id; } - $category = $transaction->categories()->first(); - $categoryName = ''; if (!is_null($category)) { $categoryName = $category->name; } - $array['budget_id'][] = $budgetId; - $array['category'][] = $categoryName; + + $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++; + } + + } return $array; diff --git a/app/Http/Requests/SplitJournalFormRequest.php b/app/Http/Requests/SplitJournalFormRequest.php index 142796293d..d98cd06a4c 100644 --- a/app/Http/Requests/SplitJournalFormRequest.php +++ b/app/Http/Requests/SplitJournalFormRequest.php @@ -35,6 +35,7 @@ class SplitJournalFormRequest extends Request public function getSplitData(): array { $data = [ + 'id' => $this->get('id') ?? 0, 'journal_description' => $this->get('journal_description'), 'journal_currency_id' => intval($this->get('journal_currency_id')), 'journal_source_account_id' => intval($this->get('journal_source_account_id')), diff --git a/resources/lang/en_US/form.php b/resources/lang/en_US/form.php index 6e2c70cd48..2bead9d151 100644 --- a/resources/lang/en_US/form.php +++ b/resources/lang/en_US/form.php @@ -25,6 +25,7 @@ return [ 'journal_currency_id' => 'Currency', 'journal_amount' => 'Amount', 'journal_asset_source_account' => 'Asset account (source)', + 'journal_source_account_id' => 'Asset account (source)', 'account_from_id' => 'From account', 'account_to_id' => 'To account', 'asset_destination_account' => 'Asset account (destination)', diff --git a/resources/views/split/journals/edit.twig b/resources/views/split/journals/edit.twig index bde2f724d5..8f94f1a411 100644 --- a/resources/views/split/journals/edit.twig +++ b/resources/views/split/journals/edit.twig @@ -46,19 +46,17 @@ {{ ExpandedForm.select('journal_currency_id', currencies, preFilled.transaction_currency_id) }} {{ ExpandedForm.staticText('journal_amount', preFilled.journal_amount|formatAmount ) }} - - {% if what == 'withdrawal' or what == 'transfer' %} - {{ ExpandedForm.select('journal_asset_source_account_id', assetAccounts, preFilled.sourceAccounts.first.id) }} + + {% if preFilled.what == 'withdrawal' or preFilled.what == 'transfer' %} + {{ ExpandedForm.select('journal_source_account_id', assetAccounts, preFilled.journal_source_account_id) }} {% endif %} - {% if what == 'deposit' %} - {{ ExpandedForm.staticText('revenue_account', sourceAccounts.first.name) }} - + {% if preFilled.what == 'deposit' %} + {{ ExpandedForm.text('journal_source_account_name', preFilled.journal_source_account_name) }} {% endif %} - {% if what == 'transfer' %} - {{ ExpandedForm.staticText('asset_destination_account', assetAccounts[preFilled.destinationAccounts.first.id]) }} - + {% if preFilled.what == 'transfer' %} + {{ ExpandedForm.select('journal_destination_account_id', assetAccounts, preFilled.journal_destination_account_id) }} {% endif %} @@ -82,6 +80,7 @@ {{ ExpandedForm.date('process_date', journal.process_date) }} +@@ -151,7 +150,15 @@ {% if preFilled.what == 'withdrawal' %}- {{ Form.select('budget_id[]', budgets, preFilled.budget_id[index], {class: 'form-control'}) }} + {% endif %}@@ -170,11 +177,40 @@
{{ 'add_another_split'|_ }} -+ + + + + +
++ ++++++{{ 'optionalFields'|_ }}
++ + {{ ExpandedForm.file('attachments[]', {'multiple': 'multiple','helpText': trans('firefly.upload_max_file_size', {'size': uploadSize|filesize}) }) }} +++ +diff --git a/resources/views/split/journals/from-store.twig b/resources/views/split/journals/from-store.twig index 8fb955924b..3c62e07552 100644 --- a/resources/views/split/journals/from-store.twig +++ b/resources/views/split/journals/from-store.twig @@ -74,7 +74,7 @@ {% if preFilled.what == 'withdrawal' or preFilled.what == 'transfer' %} - {{ ExpandedForm.select('journal_asset_source_account_id', assetAccounts, preFilled.journal_source_account_id) }} + {{ ExpandedForm.select('journal_source_account_id', assetAccounts, preFilled.journal_source_account_id) }} {% endif %} {% if preFilled.what == 'deposit' %}+++{{ 'options'|_ }}
++ {{ ExpandedForm.optionsList('update','split-transaction') }} ++