Possible fix for #1527

This commit is contained in:
James Cole 2018-07-14 17:23:44 +02:00
parent 3d1523a060
commit 780e365a78
8 changed files with 141 additions and 121 deletions

View File

@ -74,6 +74,8 @@ class AmountController extends Controller
* @param Budget $budget * @param Budget $budget
* *
* @return JsonResponse * @return JsonResponse
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/ */
public function amount(Request $request, BudgetRepositoryInterface $repository, Budget $budget): JsonResponse public function amount(Request $request, BudgetRepositoryInterface $repository, Budget $budget): JsonResponse
{ {
@ -128,14 +130,11 @@ class AmountController extends Controller
public function infoIncome(Carbon $start, Carbon $end) public function infoIncome(Carbon $start, Carbon $end)
{ {
$range = app('preferences')->get('viewRange', '1M')->data; $range = app('preferences')->get('viewRange', '1M')->data;
/** @var Carbon $searchBegin */ /** @var Carbon $searchBegin */
$searchBegin = app('navigation')->subtractPeriod($start, $range, 3); $searchBegin = app('navigation')->subtractPeriod($start, $range, 3);
$searchEnd = app('navigation')->addPeriod($end, $range, 3); $searchEnd = app('navigation')->addPeriod($end, $range, 3);
$daysInPeriod = $start->diffInDays($end); $daysInPeriod = $start->diffInDays($end);
$daysInSearchPeriod = $searchBegin->diffInDays($searchEnd); $daysInSearchPeriod = $searchBegin->diffInDays($searchEnd);
// get average available budget from repository:
$average = $this->repository->getAverageAvailable($start, $end); $average = $this->repository->getAverageAvailable($start, $end);
$available = bcmul($average, (string)$daysInPeriod); $available = bcmul($average, (string)$daysInPeriod);
@ -159,17 +158,11 @@ class AmountController extends Controller
$suggested = $spentAverage; $suggested = $spentAverage;
// if the user makes less per period, suggest that amount instead. // if the user makes less per period, suggest that amount instead.
if (bccomp($spentAverage, $earnedAverage) === 1) { if (1 === bccomp($spentAverage, $earnedAverage)) {
$suggested = $earnedAverage; $suggested = $earnedAverage;
} }
$result $result = ['available' => $available, 'earned' => $earnedAverage, 'spent' => $spentAverage, 'suggested' => $suggested,];
= [
'available' => $available,
'earned' => $earnedAverage,
'spent' => $spentAverage,
'suggested' => $suggested,
];
return view('budgets.info', compact('result', 'searchBegin', 'searchEnd', 'start', 'end')); return view('budgets.info', compact('result', 'searchBegin', 'searchEnd', 'start', 'end'));
} }

View File

@ -70,6 +70,8 @@ class IndexController extends Controller
* @param string|null $moment * @param string|null $moment
* *
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/ */
public function index(Request $request, string $moment = null) public function index(Request $request, string $moment = null)
{ {
@ -78,11 +80,10 @@ class IndexController extends Controller
$end = session('end', new Carbon); $end = session('end', new Carbon);
$page = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page'); $page = 0 === (int)$request->get('page') ? 1 : (int)$request->get('page');
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data; $pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
$days = 0; $moment = $moment ?? '';
// make date if the data is given.
// make date if present: if ('' !== (string)$moment) {
if (null !== $moment || '' !== (string)$moment) {
try { try {
$start = new Carbon($moment); $start = new Carbon($moment);
$end = app('navigation')->endOfPeriod($start, $range); $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) // 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. // otherwise, use diff between start and end.
$today = new Carbon; $dayDifference = $this->getDayDifference($start, $end);
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;
$next = clone $end; $next = clone $end;
$next->addDay(); $next->addDay();
@ -122,39 +115,12 @@ class IndexController extends Controller
$available = $this->repository->getAvailableBudget($defaultCurrency, $start, $end); $available = $this->repository->getAvailableBudget($defaultCurrency, $start, $end);
$spent = array_sum(array_column($budgetInformation, 'spent')); $spent = array_sum(array_column($budgetInformation, 'spent'));
$budgeted = array_sum(array_column($budgetInformation, 'budgeted')); $budgeted = array_sum(array_column($budgetInformation, 'budgeted'));
$previousLoop = $this->getPreviousPeriods($start, $range);
$nextLoop = $this->getNextPeriods($end, $range);
// paginate budgets // paginate budgets
$budgets = new LengthAwarePaginator($budgets, $total, $pageSize, $page); $budgets = new LengthAwarePaginator($budgets, $total, $pageSize, $page);
$budgets->setPath(route('budgets.index')); $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 // display info
$currentMonth = app('navigation')->periodShow($start, $range); $currentMonth = app('navigation')->periodShow($start, $range);
$nextText = app('navigation')->periodShow($next, $range); $nextText = app('navigation')->periodShow($next, $range);
@ -162,7 +128,8 @@ class IndexController extends Controller
return view( return view(
'budgets.index', compact( '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', 'budgetInformation', 'daysPassed',
'inactive', 'budgets', 'spent', 'budgeted', 'previousLoop', 'nextLoop', 'start', 'end' 'inactive', 'budgets', 'spent', 'budgeted', 'previousLoop', 'nextLoop', 'start', 'end'
) )
@ -170,7 +137,4 @@ class IndexController extends Controller
} }
} }

View File

@ -69,51 +69,22 @@ class ShowController extends Controller
/** /**
* @param Request $request * @param Request $request
* @param JournalRepositoryInterface $repository * @param Carbon|null $start
* @param string|null $moment * @param Carbon|null $end
* *
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View * @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: /** @var Carbon $start */
$moment = $moment ?? ''; $start = $start ?? session('start');
$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 */ /** @var Carbon $end */
$end = app('navigation')->endOfPeriod($start, $range); $end = $end ?? session('end');
$subTitle = trans( $subTitle = trans(
'firefly.without_budget_between', 'firefly.without_budget_between',
['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)] ['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
); );
$periods = $this->getPeriodOverview(); $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)]
);
}
$page = (int)$request->get('page'); $page = (int)$request->get('page');
$pageSize = (int)app('preferences')->get('listPageSize', 50)->data; $pageSize = (int)app('preferences')->get('listPageSize', 50)->data;
@ -124,9 +95,36 @@ class ShowController extends Controller
$transactions = $collector->getPaginatedJournals(); $transactions = $collector->getPaginatedJournals();
$transactions->setPath(route('budgets.no-budget')); $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')); return view('budgets.no-budget', compact('transactions', 'subTitle', 'moment', 'periods', 'start', 'end'));
} }
/** /**
* @param Request $request * @param Request $request
* @param Budget $budget * @param Budget $budget
@ -262,7 +260,13 @@ class ShowController extends Controller
/** @noinspection PhpUndefinedMethodInspection */ /** @noinspection PhpUndefinedMethodInspection */
$dateStr = $date['end']->format('Y-m-d'); $dateStr = $date['end']->format('Y-m-d');
$dateName = app('navigation')->periodShow($date['end'], $date['period']); $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); $cache->store($entries);

View File

@ -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;
}
} }

View File

@ -194,7 +194,7 @@
data-value="{{ (repAmount + budgetInformation[budget.id]['spent']) }}"> data-value="{{ (repAmount + budgetInformation[budget.id]['spent']) }}">
{{ (repAmount + budgetInformation[budget.id]['spent'])|formatAmount }} {{ (repAmount + budgetInformation[budget.id]['spent'])|formatAmount }}
{% if repAmount + budgetInformation[budget.id]['spent'] > 0 %} {% if repAmount + budgetInformation[budget.id]['spent'] > 0 %}
({{ ((repAmount + budgetInformation[budget.id]['spent']) / days)|formatAmount }}) ({{ ((repAmount + budgetInformation[budget.id]['spent']) / daysPassed)|formatAmount }})
{% endif %} {% endif %}
</td> </td>
</tr> </tr>

View File

@ -1,7 +1,7 @@
{% extends "./layout/default" %} {% extends "./layout/default" %}
{% block breadcrumbs %} {% block breadcrumbs %}
{{ Breadcrumbs.render(Route.getCurrentRoute.getName, moment, start, end) }} {{ Breadcrumbs.render(Route.getCurrentRoute.getName, '', start, end) }}
{% endblock %} {% endblock %}
{% block content %} {% block content %}
@ -26,7 +26,7 @@
{% if periods.count > 0 %} {% if periods.count > 0 %}
<p> <p>
<i class="fa fa-calendar"></i> <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> </p>
{% else %} {% else %}
<p> <p>
@ -44,7 +44,7 @@
{% if period.count > 0 %} {% if period.count > 0 %}
<div class="box {% if period.date == start %}box-solid box-primary{% endif %}"> <div class="box {% if period.date == start %}box-solid box-primary{% endif %}">
<div class="box-header with-border"> <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> </h3>
</div> </div>
<div class="box-body no-padding"> <div class="box-body no-padding">
@ -71,7 +71,7 @@
{% if periods.count > 0 %} {% if periods.count > 0 %}
<div class="row"> <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"> <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>
</div> </div>
{% endif %} {% endif %}

View File

@ -359,20 +359,21 @@ try {
function (BreadCrumbsGenerator $breadcrumbs, string $moment, Carbon $start, Carbon $end) { function (BreadCrumbsGenerator $breadcrumbs, string $moment, Carbon $start, Carbon $end) {
$breadcrumbs->parent('budgets.index'); $breadcrumbs->parent('budgets.index');
$breadcrumbs->push(trans('firefly.journals_without_budget'), route('budgets.no-budget')); $breadcrumbs->push(trans('firefly.journals_without_budget'), route('budgets.no-budget'));
// 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( $title = trans(
'firefly.between_dates_breadcrumb', 'firefly.between_dates_breadcrumb',
['start' => $start->formatLocalized((string)trans('config.month_and_day')), ['start' => $start->formatLocalized((string)trans('config.month_and_day')),
'end' => $end->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->push($title, route('budgets.no-budget', ['a', 'b']));
} }
);
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'));
} }
); );

View File

@ -205,7 +205,9 @@ Route::group(
// show // show
Route::get('show/{budget}', ['uses' => 'Budget\ShowController@show', 'as' => 'show']); Route::get('show/{budget}', ['uses' => 'Budget\ShowController@show', 'as' => 'show']);
Route::get('show/{budget}/{budgetLimit}', ['uses' => 'Budget\ShowController@showByBudgetLimit', 'as' => 'show.limit']); 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']);
// index // index
Route::get('{moment?}', ['uses' => 'Budget\IndexController@index', 'as' => 'index']); Route::get('{moment?}', ['uses' => 'Budget\IndexController@index', 'as' => 'index']);