Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Possible fix for #1527
James Cole780e365a7887
diff --git a/app/Http/Controllers/Budget/AmountController.php b/app/Http/Controllers/Budget/AmountController.phpindex f4e912cd50..0696a387d6 100644--- a/app/Http/Controllers/Budget/AmountController.php+++ b/app/Http/Controllers/Budget/AmountController.php@@ -74,6 +74,8 @@ class AmountController extends Controller* @param Budget $budget** @return JsonResponse+ *+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)*/public function amount(Request $request, BudgetRepositoryInterface $repository, Budget $budget): JsonResponse{@@ -128,16 +130,13 @@ class AmountController extends Controllerpublic function infoIncome(Carbon $start, Carbon $end){$range = app('preferences')->get('viewRange', '1M')->data;-/** @var Carbon $searchBegin */$searchBegin = app('navigation')->subtractPeriod($start, $range, 3);$searchEnd = app('navigation')->addPeriod($end, $range, 3);$daysInPeriod = $start->diffInDays($end);$daysInSearchPeriod = $searchBegin->diffInDays($searchEnd);-- // get average available budget from repository:- $average = $this->repository->getAverageAvailable($start, $end);- $available = bcmul($average, (string)$daysInPeriod);+ $average = $this->repository->getAverageAvailable($start, $end);+ $available = bcmul($average, (string)$daysInPeriod);// amount earned in this period:/** @var JournalCollectorInterface $collector */@@ -159,17 +158,11 @@ class AmountController extends Controller$suggested = $spentAverage;// if the user makes less per period, suggest that amount instead.- if (bccomp($spentAverage, $earnedAverage) === 1) {+ if (1 === bccomp($spentAverage, $earnedAverage)) {$suggested = $earnedAverage;}- $result- = [- 'available' => $available,- 'earned' => $earnedAverage,- 'spent' => $spentAverage,- 'suggested' => $suggested,- ];+ $result = ['available' => $available, 'earned' => $earnedAverage, 'spent' => $spentAverage, 'suggested' => $suggested,];return view('budgets.info', compact('result', 'searchBegin', 'searchEnd', 'start', 'end'));}diff --git a/app/Http/Controllers/Budget/IndexController.php b/app/Http/Controllers/Budget/IndexController.phpindex f1db2e95a4..602d665f58 100644--- a/app/Http/Controllers/Budget/IndexController.php+++ b/app/Http/Controllers/Budget/IndexController.php@@ -70,6 +70,8 @@ class IndexController extends Controller* @param string|null $moment** @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View+ *+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)*/public function index(Request $request, string $moment = null){@@ -78,11 +80,10 @@ class IndexController extends Controller$end = session('end', new Carbon);$page = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page');$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;- $days = 0;-+ $moment = $moment ?? '';- // make date if present:- if (null !== $moment || '' !== (string)$moment) {+ // make date if the data is given.+ if ('' !== (string)$moment) {try {$start = new Carbon($moment);$end = app('navigation')->endOfPeriod($start, $range);@@ -94,15 +95,7 @@ class IndexController extends Controller// if today is between start and end, use the diff in days between end and today (days left)// otherwise, use diff between start and end.- $today = new Carbon;- if ($today->gte($start) && $today->lte($end)) {- $days = $end->diffInDays($today);- }- if ($today->lte($start) || $today->gte($end)) {- $days = $start->diffInDays($end);- }- $days = 0 === $days ? 1 : $days;-+ $dayDifference = $this->getDayDifference($start, $end);$next = clone $end;$next->addDay();@@ -122,39 +115,12 @@ class IndexController extends Controller$available = $this->repository->getAvailableBudget($defaultCurrency, $start, $end);$spent = array_sum(array_column($budgetInformation, 'spent'));$budgeted = array_sum(array_column($budgetInformation, 'budgeted'));+ $previousLoop = $this->getPreviousPeriods($start, $range);+ $nextLoop = $this->getNextPeriods($end, $range);// paginate budgets$budgets = new LengthAwarePaginator($budgets, $total, $pageSize, $page);$budgets->setPath(route('budgets.index'));-- // select thing for last 12 periods:- $previousLoop = [];- /** @var Carbon $previousDate */- $previousDate = clone $start;- $count = 0;- while ($count < 12) {- $previousDate->subDay();- $previousDate = app('navigation')->startOfPeriod($previousDate, $range);- $format = $previousDate->format('Y-m-d');- $previousLoop[$format] = app('navigation')->periodShow($previousDate, $range);- ++$count;- }-- // select thing for next 12 periods:- $nextLoop = [];- /** @var Carbon $nextDate */- $nextDate = clone $end;- $nextDate->addDay();- $count = 0;-- while ($count < 12) {- $format = $nextDate->format('Y-m-d');- $nextLoop[$format] = app('navigation')->periodShow($nextDate, $range);- $nextDate = app('navigation')->endOfPeriod($nextDate, $range);- ++$count;- $nextDate->addDay();- }-// display info$currentMonth = app('navigation')->periodShow($start, $range);$nextText = app('navigation')->periodShow($next, $range);@@ -162,7 +128,8 @@ class IndexController extends Controllerreturn view('budgets.index', compact(- 'available', 'currentMonth', 'next', 'nextText', 'prev', 'allBudgets', 'prevText', 'periodStart', 'periodEnd', 'days', 'page',+ 'available', 'currentMonth', 'next', 'nextText', 'prev', 'allBudgets', 'prevText', 'periodStart', 'periodEnd', 'dayDifference',+ 'page','budgetInformation', 'daysPassed','inactive', 'budgets', 'spent', 'budgeted', 'previousLoop', 'nextLoop', 'start', 'end')@@ -170,7 +137,4 @@ class IndexController extends Controller}---}\ No newline at end of filediff --git a/app/Http/Controllers/Budget/ShowController.php b/app/Http/Controllers/Budget/ShowController.phpindex 168c3c9ebc..bbb9939df2 100644--- a/app/Http/Controllers/Budget/ShowController.php+++ b/app/Http/Controllers/Budget/ShowController.php@@ -68,54 +68,51 @@ class ShowController extends Controller}/**- * @param Request $request- * @param JournalRepositoryInterface $repository- * @param string|null $moment+ * @param Request $request+ * @param Carbon|null $start+ * @param Carbon|null $end** @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View*/- public function noBudget(Request $request, JournalRepositoryInterface $repository, string $moment = null)+ public function noBudget(Request $request, Carbon $start = null, Carbon $end = null){- // default values:- $moment = $moment ?? '';- $range = app('preferences')->get('viewRange', '1M')->data;- $start = null;- $end = null;- $periods = new Collection;-- // prep for "all" view.- if ('all' === $moment) {- $subTitle = trans('firefly.all_journals_without_budget');- $first = $repository->firstNull();- $start = null === $first ? new Carbon : $first->date;- $end = new Carbon;- }+ /** @var Carbon $start */+ $start = $start ?? session('start');+ /** @var Carbon $end */+ $end = $end ?? session('end');+ $subTitle = trans(+ 'firefly.without_budget_between',+ ['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]+ );+ $periods = $this->getPeriodOverview();+ $page = (int)$request->get('page');+ $pageSize = (int)app('preferences')->get('listPageSize', 50)->data;- // prep for "specific date" view.- if ('all' !== $moment && \strlen($moment) > 0) {- $start = new Carbon($moment);- /** @var Carbon $end */- $end = app('navigation')->endOfPeriod($start, $range);- $subTitle = trans(- 'firefly.without_budget_between',- ['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]- );- $periods = $this->getPeriodOverview();- }+ /** @var JournalCollectorInterface $collector */+ $collector = app(JournalCollectorInterface::class);+ $collector->setAllAssetAccounts()->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->setLimit($pageSize)->setPage($page)+ ->withoutBudget()->withOpposingAccount();+ $transactions = $collector->getPaginatedJournals();+ $transactions->setPath(route('budgets.no-budget'));- // prep for current period- if ('' === $moment) {- $start = clone session('start', app('navigation')->startOfPeriod(new Carbon, $range));- $end = clone session('end', app('navigation')->endOfPeriod(new Carbon, $range));- $periods = $this->getPeriodOverview();- $subTitle = trans(- 'firefly.without_budget_between',- ['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]- );- }+ return view('budgets.no-budget', compact('transactions', 'subTitle', 'periods', 'start', 'end'));+ }+ /**+ * @param Request $request+ * @param JournalRepositoryInterface $repository+ *+ * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View+ */+ public function noBudgetAll(Request $request, JournalRepositoryInterface $repository)+ {+ $subTitle = trans('firefly.all_journals_without_budget');+ $first = $repository->firstNull();+ $start = null === $first ? new Carbon : $first->date;+ $end = new Carbon;$page = (int)$request->get('page');$pageSize = (int)app('preferences')->get('listPageSize', 50)->data;+ $moment = 'all';/** @var JournalCollectorInterface $collector */$collector = app(JournalCollectorInterface::class);@@ -127,6 +124,7 @@ class ShowController extends Controllerreturn view('budgets.no-budget', compact('transactions', 'subTitle', 'moment', 'periods', 'start', 'end'));}+/*** @param Request $request* @param Budget $budget@@ -262,7 +260,13 @@ class ShowController extends Controller/** @noinspection PhpUndefinedMethodInspection */$dateStr = $date['end']->format('Y-m-d');$dateName = app('navigation')->periodShow($date['end'], $date['period']);- $entries->push(['string' => $dateStr, 'name' => $dateName, 'count' => $journals, 'sum' => $sum, 'date' => clone $date['end']]);+ $entries->push(+ ['string' => $dateStr, 'name' => $dateName, 'count' => $journals, 'sum' => $sum, 'date' => clone $date['end'],+ 'start' => $date['start'],+ 'end' => $date['end'],++ ]+ );}$cache->store($entries);diff --git a/app/Support/Http/Controllers/DateCalculation.php b/app/Support/Http/Controllers/DateCalculation.phpindex 353c695ef2..14b7616d49 100644--- a/app/Support/Http/Controllers/DateCalculation.php+++ b/app/Support/Http/Controllers/DateCalculation.php@@ -90,4 +90,60 @@ trait DateCalculation}+ /**+ * Get a list of the periods that will occur after this date. For example,+ * March 2018, April 2018, etc.+ *+ * @param Carbon $date+ * @param string $range+ *+ * @return array+ */+ protected function getNextPeriods(Carbon $date, string $range): array+ {+ // select thing for next 12 periods:+ $loop = [];+ /** @var Carbon $current */+ $current = clone $date;+ $current->addDay();+ $count = 0;++ while ($count < 12) {+ $format = $current->format('Y-m-d');+ $loop[$format] = app('navigation')->periodShow($current, $range);+ $current = app('navigation')->endOfPeriod($current, $range);+ ++$count;+ $current->addDay();+ }++ return $loop;+ }++ /**+ * Get a list of the periods that occurred before the start date. For example,+ * March 2018, February 2018, etc.+ *+ * @param Carbon $date+ * @param string $range+ *+ * @return array+ */+ protected function getPreviousPeriods(Carbon $date, string $range): array+ {+ // select thing for last 12 periods:+ $loop = [];+ /** @var Carbon $current */+ $current = clone $date;+ $count = 0;+ while ($count < 12) {+ $current->subDay();+ $current = app('navigation')->startOfPeriod($current, $range);+ $format = $current->format('Y-m-d');+ $loop[$format] = app('navigation')->periodShow($current, $range);+ ++$count;+ }++ return $loop;+ }+}\ No newline at end of filediff --git a/resources/views/budgets/index.twig b/resources/views/budgets/index.twigindex 89f02a1877..c5d57c0cb4 100644--- a/resources/views/budgets/index.twig+++ b/resources/views/budgets/index.twig@@ -194,7 +194,7 @@data-value="{{ (repAmount + budgetInformation[budget.id]['spent']) }}">{{ (repAmount + budgetInformation[budget.id]['spent'])|formatAmount }}{% if repAmount + budgetInformation[budget.id]['spent'] > 0 %}- ({{ ((repAmount + budgetInformation[budget.id]['spent']) / days)|formatAmount }})+ ({{ ((repAmount + budgetInformation[budget.id]['spent']) / daysPassed)|formatAmount }}){% endif %}</td></tr>diff --git a/resources/views/budgets/no-budget.twig b/resources/views/budgets/no-budget.twigindex 6b1c5fb6dc..99e99236fe 100644--- a/resources/views/budgets/no-budget.twig+++ b/resources/views/budgets/no-budget.twig@@ -1,7 +1,7 @@{% extends "./layout/default" %}{% block breadcrumbs %}- {{ Breadcrumbs.render(Route.getCurrentRoute.getName, moment, start, end) }}+ {{ Breadcrumbs.render(Route.getCurrentRoute.getName, '', start, end) }}{% endblock %}{% block content %}@@ -26,7 +26,7 @@{% if periods.count > 0 %}<p><i class="fa fa-calendar"></i>- <a href="{{ route('budgets.no-budget', ['all']) }}">{{ 'show_all_no_filter'|_ }}</a>+ <a href="{{ route('budgets.no-budget-all') }}">{{ 'show_all_no_filter'|_ }}</a></p>{% else %}<p>@@ -44,7 +44,7 @@{% if period.count > 0 %}<div class="box {% if period.date == start %}box-solid box-primary{% endif %}"><div class="box-header with-border">- <h3 class="box-title"><a href="{{ route('budgets.no-budget',[period.string]) }}">{{ period.name }}</a>+ <h3 class="box-title"><a href="{{ route('budgets.no-budget',[period.start.format('Y-m-d'), period.end.format('Y-m-d')]) }}">{{ period.name }}</a></h3></div><div class="box-body no-padding">@@ -71,7 +71,7 @@{% if periods.count > 0 %}<div class="row"><div class="col-lg-offset-9 col-lg-3 col-md-offset-9 col-md-3 col-sm-12 col-xs-12">- <p class="small text-center"><a href="{{ route('budgets.no-budget',['all']) }}">{{ 'showEverything'|_ }}</a></p>+ <p class="small text-center"><a href="{{ route('budgets.no-budget-all') }}">{{ 'showEverything'|_ }}</a></p></div></div>{% endif %}diff --git a/routes/breadcrumbs.php b/routes/breadcrumbs.phpindex 5a895da18c..dce4573a4c 100644--- a/routes/breadcrumbs.php+++ b/routes/breadcrumbs.php@@ -359,20 +359,21 @@ try {function (BreadCrumbsGenerator $breadcrumbs, string $moment, Carbon $start, Carbon $end) {$breadcrumbs->parent('budgets.index');$breadcrumbs->push(trans('firefly.journals_without_budget'), route('budgets.no-budget'));+ $title = trans(+ 'firefly.between_dates_breadcrumb',+ ['start' => $start->formatLocalized((string)trans('config.month_and_day')),+ 'end' => $end->formatLocalized((string)trans('config.month_and_day')),]+ );+ $breadcrumbs->push($title, route('budgets.no-budget', ['a', 'b']));+ }+ );- // push when is all:- if ('all' === $moment) {- $breadcrumbs->push(trans('firefly.everything'), route('budgets.no-budget', ['all']));- }- // when is specific period or when empty:- if ('all' !== $moment && '(nothing)' !== $moment) {- $title = trans(- 'firefly.between_dates_breadcrumb',- ['start' => $start->formatLocalized((string)trans('config.month_and_day')),- 'end' => $end->formatLocalized((string)trans('config.month_and_day')),]- );- $breadcrumbs->push($title, route('budgets.no-budget', [$moment]));- }+ Breadcrumbs::register(+ 'budgets.no-budget-all',+ function (BreadCrumbsGenerator $breadcrumbs) {+ $breadcrumbs->parent('budgets.index');+ $breadcrumbs->push(trans('firefly.journals_without_budget'), route('budgets.no-budget'));+ $breadcrumbs->push(trans('firefly.everything'), route('budgets.no-budget-all'));});diff --git a/routes/web.php b/routes/web.phpindex d971cf5344..ad10cd6236 100755--- a/routes/web.php+++ b/routes/web.php@@ -205,7 +205,9 @@ Route::group(// showRoute::get('show/{budget}', ['uses' => 'Budget\ShowController@show', 'as' => 'show']);Route::get('show/{budget}/{budgetLimit}', ['uses' => 'Budget\ShowController@showByBudgetLimit', 'as' => 'show.limit']);- Route::get('list/no-budget/{moment?}', ['uses' => 'Budget\ShowController@noBudget', 'as' => 'no-budget']);+ Route::get('list/no-budget/all', ['uses' => 'Budget\ShowController@noBudgetAll', 'as' => 'no-budget-all']);+ Route::get('list/no-budget/{start_date?}/{end_date?}', ['uses' => 'Budget\ShowController@noBudget', 'as' => 'no-budget']);+// indexRoute::get('{moment?}', ['uses' => 'Budget\IndexController@index', 'as' => 'index']);