From 22e6ea700fe2152987d3f438e01ba1b83fa16b2c Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 25 Apr 2016 21:37:08 +0200 Subject: [PATCH] Some extensions to budgets. --- app/Http/Controllers/BudgetController.php | 12 +- app/Http/Controllers/HomeController.php | 14 +- app/Repositories/Budget/BudgetRepository.php | 108 ++++++------ resources/lang/en_US/firefly.php | 7 + resources/views/budgets/index.twig | 165 ++++++++++--------- resources/views/preferences/index.twig | 7 + 6 files changed, 183 insertions(+), 130 deletions(-) diff --git a/app/Http/Controllers/BudgetController.php b/app/Http/Controllers/BudgetController.php index c284247fa5..4357c9fe89 100644 --- a/app/Http/Controllers/BudgetController.php +++ b/app/Http/Controllers/BudgetController.php @@ -160,6 +160,16 @@ class BudgetController extends Controller $periodEnd = $end->formatLocalized($this->monthAndDayFormat); $accounts = $accountRepository->getAccounts(['Default account', 'Asset account', 'Cash account']); + /** + * Warn user if necessary + */ + $range = Preferences::get('viewRange', '1M')->data; + $repeatFreq = Config::get('firefly.range_to_repeat_freq.' . $range); + $userWarning = ''; + if (session('is_custom_range', false) === true) { + $userWarning = strval(trans('firefly.warn_range_' . $repeatFreq)); + } + /** * Do some cleanup: */ @@ -187,7 +197,7 @@ class BudgetController extends Controller 'budgetMaximum', 'periodStart', 'periodEnd', 'period', 'range', 'budgetIncomeTotal', 'defaultCurrency', 'inactive', 'budgets', - 'spent', 'budgeted' + 'spent', 'budgeted', 'userWarning' ) ); } diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 89ec33df7b..903d44a487 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -9,6 +9,7 @@ use FireflyIII\Models\Tag; use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI; use FireflyIII\Repositories\Tag\TagRepositoryInterface; use Input; +use Log; use Preferences; use Route; use Session; @@ -33,8 +34,17 @@ class HomeController extends Controller public function dateRange() { - $start = new Carbon(Input::get('start')); - $end = new Carbon(Input::get('end')); + $start = new Carbon(Input::get('start')); + $end = new Carbon(Input::get('end')); + $label = Input::get('label'); + + // check if the label is "everything" or "Custom range" which will betray + // a possible problem with the budgets. + if ($label === strval(trans('firefly.everything')) || $label === strval(trans('firefly.customRange'))) { + Session::put('is_custom_range', true); + } else { + Session::put('is_custom_range', false); + } $diff = $start->diffInDays($end); diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index 096024ab9e..abeae76236 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -4,6 +4,7 @@ declare(strict_types = 1); namespace FireflyIII\Repositories\Budget; use Carbon\Carbon; +use Config; use DB; use FireflyIII\Models\Account; use FireflyIII\Models\Budget; @@ -19,6 +20,7 @@ use Illuminate\Database\Query\JoinClause; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; use Input; +use Preferences; /** * Class BudgetRepository @@ -554,6 +556,54 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn return $paginator; } + /** + * Returns a list of budget limits that are valid in the current given range. + * $ignore is optional. Send an empty limit rep. + * + * @param Budget $budget + * @param Carbon $start + * @param Carbon $end + * @param LimitRepetition $ignore + * + * @return Collection + */ + public function getValidRepetitions(Budget $budget, Carbon $start, Carbon $end, LimitRepetition $ignore) : Collection + { + $query = $budget->limitrepetitions() + ->where( // valid when either of these are true: + function ($q) use ($start, $end) { + $q->where( + function ($query) use ($start, $end) { + // starts before start time, and the end also after start time. + $query->where('limit_repetitions.startdate', '<=', $start->format('Y-m-d 00:00:00')); + $query->where('limit_repetitions.enddate', '>=', $start->format('Y-m-d 00:00:00')); + } + ); + $q->orWhere( + function ($query) use ($start, $end) { + // end after end time, and start is before end time + $query->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d 00:00:00')); + $query->where('limit_repetitions.enddate', '>=', $end->format('Y-m-d 00:00:00')); + } + ); + // start is after start and end is before end + $q->orWhere( + function ($query) use ($start, $end) { + // end after end time, and start is before end time + $query->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d 00:00:00')); + $query->where('limit_repetitions.enddate', '<=', $end->format('Y-m-d 00:00:00')); + } + ); + } + ); + if (!is_null($ignore->id)) { + $query->where('limit_repetitions.id', '!=', $ignore->id); + } + $data = $query->get(['limit_repetitions.*']); + + return $data; + } + /** * @param Carbon $start * @param Carbon $end @@ -819,9 +869,12 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn */ public function updateLimitAmount(Budget $budget, Carbon $date, int $amount): BudgetLimit { - // there should be a budget limit for this startdate: + // there might be a budget limit for this startdate: + + $viewRange = Preferences::get('viewRange', '1M')->data; + $repeatFreq = Config::get('firefly.range_to_repeat_freq.' . $viewRange); /** @var BudgetLimit $limit */ - $limit = $budget->budgetlimits()->where('budget_limits.startdate', $date)->first(['budget_limits.*']); + $limit = $budget->budgetlimits()->where('budget_limits.startdate', $date)->where('budget_limits.repeat_freq', $repeatFreq)->first(['budget_limits.*']); if (!$limit) { // if not, create one! @@ -829,12 +882,13 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn $limit->budget()->associate($budget); $limit->startdate = $date; $limit->amount = $amount; - $limit->repeat_freq = 'monthly'; + $limit->repeat_freq = $repeatFreq; $limit->repeats = 0; $limit->save(); // likewise, there should be a limit repetition to match the end date // (which is always the end of the month) but that is caught by an event. + // so handled automatically. } else { if ($amount > 0) { @@ -847,52 +901,4 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn return $limit; } - - /** - * Returns a list of budget limits that are valid in the current given range. - * $ignore is optional. Send an empty limit rep. - * - * @param Budget $budget - * @param Carbon $start - * @param Carbon $end - * @param LimitRepetition $ignore - * - * @return Collection - */ - public function getValidRepetitions(Budget $budget, Carbon $start, Carbon $end, LimitRepetition $ignore) : Collection - { - $query = $budget->limitrepetitions() - ->where( // valid when either of these are true: - function ($q) use ($start, $end) { - $q->where( - function ($query) use ($start, $end) { - // starts before start time, and the end also after start time. - $query->where('limit_repetitions.startdate', '<=', $start->format('Y-m-d 00:00:00')); - $query->where('limit_repetitions.enddate', '>=', $start->format('Y-m-d 00:00:00')); - } - ); - $q->orWhere( - function ($query) use ($start, $end) { - // end after end time, and start is before end time - $query->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d 00:00:00')); - $query->where('limit_repetitions.enddate', '>=', $end->format('Y-m-d 00:00:00')); - } - ); - // start is after start and end is before end - $q->orWhere( - function ($query) use ($start, $end) { - // end after end time, and start is before end time - $query->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d 00:00:00')); - $query->where('limit_repetitions.enddate', '<=', $end->format('Y-m-d 00:00:00')); - } - ); - } - ); - if (!is_null($ignore->id)) { - $query->where('limit_repetitions.id', '!=', $ignore->id); - } - $data = $query->get(['limit_repetitions.*']); - - return $data; - } } diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index 05bbac919c..174ca63034 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -257,6 +257,7 @@ return [ 'pref_1M' => 'One month', 'pref_3M' => 'Three months (quarter)', 'pref_6M' => 'Six months', + 'pref_1Y' => 'One year', 'pref_languages' => 'Languages', 'pref_languages_help' => 'Firefly III supports several languages. Which one do you prefer?', 'pref_custom_fiscal_year' => 'Fiscal year settings', @@ -476,6 +477,12 @@ return [ 'update_amount' => 'Update amount', 'update_budget' => 'Update budget', 'update_budget_amount_range' => 'Update (expected) available amount between :start and :end', + 'warn_range_weekly' => 'You have selected a custom date range using the top right date range selection tool. Your preferences indicate you prefer a "weekly" view. Changes to the budgets below will be valid for exactly one week from now.', + 'warn_range_monthly' => 'You have selected a custom date range using the top right date range selection tool. Your preferences indicate you prefer a "monthly" view. Changes to the budgets below will be valid for exactly one month from now.', + 'warn_range_quarterly' => 'You have selected a custom date range using the top right date range selection tool. Your preferences indicate you prefer a "quarter" view. Changes to the budgets below will be valid for exactly three months from now.', + 'warn_range_half-year' => 'You have selected a custom date range using the top right date range selection tool. Your preferences indicate you prefer the "six months" overview. Changes to the budgets below will be valid for exactly six months from now.', + 'warn_range_custom' => 'You have selected a custom date range using the top right date range selection tool. Your preferences indicate you prefer a custom range. Changes to the budgets below will be valid for your custom range.', + 'warn_range_yearly' => 'You have selected a custom date range using the top right date range selection tool. Your preferences indicate you prefer the "yearly" overview. Changes to the budgets below will be valid for exactly one year from now.', // bills: 'matching_on' => 'Matching on', diff --git a/resources/views/budgets/index.twig b/resources/views/budgets/index.twig index cfc1363153..2ef96a7801 100644 --- a/resources/views/budgets/index.twig +++ b/resources/views/budgets/index.twig @@ -99,52 +99,65 @@ -
- {% for budget in budgets %} -
-
-
-

- - {% if budget.currentRep.id %} - {{ budget.name }} - {% else %} - {{ budget.name }} - {% endif %} -

+ {% if userWarning != "" %} +
+
+

+ + + {{ userWarning }} + +

+
+
+ {% endif %} - -
- - +
+ {% for budget in budgets %} +
+
+
+

+ + {% if budget.currentRep.id %} + {{ budget.name }} + {% else %} + {{ budget.name }} + {% endif %} +

+ + + -
- - - - - - - + + + + + + {% if budget.otherRepetitions.count > 0 %} + + - - {% if budget.otherRepetitions.count > 0 %} - - - - {% endif %} -
- {{ 'budgeted'|_ }} -
- {{ session('start').formatLocalized(monthAndDayFormat) }} - - {{ session('end').formatLocalized(monthAndDayFormat) }}
-
-
-
-
{{ defaultCurrency.symbol|raw }}
- - -
- -
-
- {{ 'spent'|_ }} -
- {{ session('start').formatLocalized(monthAndDayFormat) }} - - {{ session('end').formatLocalized(monthAndDayFormat) }} + +
+ {{ 'spent'|_ }} +
+ {{ session('start').formatLocalized(monthAndDayFormat) }} - + {{ session('end').formatLocalized(monthAndDayFormat) }}
+
{{ budget.spent|formatAmount }}
+
    + {% for other in budget.otherRepetitions %} + {% if other.id != budget.currentRep.id %} +
  • Budgeted + {{ other.amount|formatAmountPlain }} + between + {{ other.startdate.formatLocalized(monthAndDayFormat) }} + and {{ other.enddate.formatLocalized(monthAndDayFormat) }}. +
  • + {% endif %} + {% endfor %} +
{{ budget.spent|formatAmount }}
-
    - {% for other in budget.otherRepetitions %} - {% if other.id != budget.currentRep.id %} -
  • Budgeted - {{ other.amount|formatAmountPlain }} - between - {{ other.startdate.formatLocalized(monthAndDayFormat) }} - and {{ other.enddate.formatLocalized(monthAndDayFormat) }}. -
  • - {% endif %} - {% endfor %} -
-
-
+ {% endif %} +
- {% if loop.index % 3 == 0 %} +
+ {% if loop.index % 3 == 0 %}
- {% endif %} - {% endfor %} + {% endif %} + {% endfor %}
{% if inactive|length > 0 %} diff --git a/resources/views/preferences/index.twig b/resources/views/preferences/index.twig index 549a690594..4802746cbb 100644 --- a/resources/views/preferences/index.twig +++ b/resources/views/preferences/index.twig @@ -107,6 +107,13 @@ {{ 'pref_6M'|_ }}
+
+ +