diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php index 62ccd43752..2016b2f224 100644 --- a/app/Http/Controllers/Chart/BudgetController.php +++ b/app/Http/Controllers/Chart/BudgetController.php @@ -183,11 +183,8 @@ class BudgetController extends Controller 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 Budget $budget * @param Carbon $start @@ -214,19 +211,8 @@ class BudgetController extends Controller $periods = Navigation::listOfPeriods($start, $end); $entries = $repository->getBudgetPeriodReport(new Collection([$budget]), $accounts, $start, $end); $budgeted = []; - - // the budget limits: - $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'; - } + $key = Navigation::preferredCarbonFormat($start, $end); + $range = Navigation::preferredRangeFormat($start, $end); // get budgeted: $repetitions = $repository->getAllBudgetLimitRepetitions($start, $end); diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index 2ee801dfc3..354c730011 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -28,6 +28,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Query\JoinClause; use Illuminate\Support\Collection; use Log; +use Navigation; use stdClass; /** @@ -233,16 +234,7 @@ class BudgetRepository implements BudgetRepositoryInterface // this is the date format we need: // define period to group on: - $carbonFormat = 'Y-m-d'; - // monthly report (for year) - if ($start->diffInMonths($end) > 1) { - $carbonFormat = 'Y-m'; - } - - // yearly report (for multi year) - if ($start->diffInMonths($end) > 12) { - $carbonFormat = 'Y'; - } + $carbonFormat = Navigation::preferredCarbonFormat($start, $end); // this is the set of transactions for this period // in these budgets. Now they must be grouped (manually) diff --git a/app/Support/Navigation.php b/app/Support/Navigation.php index ed24425fbf..f8ec640963 100644 --- a/app/Support/Navigation.php +++ b/app/Support/Navigation.php @@ -179,19 +179,17 @@ class Navigation { // define period to increment $increment = 'addDay'; - $format = 'Y-m-d'; + $format = self::preferredCarbonFormat($start, $end); $displayFormat = strval(trans('config.month_and_day')); // increment by month (for year) if ($start->diffInMonths($end) > 1) { $increment = 'addMonth'; - $format = 'Y-m'; $displayFormat = strval(trans('config.month')); } // increment by year (for multi year) if ($start->diffInMonths($end) > 12) { $increment = 'addYear'; - $format = 'Y'; $displayFormat = strval(trans('config.year')); } @@ -244,6 +242,78 @@ class Navigation 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 $repeatFreq