Some code simplification.

This commit is contained in:
James Cole 2016-11-26 08:41:15 +01:00
parent d6c7ff0ccb
commit 6bc6674ab1
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
3 changed files with 78 additions and 30 deletions

View File

@ -183,11 +183,8 @@ class BudgetController extends Controller
return Response::json($data); return Response::json($data);
} }
/** /**
*
* TODO use the NEW query that will be in the repository. Because that query will be shared between the budget period report (table for all budgets)
* TODO and this chart (a single budget)
*
* @param BudgetRepositoryInterface $repository * @param BudgetRepositoryInterface $repository
* @param Budget $budget * @param Budget $budget
* @param Carbon $start * @param Carbon $start
@ -214,19 +211,8 @@ class BudgetController extends Controller
$periods = Navigation::listOfPeriods($start, $end); $periods = Navigation::listOfPeriods($start, $end);
$entries = $repository->getBudgetPeriodReport(new Collection([$budget]), $accounts, $start, $end); $entries = $repository->getBudgetPeriodReport(new Collection([$budget]), $accounts, $start, $end);
$budgeted = []; $budgeted = [];
$key = Navigation::preferredCarbonFormat($start, $end);
// the budget limits: $range = Navigation::preferredRangeFormat($start, $end);
$range = '1D';
$key = 'Y-m-d';
if ($start->diffInMonths($end) > 1) {
$range = '1M';
$key = 'Y-m';
}
if ($start->diffInMonths($end) > 12) {
$range = '1Y';
$key = 'Y';
}
// get budgeted: // get budgeted:
$repetitions = $repository->getAllBudgetLimitRepetitions($start, $end); $repetitions = $repository->getAllBudgetLimitRepetitions($start, $end);

View File

@ -28,6 +28,7 @@ use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\JoinClause; use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log; use Log;
use Navigation;
use stdClass; use stdClass;
/** /**
@ -233,16 +234,7 @@ class BudgetRepository implements BudgetRepositoryInterface
// this is the date format we need: // this is the date format we need:
// define period to group on: // define period to group on:
$carbonFormat = 'Y-m-d'; $carbonFormat = Navigation::preferredCarbonFormat($start, $end);
// monthly report (for year)
if ($start->diffInMonths($end) > 1) {
$carbonFormat = 'Y-m';
}
// yearly report (for multi year)
if ($start->diffInMonths($end) > 12) {
$carbonFormat = 'Y';
}
// this is the set of transactions for this period // this is the set of transactions for this period
// in these budgets. Now they must be grouped (manually) // in these budgets. Now they must be grouped (manually)

View File

@ -179,19 +179,17 @@ class Navigation
{ {
// define period to increment // define period to increment
$increment = 'addDay'; $increment = 'addDay';
$format = 'Y-m-d'; $format = self::preferredCarbonFormat($start, $end);
$displayFormat = strval(trans('config.month_and_day')); $displayFormat = strval(trans('config.month_and_day'));
// increment by month (for year) // increment by month (for year)
if ($start->diffInMonths($end) > 1) { if ($start->diffInMonths($end) > 1) {
$increment = 'addMonth'; $increment = 'addMonth';
$format = 'Y-m';
$displayFormat = strval(trans('config.month')); $displayFormat = strval(trans('config.month'));
} }
// increment by year (for multi year) // increment by year (for multi year)
if ($start->diffInMonths($end) > 12) { if ($start->diffInMonths($end) > 12) {
$increment = 'addYear'; $increment = 'addYear';
$format = 'Y';
$displayFormat = strval(trans('config.year')); $displayFormat = strval(trans('config.year'));
} }
@ -244,6 +242,78 @@ class Navigation
throw new FireflyException(sprintf('No date formats for frequency "%s"!', $repeatFrequency)); throw new FireflyException(sprintf('No date formats for frequency "%s"!', $repeatFrequency));
} }
/**
* If the date difference between start and end is less than a month, method returns "Y-m-d". If the difference is less than a year,
* method returns "Y-m". If the date difference is larger, method returns "Y".
*
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function preferredCarbonFormat(Carbon $start, Carbon $end): string
{
$format = 'Y-m-d';
if ($start->diffInMonths($end) > 1) {
$format = 'Y-m';
}
if ($start->diffInMonths($end) > 12) {
$format = 'Y';
}
return $format;
}
/**
* If the date difference between start and end is less than a month, method returns "1D". If the difference is less than a year,
* method returns "1M". If the date difference is larger, method returns "1Y".
*
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function preferredRangeFormat(Carbon $start, Carbon $end): string
{
$format = '1D';
if ($start->diffInMonths($end) > 1) {
$format = '1M';
}
if ($start->diffInMonths($end) > 12) {
$format = '1Y';
}
return $format;
}
/**
* If the date difference between start and end is less than a month, method returns "%Y-%m-%d". If the difference is less than a year,
* method returns "%Y-%m". If the date difference is larger, method returns "%Y".
*
* @param \Carbon\Carbon $start
* @param \Carbon\Carbon $end
*
* @return string
*/
public function preferredSqlFormat(Carbon $start, Carbon $end): string
{
$format = '%Y-%m-%d';
if ($start->diffInMonths($end) > 1) {
$format = '%Y-%m';
}
if ($start->diffInMonths($end) > 12) {
$format = '%Y';
}
return $format;
}
/** /**
* @param \Carbon\Carbon $theDate * @param \Carbon\Carbon $theDate
* @param $repeatFreq * @param $repeatFreq