diff --git a/app/Http/Controllers/Budget/AmountController.php b/app/Http/Controllers/Budget/AmountController.php index 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 Controller public 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.php index 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 Controller return 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 file diff --git a/app/Http/Controllers/Budget/ShowController.php b/app/Http/Controllers/Budget/ShowController.php index 168c3c9ebc..bbb9939df2 100644 --- a/app/Http/Controllers/Budget/ShowController.php +++ b/app/Http/Controllers/Budget/ShowController.php @@ -68,52 +68,23 @@ 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; - } - - // 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(); - } - - // 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)] - ); - } - + /** @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; @@ -124,9 +95,36 @@ class ShowController extends Controller $transactions = $collector->getPaginatedJournals(); $transactions->setPath(route('budgets.no-budget')); + 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); + $collector->setAllAssetAccounts()->setRange($start, $end)->setTypes([TransactionType::WITHDRAWAL])->setLimit($pageSize)->setPage($page) + ->withoutBudget()->withOpposingAccount(); + $transactions = $collector->getPaginatedJournals(); + $transactions->setPath(route('budgets.no-budget')); + return 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.php index 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 file diff --git a/resources/views/budgets/index.twig b/resources/views/budgets/index.twig index 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 %} diff --git a/resources/views/budgets/no-budget.twig b/resources/views/budgets/no-budget.twig index 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 %}
- {{ 'show_all_no_filter'|_ }} + {{ 'show_all_no_filter'|_ }}
{% else %}@@ -44,7 +44,7 @@ {% if period.count > 0 %}