From de9ef200146de6dbc566c80d881ab2996168f515 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 25 Feb 2017 13:13:51 +0100 Subject: [PATCH] First code for #595. Charts are still broken. --- app/Http/Controllers/AccountController.php | 240 +++++++++++++-------- app/Http/breadcrumbs.php | 32 ++- resources/lang/en_US/firefly.php | 1 + resources/views/accounts/show.twig | 45 ++-- routes/web.php | 3 +- 5 files changed, 197 insertions(+), 124 deletions(-) diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 2c12693bcd..0327b6bfb8 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -227,103 +227,171 @@ class AccountController extends Controller return view('accounts.index', compact('what', 'subTitleIcon', 'subTitle', 'accounts')); } - /** - * @param Request $request - * @param JournalCollectorInterface $collector - * @param Account $account - * - * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View - */ - public function show(Request $request, JournalCollectorInterface $collector, Account $account) - { - if ($account->accountType->type === AccountType::INITIAL_BALANCE) { - return $this->redirectToOriginalAccount($account); - } - // show journals from current period only: - $subTitleIcon = config('firefly.subIconsByIdentifier.' . $account->accountType->type); - $subTitle = $account->name; - $range = Preferences::get('viewRange', '1M')->data; - $start = session('start', Navigation::startOfPeriod(new Carbon, $range)); - $end = session('end', Navigation::endOfPeriod(new Carbon, $range)); - $page = intval($request->get('page')) === 0 ? 1 : intval($request->get('page')); - $pageSize = intval(Preferences::get('transactionPageSize', 50)->data); - $chartUri = route('chart.account.single', [$account->id]); - $accountType = $account->accountType->type; - - // grab those journals: - $collector->setAccounts(new Collection([$account]))->setRange($start, $end)->setLimit($pageSize)->setPage($page); - $journals = $collector->getPaginatedJournals(); - $journals->setPath('accounts/show/' . $account->id); - - // generate entries for each period (and cache those) - $entries = $this->periodEntries($account); - - return view('accounts.show', compact('account', 'accountType', 'entries', 'subTitleIcon', 'journals', 'subTitle', 'start', 'end', 'chartUri')); - } - - /** - * @param Request $request - * @param AccountRepositoryInterface $repository - * @param Account $account - * - * @return View - */ - public function showAll(Request $request, AccountRepositoryInterface $repository, Account $account) - { - $subTitle = sprintf('%s (%s)', $account->name, strtolower(trans('firefly.everything'))); - $page = intval($request->get('page')) === 0 ? 1 : intval($request->get('page')); - $pageSize = intval(Preferences::get('transactionPageSize', 50)->data); - $chartUri = route('chart.account.all', [$account->id]); - - // replace with journal collector: - /** @var JournalCollectorInterface $collector */ - $collector = app(JournalCollectorInterface::class); - $collector->setUser(auth()->user()); - $collector->setAccounts(new Collection([$account]))->setLimit($pageSize)->setPage($page); - $journals = $collector->getPaginatedJournals(); - $journals->setPath('accounts/show/' . $account->id . '/all'); - - // get oldest and newest journal for account: - $start = $repository->oldestJournalDate($account); - $end = $repository->newestJournalDate($account); - - // same call, except "entries". - return view('accounts.show', compact('account', 'subTitleIcon', 'journals', 'subTitle', 'start', 'end', 'chartUri')); - } /** * @param Request $request * @param Account $account - * @param string $date - * - * @return View + * @param string $moment */ - public function showByDate(Request $request, Account $account, string $date) + public function show(Request $request, Account $account, string $moment = '') { - $carbon = new Carbon($date); - $range = Preferences::get('viewRange', '1M')->data; - $start = Navigation::startOfPeriod($carbon, $range); - $end = Navigation::endOfPeriod($carbon, $range); - $subTitle = $account->name . ' (' . Navigation::periodShow($start, $range) . ')'; - $page = intval($request->get('page')) === 0 ? 1 : intval($request->get('page')); - $pageSize = intval(Preferences::get('transactionPageSize', 50)->data); - $chartUri = route('chart.account.period', [$account->id, $carbon->format('Y-m-d')]); + if ($account->accountType->type === AccountType::INITIAL_BALANCE) { + return $this->redirectToOriginalAccount($account); + } + $subTitle = $account->name; + $range = Preferences::get('viewRange', '1M')->data; + $subTitleIcon = config('firefly.subIconsByIdentifier.' . $account->accountType->type); + $page = intval($request->get('page')) === 0 ? 1 : intval($request->get('page')); + $pageSize = intval(Preferences::get('transactionPageSize', 50)->data); + $chartUri = route('chart.account.single', [$account->id]); + $start = null; + $end = null; + $periods = new Collection; + + // prep for "all" view. + if ($moment === 'all') { + $subTitle = $account->name . ' (' . strtolower(strval(trans('firefly.everything'))) . ')'; + $chartUri = route('chart.account.all', [$account->id]); + } + + // prep for "specific date" view. + if (strlen($moment) > 0 && $moment !== 'all') { + $start = new Carbon($moment); + $end = Navigation::endOfPeriod($start, $range); + $subTitle = $account->name . ' (' . strval(trans('firefly.from_to_breadcrumb', ['start' => 'x', 'end' => 'x'])) . ')'; + $chartUri = route('chart.account.period', [$account->id, $start->format('Y-m-d')]); + $periods = $this->periodEntries($account); + } + + // prep for current period + if (strlen($moment) === 0) { + $start = session('start', Navigation::startOfPeriod(new Carbon, $range)); + $end = session('end', Navigation::endOfPeriod(new Carbon, $range)); + $periods = $this->periodEntries($account); + } + $accountType = $account->accountType->type; + $count = 0; + // grab journals, but be prepared to jump a period back to get the right ones: + while ($count === 0) { + $collector = app(JournalCollectorInterface::class); + Log::debug('Count is zero, search for journals.'); + $collector->setAccounts(new Collection([$account]))->setLimit($pageSize)->setPage($page); + if (!is_null($start)) { + $collector->setRange($start, $end); + } + $journals = $collector->getPaginatedJournals(); + $journals->setPath('accounts/show/' . $account->id); + $count = $journals->getCollection()->count(); + if ($count === 0) { + $start->subDay(); + $start = Navigation::startOfPeriod($start, $range); + $end = Navigation::endOfPeriod($start, $range); + Log::debug(sprintf('Count is still zero, go back in time to "%s" and "%s"!', $start->format('Y-m-d'), $end->format('Y-m-d'))); + } + } - // replace with journal collector: - /** @var JournalCollectorInterface $collector */ - $collector = app(JournalCollectorInterface::class); - $collector->setAccounts(new Collection([$account]))->setRange($start, $end)->setLimit($pageSize)->setPage($page); - $journals = $collector->getPaginatedJournals(); - $journals->setPath('accounts/show/' . $account->id . '/' . $date); - // generate entries for each period (and cache those) - $entries = $this->periodEntries($account); - - // same call, except "entries". - return view('accounts.show', compact('account', 'accountType', 'entries', 'subTitleIcon', 'journals', 'subTitle', 'start', 'end', 'chartUri')); + return view('accounts.show', compact('account', 'accountType', 'periods', 'subTitleIcon', 'journals', 'subTitle', 'start', 'end', 'chartUri')); } + // /** + // * @param Request $request + // * @param JournalCollectorInterface $collector + // * @param Account $account + // * + // * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View + // */ + // public function show(Request $request, JournalCollectorInterface $collector, Account $account) + // { + // if ($account->accountType->type === AccountType::INITIAL_BALANCE) { + // return $this->redirectToOriginalAccount($account); + // } + // // show journals from current period only: + // $subTitleIcon = config('firefly.subIconsByIdentifier.' . $account->accountType->type); + // $subTitle = $account->name; + // $range = Preferences::get('viewRange', '1M')->data; + // $start = session('start', Navigation::startOfPeriod(new Carbon, $range)); + // $end = session('end', Navigation::endOfPeriod(new Carbon, $range)); + // $page = intval($request->get('page')) === 0 ? 1 : intval($request->get('page')); + // $pageSize = intval(Preferences::get('transactionPageSize', 50)->data); + // $chartUri = route('chart.account.single', [$account->id]); + // $accountType = $account->accountType->type; + // + // // grab those journals: + // $collector->setAccounts(new Collection([$account]))->setRange($start, $end)->setLimit($pageSize)->setPage($page); + // $journals = $collector->getPaginatedJournals(); + // $journals->setPath('accounts/show/' . $account->id); + // + // // generate entries for each period (and cache those) + // $entries = $this->periodEntries($account); + // + // return view('accounts.show', compact('account', 'accountType', 'entries', 'subTitleIcon', 'journals', 'subTitle', 'start', 'end', 'chartUri')); + // } + // + // /** + // * @param Request $request + // * @param AccountRepositoryInterface $repository + // * @param Account $account + // * + // * @return View + // */ + // public function showAll(Request $request, AccountRepositoryInterface $repository, Account $account) + // { + // $subTitle = sprintf('%s (%s)', $account->name, strtolower(trans('firefly.everything'))); + // $page = intval($request->get('page')) === 0 ? 1 : intval($request->get('page')); + // $pageSize = intval(Preferences::get('transactionPageSize', 50)->data); + // $chartUri = route('chart.account.all', [$account->id]); + // + // // replace with journal collector: + // /** @var JournalCollectorInterface $collector */ + // $collector = app(JournalCollectorInterface::class); + // $collector->setUser(auth()->user()); + // $collector->setAccounts(new Collection([$account]))->setLimit($pageSize)->setPage($page); + // $journals = $collector->getPaginatedJournals(); + // $journals->setPath('accounts/show/' . $account->id . '/all'); + // + // // get oldest and newest journal for account: + // $start = $repository->oldestJournalDate($account); + // $end = $repository->newestJournalDate($account); + // + // // same call, except "entries". + // return view('accounts.show', compact('account', 'subTitleIcon', 'journals', 'subTitle', 'start', 'end', 'chartUri')); + // } + // + // /** + // * @param Request $request + // * @param Account $account + // * @param string $date + // * + // * @return View + // */ + // public function showByDate(Request $request, Account $account, string $date) + // { + // $carbon = new Carbon($date); + // $range = Preferences::get('viewRange', '1M')->data; + // $start = Navigation::startOfPeriod($carbon, $range); + // $end = Navigation::endOfPeriod($carbon, $range); + // $subTitle = $account->name . ' (' . Navigation::periodShow($start, $range) . ')'; + // $page = intval($request->get('page')) === 0 ? 1 : intval($request->get('page')); + // $pageSize = intval(Preferences::get('transactionPageSize', 50)->data); + // $chartUri = route('chart.account.period', [$account->id, $carbon->format('Y-m-d')]); + // $accountType = $account->accountType->type; + // + // // replace with journal collector: + // /** @var JournalCollectorInterface $collector */ + // $collector = app(JournalCollectorInterface::class); + // $collector->setAccounts(new Collection([$account]))->setRange($start, $end)->setLimit($pageSize)->setPage($page); + // $journals = $collector->getPaginatedJournals(); + // $journals->setPath('accounts/show/' . $account->id . '/' . $date); + // + // // generate entries for each period (and cache those) + // $entries = $this->periodEntries($account); + // + // // same call, except "entries". + // return view('accounts.show', compact('account', 'accountType', 'entries', 'subTitleIcon', 'journals', 'subTitle', 'start', 'end', 'chartUri')); + // } + /** * @param AccountFormRequest $request * @param AccountRepositoryInterface $repository diff --git a/app/Http/breadcrumbs.php b/app/Http/breadcrumbs.php index 62d7311e0a..356188163f 100644 --- a/app/Http/breadcrumbs.php +++ b/app/Http/breadcrumbs.php @@ -76,30 +76,26 @@ Breadcrumbs::register( ); Breadcrumbs::register( - 'accounts.show.date', function (BreadCrumbGenerator $breadcrumbs, Account $account, Carbon $start, Carbon $end) { + 'accounts.show.date', function (BreadCrumbGenerator $breadcrumbs, Account $account, Carbon $start = null, Carbon $end = null) { - $startString = $start->formatLocalized(strval(trans('config.month_and_day'))); - $endString = $end->formatLocalized(strval(trans('config.month_and_day'))); - $title = sprintf('%s (%s)', $account->name, trans('firefly.from_to', ['start' => $startString, 'end' => $endString])); + $title = ''; + $route = ''; + if (!is_null($start) && !is_null($end)) { + $startString = $start->formatLocalized(strval(trans('config.month_and_day'))); + $endString = $end->formatLocalized(strval(trans('config.month_and_day'))); + $title = sprintf('%s (%s)', $account->name, trans('firefly.from_to_breadcrumb', ['start' => $startString, 'end' => $endString])); + $route = route('accounts.show.date', [$account->id, $start->format('Y-m-d')]); + } + if (is_null($start) && is_null($end)) { + $title = $title = $account->name . ' (' . strtolower(strval(trans('firefly.everything'))) . ')'; + $route = route('accounts.show.date', [$account->id, 'all']); + } $breadcrumbs->parent('accounts.show', $account); - $breadcrumbs->push($title, route('accounts.show.date', [$account->id, $start->format('Y-m-d')])); + $breadcrumbs->push($title, $route); } ); -Breadcrumbs::register( - 'accounts.show.all', function (BreadCrumbGenerator $breadcrumbs, Account $account, Carbon $start, Carbon $end) { - - $startString = $start->formatLocalized(strval(trans('config.month_and_day'))); - $endString = $end->formatLocalized(strval(trans('config.month_and_day'))); - $title = sprintf('%s (%s)', $account->name, trans('firefly.from_to', ['start' => $startString, 'end' => $endString])); - - $breadcrumbs->parent('accounts.show', $account); - $breadcrumbs->push($title, route('accounts.show.all', [$account->id, $start->format('Y-m-d')])); -} -); - - Breadcrumbs::register( 'accounts.delete', function (BreadCrumbGenerator $breadcrumbs, Account $account) { $breadcrumbs->parent('accounts.show', $account); diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index f5240948f6..5406586eb2 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -558,6 +558,7 @@ return [ 'select_more_than_one_budget' => 'Please select more than one budget', 'select_more_than_one_tag' => 'Please select more than one tag', 'from_to' => 'From :start to :end', + 'from_to_breadcrumb' => 'from :start to :end', // categories: 'new_category' => 'New category', diff --git a/resources/views/accounts/show.twig b/resources/views/accounts/show.twig index 4d9d3444a8..395e71767b 100644 --- a/resources/views/accounts/show.twig +++ b/resources/views/accounts/show.twig @@ -10,8 +10,12 @@

{{ account.name }} - ({{ trans('firefly.from_to', {start: start.formatLocalized(monthAndDayFormat), end: end.formatLocalized(monthAndDayFormat)}) }})

- + {% if start and end %} + ({{ trans('firefly.from_to_breadcrumb', {start: start.formatLocalized(monthAndDayFormat), end: end.formatLocalized(monthAndDayFormat)}) }}) + {% else %} + ({{ trans('firefly.everything')|lower }}) + {% endif %} +
@@ -67,26 +71,26 @@
-{% if entries %} -
-
-

{{ 'showEverything'|_ }}

+ {% if periods.count > 0 %} + -
-{% endif %} + {% endif %}
-
+

{{ 'transactions'|_ }}

{% include 'list.journals-tasker' with {sorting:true, hideBills:true, hideBudgets: true, hideCategories: true} %} - {% if entries %} + {% if periods.count > 0 %}

- + {{ 'show_all_no_filter'|_ }}

@@ -101,10 +105,9 @@
- {% if entries %} + {% if periods.count > 0 %}
- - {% for entry in entries %} + {% for entry in periods %} {% if (entry[2] != 0 or entry[3] != 0) or (accountType == 'Asset account') %}
@@ -130,7 +133,7 @@
{% endif %} {% endfor %} -

{{ 'showEverything'|_ }}

+

{{ 'showEverything'|_ }}

{% endif %}
@@ -144,9 +147,15 @@ var accountID = {{ account.id }}; // uri's for charts: var chartUri = '{{ chartUri }}'; - var incomeCategoryUri = '{{ route('chart.account.income-category', [account.id, start.format('Ymd'), end.format('Ymd')]) }}'; - var expenseCategoryUri = '{{ route('chart.account.expense-category', [account.id, start.format('Ymd'), end.format('Ymd')]) }}'; - var expenseBudgetUri = '{{ route('chart.account.expense-budget', [account.id, start.format('Ymd'), end.format('Ymd')]) }}'; + {% if start and end %} + var incomeCategoryUri = '{{ route('chart.account.income-category', [account.id, start.format('Ymd'), end.format('Ymd')]) }}'; + var expenseCategoryUri = '{{ route('chart.account.expense-category', [account.id, start.format('Ymd'), end.format('Ymd')]) }}'; + var expenseBudgetUri = '{{ route('chart.account.expense-budget', [account.id, start.format('Ymd'), end.format('Ymd')]) }}'; + {% else %} + var incomeCategoryUri = '{{ route('chart.account.income-category', [account.id, 'all', 'all']) }}'; + var expenseCategoryUri = '{{ route('chart.account.expense-category', [account.id, 'all', 'all']) }}'; + var expenseBudgetUri = '{{ route('chart.account.expense-budget', [account.id, 'all', 'all']) }}'; + {% endif %} diff --git a/routes/web.php b/routes/web.php index 5a70819fd4..cf64bc32e9 100755 --- a/routes/web.php +++ b/routes/web.php @@ -88,8 +88,7 @@ Route::group( Route::get('delete/{account}', ['uses' => 'AccountController@delete', 'as' => 'delete']); Route::get('show/{account}', ['uses' => 'AccountController@show', 'as' => 'show']); - Route::get('show/{account}/all', ['uses' => 'AccountController@showAll', 'as' => 'show.all']); - Route::get('show/{account}/{date}', ['uses' => 'AccountController@showByDate', 'as' => 'show.date']); + Route::get('show/{account}/{date}', ['uses' => 'AccountController@show', 'as' => 'show.date']); Route::post('store', ['uses' => 'AccountController@store', 'as' => 'store']); Route::post('update/{account}', ['uses' => 'AccountController@update', 'as' => 'update']);