From 13f6bd759b8db40cbc28effdf4aed03f3b7e83f2 Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 30 Dec 2016 07:39:42 +0100 Subject: [PATCH] Views must pick up on this #508 --- app/Http/Controllers/BudgetController.php | 42 ++++++++++++----------- app/Http/breadcrumbs.php | 17 ++++++--- app/Models/BudgetLimit.php | 28 ++++++++++++--- config/firefly.php | 1 + resources/views/budgets/show.twig | 15 ++++---- 5 files changed, 68 insertions(+), 35 deletions(-) diff --git a/app/Http/Controllers/BudgetController.php b/app/Http/Controllers/BudgetController.php index aedb331883..82030bb7bf 100644 --- a/app/Http/Controllers/BudgetController.php +++ b/app/Http/Controllers/BudgetController.php @@ -22,7 +22,6 @@ use FireflyIII\Http\Requests\BudgetIncomeRequest; use FireflyIII\Models\AccountType; use FireflyIII\Models\Budget; use FireflyIII\Models\BudgetLimit; -use FireflyIII\Models\LimitRepetition; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use Illuminate\Http\Request; @@ -77,7 +76,7 @@ class BudgetController extends Controller /** @var Carbon $start */ $start = session('start', Carbon::now()->startOfMonth()); /** @var Carbon $end */ - $end = session('end', Carbon::now()->endOfMonth()); + $end = session('end', Carbon::now()->endOfMonth()); $budgetLimit = $repository->updateLimitAmount($budget, $start, $end, $amount); if ($amount == 0) { $budgetLimit = null; @@ -263,13 +262,13 @@ class BudgetController extends Controller $journals->setPath('/budgets/show/' . $budget->id); - $set = $budget->limitrepetitions()->orderBy('startdate', 'DESC')->get(); + $set = $budget->budgetlimits()->orderBy('start_date', 'DESC')->get(); $subTitle = e($budget->name); $limits = new Collection(); - /** @var LimitRepetition $entry */ + /** @var BudgetLimit $entry */ foreach ($set as $entry) { - $entry->spent = $repository->spentInPeriod(new Collection([$budget]), $accounts, $entry->startdate, $entry->enddate); + $entry->spent = $repository->spentInPeriod(new Collection([$budget]), $accounts, $entry->start_date, $entry->end_date); $limits->push($entry); } @@ -277,16 +276,17 @@ class BudgetController extends Controller } /** - * @param Request $request - * @param Budget $budget - * @param LimitRepetition $repetition + * @param Request $request + * @param Budget $budget + * @param BudgetLimit $budgetLimit * * @return View * @throws FireflyException */ - public function showByRepetition(Request $request, Budget $budget, LimitRepetition $repetition) + public function showByBudgetLimit(Request $request, Budget $budget, BudgetLimit $budgetLimit) { - if ($repetition->budgetLimit->budget->id != $budget->id) { + if ($budgetLimit->budget->id + != $budget->id) { throw new FireflyException('This budget limit is not part of this budget.'); } @@ -294,28 +294,30 @@ class BudgetController extends Controller $repository = app(BudgetRepositoryInterface::class); /** @var AccountRepositoryInterface $accountRepository */ $accountRepository = app(AccountRepositoryInterface::class); - $start = $repetition->startdate; - $end = $repetition->enddate; $page = intval($request->get('page')) == 0 ? 1 : intval($request->get('page')); $pageSize = intval(Preferences::get('transactionPageSize', 50)->data); $subTitle = trans( - 'firefly.budget_in_month', ['name' => $budget->name, 'month' => $repetition->startdate->formatLocalized($this->monthFormat)] + 'firefly.budget_in_period', [ + 'name' => $budget->name, + 'start' => $budgetLimit->start_date->formatLocalized($this->monthAndDayFormat), + 'end' => $budgetLimit->end_date->formatLocalized($this->monthAndDayFormat), + ] ); $accounts = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET, AccountType::CASH]); - // collector: /** @var JournalCollectorInterface $collector */ $collector = app(JournalCollectorInterface::class, [auth()->user()]); - $collector->setAllAssetAccounts()->setRange($start, $end)->setBudget($budget)->setLimit($pageSize)->setPage($page)->withCategoryInformation(); + $collector->setAllAssetAccounts()->setRange($budgetLimit->start_date, $budgetLimit->end_date) + ->setBudget($budget)->setLimit($pageSize)->setPage($page)->withCategoryInformation(); $journals = $collector->getPaginatedJournals(); - $journals->setPath('/budgets/show/' . $budget->id . '/' . $repetition->id); + $journals->setPath('/budgets/show/' . $budget->id . '/' . $budgetLimit->id); - $repetition->spent = $repository->spentInPeriod(new Collection([$budget]), $accounts, $repetition->startdate, $repetition->enddate); - $limits = new Collection([$repetition]); + $budgetLimit->spent = $repository->spentInPeriod(new Collection([$budget]), $accounts, $budgetLimit->start_date, $budgetLimit->end_date); + $limits = new Collection([$budgetLimit]); - return view('budgets.show', compact('limits', 'budget', 'repetition', 'journals', 'subTitle')); + return view('budgets.show', compact('limits', 'budget', 'budgetLimit', 'journals', 'subTitle')); } @@ -416,7 +418,7 @@ class BudgetController extends Controller if ($limit->start_date->isSameDay($start) && $limit->end_date->isSameDay($end) ) { $return[$budgetId]['currentLimit'] = $limit; - $return[$budgetId]['budgeted'] = $limit->amount; + $return[$budgetId]['budgeted'] = $limit->amount; continue; } // otherwise it's just one of the many relevant repetitions: diff --git a/app/Http/breadcrumbs.php b/app/Http/breadcrumbs.php index f83a4b0153..e77a25f372 100644 --- a/app/Http/breadcrumbs.php +++ b/app/Http/breadcrumbs.php @@ -17,9 +17,9 @@ use FireflyIII\Models\Account; use FireflyIII\Models\Attachment; use FireflyIII\Models\Bill; use FireflyIII\Models\Budget; +use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\Category; use FireflyIII\Models\ImportJob; -use FireflyIII\Models\LimitRepetition; use FireflyIII\Models\PiggyBank; use FireflyIII\Models\Rule; use FireflyIII\Models\RuleGroup; @@ -274,11 +274,20 @@ Breadcrumbs::register( ); Breadcrumbs::register( - 'budgets.show.repetition', function (BreadCrumbGenerator $breadcrumbs, Budget $budget, LimitRepetition $repetition) { + 'budgets.show.limit', function (BreadCrumbGenerator $breadcrumbs, Budget $budget, BudgetLimit $budgetLimit) { $breadcrumbs->parent('budgets.index'); - $breadcrumbs->push(e($budget->name), route('budgets.show.repetition', [$budget->id, $repetition->id])); + $breadcrumbs->push(e($budget->name), route('budgets.show', [$budget->id])); + + $title = trans( + 'firefly.budget_in_period_breadcrumb', [ + 'name' => $budget->name, + 'start' => $budgetLimit->start_date->formatLocalized(strval(trans('config.month_and_day'))), + 'end' => $budgetLimit->end_date->formatLocalized(strval(trans('config.month_and_day'))), + ] + ); + $breadcrumbs->push( - Navigation::periodShow($repetition->startdate, $repetition->budgetLimit->repeat_freq), route('budgets.show', [$budget->id, $repetition->id]) + $title, route('budgets.show.limit', [$budget->id, $budgetLimit->id]) ); } ); diff --git a/app/Models/BudgetLimit.php b/app/Models/BudgetLimit.php index b606529277..c1153028d8 100644 --- a/app/Models/BudgetLimit.php +++ b/app/Models/BudgetLimit.php @@ -14,6 +14,7 @@ declare(strict_types = 1); namespace FireflyIII\Models; use Illuminate\Database\Eloquent\Model; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** * Class BudgetLimit @@ -29,15 +30,34 @@ class BudgetLimit extends Model * @var array */ protected $casts - = [ + = [ 'created_at' => 'date', 'updated_at' => 'date', - 'start_date' => 'date', - 'end_date' => 'date', + 'start_date' => 'date', + 'end_date' => 'date', 'repeats' => 'boolean', ]; /** @var array */ - protected $dates = ['created_at', 'updated_at','start_date','end_date']; + protected $dates = ['created_at', 'updated_at', 'start_date', 'end_date']; + + /** + * @param $value + * + * @return mixed + */ + public static function routeBinder($value) + { + if (auth()->check()) { + $object = self::where('budget_limits.id', $value) + ->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id') + ->where('budgets.user_id', auth()->user()->id) + ->first(['budget_limits.*']); + if ($object) { + return $object; + } + } + throw new NotFoundHttpException; + } /** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo diff --git a/config/firefly.php b/config/firefly.php index 402ea01558..1cba0ac243 100644 --- a/config/firefly.php +++ b/config/firefly.php @@ -149,6 +149,7 @@ return [ 'transaction_type' => 'FireflyIII\Models\TransactionType', 'currency' => 'FireflyIII\Models\TransactionCurrency', 'limitrepetition' => 'FireflyIII\Models\LimitRepetition', + 'budgetlimit' => 'FireflyIII\Models\BudgetLimit', 'piggyBank' => 'FireflyIII\Models\PiggyBank', 'tj' => 'FireflyIII\Models\TransactionJournal', 'unfinishedJournal' => 'FireflyIII\Support\Binder\UnfinishedJournal', diff --git a/resources/views/budgets/show.twig b/resources/views/budgets/show.twig index 85f8c12f7c..fc7e71c7b5 100644 --- a/resources/views/budgets/show.twig +++ b/resources/views/budgets/show.twig @@ -1,7 +1,7 @@ {% extends "./layout/default" %} {% block breadcrumbs %} - {{ Breadcrumbs.renderIfExists(Route.getCurrentRoute.getName, budget, repetition) }} + {{ Breadcrumbs.renderIfExists(Route.getCurrentRoute.getName, budget, budgetLimit) }} {% endblock %} {% block content %} @@ -45,7 +45,10 @@

{{ limit.startdate.formatLocalized(monthFormat) }} + href="{{ route('budgets.show.limit',[budget.id,limit.id]) }}"> + {{ limit.start_date.formatLocalized(monthAndDayFormat) }} — + {{ limit.end_date.formatLocalized(monthAndDayFormat) }} +

@@ -93,12 +96,10 @@ {% block scripts %}