firefly-iii/app/Repositories/Budget/BudgetRepository.php

347 lines
10 KiB
PHP
Raw Normal View History

2015-02-22 02:46:21 -06:00
<?php
namespace FireflyIII\Repositories\Budget;
2015-04-05 03:36:28 -05:00
use Auth;
2015-02-22 02:46:21 -06:00
use Carbon\Carbon;
use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\LimitRepetition;
2015-06-05 03:02:40 -05:00
use FireflyIII\Repositories\Shared\ComponentRepository;
2015-04-07 10:51:22 -05:00
use Illuminate\Database\Query\Builder as QueryBuilder;
2015-02-22 08:40:13 -06:00
use Illuminate\Pagination\LengthAwarePaginator;
2015-04-05 03:36:28 -05:00
use Illuminate\Support\Collection;
2015-05-08 07:00:49 -05:00
use Input;
2015-02-22 02:46:21 -06:00
/**
* Class BudgetRepository
*
* @package FireflyIII\Repositories\Budget
*/
2015-06-05 03:02:40 -05:00
class BudgetRepository extends ComponentRepository implements BudgetRepositoryInterface
2015-02-22 02:46:21 -06:00
{
2015-04-03 12:39:36 -05:00
/**
* @return void
*/
public function cleanupBudgets()
{
// delete limits with amount 0:
2015-04-05 03:36:28 -05:00
BudgetLimit::where('amount', 0)->delete();
2015-04-03 12:39:36 -05:00
}
2015-02-22 08:40:13 -06:00
/**
* @param Budget $budget
*
* @return boolean
*/
public function destroy(Budget $budget)
{
$budget->delete();
return true;
}
2015-04-07 10:51:22 -05:00
/**
* @param Budget $budget
* @param Carbon $date
*
* @return float
*/
2015-05-22 08:05:32 -05:00
public function expensesOnDayCorrected(Budget $budget, Carbon $date)
2015-04-07 10:51:22 -05:00
{
2015-05-22 08:05:32 -05:00
$sum = floatval($budget->transactionjournals()->transactionTypes(['Withdrawal'])->onDate($date)->get(['transaction_journals.*'])->sum('amount'));
return $sum * -1;
2015-04-07 10:51:22 -05:00
}
2015-04-05 03:36:28 -05:00
/**
* @return Collection
*/
public function getActiveBudgets()
{
$budgets = Auth::user()->budgets()->where('active', 1)->get();
$budgets->sortBy('name');
return $budgets;
}
/**
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getBudgetLimitRepetitions(Budget $budget, Carbon $start, Carbon $end)
{
/** @var Collection $repetitions */
return LimitRepetition::
leftJoin('budget_limits', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.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('budget_limits.budget_id', $budget->id)
->get(['limit_repetitions.*']);
}
/**
* @param Budget $budget
*
* @return Collection
*/
public function getBudgetLimits(Budget $budget)
{
return $budget->budgetLimits()->orderBy('startdate', 'DESC')->get();
}
/**
* @return Collection
*/
public function getBudgets()
{
$budgets = Auth::user()->budgets()->get();
return $budgets;
2015-04-05 03:36:28 -05:00
}
/**
* @param Budget $budget
* @param Carbon $date
*
* @return LimitRepetition|null
*/
public function getCurrentRepetition(Budget $budget, Carbon $date)
{
return $budget->limitrepetitions()->where('limit_repetitions.startdate', $date)->first(['limit_repetitions.*']);
}
2015-04-07 10:51:22 -05:00
/**
* @param Budget $budget
*
* @return Carbon
*/
public function getFirstBudgetLimitDate(Budget $budget)
{
$limit = $budget->budgetlimits()->orderBy('startdate', 'ASC')->first();
if ($limit) {
return $limit->startdate;
}
return Carbon::now()->startOfYear();
}
2015-04-05 03:36:28 -05:00
/**
* @return Collection
*/
public function getInactiveBudgets()
{
2015-04-19 12:10:53 -05:00
return Auth::user()->budgets()->where('active', 0)->get();
2015-04-05 03:36:28 -05:00
}
2015-02-22 08:40:13 -06:00
/**
* Returns all the transaction journals for a limit, possibly limited by a limit repetition.
*
* @param Budget $budget
* @param LimitRepetition $repetition
* @param int $take
*
2015-05-26 13:28:18 -05:00
* @return LengthAwarePaginator
2015-02-22 08:40:13 -06:00
*/
public function getJournals(Budget $budget, LimitRepetition $repetition = null, $take = 50)
{
2015-05-08 07:00:49 -05:00
$offset = intval(Input::get('page')) > 0 ? intval(Input::get('page')) * $take : 0;
2015-02-22 08:40:13 -06:00
2015-03-27 01:32:06 -05:00
$setQuery = $budget->transactionJournals()->withRelevantData()->take($take)->offset($offset)
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.order', 'ASC')
->orderBy('transaction_journals.id', 'DESC');
2015-02-22 08:40:13 -06:00
$countQuery = $budget->transactionJournals();
if (!is_null($repetition->id)) {
$setQuery->after($repetition->startdate)->before($repetition->enddate);
$countQuery->after($repetition->startdate)->before($repetition->enddate);
}
$set = $setQuery->get(['transaction_journals.*']);
$count = $countQuery->count();
2015-03-29 14:27:51 -05:00
2015-02-23 14:55:52 -06:00
return new LengthAwarePaginator($set, $count, $take, $offset);
2015-02-22 08:40:13 -06:00
}
2015-04-07 10:51:22 -05:00
/**
* @param Budget $budget
*
* @return Carbon
*/
public function getLastBudgetLimitDate(Budget $budget)
{
$limit = $budget->budgetlimits()->orderBy('startdate', 'DESC')->first();
if ($limit) {
return $limit->startdate;
}
return Carbon::now()->startOfYear();
}
/**
* @param Budget $budget
* @param Carbon $date
*
* @return float|null
*/
public function getLimitAmountOnDate(Budget $budget, Carbon $date)
{
$repetition = LimitRepetition::leftJoin('budget_limits', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
->where('limit_repetitions.startdate', $date->format('Y-m-d 00:00:00'))
->where('budget_limits.budget_id', $budget->id)
->first(['limit_repetitions.*']);
2015-04-07 10:51:22 -05:00
if ($repetition) {
return floatval($repetition->amount);
}
return null;
}
2015-04-05 03:36:28 -05:00
/**
* @param Carbon $start
* @param Carbon $end
*
* @return Collection
*/
public function getWithoutBudget(Carbon $start, Carbon $end)
{
return Auth::user()
->transactionjournals()
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->whereNull('budget_transaction_journal.id')
->before($end)
->after($start)
->orderBy('transaction_journals.date', 'DESC')
->orderBy('transaction_journals.order', 'ASC')
->orderBy('transaction_journals.id', 'DESC')
->get(['transaction_journals.*']);
2015-04-05 03:36:28 -05:00
}
/**
* @param Carbon $start
* @param Carbon $end
*
* @return double
*/
public function getWithoutBudgetSum(Carbon $start, Carbon $end)
{
$noBudgetSet = Auth::user()
->transactionjournals()
->whereNotIn(
'transaction_journals.id', function (QueryBuilder $query) use ($start, $end) {
$query
->select('transaction_journals.id')
->from('transaction_journals')
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
->where('transaction_journals.date', '>=', $start->format('Y-m-d 00:00:00'))
->where('transaction_journals.date', '<=', $end->format('Y-m-d 00:00:00'))
->whereNotNull('budget_transaction_journal.budget_id');
}
)
->after($start)
->before($end)
->transactionTypes(['Withdrawal'])
->get(['transaction_journals.*'])->sum('amount');
2015-05-14 02:51:54 -05:00
2015-05-14 01:08:25 -05:00
return floatval($noBudgetSet) * -1;
}
/**
* @param Budget $budget
* @param Carbon $start
* @param Carbon $end
* @param bool $shared
*
2015-06-05 03:02:40 -05:00
* @return string
*/
public function spentInPeriodCorrected(Budget $budget, Carbon $start, Carbon $end, $shared = true)
{
2015-06-05 03:02:40 -05:00
return $this->spentInPeriod($budget, $start, $end, $shared);
}
2015-02-22 08:40:13 -06:00
/**
* @param array $data
*
* @return Budget
*/
public function store(array $data)
{
$newBudget = new Budget(
[
'user_id' => $data['user'],
'name' => $data['name'],
]
);
$newBudget->save();
return $newBudget;
}
2015-05-22 08:05:32 -05:00
2015-02-22 08:40:13 -06:00
/**
* @param Budget $budget
* @param array $data
*
* @return Budget
*/
public function update(Budget $budget, array $data)
{
// update the account:
2015-03-29 14:27:51 -05:00
$budget->name = $data['name'];
$budget->active = $data['active'];
2015-02-22 08:40:13 -06:00
$budget->save();
return $budget;
}
2015-02-22 02:46:21 -06:00
/**
* @param Budget $budget
* @param Carbon $date
* @param $amount
*
* @return BudgetLimit
2015-02-22 02:46:21 -06:00
*/
public function updateLimitAmount(Budget $budget, Carbon $date, $amount)
{
2015-04-03 12:11:55 -05:00
// there should be a budget limit for this startdate:
2015-02-22 02:46:21 -06:00
/** @var BudgetLimit $limit */
2015-04-03 12:11:55 -05:00
$limit = $budget->budgetlimits()->where('budget_limits.startdate', $date)->first(['budget_limits.*']);
2015-02-22 02:46:21 -06:00
if (!$limit) {
2015-04-03 12:11:55 -05:00
// if not, create one!
2015-02-22 02:46:21 -06:00
$limit = new BudgetLimit;
$limit->budget()->associate($budget);
$limit->startdate = $date;
$limit->amount = $amount;
$limit->repeat_freq = 'monthly';
$limit->repeats = 0;
$limit->save();
2015-04-03 12:11:55 -05:00
// 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.
2015-02-22 02:46:21 -06:00
} else {
if ($amount > 0) {
$limit->amount = $amount;
$limit->save();
} else {
$limit->delete();
}
}
2015-02-22 08:40:13 -06:00
2015-02-22 02:46:21 -06:00
return $limit;
}
2015-03-29 01:14:32 -05:00
}