From 201790ff8dd5f175b1f902a9867268636cb3c2f2 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 20 Mar 2016 17:12:34 +0100 Subject: [PATCH] Cleanup transaction journal edit. #215 --- app/Helpers/Collection/BalanceLine.php | 2 +- .../Controllers/Chart/BudgetController.php | 4 +- .../Controllers/TransactionController.php | 85 +++++++++---------- .../Models/TransactionJournalSupport.php | 65 ++++++++++++++ resources/lang/en_US/firefly.php | 2 +- resources/lang/en_US/form.php | 1 - .../views/reports/default/multi-year.twig | 2 +- resources/views/reports/partials/budgets.twig | 2 +- resources/views/transactions/edit.twig | 12 +-- 9 files changed, 116 insertions(+), 59 deletions(-) diff --git a/app/Helpers/Collection/BalanceLine.php b/app/Helpers/Collection/BalanceLine.php index e6157f6c06..a57a7b403b 100644 --- a/app/Helpers/Collection/BalanceLine.php +++ b/app/Helpers/Collection/BalanceLine.php @@ -102,7 +102,7 @@ class BalanceLine return $this->getBudget()->name; } if ($this->getRole() == self::ROLE_DEFAULTROLE) { - return trans('firefly.noBudget'); + return trans('firefly.no_budget'); } if ($this->getRole() == self::ROLE_TAGROLE) { return trans('firefly.coveredWithTags'); diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php index 18a61759d2..54dd637839 100644 --- a/app/Http/Controllers/Chart/BudgetController.php +++ b/app/Http/Controllers/Chart/BudgetController.php @@ -198,7 +198,7 @@ class BudgetController extends Controller } $noBudgetExpenses = $repository->getWithoutBudgetSum($accounts, $start, $end); - $allEntries->push([trans('firefly.noBudget'), '0', '0', $noBudgetExpenses, '0', '0']); + $allEntries->push([trans('firefly.no_budget'), '0', '0', $noBudgetExpenses, '0', '0']); $data = $this->generator->frontpage($allEntries); $cache->store($data); @@ -258,7 +258,7 @@ class BudgetController extends Controller // basic information: $year = $currentStart->year; - $entry['name'] = $budget->name ?? (string)trans('firefly.noBudget'); + $entry['name'] = $budget->name ?? (string)trans('firefly.no_budget'); $spent = 0; // this might be a good moment to collect no budget stuff. if (is_null($budget->id)) { diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index a4848f07de..a0971dd904 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -7,7 +7,6 @@ use Config; use ExpandedForm; use FireflyIII\Events\TransactionJournalStored; use FireflyIII\Events\TransactionJournalUpdated; -use FireflyIII\Exceptions\FireflyException; use FireflyIII\Helpers\Attachments\AttachmentHelperInterface; use FireflyIII\Http\Requests\JournalFormRequest; use FireflyIII\Models\PiggyBank; @@ -16,7 +15,9 @@ use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionType; use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI; +use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; +use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; use Illuminate\Support\Collection; use Input; use Preferences; @@ -57,7 +58,7 @@ class TransactionController extends Controller $uploadSize = min($maxFileSize, $maxPostSize); $accounts = ExpandedForm::makeSelectList($repository->getAccounts(['Default account', 'Asset account'])); $budgets = ExpandedForm::makeSelectList(Auth::user()->budgets()->get()); - $budgets[0] = trans('form.noBudget'); + $budgets[0] = trans('firefly.no_budget'); $piggyBanks = Auth::user()->piggyBanks()->orderBy('order', 'ASC')->get(); /** @var PiggyBank $piggy */ foreach ($piggyBanks as $piggy) { @@ -131,53 +132,45 @@ class TransactionController extends Controller } /** - * Shows the view to edit a transaction. - * - * @param ARI $repository * @param TransactionJournal $journal * - * @return $this - * @throws FireflyException + * @return mixed */ - public function edit(ARI $repository, TransactionJournal $journal) + public function edit(TransactionJournal $journal) { - $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)); - $accounts = ExpandedForm::makeSelectList($repository->getAccounts(['Default account', 'Asset account'])); - $budgets = ExpandedForm::makeSelectList(Auth::user()->budgets()->get()); - $budgets[0] = trans('form.noBudget'); - $piggies = ExpandedForm::makeSelectList(Auth::user()->piggyBanks()->get()); - $piggies[0] = trans('form.noPiggybank'); - $subTitle = trans('breadcrumbs.edit_journal', ['description' => $journal->description]); - $preFilled = [ - 'date' => $journal->date->format('Y-m-d'), - 'interest_date' => $journal->interest_date ? $journal->interest_date->format('Y-m-d') : '', - 'book_date' => $journal->book_date ? $journal->book_date->format('Y-m-d') : '', - 'process_date' => $journal->process_date ? $journal->process_date->format('Y-m-d') : '', - 'category' => '', - 'budget_id' => 0, - 'piggy_bank_id' => 0, - 'tags' => join(',', $journal->tags->pluck('tag')->toArray()), + /** @var ARI $accountRepository */ + $accountRepository = app('FireflyIII\Repositories\Account\AccountRepositoryInterface'); + /** @var BudgetRepositoryInterface $budgetRepository */ + $budgetRepository = app('FireflyIII\Repositories\Budget\BudgetRepositoryInterface'); + /** @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]); + + + $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), ]; - $category = $journal->categories()->first(); - if (!is_null($category)) { - $preFilled['category'] = $category->name; - } - - $budget = $journal->budgets()->first(); - if (!is_null($budget)) { - $preFilled['budget_id'] = $budget->id; - } - - if ($journal->piggyBankEvents()->count() > 0) { - $preFilled['piggy_bank_id'] = $journal->piggyBankEvents()->orderBy('date', 'DESC')->first()->piggy_bank_id; - } - - $preFilled['amount'] = TransactionJournal::amountPositive($journal); - if ($journal->isWithdrawal()) { $preFilled['account_id'] = TransactionJournal::sourceAccount($journal)->id; if (TransactionJournal::destinationAccountTypeStr($journal) != 'Cash account') { @@ -190,8 +183,6 @@ class TransactionController extends Controller } } - $preFilled['account_from_id'] = TransactionJournal::sourceAccount($journal)->id; - $preFilled['account_to_id'] = TransactionJournal::destinationAccount($journal)->id; Session::flash('preFilled', $preFilled); Session::flash('gaEventCategory', 'transactions'); @@ -204,7 +195,9 @@ class TransactionController extends Controller Session::forget('transactions.edit.fromUpdate'); - return view('transactions.edit', compact('journal', 'uploadSize', 'accounts', 'what', 'budgets', 'piggies', 'subTitle'))->with('data', $preFilled); + return view('transactions.edit', compact('journal', 'uploadSize', 'accountList', 'what', 'budgetList', 'piggyBankList', 'subTitle'))->with( + 'data', $preFilled + ); } /** diff --git a/app/Support/Models/TransactionJournalSupport.php b/app/Support/Models/TransactionJournalSupport.php index 53688699a0..75fe75ac4f 100644 --- a/app/Support/Models/TransactionJournalSupport.php +++ b/app/Support/Models/TransactionJournalSupport.php @@ -10,6 +10,7 @@ namespace FireflyIII\Support\Models; +use Carbon\Carbon; use FireflyIII\Models\Account; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; @@ -79,6 +80,56 @@ class TransactionJournalSupport extends Model return $amount; } + /** + * @param TransactionJournal $journal + * + * @return int + */ + public static function budgetId(TransactionJournal $journal): int + { + $budget = $journal->budgets()->first(); + if (!is_null($budget)) { + return $budget->id; + } + + return 0; + } + + /** + * @param TransactionJournal $journal + * + * @return string + */ + public static function categoryAsString(TransactionJournal $journal): string + { + $category = $journal->categories()->first(); + if (!is_null($category)) { + return $category->name; + } + + return ''; + } + + /** + * @param TransactionJournal $journal + * @param string $dateField + * + * @return string + */ + public static function dateAsString(TransactionJournal $journal, string $dateField = ''): string + { + if ($dateField === '') { + return $journal->date->format('Y-m-d'); + } + if (!is_null($journal->$dateField) && $journal->$dateField instanceof Carbon) { + return $journal->$dateField->format('Y-m-d'); + } + + return ''; + + + } + /** * @param TransactionJournal $journal * @@ -147,6 +198,20 @@ class TransactionJournalSupport extends Model return false; } + /** + * @param TransactionJournal $journal + * + * @return int + */ + public static function piggyBankId(TransactionJournal $journal): int + { + if ($journal->piggyBankEvents()->count() > 0) { + return $journal->piggyBankEvents()->orderBy('date', 'DESC')->first()->piggy_bank_id; + } + + return 0; + } + /** * @param TransactionJournal $journal * diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index 483621962c..bc4064ba9e 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -650,7 +650,7 @@ return [ 'earned' => 'Earned', 'overspent' => 'Overspent', 'left' => 'Left', - 'noBudget' => '(no budget)', + 'no_budget' => '(no budget)', 'maxAmount' => 'Maximum amount', 'minAmount' => 'Minumum amount', 'billEntry' => 'Current bill entry', diff --git a/resources/lang/en_US/form.php b/resources/lang/en_US/form.php index 5361c8daac..85263d00ed 100644 --- a/resources/lang/en_US/form.php +++ b/resources/lang/en_US/form.php @@ -74,7 +74,6 @@ return [ 'add_new_deposit' => 'Add a new deposit', 'add_new_transfer' => 'Add a new transfer', 'noPiggybank' => '(no piggy bank)', - 'noBudget' => '(no budget)', 'title' => 'Title', 'notes' => 'Notes', 'filename' => 'File name', diff --git a/resources/views/reports/default/multi-year.twig b/resources/views/reports/default/multi-year.twig index 1019861620..f08c3a7cc4 100644 --- a/resources/views/reports/default/multi-year.twig +++ b/resources/views/reports/default/multi-year.twig @@ -100,7 +100,7 @@
{% for budget in budgets %}