2015-02-22 02:46:21 -06:00
|
|
|
<?php
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* BudgetRepository.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-04 23:52:15 -05:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-05-20 05:41:23 -05:00
|
|
|
*/
|
|
|
|
|
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;
|
2016-10-22 02:31:27 -05:00
|
|
|
use FireflyIII\Events\StoredBudgetLimit;
|
|
|
|
use FireflyIII\Events\UpdatedBudgetLimit;
|
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;
|
2016-03-03 01:44:20 -06:00
|
|
|
use FireflyIII\User;
|
2016-10-06 14:18:43 -05:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2016-10-06 22:44:21 -05:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2016-05-11 02:08:18 -05:00
|
|
|
use Illuminate\Database\Query\JoinClause;
|
2015-04-05 03:36:28 -05:00
|
|
|
use Illuminate\Support\Collection;
|
2015-02-22 02:46:21 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class BudgetRepository
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Repositories\Budget
|
|
|
|
*/
|
2016-05-05 23:15:46 -05:00
|
|
|
class BudgetRepository 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
|
|
|
|
2016-06-16 13:52:59 -05:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function cleanupBudgets(): bool
|
|
|
|
{
|
|
|
|
// delete limits with amount 0:
|
|
|
|
BudgetLimit::where('amount', 0)->delete();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
|
2016-07-24 11:47:55 -05:00
|
|
|
/**
|
|
|
|
* Find a budget.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return Budget
|
|
|
|
*/
|
|
|
|
public function findByName(string $name): Budget
|
|
|
|
{
|
2016-08-11 03:21:32 -05:00
|
|
|
$budgets = $this->user->budgets()->get(['budgets.*']);
|
2016-07-24 11:47:55 -05:00
|
|
|
/** @var Budget $budget */
|
|
|
|
foreach ($budgets as $budget) {
|
|
|
|
if ($budget->name === $name) {
|
|
|
|
return $budget;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Budget;
|
|
|
|
}
|
|
|
|
|
2016-05-06 03:32:26 -05:00
|
|
|
/**
|
|
|
|
* This method returns the oldest journal or transaction date known to this budget.
|
|
|
|
* Will cache result.
|
|
|
|
*
|
|
|
|
* @param Budget $budget
|
|
|
|
*
|
|
|
|
* @return Carbon
|
|
|
|
*/
|
|
|
|
public function firstUseDate(Budget $budget): Carbon
|
|
|
|
{
|
|
|
|
$oldest = Carbon::create()->startOfYear();
|
2016-08-26 02:30:52 -05:00
|
|
|
$journal = $budget->transactionJournals()->orderBy('date', 'ASC')->first();
|
2016-05-11 10:17:43 -05:00
|
|
|
if (!is_null($journal)) {
|
2016-05-06 03:32:26 -05:00
|
|
|
$oldest = $journal->date < $oldest ? $journal->date : $oldest;
|
|
|
|
}
|
|
|
|
|
|
|
|
$transaction = $budget
|
|
|
|
->transactions()
|
|
|
|
->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.id')
|
|
|
|
->orderBy('transaction_journals.date', 'ASC')->first(['transactions.*', 'transaction_journals.date']);
|
2016-05-11 10:17:43 -05:00
|
|
|
if (!is_null($transaction)) {
|
|
|
|
$carbon = new Carbon($transaction->date);
|
|
|
|
$oldest = $carbon < $oldest ? $carbon : $oldest;
|
2016-05-06 03:32:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return $oldest;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-04-06 02:15:59 -05:00
|
|
|
/**
|
2016-05-06 03:32:26 -05:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
2015-04-06 02:15:59 -05:00
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-05-06 03:32:26 -05:00
|
|
|
public function getAllBudgetLimitRepetitions(Carbon $start, Carbon $end): Collection
|
2015-04-06 02:15:59 -05:00
|
|
|
{
|
2016-05-06 03:32:26 -05:00
|
|
|
$query = 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', $this->user->id);
|
2015-07-09 04:13:38 -05:00
|
|
|
|
2016-05-06 03:32:26 -05:00
|
|
|
$set = $query->get(['limit_repetitions.*', 'budget_limits.budget_id']);
|
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
|
|
|
}
|
|
|
|
|
2016-01-20 08:21:27 -06:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-05-06 03:32:26 -05:00
|
|
|
public function getBudgets(): Collection
|
2016-01-20 08:21:27 -06:00
|
|
|
{
|
|
|
|
/** @var Collection $set */
|
2016-05-06 03:32:26 -05:00
|
|
|
$set = $this->user->budgets()->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
|
|
|
|
2016-05-06 03:32:26 -05:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getInactiveBudgets(): Collection
|
|
|
|
{
|
|
|
|
/** @var Collection $set */
|
|
|
|
$set = $this->user->budgets()->where('active', 0)->get();
|
|
|
|
|
|
|
|
$set = $set->sortBy(
|
|
|
|
function (Budget $budget) {
|
|
|
|
return strtolower($budget->name);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
|
|
|
|
2016-05-05 23:15:46 -05:00
|
|
|
/**
|
|
|
|
* @param Collection $budgets
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function journalsInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): Collection
|
|
|
|
{
|
|
|
|
$return = new Collection;
|
|
|
|
$accountIds = [];
|
2016-05-11 01:40:22 -05:00
|
|
|
// expand the number of grabbed fields:
|
|
|
|
$fields = TransactionJournal::queryFields();
|
|
|
|
$fields[] = 'source.account_id';
|
2016-05-05 23:15:46 -05:00
|
|
|
if ($accounts->count() > 0) {
|
|
|
|
$accountIds = $accounts->pluck('id')->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
// first get all journals for all budget(s):
|
2016-08-26 02:30:52 -05:00
|
|
|
$journalQuery = $this->user->transactionJournals()
|
2016-05-05 23:15:46 -05:00
|
|
|
->expanded()
|
2016-05-11 01:10:05 -05:00
|
|
|
->sortCorrectly()
|
2016-05-11 10:17:43 -05:00
|
|
|
->before($end)
|
2016-05-05 23:15:46 -05:00
|
|
|
->after($start)
|
2016-05-11 02:08:18 -05:00
|
|
|
->leftJoin(
|
|
|
|
'transactions as source',
|
|
|
|
function (JoinClause $join) {
|
|
|
|
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', '0');
|
|
|
|
}
|
|
|
|
)
|
2016-05-05 23:15:46 -05:00
|
|
|
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
|
|
|
->whereIn('budget_transaction_journal.budget_id', $budgets->pluck('id')->toArray());
|
|
|
|
// add account id's, if relevant:
|
|
|
|
if (count($accountIds) > 0) {
|
|
|
|
$journalQuery->whereIn('source.account_id', $accountIds);
|
|
|
|
}
|
|
|
|
// get them:
|
|
|
|
$journals = $journalQuery->get(TransactionJournal::queryFields());
|
2016-05-11 10:17:43 -05:00
|
|
|
|
2016-05-05 23:15:46 -05:00
|
|
|
// then get transactions themselves.
|
2016-08-26 02:30:52 -05:00
|
|
|
$transactionQuery = $this->user->transactionJournals()
|
2016-05-05 23:15:46 -05:00
|
|
|
->expanded()
|
|
|
|
->before($end)
|
2016-05-11 01:10:05 -05:00
|
|
|
->sortCorrectly()
|
2016-05-05 23:15:46 -05:00
|
|
|
->after($start)
|
|
|
|
->leftJoin('transactions as related', 'related.transaction_journal_id', '=', 'transaction_journals.id')
|
|
|
|
->leftJoin('budget_transaction', 'budget_transaction.transaction_id', '=', 'related.id')
|
2016-05-11 02:08:18 -05:00
|
|
|
->leftJoin(
|
|
|
|
'transactions as source',
|
|
|
|
function (JoinClause $join) {
|
|
|
|
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', '0');
|
|
|
|
}
|
|
|
|
)
|
2016-09-16 04:04:24 -05:00
|
|
|
->groupBy(['source.account_id'])
|
2016-05-05 23:15:46 -05:00
|
|
|
->whereIn('budget_transaction.budget_id', $budgets->pluck('id')->toArray());
|
|
|
|
|
|
|
|
if (count($accountIds) > 0) {
|
|
|
|
$transactionQuery->whereIn('source.account_id', $accountIds);
|
|
|
|
}
|
2016-05-11 01:40:22 -05:00
|
|
|
|
|
|
|
$transactions = $transactionQuery->get($fields);
|
2016-05-05 23:15:46 -05:00
|
|
|
|
|
|
|
// return complete set:
|
|
|
|
$return = $return->merge($transactions);
|
|
|
|
$return = $return->merge($journals);
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function journalsInPeriodWithoutBudget(Collection $accounts, Carbon $start, Carbon $end): Collection
|
|
|
|
{
|
2016-05-11 02:08:18 -05:00
|
|
|
$accountIds = [];
|
|
|
|
if ($accounts->count() > 0) {
|
|
|
|
$accountIds = $accounts->pluck('id')->toArray();
|
|
|
|
}
|
|
|
|
|
2016-05-05 23:15:46 -05:00
|
|
|
/** @var Collection $set */
|
2016-05-11 02:08:18 -05:00
|
|
|
$query = $this->user
|
2016-08-26 02:30:52 -05:00
|
|
|
->transactionJournals()
|
2016-05-05 23:15:46 -05:00
|
|
|
->expanded()
|
2016-05-11 01:10:05 -05:00
|
|
|
->sortCorrectly()
|
2016-05-05 23:15:46 -05:00
|
|
|
->transactionTypes([TransactionType::WITHDRAWAL])
|
|
|
|
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
|
|
|
->whereNull('budget_transaction_journal.id')
|
2016-05-11 02:08:18 -05:00
|
|
|
->leftJoin(
|
|
|
|
'transactions as source',
|
|
|
|
function (JoinClause $join) {
|
|
|
|
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', '0');
|
|
|
|
}
|
|
|
|
)
|
2016-05-05 23:15:46 -05:00
|
|
|
->before($end)
|
|
|
|
->after($start)->with(
|
|
|
|
[
|
|
|
|
'transactions' => function (HasMany $query) {
|
|
|
|
$query->where('transactions.amount', '<', 0);
|
|
|
|
},
|
|
|
|
'transactions.budgets',
|
|
|
|
]
|
2016-05-11 02:08:18 -05:00
|
|
|
);
|
2016-05-05 23:15:46 -05:00
|
|
|
|
2016-05-11 02:08:18 -05:00
|
|
|
// add account id's, if relevant:
|
|
|
|
if (count($accountIds) > 0) {
|
|
|
|
$query->whereIn('source.account_id', $accountIds);
|
|
|
|
}
|
|
|
|
|
|
|
|
$set = $query->get(TransactionJournal::queryFields());
|
2016-05-05 23:15:46 -05:00
|
|
|
$set = $set->filter(
|
|
|
|
function (TransactionJournal $journal) {
|
|
|
|
foreach ($journal->transactions as $t) {
|
|
|
|
if ($t->budgets->count() === 0) {
|
2016-08-26 02:30:52 -05:00
|
|
|
return true;
|
2016-05-05 23:15:46 -05:00
|
|
|
}
|
|
|
|
}
|
2016-09-16 04:04:24 -05:00
|
|
|
|
2016-08-26 02:30:52 -05:00
|
|
|
return false;
|
2016-05-05 23:15:46 -05:00
|
|
|
}
|
|
|
|
);
|
2016-05-06 03:32:26 -05:00
|
|
|
|
2016-05-05 23:15:46 -05:00
|
|
|
return $set;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Collection $budgets
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function spentInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end) : string
|
|
|
|
{
|
2016-05-11 10:17:43 -05:00
|
|
|
// first collect actual transaction journals (fairly easy)
|
|
|
|
$query = $this->user
|
2016-08-26 02:30:52 -05:00
|
|
|
->transactionJournals()
|
2016-05-11 10:17:43 -05:00
|
|
|
->leftJoin(
|
2016-05-15 12:23:19 -05:00
|
|
|
'transactions as source', function (JoinClause $join) {
|
|
|
|
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', 0);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
->leftJoin(
|
|
|
|
'transactions as destination', function (JoinClause $join) {
|
|
|
|
$join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0);
|
2016-05-11 10:17:43 -05:00
|
|
|
}
|
|
|
|
);
|
2016-08-02 12:42:41 -05:00
|
|
|
$query->whereNull('source.deleted_at');
|
|
|
|
$query->whereNull('destination.deleted_at');
|
|
|
|
$query->where('transaction_journals.completed', 1);
|
2016-05-11 10:17:43 -05:00
|
|
|
|
|
|
|
if ($end >= $start) {
|
|
|
|
$query->before($end)->after($start);
|
|
|
|
}
|
|
|
|
if ($accounts->count() > 0) {
|
|
|
|
$accountIds = $accounts->pluck('id')->toArray();
|
2016-10-06 14:18:43 -05:00
|
|
|
$query->where(
|
2016-10-06 22:44:21 -05:00
|
|
|
// source.account_id in accountIds XOR destination.account_id in accountIds
|
|
|
|
function (Builder $query) use ($accountIds) {
|
|
|
|
$query->where(
|
|
|
|
function (Builder $q1) use ($accountIds) {
|
|
|
|
$q1->whereIn('source.account_id', $accountIds)
|
|
|
|
->whereNotIn('destination.account_id', $accountIds);
|
2016-10-06 14:18:43 -05:00
|
|
|
}
|
|
|
|
)->orWhere(
|
2016-10-06 22:44:21 -05:00
|
|
|
function (Builder $q2) use ($accountIds) {
|
|
|
|
$q2->whereIn('destination.account_id', $accountIds)
|
|
|
|
->whereNotIn('source.account_id', $accountIds);
|
2016-10-06 14:18:43 -05:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2016-05-11 10:17:43 -05:00
|
|
|
}
|
|
|
|
if ($budgets->count() > 0) {
|
|
|
|
$budgetIds = $budgets->pluck('id')->toArray();
|
|
|
|
$query->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id');
|
|
|
|
$query->whereIn('budget_transaction_journal.budget_id', $budgetIds);
|
2016-08-02 12:42:41 -05:00
|
|
|
|
2016-05-05 23:15:46 -05:00
|
|
|
}
|
|
|
|
|
2016-05-11 10:17:43 -05:00
|
|
|
// that should do it:
|
2016-08-02 12:42:41 -05:00
|
|
|
$ids = $query->distinct()->get(['transaction_journals.id'])->pluck('id')->toArray();
|
|
|
|
$first = '0';
|
|
|
|
if (count($ids) > 0) {
|
|
|
|
$first = strval(
|
|
|
|
$this->user->transactions()
|
|
|
|
->whereIn('transaction_journal_id', $ids)
|
|
|
|
->where('amount', '<', '0')
|
|
|
|
->whereNull('transactions.deleted_at')
|
|
|
|
->sum('amount')
|
|
|
|
);
|
|
|
|
}
|
2016-05-11 10:17:43 -05:00
|
|
|
// then collection transactions (harder)
|
|
|
|
$query = $this->user->transactions()
|
|
|
|
->where('transactions.amount', '<', 0)
|
|
|
|
->where('transaction_journals.date', '>=', $start->format('Y-m-d 00:00:00'))
|
|
|
|
->where('transaction_journals.date', '<=', $end->format('Y-m-d 23:59:59'));
|
|
|
|
if ($accounts->count() > 0) {
|
|
|
|
$accountIds = $accounts->pluck('id')->toArray();
|
|
|
|
$query->whereIn('transactions.account_id', $accountIds);
|
|
|
|
}
|
|
|
|
if ($budgets->count() > 0) {
|
|
|
|
$budgetIds = $budgets->pluck('id')->toArray();
|
|
|
|
$query->leftJoin('budget_transaction', 'budget_transaction.transaction_id', '=', 'transactions.id');
|
|
|
|
$query->whereIn('budget_transaction.budget_id', $budgetIds);
|
|
|
|
}
|
|
|
|
$second = strval($query->sum('transactions.amount'));
|
|
|
|
|
|
|
|
return bcadd($first, $second);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Collection $accounts
|
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function spentInPeriodWithoutBudget(Collection $accounts, Carbon $start, Carbon $end): string
|
|
|
|
{
|
2016-05-16 00:13:54 -05:00
|
|
|
$types = [TransactionType::WITHDRAWAL];
|
2016-08-26 02:30:52 -05:00
|
|
|
$query = $this->user->transactionJournals()
|
2016-05-11 10:17:43 -05:00
|
|
|
->distinct()
|
2016-05-16 00:13:54 -05:00
|
|
|
->transactionTypes($types)
|
2016-05-11 10:17:43 -05:00
|
|
|
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
|
|
|
->leftJoin(
|
2016-05-16 00:13:54 -05:00
|
|
|
'transactions as source', function (JoinClause $join) {
|
|
|
|
$join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', 0);
|
2016-05-11 10:17:43 -05:00
|
|
|
}
|
|
|
|
)
|
2016-05-16 00:13:54 -05:00
|
|
|
->leftJoin(
|
|
|
|
'transactions as destination', function (JoinClause $join) {
|
|
|
|
$join->on('destination.transaction_journal_id', '=', 'transaction_journals.id')->where('destination.amount', '>', 0);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
->leftJoin('budget_transaction', 'source.id', '=', 'budget_transaction.transaction_id')
|
2016-05-11 10:17:43 -05:00
|
|
|
->whereNull('budget_transaction_journal.id')
|
|
|
|
->whereNull('budget_transaction.id')
|
|
|
|
->before($end)
|
2016-08-02 12:42:41 -05:00
|
|
|
->after($start)
|
|
|
|
->whereNull('source.deleted_at')
|
|
|
|
->whereNull('destination.deleted_at')
|
|
|
|
->where('transaction_journals.completed', 1);
|
2016-05-11 10:17:43 -05:00
|
|
|
|
|
|
|
if ($accounts->count() > 0) {
|
|
|
|
$accountIds = $accounts->pluck('id')->toArray();
|
2016-10-06 14:18:43 -05:00
|
|
|
$query->where(
|
2016-10-06 22:44:21 -05:00
|
|
|
// source.account_id in accountIds XOR destination.account_id in accountIds
|
2016-10-06 14:18:43 -05:00
|
|
|
function (Builder $sourceXorDestinationQuery) use ($accountIds) {
|
|
|
|
$sourceXorDestinationQuery->where(
|
|
|
|
function (Builder $inSourceButNotDestinationQuery) use ($accountIds) {
|
|
|
|
$inSourceButNotDestinationQuery->whereIn('source.account_id', $accountIds)
|
|
|
|
->whereNotIn('destination.account_id', $accountIds);
|
|
|
|
}
|
|
|
|
)->orWhere(
|
|
|
|
function (Builder $inDestinationButNotSourceQuery) use ($accountIds) {
|
|
|
|
$inDestinationButNotSourceQuery->whereIn('destination.account_id', $accountIds)
|
|
|
|
->whereNotIn('source.account_id', $accountIds);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2016-05-11 10:17:43 -05:00
|
|
|
}
|
2016-08-02 12:42:41 -05:00
|
|
|
$ids = $query->get(['transaction_journals.id'])->pluck('id')->toArray();
|
|
|
|
$sum = '0';
|
|
|
|
if (count($ids) > 0) {
|
|
|
|
$sum = strval(
|
|
|
|
$this->user->transactions()
|
|
|
|
->whereIn('transaction_journal_id', $ids)
|
|
|
|
->where('amount', '<', '0')
|
|
|
|
->whereNull('transactions.deleted_at')
|
|
|
|
->sum('amount')
|
|
|
|
);
|
|
|
|
}
|
2016-05-06 03:32:26 -05:00
|
|
|
|
2016-05-05 23:15:46 -05:00
|
|
|
return $sum;
|
|
|
|
}
|
2016-01-20 08:21:27 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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(
|
|
|
|
[
|
2016-10-23 05:19:32 -05:00
|
|
|
'user_id' => $this->user->id,
|
2016-01-20 08:21:27 -06:00
|
|
|
'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
|
2016-04-28 03:59:36 -05:00
|
|
|
* @param Carbon $start
|
|
|
|
* @param Carbon $end
|
|
|
|
* @param string $range
|
2016-02-05 08:41:40 -06:00
|
|
|
* @param int $amount
|
2016-01-20 08:21:27 -06:00
|
|
|
*
|
|
|
|
* @return BudgetLimit
|
|
|
|
*/
|
2016-04-28 03:59:36 -05:00
|
|
|
public function updateLimitAmount(Budget $budget, Carbon $start, Carbon $end, string $range, 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:
|
2016-04-28 03:59:36 -05:00
|
|
|
$repeatFreq = config('firefly.range_to_repeat_freq.' . $range);
|
2016-01-20 08:21:27 -06:00
|
|
|
/** @var BudgetLimit $limit */
|
2016-04-28 03:59:36 -05:00
|
|
|
$limit = $budget->budgetlimits()
|
|
|
|
->where('budget_limits.startdate', $start)
|
|
|
|
->where('budget_limits.repeat_freq', $repeatFreq)->first(['budget_limits.*']);
|
|
|
|
|
|
|
|
// delete if amount is zero.
|
|
|
|
if (!is_null($limit) && $amount <= 0.0) {
|
|
|
|
$limit->delete();
|
|
|
|
|
|
|
|
return new BudgetLimit;
|
|
|
|
}
|
|
|
|
// update if exists:
|
|
|
|
if (!is_null($limit)) {
|
|
|
|
$limit->amount = $amount;
|
2016-01-20 08:21:27 -06:00
|
|
|
$limit->save();
|
|
|
|
|
2016-04-28 03:59:36 -05:00
|
|
|
// fire event to create or update LimitRepetition.
|
2016-10-22 02:31:27 -05:00
|
|
|
event(new UpdatedBudgetLimit($limit, $end));
|
2016-01-20 08:21:27 -06:00
|
|
|
|
2016-04-28 03:59:36 -05:00
|
|
|
return $limit;
|
2016-01-20 08:21:27 -06:00
|
|
|
}
|
|
|
|
|
2016-04-28 03:59:36 -05:00
|
|
|
// create one and return it.
|
|
|
|
$limit = new BudgetLimit;
|
|
|
|
$limit->budget()->associate($budget);
|
|
|
|
$limit->startdate = $start;
|
|
|
|
$limit->amount = $amount;
|
|
|
|
$limit->repeat_freq = $repeatFreq;
|
|
|
|
$limit->repeats = 0;
|
|
|
|
$limit->save();
|
2016-10-22 02:31:27 -05:00
|
|
|
event(new StoredBudgetLimit($limit, $end));
|
2016-04-28 03:59:36 -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.
|
|
|
|
// so handled automatically.
|
|
|
|
|
2016-01-20 08:21:27 -06:00
|
|
|
return $limit;
|
|
|
|
}
|
2015-03-29 01:14:32 -05:00
|
|
|
}
|