mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-11-01 11:31:06 +00:00
Sort by alphabet.
This commit is contained in:
@@ -25,6 +25,19 @@ use Input;
|
||||
class BudgetRepository extends ComponentRepository implements BudgetRepositoryInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Collection $accounts
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function balanceInPeriod(Budget $budget, Carbon $start, Carbon $end, Collection $accounts)
|
||||
{
|
||||
return $this->commonBalanceInPeriod($budget, $start, $end, $accounts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
@@ -35,6 +48,299 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function destroy(Budget $budget)
|
||||
{
|
||||
$budget->delete();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
public function firstActivity(Budget $budget)
|
||||
{
|
||||
$first = $budget->transactionjournals()->orderBy('date', 'ASC')->first();
|
||||
if ($first) {
|
||||
return $first->date;
|
||||
}
|
||||
|
||||
return new Carbon;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getActiveBudgets()
|
||||
{
|
||||
/** @var Collection $set */
|
||||
$set = Auth::user()->budgets()->where('active', 1)->get();
|
||||
|
||||
$set = $set->sortBy(
|
||||
function (Budget $budget) {
|
||||
return strtolower($budget->name);
|
||||
}
|
||||
);
|
||||
|
||||
return $set;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getAllBudgetLimitRepetitions(Carbon $start, Carbon $end)
|
||||
{
|
||||
/** @var Collection $repetitions */
|
||||
return LimitRepetition::
|
||||
leftJoin('budget_limits', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
|
||||
->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
|
||||
->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d 00:00:00'))
|
||||
->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d 00:00:00'))
|
||||
->where('budgets.user_id', Auth::user()->id)
|
||||
->get(['limit_repetitions.*', 'budget_limits.budget_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the budgeted amounts for each budgets in each year.
|
||||
*
|
||||
* @param Collection $budgets
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getBudgetedPerYear(Collection $budgets, Carbon $start, Carbon $end)
|
||||
{
|
||||
$budgetIds = $budgets->pluck('id')->toArray();
|
||||
|
||||
$set = Auth::user()->budgets()
|
||||
->leftJoin('budget_limits', 'budgets.id', '=', 'budget_limits.budget_id')
|
||||
->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
|
||||
->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d'))
|
||||
->where('limit_repetitions.enddate', '<=', $end->format('Y-m-d'))
|
||||
->groupBy('budgets.id')
|
||||
->groupBy('dateFormatted')
|
||||
->whereIn('budgets.id', $budgetIds)
|
||||
->get(
|
||||
[
|
||||
'budgets.*',
|
||||
DB::Raw('DATE_FORMAT(`limit_repetitions`.`startdate`,"%Y") as `dateFormatted`'),
|
||||
DB::Raw('SUM(`limit_repetitions`.`amount`) as `budgeted`'),
|
||||
]
|
||||
);
|
||||
|
||||
return $set;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getBudgets()
|
||||
{
|
||||
/** @var Collection $set */
|
||||
$set = Auth::user()->budgets()->get();
|
||||
|
||||
$set = $set->sortBy(
|
||||
function (Budget $budget) {
|
||||
return strtolower($budget->name);
|
||||
}
|
||||
);
|
||||
|
||||
return $set;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array with every budget in it and the expenses for each budget
|
||||
* per month.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getBudgetsAndExpensesPerMonth(Collection $accounts, Carbon $start, Carbon $end)
|
||||
{
|
||||
$ids = $accounts->pluck('id')->toArray();
|
||||
|
||||
/** @var Collection $set */
|
||||
$set = Auth::user()->budgets()
|
||||
->leftJoin('budget_transaction_journal', 'budgets.id', '=', 'budget_transaction_journal.budget_id')
|
||||
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id')
|
||||
->leftJoin(
|
||||
'transactions', function (JoinClause $join) {
|
||||
$join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0);
|
||||
}
|
||||
)
|
||||
->groupBy('budgets.id')
|
||||
->groupBy('dateFormatted')
|
||||
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
||||
->whereIn('transactions.account_id', $ids)
|
||||
->get(
|
||||
[
|
||||
'budgets.*',
|
||||
DB::Raw('DATE_FORMAT(`transaction_journals`.`date`, "%Y-%m") AS `dateFormatted`'),
|
||||
DB::Raw('SUM(`transactions`.`amount`) AS `sumAmount`'),
|
||||
]
|
||||
);
|
||||
|
||||
$set = $set->sortBy(
|
||||
function (Budget $budget) {
|
||||
return strtolower($budget->name);
|
||||
}
|
||||
);
|
||||
|
||||
$return = [];
|
||||
foreach ($set as $budget) {
|
||||
$id = $budget->id;
|
||||
if (!isset($return[$id])) {
|
||||
$return[$id] = [
|
||||
'budget' => $budget,
|
||||
'entries' => [],
|
||||
];
|
||||
}
|
||||
// store each entry:
|
||||
$return[$id]['entries'][$budget->dateFormatted] = $budget->sumAmount;
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array with every budget in it and the expenses for each budget
|
||||
* per year for.
|
||||
*
|
||||
* @param Collection $budgets
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) // it's a query.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getBudgetsAndExpensesPerYear(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end)
|
||||
{
|
||||
$ids = $accounts->pluck('id')->toArray();
|
||||
$budgetIds = $budgets->pluck('id')->toArray();
|
||||
|
||||
/** @var Collection $set */
|
||||
$set = Auth::user()->budgets()
|
||||
->leftJoin('budget_transaction_journal', 'budgets.id', '=', 'budget_transaction_journal.budget_id')
|
||||
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id')
|
||||
->leftJoin(
|
||||
'transactions', function (JoinClause $join) {
|
||||
$join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0);
|
||||
}
|
||||
)
|
||||
->groupBy('budgets.id')
|
||||
->groupBy('dateFormatted')
|
||||
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
||||
->whereIn('transactions.account_id', $ids)
|
||||
->whereIn('budgets.id', $budgetIds)
|
||||
->get(
|
||||
[
|
||||
'budgets.*',
|
||||
DB::Raw('DATE_FORMAT(`transaction_journals`.`date`, "%Y") AS `dateFormatted`'),
|
||||
DB::Raw('SUM(`transactions`.`amount`) AS `sumAmount`'),
|
||||
]
|
||||
);
|
||||
|
||||
$set = $set->sortBy(
|
||||
function (Budget $budget) {
|
||||
return strtolower($budget->name);
|
||||
}
|
||||
);
|
||||
|
||||
$return = [];
|
||||
foreach ($set as $budget) {
|
||||
$id = $budget->id;
|
||||
if (!isset($return[$id])) {
|
||||
$return[$id] = [
|
||||
'budget' => $budget,
|
||||
'entries' => [],
|
||||
];
|
||||
}
|
||||
// store each entry:
|
||||
$return[$id]['entries'][$budget->dateFormatted] = $budget->sumAmount;
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of budgets, budget limits and limit repetitions
|
||||
* (doubling any of them in a left join)
|
||||
*
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getBudgetsAndLimitsInRange(Carbon $start, Carbon $end)
|
||||
{
|
||||
/** @var Collection $set */
|
||||
$set = Auth::user()
|
||||
->budgets()
|
||||
->leftJoin('budget_limits', 'budget_limits.budget_id', '=', 'budgets.id')
|
||||
->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
|
||||
->where(
|
||||
function (Builder $query) use ($start, $end) {
|
||||
$query->where(
|
||||
function (Builder $query) use ($start, $end) {
|
||||
$query->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d'));
|
||||
$query->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d'));
|
||||
}
|
||||
);
|
||||
$query->orWhere(
|
||||
function (Builder $query) {
|
||||
$query->whereNull('limit_repetitions.startdate');
|
||||
$query->whereNull('limit_repetitions.enddate');
|
||||
}
|
||||
);
|
||||
}
|
||||
)
|
||||
->get(['budgets.*', 'limit_repetitions.startdate', 'limit_repetitions.enddate', 'limit_repetitions.amount']);
|
||||
|
||||
$set = $set->sortBy(
|
||||
function (Budget $budget) {
|
||||
return strtolower($budget->name);
|
||||
}
|
||||
);
|
||||
|
||||
return $set;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return LimitRepetition|null
|
||||
*/
|
||||
public function getCurrentRepetition(Budget $budget, Carbon $start, Carbon $end)
|
||||
{
|
||||
$data = $budget->limitrepetitions()
|
||||
->where('limit_repetitions.startdate', $start->format('Y-m-d 00:00:00'))
|
||||
->where('limit_repetitions.enddate', $end->format('Y-m-d 00:00:00'))
|
||||
->first(['limit_repetitions.*']);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the expenses for this budget grouped per day, with the date
|
||||
* in "date" (a string, not a Carbon) and the amount in "dailyAmount".
|
||||
@@ -96,179 +402,6 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
return $set;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function destroy(Budget $budget)
|
||||
{
|
||||
$budget->delete();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getActiveBudgets()
|
||||
{
|
||||
/** @var Collection $set */
|
||||
$set = Auth::user()->budgets()->where('active', 1)->get();
|
||||
|
||||
$set = $set->sortBy(
|
||||
function (Budget $budget) {
|
||||
return strtolower($budget->name);
|
||||
}
|
||||
);
|
||||
|
||||
return $set;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of budgets, budget limits and limit repetitions
|
||||
* (doubling any of them in a left join)
|
||||
*
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getBudgetsAndLimitsInRange(Carbon $start, Carbon $end)
|
||||
{
|
||||
/** @var Collection $set */
|
||||
$set = Auth::user()
|
||||
->budgets()
|
||||
->leftJoin('budget_limits', 'budget_limits.budget_id', '=', 'budgets.id')
|
||||
->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
|
||||
->where(
|
||||
function (Builder $query) use ($start, $end) {
|
||||
$query->where(
|
||||
function (Builder $query) use ($start, $end) {
|
||||
$query->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d'));
|
||||
$query->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d'));
|
||||
}
|
||||
);
|
||||
$query->orWhere(
|
||||
function (Builder $query) {
|
||||
$query->whereNull('limit_repetitions.startdate');
|
||||
$query->whereNull('limit_repetitions.enddate');
|
||||
}
|
||||
);
|
||||
}
|
||||
)
|
||||
->get(['budgets.*', 'limit_repetitions.startdate', 'limit_repetitions.enddate', 'limit_repetitions.amount']);
|
||||
|
||||
$set = $set->sortBy(
|
||||
function (Budget $budget) {
|
||||
return strtolower($budget->name);
|
||||
}
|
||||
);
|
||||
|
||||
return $set;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
*
|
||||
* @return Carbon
|
||||
*/
|
||||
public function firstActivity(Budget $budget)
|
||||
{
|
||||
$first = $budget->transactionjournals()->orderBy('date', 'ASC')->first();
|
||||
if ($first) {
|
||||
return $first->date;
|
||||
}
|
||||
|
||||
return new Carbon;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getAllBudgetLimitRepetitions(Carbon $start, Carbon $end)
|
||||
{
|
||||
/** @var Collection $repetitions */
|
||||
return LimitRepetition::
|
||||
leftJoin('budget_limits', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
|
||||
->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
|
||||
->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d 00:00:00'))
|
||||
->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d 00:00:00'))
|
||||
->where('budgets.user_id', Auth::user()->id)
|
||||
->get(['limit_repetitions.*', 'budget_limits.budget_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array with the following key:value pairs:
|
||||
*
|
||||
* yyyy-mm-dd:<amount>
|
||||
*
|
||||
* Where yyyy-mm-dd is the date and <amount> is the money spent using DEPOSITS in the $budget
|
||||
* from all the users accounts.
|
||||
*
|
||||
* @param Budget $budget
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function spentPerDay(Budget $budget, Carbon $start, Carbon $end)
|
||||
{
|
||||
/** @var Collection $query */
|
||||
$query = $budget->transactionJournals()
|
||||
->transactionTypes([TransactionType::WITHDRAWAL])
|
||||
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->where('transactions.amount', '<', 0)
|
||||
->before($end)
|
||||
->after($start)
|
||||
->groupBy('dateFormatted')->get(['transaction_journals.date as dateFormatted', DB::Raw('SUM(`transactions`.`amount`) AS `sum`')]);
|
||||
|
||||
$return = [];
|
||||
foreach ($query->toArray() as $entry) {
|
||||
$return[$entry['dateFormatted']] = $entry['sum'];
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getBudgets()
|
||||
{
|
||||
/** @var Collection $set */
|
||||
$set = Auth::user()->budgets()->get();
|
||||
|
||||
$set = $set->sortBy(
|
||||
function (Budget $budget) {
|
||||
return strtolower($budget->name);
|
||||
}
|
||||
);
|
||||
|
||||
return $set;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return LimitRepetition|null
|
||||
*/
|
||||
public function getCurrentRepetition(Budget $budget, Carbon $start, Carbon $end)
|
||||
{
|
||||
$data = $budget->limitrepetitions()
|
||||
->where('limit_repetitions.startdate', $start->format('Y-m-d 00:00:00'))
|
||||
->where('limit_repetitions.enddate', $end->format('Y-m-d 00:00:00'))
|
||||
->first(['limit_repetitions.*']);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
*
|
||||
@@ -284,64 +417,6 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
return Carbon::now()->startOfYear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array with every budget in it and the expenses for each budget
|
||||
* per month.
|
||||
*
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getBudgetsAndExpensesPerMonth(Collection $accounts, Carbon $start, Carbon $end)
|
||||
{
|
||||
$ids = $accounts->pluck('id')->toArray();
|
||||
|
||||
/** @var Collection $set */
|
||||
$set = Auth::user()->budgets()
|
||||
->leftJoin('budget_transaction_journal', 'budgets.id', '=', 'budget_transaction_journal.budget_id')
|
||||
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id')
|
||||
->leftJoin(
|
||||
'transactions', function (JoinClause $join) {
|
||||
$join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0);
|
||||
}
|
||||
)
|
||||
->groupBy('budgets.id')
|
||||
->groupBy('dateFormatted')
|
||||
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
||||
->whereIn('transactions.account_id', $ids)
|
||||
->get(
|
||||
[
|
||||
'budgets.*',
|
||||
DB::Raw('DATE_FORMAT(`transaction_journals`.`date`, "%Y-%m") AS `dateFormatted`'),
|
||||
DB::Raw('SUM(`transactions`.`amount`) AS `sumAmount`'),
|
||||
]
|
||||
);
|
||||
|
||||
$set = $set->sortBy(
|
||||
function (Budget $budget) {
|
||||
return strtolower($budget->name);
|
||||
}
|
||||
);
|
||||
|
||||
$return = [];
|
||||
foreach ($set as $budget) {
|
||||
$id = $budget->id;
|
||||
if (!isset($return[$id])) {
|
||||
$return[$id] = [
|
||||
'budget' => $budget,
|
||||
'entries' => [],
|
||||
];
|
||||
}
|
||||
// store each entry:
|
||||
$return[$id]['entries'][$budget->dateFormatted] = $budget->sumAmount;
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
@@ -448,187 +523,6 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
return $entry->journalAmount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
* @param Collection $accounts
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function balanceInPeriod(Budget $budget, Carbon $start, Carbon $end, Collection $accounts)
|
||||
{
|
||||
return $this->commonBalanceInPeriod($budget, $start, $end, $accounts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return Budget
|
||||
*/
|
||||
public function store(array $data)
|
||||
{
|
||||
$newBudget = new Budget(
|
||||
[
|
||||
'user_id' => $data['user'],
|
||||
'name' => $data['name'],
|
||||
]
|
||||
);
|
||||
$newBudget->save();
|
||||
|
||||
return $newBudget;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
* @param array $data
|
||||
*
|
||||
* @return Budget
|
||||
*/
|
||||
public function update(Budget $budget, array $data)
|
||||
{
|
||||
// update the account:
|
||||
$budget->name = $data['name'];
|
||||
$budget->active = $data['active'];
|
||||
$budget->save();
|
||||
|
||||
return $budget;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
* @param Carbon $date
|
||||
* @param $amount
|
||||
*
|
||||
* @return BudgetLimit
|
||||
*/
|
||||
public function updateLimitAmount(Budget $budget, Carbon $date, $amount)
|
||||
{
|
||||
// there should be a budget limit for this startdate:
|
||||
/** @var BudgetLimit $limit */
|
||||
$limit = $budget->budgetlimits()->where('budget_limits.startdate', $date)->first(['budget_limits.*']);
|
||||
|
||||
if (!$limit) {
|
||||
// if not, create one!
|
||||
$limit = new BudgetLimit;
|
||||
$limit->budget()->associate($budget);
|
||||
$limit->startdate = $date;
|
||||
$limit->amount = $amount;
|
||||
$limit->repeat_freq = 'monthly';
|
||||
$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.
|
||||
|
||||
} else {
|
||||
if ($amount > 0) {
|
||||
$limit->amount = $amount;
|
||||
$limit->save();
|
||||
} else {
|
||||
$limit->delete();
|
||||
}
|
||||
}
|
||||
|
||||
return $limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the budgeted amounts for each budgets in each year.
|
||||
*
|
||||
* @param Collection $budgets
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getBudgetedPerYear(Collection $budgets, Carbon $start, Carbon $end)
|
||||
{
|
||||
$budgetIds = $budgets->pluck('id')->toArray();
|
||||
|
||||
$set = Auth::user()->budgets()
|
||||
->leftJoin('budget_limits', 'budgets.id', '=', 'budget_limits.budget_id')
|
||||
->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
|
||||
->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d'))
|
||||
->where('limit_repetitions.enddate', '<=', $end->format('Y-m-d'))
|
||||
->groupBy('budgets.id')
|
||||
->groupBy('dateFormatted')
|
||||
->whereIn('budgets.id', $budgetIds)
|
||||
->get(
|
||||
[
|
||||
'budgets.*',
|
||||
DB::Raw('DATE_FORMAT(`limit_repetitions`.`startdate`,"%Y") as `dateFormatted`'),
|
||||
DB::Raw('SUM(`limit_repetitions`.`amount`) as `budgeted`'),
|
||||
]
|
||||
);
|
||||
|
||||
return $set;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array with every budget in it and the expenses for each budget
|
||||
* per year for.
|
||||
*
|
||||
* @param Collection $budgets
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) // it's a query.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getBudgetsAndExpensesPerYear(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end)
|
||||
{
|
||||
$ids = $accounts->pluck('id')->toArray();
|
||||
$budgetIds = $budgets->pluck('id')->toArray();
|
||||
|
||||
/** @var Collection $set */
|
||||
$set = Auth::user()->budgets()
|
||||
->leftJoin('budget_transaction_journal', 'budgets.id', '=', 'budget_transaction_journal.budget_id')
|
||||
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id')
|
||||
->leftJoin(
|
||||
'transactions', function (JoinClause $join) {
|
||||
$join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0);
|
||||
}
|
||||
)
|
||||
->groupBy('budgets.id')
|
||||
->groupBy('dateFormatted')
|
||||
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
||||
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
||||
->whereIn('transactions.account_id', $ids)
|
||||
->whereIn('budgets.id', $budgetIds)
|
||||
->get(
|
||||
[
|
||||
'budgets.*',
|
||||
DB::Raw('DATE_FORMAT(`transaction_journals`.`date`, "%Y") AS `dateFormatted`'),
|
||||
DB::Raw('SUM(`transactions`.`amount`) AS `sumAmount`'),
|
||||
]
|
||||
);
|
||||
|
||||
$set = $set->sortBy(
|
||||
function (Budget $budget) {
|
||||
return strtolower($budget->name);
|
||||
}
|
||||
);
|
||||
|
||||
$return = [];
|
||||
foreach ($set as $budget) {
|
||||
$id = $budget->id;
|
||||
if (!isset($return[$id])) {
|
||||
$return[$id] = [
|
||||
'budget' => $budget,
|
||||
'entries' => [],
|
||||
];
|
||||
}
|
||||
// store each entry:
|
||||
$return[$id]['entries'][$budget->dateFormatted] = $budget->sumAmount;
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array with the following key:value pairs:
|
||||
*
|
||||
@@ -727,4 +621,109 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
return $set;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array with the following key:value pairs:
|
||||
*
|
||||
* yyyy-mm-dd:<amount>
|
||||
*
|
||||
* Where yyyy-mm-dd is the date and <amount> is the money spent using DEPOSITS in the $budget
|
||||
* from all the users accounts.
|
||||
*
|
||||
* @param Budget $budget
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function spentPerDay(Budget $budget, Carbon $start, Carbon $end)
|
||||
{
|
||||
/** @var Collection $query */
|
||||
$query = $budget->transactionJournals()
|
||||
->transactionTypes([TransactionType::WITHDRAWAL])
|
||||
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->where('transactions.amount', '<', 0)
|
||||
->before($end)
|
||||
->after($start)
|
||||
->groupBy('dateFormatted')->get(['transaction_journals.date as dateFormatted', DB::Raw('SUM(`transactions`.`amount`) AS `sum`')]);
|
||||
|
||||
$return = [];
|
||||
foreach ($query->toArray() as $entry) {
|
||||
$return[$entry['dateFormatted']] = $entry['sum'];
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return Budget
|
||||
*/
|
||||
public function store(array $data)
|
||||
{
|
||||
$newBudget = new Budget(
|
||||
[
|
||||
'user_id' => $data['user'],
|
||||
'name' => $data['name'],
|
||||
]
|
||||
);
|
||||
$newBudget->save();
|
||||
|
||||
return $newBudget;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
* @param array $data
|
||||
*
|
||||
* @return Budget
|
||||
*/
|
||||
public function update(Budget $budget, array $data)
|
||||
{
|
||||
// update the account:
|
||||
$budget->name = $data['name'];
|
||||
$budget->active = $data['active'];
|
||||
$budget->save();
|
||||
|
||||
return $budget;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
* @param Carbon $date
|
||||
* @param $amount
|
||||
*
|
||||
* @return BudgetLimit
|
||||
*/
|
||||
public function updateLimitAmount(Budget $budget, Carbon $date, $amount)
|
||||
{
|
||||
// there should be a budget limit for this startdate:
|
||||
/** @var BudgetLimit $limit */
|
||||
$limit = $budget->budgetlimits()->where('budget_limits.startdate', $date)->first(['budget_limits.*']);
|
||||
|
||||
if (!$limit) {
|
||||
// if not, create one!
|
||||
$limit = new BudgetLimit;
|
||||
$limit->budget()->associate($budget);
|
||||
$limit->startdate = $date;
|
||||
$limit->amount = $amount;
|
||||
$limit->repeat_freq = 'monthly';
|
||||
$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.
|
||||
|
||||
} else {
|
||||
if ($amount > 0) {
|
||||
$limit->amount = $amount;
|
||||
$limit->save();
|
||||
} else {
|
||||
$limit->delete();
|
||||
}
|
||||
}
|
||||
|
||||
return $limit;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user