mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-12-19 04:49:30 +00:00
Some extensions to budgets.
This commit is contained in:
@@ -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'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user