2015-02-22 02:46:21 -06:00
|
|
|
<?php
|
2016-02-05 05:08:25 -06:00
|
|
|
declare(strict_types = 1);
|
2015-02-22 02:46:21 -06:00
|
|
|
|
|
|
|
namespace FireflyIII\Repositories\Budget;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2015-12-27 14:17:04 -06:00
|
|
|
use DB;
|
2016-04-03 06:56:06 -05:00
|
|
|
use FireflyIII\Models\Account;
|
2015-02-22 02:46:21 -06:00
|
|
|
use FireflyIII\Models\Budget;
|
|
|
|
use FireflyIII\Models\BudgetLimit;
|
|
|
|
use FireflyIII\Models\LimitRepetition;
|
2016-03-02 06:55:48 -06:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2015-12-09 18:39:50 -06:00
|
|
|
use FireflyIII\Models\TransactionType;
|
2015-06-05 03:02:40 -05:00
|
|
|
use FireflyIII\Repositories\Shared\ComponentRepository;
|
2016-03-03 01:44:20 -06:00
|
|
|
use FireflyIII\User;
|
2015-12-27 14:17:04 -06:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2015-04-07 10:51:22 -05:00
|
|
|
use Illuminate\Database\Query\Builder as QueryBuilder;
|
2015-12-27 14:17:04 -06:00
|
|
|
use Illuminate\Database\Query\JoinClause;
|
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;
|
2016-04-25 14:37:08 -05:00
|
|
|
use Preferences;
|
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
|
|
|
{
|
2016-03-03 01:44:20 -06:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
/**
|
2016-04-26 02:21:57 -05:00
|
|
|
* BudgetRepository constructor.
|
2016-03-03 01:44:20 -06:00
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function __construct(User $user)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
2015-02-22 02:46:21 -06:00
|
|
|
|
2015-04-03 12:39:36 -05:00
|
|
|
/**
|
2016-01-20 08:21:27 -06:00
|
|
|
* @param Budget $budget
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
2015-12-29 01:27:13 -06:00
|
|
|
*
|
2016-01-20 08:21:27 -06:00
|
|
|
* @return string
|
2015-12-29 01:27:13 -06:00
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function balanceInPeriod(Budget $budget, Carbon $start, Carbon $end, Collection $accounts): string
|
2015-12-29 01:27:13 -06:00
|
|
|
{
|
2016-01-20 08:21:27 -06:00
|
|
|
return $this->commonBalanceInPeriod($budget, $start, $end, $accounts);
|
2015-12-29 01:45:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-05 15:00:03 -05:00
|
|
|
* @return bool
|
2015-12-29 01:45:43 -06:00
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function cleanupBudgets(): bool
|
2015-12-29 01:45:43 -06:00
|
|
|
{
|
2016-01-20 08:21:27 -06:00
|
|
|
// delete limits with amount 0:
|
|
|
|
BudgetLimit::where('amount', 0)->delete();
|
2016-04-21 02:00:32 -05:00
|
|
|
|
2016-04-05 15:00:03 -05:00
|
|
|
return true;
|
2015-12-29 01:27:13 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-22 08:40:13 -06:00
|
|
|
/**
|
|
|
|
* @param Budget $budget
|
|
|
|
*
|
2016-04-05 15:00:03 -05:00
|
|
|
* @return bool
|
2015-02-22 08:40:13 -06:00
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function destroy(Budget $budget): bool
|
2015-02-22 08:40:13 -06:00
|
|
|
{
|
|
|
|
$budget->delete();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-03 06:56:06 -05:00
|
|
|
/**
|
|
|
|
* @param Budget $budget
|
|
|
|
* @param Account $account
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function expensesSplit(Budget $budget, Account $account, Carbon $start, Carbon $end): Collection
|
|
|
|
{
|
|
|
|
return $budget->transactionjournals()->expanded()
|
|
|
|
->before($end)
|
|
|
|
->after($start)
|
|
|
|
->where('source_account.id', $account->id)
|
|
|
|
->get(TransactionJournal::QUERYFIELDS);
|
|
|
|
}
|
|
|
|
|
2016-04-01 06:17:07 -05:00
|
|
|
/**
|
|
|
|
* Find a budget.
|
|
|
|
*
|
|
|
|
* @param int $budgetId
|
|
|
|
*
|
|
|
|
* @return Budget
|
|
|
|
*/
|
|
|
|
public function find(int $budgetId): Budget
|
|
|
|
{
|
|
|
|
$budget = $this->user->budgets()->find($budgetId);
|
|
|
|
if (is_null($budget)) {
|
|
|
|
$budget = new Budget;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $budget;
|
|
|
|
}
|
|
|
|
|
2015-04-05 03:36:28 -05:00
|
|
|
/**
|
2016-01-20 08:21:27 -06:00
|
|
|
* @param Budget $budget
|
|
|
|
*
|
|
|
|
* @return Carbon
|
2015-04-05 03:36:28 -05:00
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function firstActivity(Budget $budget): Carbon
|
2015-04-05 03:36:28 -05:00
|
|
|
{
|
2016-01-20 08:21:27 -06:00
|
|
|
$first = $budget->transactionjournals()->orderBy('date', 'ASC')->first();
|
|
|
|
if ($first) {
|
|
|
|
return $first->date;
|
|
|
|
}
|
2015-07-08 06:11:51 -05:00
|
|
|
|
2016-01-20 08:21:27 -06:00
|
|
|
return new Carbon;
|
2015-04-06 02:15:59 -05:00
|
|
|
}
|
|
|
|
|
2015-12-27 14:17:04 -06:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function getActiveBudgets(): Collection
|
2015-12-27 14:17:04 -06:00
|
|
|
{
|
|
|
|
/** @var Collection $set */
|
2016-03-03 01:44:20 -06:00
|
|
|
$set = $this->user->budgets()->where('active', 1)->get();
|
2015-12-27 14:17:04 -06:00
|
|
|
|
|
|
|
$set = $set->sortBy(
|
|
|
|
function (Budget $budget) {
|
|
|
|
return strtolower($budget->name);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return $set;
|
2015-12-31 10:46:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-01 06:54:23 -06:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2016-04-24 13:41:12 -05:00
|
|
|
* @param Budget $budget
|
2015-12-31 10:46:34 -06:00
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-04-24 13:41:12 -05:00
|
|
|
public function getAllBudgetLimitRepetitions(Carbon $start, Carbon $end, Budget $budget = null): Collection
|
2016-01-01 05:41:00 -06:00
|
|
|
{
|
2016-04-24 13:41:12 -05:00
|
|
|
$query = LimitRepetition::
|
2016-01-01 06:54:23 -06:00
|
|
|
leftJoin('budget_limits', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
|
2016-04-24 13:41:12 -05:00
|
|
|
->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', $this->user->id);
|
|
|
|
|
|
|
|
if (!is_null($budget)) {
|
|
|
|
$query->where('budgets.id', $budget->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$set = $query->get(['limit_repetitions.*', 'budget_limits.budget_id']);
|
|
|
|
|
|
|
|
return $set;
|
2015-12-31 10:46:34 -06:00
|
|
|
}
|
|
|
|
|
2016-04-03 06:56:06 -05:00
|
|
|
/**
|
|
|
|
* @param Account $account
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function getAllWithoutBudget(Account $account, Collection $accounts, Carbon $start, Carbon $end): Collection
|
2016-04-03 06:56:06 -05:00
|
|
|
{
|
|
|
|
$ids = $accounts->pluck('id')->toArray();
|
|
|
|
|
|
|
|
return $this->user
|
|
|
|
->transactionjournals()
|
|
|
|
->expanded()
|
|
|
|
->where('source_account.id', $account->id)
|
|
|
|
->whereNotIn('destination_account.id', $ids)
|
|
|
|
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
|
|
|
->whereNull('budget_transaction_journal.id')
|
|
|
|
->before($end)
|
|
|
|
->after($start)
|
|
|
|
->get(TransactionJournal::QUERYFIELDS);
|
|
|
|
}
|
|
|
|
|
2015-12-31 10:46:34 -06:00
|
|
|
/**
|
2016-01-20 08:21:27 -06:00
|
|
|
* Get the budgeted amounts for each budgets in each year.
|
2015-12-31 10:46:34 -06:00
|
|
|
*
|
2016-01-20 08:21:27 -06:00
|
|
|
* @param Collection $budgets
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2015-12-31 10:46:34 -06:00
|
|
|
*
|
2016-01-20 08:21:27 -06:00
|
|
|
* @return Collection
|
2015-12-31 10:46:34 -06:00
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function getBudgetedPerYear(Collection $budgets, Carbon $start, Carbon $end): Collection
|
2015-12-31 10:46:34 -06:00
|
|
|
{
|
2016-01-20 08:21:27 -06:00
|
|
|
$budgetIds = $budgets->pluck('id')->toArray();
|
2016-01-01 06:54:23 -06:00
|
|
|
|
2016-03-03 01:44:20 -06:00
|
|
|
$set = $this->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`'),
|
|
|
|
]
|
|
|
|
);
|
2016-01-01 06:54:23 -06:00
|
|
|
|
2016-01-20 08:21:27 -06:00
|
|
|
return $set;
|
2016-01-01 06:54:23 -06:00
|
|
|
}
|
|
|
|
|
2015-04-06 02:15:59 -05:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function getBudgets(): Collection
|
2015-04-06 02:15:59 -05:00
|
|
|
{
|
2015-07-09 04:13:38 -05:00
|
|
|
/** @var Collection $set */
|
2016-03-03 01:44:20 -06:00
|
|
|
$set = $this->user->budgets()->get();
|
2015-07-09 04:13:38 -05:00
|
|
|
|
|
|
|
$set = $set->sortBy(
|
|
|
|
function (Budget $budget) {
|
|
|
|
return strtolower($budget->name);
|
|
|
|
}
|
|
|
|
);
|
2015-05-19 23:50:15 -05:00
|
|
|
|
2015-07-09 04:13:38 -05:00
|
|
|
return $set;
|
2015-04-05 03:36:28 -05:00
|
|
|
}
|
|
|
|
|
2015-12-28 10:57:03 -06:00
|
|
|
/**
|
|
|
|
* Returns an array with every budget in it and the expenses for each budget
|
|
|
|
* per month.
|
|
|
|
*
|
2015-12-28 12:56:28 -06:00
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2015-12-28 10:57:03 -06:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function getBudgetsAndExpensesPerMonth(Collection $accounts, Carbon $start, Carbon $end): array
|
2015-12-28 10:57:03 -06:00
|
|
|
{
|
2015-12-30 02:17:05 -06:00
|
|
|
$ids = $accounts->pluck('id')->toArray();
|
2015-12-28 12:56:28 -06:00
|
|
|
|
2015-12-28 10:57:03 -06:00
|
|
|
/** @var Collection $set */
|
2016-03-03 01:44:20 -06:00
|
|
|
$set = $this->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`'),
|
|
|
|
]
|
|
|
|
);
|
2015-12-28 10:57:03 -06:00
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
2015-12-28 12:56:28 -06:00
|
|
|
|
2015-12-28 10:57:03 -06:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2015-04-05 03:36:28 -05:00
|
|
|
/**
|
2016-01-20 08:21:27 -06:00
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* @return array
|
2015-04-05 03:36:28 -05:00
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function getBudgetsAndExpensesPerYear(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array
|
2015-04-05 03:36:28 -05:00
|
|
|
{
|
2016-01-20 08:21:27 -06:00
|
|
|
$ids = $accounts->pluck('id')->toArray();
|
|
|
|
$budgetIds = $budgets->pluck('id')->toArray();
|
|
|
|
|
2015-07-08 06:11:51 -05:00
|
|
|
/** @var Collection $set */
|
2016-03-03 01:44:20 -06:00
|
|
|
$set = $this->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`'),
|
|
|
|
]
|
|
|
|
);
|
2015-07-08 06:11:51 -05:00
|
|
|
|
|
|
|
$set = $set->sortBy(
|
|
|
|
function (Budget $budget) {
|
|
|
|
return strtolower($budget->name);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-01-20 08:21:27 -06:00
|
|
|
$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
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function getBudgetsAndLimitsInRange(Carbon $start, Carbon $end): Collection
|
2016-01-20 08:21:27 -06:00
|
|
|
{
|
|
|
|
/** @var Collection $set */
|
2016-03-03 01:44:20 -06:00
|
|
|
$set = $this->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');
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
)
|
2016-04-25 07:53:41 -05:00
|
|
|
->orderBy('budgets.id', 'budget_limits.startdate', 'limit_repetitions.enddate')
|
2016-03-03 01:44:20 -06:00
|
|
|
->get(['budgets.*', 'limit_repetitions.startdate', 'limit_repetitions.enddate', 'limit_repetitions.amount']);
|
2016-01-20 08:21:27 -06:00
|
|
|
|
|
|
|
$set = $set->sortBy(
|
|
|
|
function (Budget $budget) {
|
|
|
|
return strtolower($budget->name);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return $set;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Budget $budget
|
2016-04-25 06:20:42 -05:00
|
|
|
* @param string $repeatFreq
|
2016-01-20 08:21:27 -06:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
2016-04-05 15:00:03 -05:00
|
|
|
* @return LimitRepetition
|
2016-01-20 08:21:27 -06:00
|
|
|
*/
|
2016-04-25 06:20:42 -05:00
|
|
|
public function getCurrentRepetition(Budget $budget, string $repeatFreq, Carbon $start, Carbon $end): LimitRepetition
|
2016-01-20 08:21:27 -06:00
|
|
|
{
|
|
|
|
$data = $budget->limitrepetitions()
|
2016-04-25 06:20:42 -05:00
|
|
|
->where('budget_limits.repeat_freq', $repeatFreq)
|
2016-01-20 08:21:27 -06:00
|
|
|
->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.*']);
|
2016-04-21 02:00:32 -05:00
|
|
|
if (is_null($data)) {
|
2016-04-05 15:00:03 -05:00
|
|
|
return new LimitRepetition;
|
|
|
|
}
|
2016-01-20 08:21:27 -06:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2016-04-01 09:06:55 -05:00
|
|
|
/**
|
|
|
|
* Returns all expenses for the given budget and the given accounts, in the given period.
|
|
|
|
*
|
|
|
|
* @param Budget $budget
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getExpenses(Budget $budget, Collection $accounts, Carbon $start, Carbon $end):Collection
|
|
|
|
{
|
|
|
|
$ids = $accounts->pluck('id')->toArray();
|
|
|
|
$set = $budget->transactionjournals()
|
|
|
|
->before($end)
|
|
|
|
->after($start)
|
|
|
|
->expanded()
|
2016-04-01 09:23:12 -05:00
|
|
|
->where('transaction_types.type', TransactionType::WITHDRAWAL)
|
2016-04-01 09:06:55 -05:00
|
|
|
->whereIn('source_account.id', $ids)
|
|
|
|
->get(TransactionJournal::QUERYFIELDS);
|
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
|
|
|
|
2016-01-20 08:21:27 -06:00
|
|
|
/**
|
|
|
|
* Returns the expenses for this budget grouped per day, with the date
|
|
|
|
* in "date" (a string, not a Carbon) and the amount in "dailyAmount".
|
|
|
|
*
|
2016-04-24 13:28:08 -05:00
|
|
|
* @param Budget $budget
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param Collection $accounts
|
2016-01-20 08:21:27 -06:00
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-04-24 13:28:08 -05:00
|
|
|
public function getExpensesPerDay(Budget $budget, Carbon $start, Carbon $end, Collection $accounts = null): Collection
|
2016-01-20 08:21:27 -06:00
|
|
|
{
|
2016-04-24 13:28:08 -05:00
|
|
|
$query = $this->user->budgets()
|
|
|
|
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.budget_id', '=', 'budgets.id')
|
|
|
|
->leftJoin('transaction_journals', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
|
|
|
->leftJoin('transactions', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
|
|
|
|
->where('transaction_journals.date', '>=', $start->format('Y-m-d'))
|
|
|
|
->whereNull('transaction_journals.deleted_at')
|
|
|
|
->where('transaction_journals.date', '<=', $end->format('Y-m-d'))
|
|
|
|
->where('budgets.id', $budget->id)
|
|
|
|
->where('transactions.amount', '<', 0)
|
|
|
|
->groupBy('transaction_journals.date')
|
|
|
|
->orderBy('transaction_journals.date');
|
|
|
|
if (!is_null($accounts) && $accounts->count() > 0) {
|
|
|
|
$ids = $accounts->pluck('id')->toArray();
|
|
|
|
$query->whereIn('transactions.account_id', $ids);
|
|
|
|
}
|
|
|
|
$set
|
|
|
|
= $query->get(['transaction_journals.date', DB::raw('SUM(`transactions`.`amount`) as `dailyAmount`')]);
|
2016-01-20 08:21:27 -06:00
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Budget $budget
|
|
|
|
*
|
|
|
|
* @return Carbon
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function getFirstBudgetLimitDate(Budget $budget): Carbon
|
2016-01-20 08:21:27 -06:00
|
|
|
{
|
|
|
|
$limit = $budget->budgetlimits()->orderBy('startdate', 'ASC')->first();
|
|
|
|
if ($limit) {
|
|
|
|
return $limit->startdate;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Carbon::now()->startOfYear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function getInactiveBudgets(): Collection
|
2016-01-20 08:21:27 -06:00
|
|
|
{
|
|
|
|
/** @var Collection $set */
|
2016-03-03 01:44:20 -06:00
|
|
|
$set = $this->user->budgets()->where('active', 0)->get();
|
2016-01-20 08:21:27 -06:00
|
|
|
|
|
|
|
$set = $set->sortBy(
|
|
|
|
function (Budget $budget) {
|
|
|
|
return strtolower($budget->name);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
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.
|
|
|
|
*
|
2015-12-18 09:38:50 -06:00
|
|
|
* @param Budget $budget
|
2015-02-22 08:40:13 -06:00
|
|
|
* @param LimitRepetition $repetition
|
2015-12-18 09:38:50 -06:00
|
|
|
* @param int $take
|
2015-02-22 08:40:13 -06:00
|
|
|
*
|
2015-05-26 13:28:18 -05:00
|
|
|
* @return LengthAwarePaginator
|
2015-02-22 08:40:13 -06:00
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function getJournals(Budget $budget, LimitRepetition $repetition = null, int $take = 50): LengthAwarePaginator
|
2015-02-22 08:40:13 -06:00
|
|
|
{
|
2015-12-18 09:38:50 -06:00
|
|
|
$offset = intval(Input::get('page')) > 0 ? intval(Input::get('page')) * $take : 0;
|
2016-03-02 06:55:48 -06:00
|
|
|
$setQuery = $budget->transactionjournals()->expanded()
|
2016-03-02 13:11:28 -06:00
|
|
|
->take($take)->offset($offset)
|
2015-12-18 09:38:50 -06:00
|
|
|
->orderBy('transaction_journals.date', 'DESC')
|
|
|
|
->orderBy('transaction_journals.order', 'ASC')
|
|
|
|
->orderBy('transaction_journals.id', 'DESC');
|
2016-02-07 00:56:58 -06:00
|
|
|
$countQuery = $budget->transactionjournals();
|
2015-02-22 08:40:13 -06:00
|
|
|
|
|
|
|
|
|
|
|
if (!is_null($repetition->id)) {
|
|
|
|
$setQuery->after($repetition->startdate)->before($repetition->enddate);
|
|
|
|
$countQuery->after($repetition->startdate)->before($repetition->enddate);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-02 06:55:48 -06:00
|
|
|
$set = $setQuery->get(TransactionJournal::QUERYFIELDS);
|
2015-02-22 08:40:13 -06:00
|
|
|
$count = $countQuery->count();
|
2015-03-29 14:27:51 -05:00
|
|
|
|
2015-06-05 12:02:23 -05:00
|
|
|
|
|
|
|
$paginator = new LengthAwarePaginator($set, $count, $take, $offset);
|
|
|
|
|
|
|
|
return $paginator;
|
2015-02-22 08:40:13 -06:00
|
|
|
}
|
|
|
|
|
2016-04-25 14:37:08 -05:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2015-04-05 03:36:28 -05:00
|
|
|
/**
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2016-04-21 02:00:32 -05:00
|
|
|
* @param int $page
|
|
|
|
* @param int $pageSize
|
2015-04-05 03:36:28 -05:00
|
|
|
*
|
2016-04-21 02:00:32 -05:00
|
|
|
* @return LengthAwarePaginator
|
2015-04-05 03:36:28 -05:00
|
|
|
*/
|
2016-04-21 02:00:32 -05:00
|
|
|
public function getWithoutBudget(Carbon $start, Carbon $end, int $page, int $pageSize = 50): LengthAwarePaginator
|
2015-04-05 03:36:28 -05:00
|
|
|
{
|
2016-04-21 02:00:32 -05:00
|
|
|
$offset = ($page - 1) * $pageSize;
|
|
|
|
$query = $this->user
|
2016-03-03 01:44:20 -06:00
|
|
|
->transactionjournals()
|
2016-04-01 09:23:12 -05:00
|
|
|
->expanded()
|
|
|
|
->where('transaction_types.type', TransactionType::WITHDRAWAL)
|
2016-03-03 01:44:20 -06:00
|
|
|
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
|
|
|
->whereNull('budget_transaction_journal.id')
|
|
|
|
->before($end)
|
2016-04-21 02:00:32 -05:00
|
|
|
->after($start);
|
|
|
|
|
|
|
|
$count = $query->count();
|
|
|
|
$set = $query->take($pageSize)->offset($offset)->get(TransactionJournal::QUERYFIELDS);
|
|
|
|
$paginator = new LengthAwarePaginator($set, $count, $pageSize, $page);
|
|
|
|
|
|
|
|
return $paginator;
|
2016-04-01 09:23:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function getWithoutBudgetForAccounts(Collection $accounts, Carbon $start, Carbon $end): Collection
|
2016-04-01 09:23:12 -05:00
|
|
|
{
|
|
|
|
$ids = $accounts->pluck('id')->toArray();
|
|
|
|
|
|
|
|
return $this->user
|
|
|
|
->transactionjournals()
|
|
|
|
->expanded()
|
|
|
|
->whereIn('source_account.id', $ids)
|
|
|
|
->where('transaction_types.type', TransactionType::WITHDRAWAL)
|
|
|
|
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
|
|
|
->whereNull('budget_transaction_journal.id')
|
|
|
|
->before($end)
|
|
|
|
->after($start)
|
|
|
|
->get(TransactionJournal::QUERYFIELDS);
|
2015-04-05 03:36:28 -05:00
|
|
|
}
|
|
|
|
|
2015-04-06 02:15:59 -05:00
|
|
|
/**
|
2016-03-12 00:44:20 -06:00
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
2015-04-06 02:15:59 -05:00
|
|
|
*
|
2016-02-05 02:25:15 -06:00
|
|
|
* @return string
|
2015-04-06 02:15:59 -05:00
|
|
|
*/
|
2016-03-12 00:44:20 -06:00
|
|
|
public function getWithoutBudgetSum(Collection $accounts, Carbon $start, Carbon $end): string
|
2015-04-06 02:15:59 -05:00
|
|
|
{
|
2016-03-12 00:44:20 -06:00
|
|
|
$ids = $accounts->pluck('id')->toArray();
|
2016-03-03 01:44:20 -06:00
|
|
|
$entry = $this->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)
|
|
|
|
->leftJoin(
|
|
|
|
'transactions', function (JoinClause $join) {
|
|
|
|
$join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0);
|
|
|
|
}
|
|
|
|
)
|
2016-03-12 00:44:20 -06:00
|
|
|
->whereIn('transactions.account_id', $ids)
|
2016-03-03 01:44:20 -06:00
|
|
|
->transactionTypes([TransactionType::WITHDRAWAL])
|
|
|
|
->first([DB::raw('SUM(`transactions`.`amount`) as `journalAmount`')]);
|
2016-02-05 02:25:15 -06:00
|
|
|
if (is_null($entry->journalAmount)) {
|
2016-02-24 10:37:08 -06:00
|
|
|
return '0';
|
2016-02-05 02:25:15 -06:00
|
|
|
}
|
2015-12-27 14:17:04 -06:00
|
|
|
|
|
|
|
return $entry->journalAmount;
|
2015-04-06 02:15:59 -05:00
|
|
|
}
|
|
|
|
|
2016-01-01 06:54:23 -06:00
|
|
|
/**
|
|
|
|
* Returns an array with the following key:value pairs:
|
|
|
|
*
|
|
|
|
* yyyy-mm-dd:<array>
|
|
|
|
*
|
|
|
|
* That array contains:
|
|
|
|
*
|
|
|
|
* budgetid:<amount>
|
|
|
|
*
|
|
|
|
* Where yyyy-mm-dd is the date and <amount> is the money spent using WITHDRAWALS in the $budget
|
|
|
|
* from the given users accounts..
|
|
|
|
*
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function spentAllPerDayForAccounts(Collection $accounts, Carbon $start, Carbon $end): array
|
2016-01-01 06:54:23 -06:00
|
|
|
{
|
|
|
|
$ids = $accounts->pluck('id')->toArray();
|
|
|
|
/** @var Collection $query */
|
2016-03-03 01:44:20 -06:00
|
|
|
$query = $this->user->transactionJournals()
|
|
|
|
->transactionTypes([TransactionType::WITHDRAWAL])
|
|
|
|
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
|
|
|
->leftJoin('budget_transaction_journal', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id')
|
|
|
|
->whereIn('transactions.account_id', $ids)
|
|
|
|
->where('transactions.amount', '<', 0)
|
|
|
|
->before($end)
|
|
|
|
->after($start)
|
|
|
|
->groupBy('budget_id')
|
|
|
|
->groupBy('dateFormatted')
|
|
|
|
->get(
|
|
|
|
['transaction_journals.date as dateFormatted', 'budget_transaction_journal.budget_id',
|
|
|
|
DB::raw('SUM(`transactions`.`amount`) AS `sum`')]
|
|
|
|
);
|
2016-01-01 06:54:23 -06:00
|
|
|
|
|
|
|
$return = [];
|
|
|
|
foreach ($query->toArray() as $entry) {
|
|
|
|
$budgetId = $entry['budget_id'];
|
|
|
|
if (!isset($return[$budgetId])) {
|
|
|
|
$return[$budgetId] = [];
|
|
|
|
}
|
|
|
|
$return[$budgetId][$entry['dateFormatted']] = $entry['sum'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
2016-01-01 12:46:12 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of expenses (in the field "spent", grouped per budget per account.
|
|
|
|
*
|
|
|
|
* @param Collection $budgets
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function spentPerBudgetPerAccount(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): Collection
|
2016-01-01 12:46:12 -06:00
|
|
|
{
|
|
|
|
$accountIds = $accounts->pluck('id')->toArray();
|
|
|
|
$budgetIds = $budgets->pluck('id')->toArray();
|
2016-03-03 01:44:20 -06:00
|
|
|
$set = $this->user->transactionjournals()
|
|
|
|
->leftJoin(
|
|
|
|
'transactions AS t_from', function (JoinClause $join) {
|
|
|
|
$join->on('transaction_journals.id', '=', 't_from.transaction_journal_id')->where('t_from.amount', '<', 0);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
->leftJoin(
|
|
|
|
'transactions AS t_to', function (JoinClause $join) {
|
|
|
|
$join->on('transaction_journals.id', '=', 't_to.transaction_journal_id')->where('t_to.amount', '>', 0);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
->leftJoin('budget_transaction_journal', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id')
|
|
|
|
->whereIn('t_from.account_id', $accountIds)
|
|
|
|
->whereNotIn('t_to.account_id', $accountIds)
|
|
|
|
->where(
|
|
|
|
function (Builder $q) use ($budgetIds) {
|
|
|
|
$q->whereIn('budget_transaction_journal.budget_id', $budgetIds);
|
|
|
|
$q->orWhereNull('budget_transaction_journal.budget_id');
|
|
|
|
}
|
|
|
|
)
|
|
|
|
->after($start)
|
|
|
|
->before($end)
|
|
|
|
->groupBy('t_from.account_id')
|
|
|
|
->groupBy('budget_transaction_journal.budget_id')
|
|
|
|
->transactionTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])// opening balance is not an expense.
|
|
|
|
->get(
|
2016-03-02 06:55:48 -06:00
|
|
|
[
|
|
|
|
't_from.account_id', 'budget_transaction_journal.budget_id',
|
|
|
|
DB::raw('SUM(`t_from`.`amount`) AS `spent`'),
|
|
|
|
]
|
|
|
|
);
|
2016-01-01 12:46:12 -06:00
|
|
|
|
|
|
|
return $set;
|
|
|
|
|
|
|
|
}
|
2016-01-20 08:21:27 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2016-02-10 05:01:45 -06:00
|
|
|
public function spentPerDay(Budget $budget, Carbon $start, Carbon $end): array
|
2016-01-20 08:21:27 -06:00
|
|
|
{
|
|
|
|
/** @var Collection $query */
|
2016-02-07 00:56:58 -06:00
|
|
|
$query = $budget->transactionjournals()
|
2016-01-20 08:21:27 -06:00
|
|
|
->transactionTypes([TransactionType::WITHDRAWAL])
|
|
|
|
->leftJoin('transactions', 'transactions.transaction_journal_id', '=', 'transaction_journals.id')
|
|
|
|
->where('transactions.amount', '<', 0)
|
|
|
|
->before($end)
|
|
|
|
->after($start)
|
2016-02-07 00:56:58 -06:00
|
|
|
->groupBy('dateFormatted')->get(['transaction_journals.date as dateFormatted', DB::raw('SUM(`transactions`.`amount`) AS `sum`')]);
|
2016-01-20 08:21:27 -06:00
|
|
|
|
|
|
|
$return = [];
|
|
|
|
foreach ($query->toArray() as $entry) {
|
|
|
|
$return[$entry['dateFormatted']] = $entry['sum'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Budget
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function store(array $data): Budget
|
2016-01-20 08:21:27 -06:00
|
|
|
{
|
|
|
|
$newBudget = new Budget(
|
|
|
|
[
|
|
|
|
'user_id' => $data['user'],
|
|
|
|
'name' => $data['name'],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$newBudget->save();
|
|
|
|
|
|
|
|
return $newBudget;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Budget $budget
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Budget
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function update(Budget $budget, array $data): Budget
|
2016-01-20 08:21:27 -06:00
|
|
|
{
|
|
|
|
// update the account:
|
|
|
|
$budget->name = $data['name'];
|
|
|
|
$budget->active = $data['active'];
|
|
|
|
$budget->save();
|
|
|
|
|
|
|
|
return $budget;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Budget $budget
|
|
|
|
* @param Carbon $date
|
2016-02-05 08:41:40 -06:00
|
|
|
* @param int $amount
|
2016-01-20 08:21:27 -06:00
|
|
|
*
|
|
|
|
* @return BudgetLimit
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function updateLimitAmount(Budget $budget, Carbon $date, int $amount): BudgetLimit
|
2016-01-20 08:21:27 -06:00
|
|
|
{
|
2016-04-25 14:37:08 -05:00
|
|
|
// there might be a budget limit for this startdate:
|
|
|
|
|
|
|
|
$viewRange = Preferences::get('viewRange', '1M')->data;
|
2016-04-26 14:40:15 -05:00
|
|
|
$repeatFreq = config('firefly.range_to_repeat_freq.' . $viewRange);
|
2016-01-20 08:21:27 -06:00
|
|
|
/** @var BudgetLimit $limit */
|
2016-04-25 14:37:08 -05:00
|
|
|
$limit = $budget->budgetlimits()->where('budget_limits.startdate', $date)->where('budget_limits.repeat_freq', $repeatFreq)->first(['budget_limits.*']);
|
2016-01-20 08:21:27 -06:00
|
|
|
|
|
|
|
if (!$limit) {
|
|
|
|
// if not, create one!
|
|
|
|
$limit = new BudgetLimit;
|
|
|
|
$limit->budget()->associate($budget);
|
|
|
|
$limit->startdate = $date;
|
|
|
|
$limit->amount = $amount;
|
2016-04-25 14:37:08 -05:00
|
|
|
$limit->repeat_freq = $repeatFreq;
|
2016-01-20 08:21:27 -06:00
|
|
|
$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.
|
2016-04-25 14:37:08 -05:00
|
|
|
// so handled automatically.
|
2016-01-20 08:21:27 -06:00
|
|
|
|
|
|
|
} else {
|
|
|
|
if ($amount > 0) {
|
|
|
|
$limit->amount = $amount;
|
|
|
|
$limit->save();
|
|
|
|
} else {
|
|
|
|
$limit->delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $limit;
|
|
|
|
}
|
2015-03-29 01:14:32 -05:00
|
|
|
}
|