From bdcd033952f1bd3610dcd36bf284b10f9745c9cf Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 30 Apr 2016 12:46:21 +0200 Subject: [PATCH] Fix edit routine --- .../Controllers/TransactionController.php | 60 +++++++++---------- .../Journal/JournalRepository.php | 6 +- resources/views/transactions/edit.twig | 39 ++++++++---- 3 files changed, 56 insertions(+), 49 deletions(-) diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index 1b251f9b10..74a35cc1b9 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -145,42 +145,37 @@ class TransactionController extends Controller /** @var PiggyBankRepositoryInterface $piggyRepository */ $piggyRepository = app('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface'); - $accountList = ExpandedForm::makeSelectList($accountRepository->getAccounts(['Default account', 'Asset account'])); - $budgetList = ExpandedForm::makeSelectList($budgetRepository->getActiveBudgets()); - $piggyBankList = ExpandedForm::makeSelectList($piggyRepository->getPiggyBanks()); - $budgetList[0] = trans('firefly.no_budget'); - $piggyBankList[0] = trans('form.noPiggybank'); - $maxFileSize = Steam::phpBytes(ini_get('upload_max_filesize')); - $maxPostSize = Steam::phpBytes(ini_get('post_max_size')); - $uploadSize = min($maxFileSize, $maxPostSize); - $what = strtolower(TransactionJournal::transactionTypeStr($journal)); - $subTitle = trans('breadcrumbs.edit_journal', ['description' => $journal->description]); + $assetAccounts = ExpandedForm::makeSelectList($accountRepository->getAccounts(['Default account', 'Asset account'])); + $budgetList = ExpandedForm::makeSelectListWithEmpty($budgetRepository->getActiveBudgets()); + $piggyBankList = ExpandedForm::makeSelectListWithEmpty($piggyRepository->getPiggyBanks()); + $maxFileSize = Steam::phpBytes(ini_get('upload_max_filesize')); + $maxPostSize = Steam::phpBytes(ini_get('post_max_size')); + $uploadSize = min($maxFileSize, $maxPostSize); + $what = strtolower(TransactionJournal::transactionTypeStr($journal)); + $subTitle = trans('breadcrumbs.edit_journal', ['description' => $journal->description]); $preFilled = [ - 'date' => TransactionJournal::dateAsString($journal), - 'interest_date' => TransactionJournal::dateAsString($journal, 'interest_date'), - 'book_date' => TransactionJournal::dateAsString($journal, 'book_date'), - 'process_date' => TransactionJournal::dateAsString($journal, 'process_date'), - 'category' => TransactionJournal::categoryAsString($journal), - 'budget_id' => TransactionJournal::budgetId($journal), - 'piggy_bank_id' => TransactionJournal::piggyBankId($journal), - 'tags' => join(',', $journal->tags->pluck('tag')->toArray()), - 'account_from_id' => TransactionJournal::sourceAccount($journal)->id, - 'account_to_id' => TransactionJournal::destinationAccount($journal)->id, - 'amount' => TransactionJournal::amountPositive($journal), + 'date' => TransactionJournal::dateAsString($journal), + 'interest_date' => TransactionJournal::dateAsString($journal, 'interest_date'), + 'book_date' => TransactionJournal::dateAsString($journal, 'book_date'), + 'process_date' => TransactionJournal::dateAsString($journal, 'process_date'), + 'category' => TransactionJournal::categoryAsString($journal), + 'budget_id' => TransactionJournal::budgetId($journal), + 'piggy_bank_id' => TransactionJournal::piggyBankId($journal), + 'tags' => join(',', $journal->tags->pluck('tag')->toArray()), + 'source_account_id' => TransactionJournal::sourceAccount($journal)->id, + 'source_account_name' => TransactionJournal::sourceAccount($journal)->name, + 'destination_account_id' => TransactionJournal::destinationAccount($journal)->id, + 'destination_account_name' => TransactionJournal::destinationAccount($journal)->name, + 'amount' => TransactionJournal::amountPositive($journal), ]; - if ($journal->isWithdrawal()) { - $preFilled['account_id'] = TransactionJournal::sourceAccount($journal)->id; - if (TransactionJournal::destinationAccountTypeStr($journal) != 'Cash account') { - $preFilled['expense_account'] = TransactionJournal::destinationAccount($journal)->name; - } - } else { - $preFilled['account_id'] = TransactionJournal::destinationAccount($journal)->id; - if (TransactionJournal::sourceAccountTypeStr($journal) != 'Cash account') { - $preFilled['revenue_account'] = TransactionJournal::sourceAccount($journal)->name; - } + if ($journal->isWithdrawal() && TransactionJournal::destinationAccountTypeStr($journal) == 'Cash account') { + $preFilled['destination_account_name'] = ''; + } + if ($journal->isDeposit() && TransactionJournal::sourceAccountTypeStr($journal) == 'Cash account') { + $preFilled['source_account_name'] = ''; } @@ -195,7 +190,7 @@ class TransactionController extends Controller Session::forget('transactions.edit.fromUpdate'); - return view('transactions.edit', compact('journal', 'uploadSize', 'accountList', 'what', 'budgetList', 'piggyBankList', 'subTitle'))->with( + return view('transactions.edit', compact('journal', 'uploadSize', 'assetAccounts', 'what', 'budgetList', 'piggyBankList', 'subTitle'))->with( 'data', $preFilled ); } @@ -484,7 +479,6 @@ class TransactionController extends Controller */ public function update(JournalFormRequest $request, JournalRepositoryInterface $repository, AttachmentHelperInterface $att, TransactionJournal $journal) { - $journalData = $request->getJournalData(); $repository->update($journal, $journalData); diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php index 7e17636398..2f38596910 100644 --- a/app/Repositories/Journal/JournalRepository.php +++ b/app/Repositories/Journal/JournalRepository.php @@ -305,7 +305,7 @@ class JournalRepository implements JournalRepositoryInterface $journal->budgets()->detach(); if (intval($data['budget_id']) > 0) { /** @var \FireflyIII\Models\Budget $budget */ - $budget = Budget::find($data['budget_id']); + $budget = Budget::where('user_id', $this->user->id)->where('id', $data['budget_id'])->first(); $journal->budgets()->save($budget); } @@ -402,8 +402,8 @@ class JournalRepository implements JournalRepositoryInterface break; case TransactionType::TRANSFER: - $sourceAccount = Account::where('user_id', $this->user->id)->where('id', $data['account_from_id'])->first(); - $destinationAccount = Account::where('user_id', $this->user->id)->where('id', $data['account_to_id'])->first(); + $sourceAccount = Account::where('user_id', $this->user->id)->where('id', $data['source_account_id'])->first(); + $destinationAccount = Account::where('user_id', $this->user->id)->where('id', $data['destination_account_id'])->first(); break; default: throw new FireflyException('Did not recognise transaction type.'); diff --git a/resources/views/transactions/edit.twig b/resources/views/transactions/edit.twig index 03b09a1a77..03e8e286d3 100644 --- a/resources/views/transactions/edit.twig +++ b/resources/views/transactions/edit.twig @@ -12,6 +12,20 @@ + {% if errors.all|length > 0 %} +
+
+

Errors

+
    + {% for err in errors.all %} +
  • {{ err }}
  • + {% endfor %} +
+
+
+ {% endif %} + +
@@ -22,25 +36,24 @@ {{ ExpandedForm.text('description',journal.description) }} - - {% if what == 'deposit' or what == 'withdrawal' %} - {{ ExpandedForm.select('account_id',accountList,data['account_id']) }} + + {% if what == 'transfer' or what == 'withdrawal' %} + {{ ExpandedForm.select('source_account_id',assetAccounts, data.source_account_id, {label: trans('form.asset_source_account')}) }} {% endif %} - - {% if what == 'withdrawal' %} - {{ ExpandedForm.text('expense_account',data['expense_account']) }} - {% endif %} - - + {% if what == 'deposit' %} - {{ ExpandedForm.text('revenue_account',data['revenue_account']) }} + {{ ExpandedForm.text('source_account_name',data.source_account_name, {label: trans('form.revenue_account')}) }} {% endif %} - + + {% if what == 'withdrawal' %} + {{ ExpandedForm.text('destination_account_name',data.destination_account_name, {label: trans('form.expense_account')}) }} + {% endif %} + + {% if what == 'transfer' %} - {{ ExpandedForm.select('account_from_id',accountList,data['account_from_id']) }} - {{ ExpandedForm.select('account_to_id',accountList,data['account_to_id']) }} + {{ ExpandedForm.select('destination_account_id',assetAccounts, data.destination_account_id, {label: trans('form.asset_destination_account')} ) }} {% endif %}