mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-07 06:31:22 +00:00
More cleanup for budgets.
This commit is contained in:
@@ -4,7 +4,6 @@ declare(strict_types = 1);
|
||||
namespace FireflyIII\Repositories\Budget;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use DB;
|
||||
use FireflyIII\Events\BudgetLimitStored;
|
||||
use FireflyIII\Events\BudgetLimitUpdated;
|
||||
use FireflyIII\Models\Budget;
|
||||
@@ -12,21 +11,18 @@ use FireflyIII\Models\BudgetLimit;
|
||||
use FireflyIII\Models\LimitRepetition;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use FireflyIII\Repositories\Shared\ComponentRepository;
|
||||
use FireflyIII\User;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Query\Builder as QueryBuilder;
|
||||
use Illuminate\Database\Query\JoinClause;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Collection;
|
||||
use Input;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class BudgetRepository
|
||||
*
|
||||
* @package FireflyIII\Repositories\Budget
|
||||
*/
|
||||
class BudgetRepository extends ComponentRepository implements BudgetRepositoryInterface
|
||||
class BudgetRepository implements BudgetRepositoryInterface
|
||||
{
|
||||
/** @var User */
|
||||
private $user;
|
||||
@@ -445,66 +441,66 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
//
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * @param Budget $budget
|
||||
// * @param string $repeatFreq
|
||||
// * @param Carbon $start
|
||||
// * @param Carbon $end
|
||||
// *
|
||||
// * @return LimitRepetition
|
||||
// */
|
||||
// public function getCurrentRepetition(Budget $budget, string $repeatFreq, Carbon $start, Carbon $end): LimitRepetition
|
||||
// {
|
||||
// $data = $budget->limitrepetitions()
|
||||
// ->where('budget_limits.repeat_freq', $repeatFreq)
|
||||
// ->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.*']);
|
||||
// if (is_null($data)) {
|
||||
// return new LimitRepetition;
|
||||
// }
|
||||
//
|
||||
// return $data;
|
||||
// }
|
||||
// /**
|
||||
// * @param Budget $budget
|
||||
// * @param string $repeatFreq
|
||||
// * @param Carbon $start
|
||||
// * @param Carbon $end
|
||||
// *
|
||||
// * @return LimitRepetition
|
||||
// */
|
||||
// public function getCurrentRepetition(Budget $budget, string $repeatFreq, Carbon $start, Carbon $end): LimitRepetition
|
||||
// {
|
||||
// $data = $budget->limitrepetitions()
|
||||
// ->where('budget_limits.repeat_freq', $repeatFreq)
|
||||
// ->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.*']);
|
||||
// if (is_null($data)) {
|
||||
// return new LimitRepetition;
|
||||
// }
|
||||
//
|
||||
// return $data;
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * 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()
|
||||
// ->where('transaction_types.type', TransactionType::WITHDRAWAL)
|
||||
// ->whereIn('source_account.id', $ids)
|
||||
// ->get(TransactionJournal::queryFields());
|
||||
//
|
||||
// return $set;
|
||||
// }
|
||||
// /**
|
||||
// * 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()
|
||||
// ->where('transaction_types.type', TransactionType::WITHDRAWAL)
|
||||
// ->whereIn('source_account.id', $ids)
|
||||
// ->get(TransactionJournal::queryFields());
|
||||
//
|
||||
// return $set;
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * @param Budget $budget
|
||||
// *
|
||||
// * @return Carbon
|
||||
// */
|
||||
// public function getFirstBudgetLimitDate(Budget $budget): Carbon
|
||||
// {
|
||||
// $limit = $budget->budgetlimits()->orderBy('startdate', 'ASC')->first();
|
||||
// if ($limit) {
|
||||
// return $limit->startdate;
|
||||
// }
|
||||
//
|
||||
// return Carbon::now()->startOfYear();
|
||||
// }
|
||||
// /**
|
||||
// * @param Budget $budget
|
||||
// *
|
||||
// * @return Carbon
|
||||
// */
|
||||
// public function getFirstBudgetLimitDate(Budget $budget): Carbon
|
||||
// {
|
||||
// $limit = $budget->budgetlimits()->orderBy('startdate', 'ASC')->first();
|
||||
// if ($limit) {
|
||||
// return $limit->startdate;
|
||||
// }
|
||||
//
|
||||
// return Carbon::now()->startOfYear();
|
||||
// }
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
@@ -523,315 +519,428 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
return $set;
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Returns all the transaction journals for a limit, possibly limited by a limit repetition.
|
||||
// *
|
||||
// * @param Budget $budget
|
||||
// * @param LimitRepetition $repetition
|
||||
// * @param int $take
|
||||
// *
|
||||
// * @return LengthAwarePaginator
|
||||
// */
|
||||
// public function getJournals(Budget $budget, LimitRepetition $repetition = null, int $take = 50): LengthAwarePaginator
|
||||
// {
|
||||
// $offset = intval(Input::get('page')) > 0 ? intval(Input::get('page')) * $take : 0;
|
||||
// $setQuery = $budget->transactionjournals()->expanded()
|
||||
// ->take($take)->offset($offset)
|
||||
// ->orderBy('transaction_journals.date', 'DESC')
|
||||
// ->orderBy('transaction_journals.order', 'ASC')
|
||||
// ->orderBy('transaction_journals.id', 'DESC');
|
||||
// $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(TransactionJournal::queryFields());
|
||||
// $count = $countQuery->count();
|
||||
//
|
||||
//
|
||||
// $paginator = new LengthAwarePaginator($set, $count, $take, $offset);
|
||||
//
|
||||
// return $paginator;
|
||||
// }
|
||||
// /**
|
||||
// * Returns all the transaction journals for a limit, possibly limited by a limit repetition.
|
||||
// *
|
||||
// * @param Budget $budget
|
||||
// * @param LimitRepetition $repetition
|
||||
// * @param int $take
|
||||
// *
|
||||
// * @return LengthAwarePaginator
|
||||
// */
|
||||
// public function getJournals(Budget $budget, LimitRepetition $repetition = null, int $take = 50): LengthAwarePaginator
|
||||
// {
|
||||
// $offset = intval(Input::get('page')) > 0 ? intval(Input::get('page')) * $take : 0;
|
||||
// $setQuery = $budget->transactionjournals()->expanded()
|
||||
// ->take($take)->offset($offset)
|
||||
// ->orderBy('transaction_journals.date', 'DESC')
|
||||
// ->orderBy('transaction_journals.order', 'ASC')
|
||||
// ->orderBy('transaction_journals.id', 'DESC');
|
||||
// $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(TransactionJournal::queryFields());
|
||||
// $count = $countQuery->count();
|
||||
//
|
||||
//
|
||||
// $paginator = new LengthAwarePaginator($set, $count, $take, $offset);
|
||||
//
|
||||
// return $paginator;
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Returns a list of budget limits that are valid in the current given range.
|
||||
// * $ignore is optional. Send an empty limit rep.
|
||||
// *
|
||||
// * @param Budget $budget
|
||||
// * @param Carbon $start
|
||||
// * @param Carbon $end
|
||||
// * @param LimitRepetition $ignore
|
||||
// *
|
||||
// * @return Collection
|
||||
// */
|
||||
// public function getValidRepetitions(Budget $budget, Carbon $start, Carbon $end, LimitRepetition $ignore) : Collection
|
||||
// {
|
||||
// $query = $budget->limitrepetitions()
|
||||
// // starts before start time, and the end also after start time.
|
||||
// ->where('limit_repetitions.enddate', '>=', $start->format('Y-m-d 00:00:00'))
|
||||
// ->where('limit_repetitions.startdate', '<=', $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;
|
||||
// }
|
||||
// /**
|
||||
// * 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()
|
||||
// // starts before start time, and the end also after start time.
|
||||
// ->where('limit_repetitions.enddate', '>=', $start->format('Y-m-d 00:00:00'))
|
||||
// ->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d 00:00:00'));
|
||||
// if (!is_null($ignore->id)) {
|
||||
// $query->where('limit_repetitions.id', '!=', $ignore->id);
|
||||
// }
|
||||
// $data = $query->get(['limit_repetitions.*']);
|
||||
//
|
||||
// return $data;
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * @param Carbon $start
|
||||
// * @param Carbon $end
|
||||
// * @param int $page
|
||||
// * @param int $pageSize
|
||||
// *
|
||||
// * @return LengthAwarePaginator
|
||||
// */
|
||||
// public function getWithoutBudget(Carbon $start, Carbon $end, int $page, int $pageSize = 50): LengthAwarePaginator
|
||||
// {
|
||||
// $offset = ($page - 1) * $pageSize;
|
||||
// $query = $this->user
|
||||
// ->transactionjournals()
|
||||
// ->expanded()
|
||||
// ->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);
|
||||
//
|
||||
// $count = $query->count();
|
||||
// $set = $query->take($pageSize)->offset($offset)->get(TransactionJournal::queryFields());
|
||||
// $paginator = new LengthAwarePaginator($set, $count, $pageSize, $page);
|
||||
//
|
||||
// return $paginator;
|
||||
// }
|
||||
// /**
|
||||
// * @param Carbon $start
|
||||
// * @param Carbon $end
|
||||
// * @param int $page
|
||||
// * @param int $pageSize
|
||||
// *
|
||||
// * @return LengthAwarePaginator
|
||||
// */
|
||||
// public function getWithoutBudget(Carbon $start, Carbon $end, int $page, int $pageSize = 50): LengthAwarePaginator
|
||||
// {
|
||||
// $offset = ($page - 1) * $pageSize;
|
||||
// $query = $this->user
|
||||
// ->transactionjournals()
|
||||
// ->expanded()
|
||||
// ->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);
|
||||
//
|
||||
// $count = $query->count();
|
||||
// $set = $query->take($pageSize)->offset($offset)->get(TransactionJournal::queryFields());
|
||||
// $paginator = new LengthAwarePaginator($set, $count, $pageSize, $page);
|
||||
//
|
||||
// return $paginator;
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * @param Collection $accounts
|
||||
// * @param Carbon $start
|
||||
// * @param Carbon $end
|
||||
// *
|
||||
// * @return Collection
|
||||
// */
|
||||
// public function getWithoutBudgetForAccounts(Collection $accounts, Carbon $start, Carbon $end): Collection
|
||||
// {
|
||||
// $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());
|
||||
// }
|
||||
// /**
|
||||
// * @param Collection $accounts
|
||||
// * @param Carbon $start
|
||||
// * @param Carbon $end
|
||||
// *
|
||||
// * @return Collection
|
||||
// */
|
||||
// public function getWithoutBudgetForAccounts(Collection $accounts, Carbon $start, Carbon $end): Collection
|
||||
// {
|
||||
// $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());
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * @param Collection $accounts
|
||||
// * @param Carbon $start
|
||||
// * @param Carbon $end
|
||||
// *
|
||||
// * @return string
|
||||
// */
|
||||
// public function getWithoutBudgetSum(Collection $accounts, Carbon $start, Carbon $end): string
|
||||
// {
|
||||
// $ids = $accounts->pluck('id')->toArray();
|
||||
// $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);
|
||||
// }
|
||||
// )
|
||||
// ->whereIn('transactions.account_id', $ids)
|
||||
// //->having('transaction_count', '=', 1) TODO check if this still works
|
||||
// ->transactionTypes([TransactionType::WITHDRAWAL])
|
||||
// ->first(
|
||||
// [
|
||||
// DB::raw('SUM(`transactions`.`amount`) as `journalAmount`'),
|
||||
// DB::raw('COUNT(`transactions`.`id`) as `transaction_count`'),
|
||||
// ]
|
||||
// );
|
||||
// if (is_null($entry)) {
|
||||
// return '0';
|
||||
// }
|
||||
// if (is_null($entry->journalAmount)) {
|
||||
// return '0';
|
||||
// }
|
||||
//
|
||||
// return $entry->journalAmount;
|
||||
// }
|
||||
// /**
|
||||
// * @param Collection $accounts
|
||||
// * @param Carbon $start
|
||||
// * @param Carbon $end
|
||||
// *
|
||||
// * @return string
|
||||
// */
|
||||
// public function getWithoutBudgetSum(Collection $accounts, Carbon $start, Carbon $end): string
|
||||
// {
|
||||
// $ids = $accounts->pluck('id')->toArray();
|
||||
// $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);
|
||||
// }
|
||||
// )
|
||||
// ->whereIn('transactions.account_id', $ids)
|
||||
// //->having('transaction_count', '=', 1) TODO check if this still works
|
||||
// ->transactionTypes([TransactionType::WITHDRAWAL])
|
||||
// ->first(
|
||||
// [
|
||||
// DB::raw('SUM(`transactions`.`amount`) as `journalAmount`'),
|
||||
// DB::raw('COUNT(`transactions`.`id`) as `transaction_count`'),
|
||||
// ]
|
||||
// );
|
||||
// if (is_null($entry)) {
|
||||
// return '0';
|
||||
// }
|
||||
// if (is_null($entry->journalAmount)) {
|
||||
// return '0';
|
||||
// }
|
||||
//
|
||||
// return $entry->journalAmount;
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * 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
|
||||
// */
|
||||
// public function spentAllPerDayForAccounts(Collection $accounts, Carbon $start, Carbon $end): array
|
||||
// {
|
||||
// $ids = $accounts->pluck('id')->toArray();
|
||||
// /** @var Collection $query */
|
||||
// $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`')]
|
||||
// );
|
||||
//
|
||||
// $return = [];
|
||||
// foreach ($query->toArray() as $entry) {
|
||||
// $budgetId = $entry['budget_id'];
|
||||
// if (!isset($return[$budgetId])) {
|
||||
// $return[$budgetId] = [];
|
||||
// }
|
||||
// $return[$budgetId][$entry['dateFormatted']] = $entry['sum'];
|
||||
// }
|
||||
//
|
||||
// return $return;
|
||||
// }
|
||||
// /**
|
||||
// * 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
|
||||
// */
|
||||
// public function spentAllPerDayForAccounts(Collection $accounts, Carbon $start, Carbon $end): array
|
||||
// {
|
||||
// $ids = $accounts->pluck('id')->toArray();
|
||||
// /** @var Collection $query */
|
||||
// $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`')]
|
||||
// );
|
||||
//
|
||||
// $return = [];
|
||||
// foreach ($query->toArray() as $entry) {
|
||||
// $budgetId = $entry['budget_id'];
|
||||
// if (!isset($return[$budgetId])) {
|
||||
// $return[$budgetId] = [];
|
||||
// }
|
||||
// $return[$budgetId][$entry['dateFormatted']] = $entry['sum'];
|
||||
// }
|
||||
//
|
||||
// return $return;
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * 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
|
||||
// */
|
||||
// public function spentPerBudgetPerAccount(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): Collection
|
||||
// {
|
||||
// $accountIds = $accounts->pluck('id')->toArray();
|
||||
// $budgetIds = $budgets->pluck('id')->toArray();
|
||||
// $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(
|
||||
// [
|
||||
// 't_from.account_id', 'budget_transaction_journal.budget_id',
|
||||
// DB::raw('SUM(`t_from`.`amount`) AS `spent`'),
|
||||
// ]
|
||||
// );
|
||||
//
|
||||
// return $set;
|
||||
//
|
||||
// }
|
||||
// /**
|
||||
// * 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
|
||||
// */
|
||||
// public function spentPerBudgetPerAccount(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): Collection
|
||||
// {
|
||||
// $accountIds = $accounts->pluck('id')->toArray();
|
||||
// $budgetIds = $budgets->pluck('id')->toArray();
|
||||
// $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(
|
||||
// [
|
||||
// 't_from.account_id', 'budget_transaction_journal.budget_id',
|
||||
// DB::raw('SUM(`t_from`.`amount`) AS `spent`'),
|
||||
// ]
|
||||
// );
|
||||
//
|
||||
// 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
|
||||
// * @param Collection $accounts
|
||||
// *
|
||||
// * @return array
|
||||
// */
|
||||
// public function spentPerDay(Budget $budget, Carbon $start, Carbon $end, Collection $accounts): array
|
||||
// {
|
||||
// /** @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'];
|
||||
// }
|
||||
//
|
||||
// // also search transactions:
|
||||
// $query = $budget->transactions()
|
||||
// ->transactionTypes([TransactionType::WITHDRAWAL])
|
||||
// ->where('transactions.amount', '<', 0)
|
||||
// ->before($end)
|
||||
// ->after($start)
|
||||
// ->groupBy('dateFormatted')->get(['transaction_journals.date as dateFormatted', DB::raw('SUM(`transactions`.`amount`) AS `sum`')]);
|
||||
// foreach ($query as $newEntry) {
|
||||
// // add to return array.
|
||||
// $date = $newEntry['dateFormatted'];
|
||||
// if (isset($return[$date])) {
|
||||
// $return[$date] = bcadd($newEntry['sum'], $return[$date]);
|
||||
// continue;
|
||||
// }
|
||||
//
|
||||
// $return[$date] = $newEntry['sum'];
|
||||
// }
|
||||
//
|
||||
// return $return;
|
||||
// }
|
||||
// /**
|
||||
// * 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
|
||||
// * @param Collection $accounts
|
||||
// *
|
||||
// * @return array
|
||||
// */
|
||||
// public function spentPerDay(Budget $budget, Carbon $start, Carbon $end, Collection $accounts): array
|
||||
// {
|
||||
// /** @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'];
|
||||
// }
|
||||
//
|
||||
// // also search transactions:
|
||||
// $query = $budget->transactions()
|
||||
// ->transactionTypes([TransactionType::WITHDRAWAL])
|
||||
// ->where('transactions.amount', '<', 0)
|
||||
// ->before($end)
|
||||
// ->after($start)
|
||||
// ->groupBy('dateFormatted')->get(['transaction_journals.date as dateFormatted', DB::raw('SUM(`transactions`.`amount`) AS `sum`')]);
|
||||
// foreach ($query as $newEntry) {
|
||||
// // add to return array.
|
||||
// $date = $newEntry['dateFormatted'];
|
||||
// if (isset($return[$date])) {
|
||||
// $return[$date] = bcadd($newEntry['sum'], $return[$date]);
|
||||
// continue;
|
||||
// }
|
||||
//
|
||||
// $return[$date] = $newEntry['sum'];
|
||||
// }
|
||||
//
|
||||
// return $return;
|
||||
// }
|
||||
|
||||
/**
|
||||
* @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 = [];
|
||||
if ($accounts->count() > 0) {
|
||||
$accountIds = $accounts->pluck('id')->toArray();
|
||||
}
|
||||
|
||||
// first get all journals for all budget(s):
|
||||
$journalQuery = $this->user->transactionjournals()
|
||||
->expanded()
|
||||
->before($end)
|
||||
->after($start)
|
||||
->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->leftJoin('transactions as source', 'source.transaction_journal_id', '=', 'transaction_journals.id');
|
||||
$journalQuery->whereIn('source.account_id', $accountIds);
|
||||
}
|
||||
// get them:
|
||||
$journals = $journalQuery->get(TransactionJournal::queryFields());
|
||||
Log::debug('journalsInPeriod journal count is ' . $journals->count());
|
||||
|
||||
// then get transactions themselves.
|
||||
$transactionQuery = $this->user->transactionjournals()
|
||||
->expanded()
|
||||
->before($end)
|
||||
->after($start)
|
||||
->leftJoin('transactions as related', 'related.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->leftJoin('budget_transaction', 'budget_transaction.transaction_id', '=', 'related.id')
|
||||
->whereIn('budget_transaction.budget_id', $budgets->pluck('id')->toArray());
|
||||
|
||||
if (count($accountIds) > 0) {
|
||||
$transactionQuery->leftJoin('transactions as source', 'source.transaction_journal_id', '=', 'transaction_journals.id');
|
||||
$transactionQuery->whereIn('source.account_id', $accountIds);
|
||||
}
|
||||
$transactions = $transactionQuery->get(TransactionJournal::queryFields());
|
||||
|
||||
// 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
|
||||
{
|
||||
/** @var Collection $set */
|
||||
$set = $this->user
|
||||
->transactionjournals()
|
||||
->expanded()
|
||||
->transactionTypes([TransactionType::WITHDRAWAL])
|
||||
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->whereNull('budget_transaction_journal.id')
|
||||
->before($end)
|
||||
->after($start)->with(
|
||||
[
|
||||
'transactions' => function (HasMany $query) {
|
||||
$query->where('transactions.amount', '<', 0);
|
||||
},
|
||||
'transactions.budgets',
|
||||
]
|
||||
)->get(TransactionJournal::queryFields());
|
||||
|
||||
$set = $set->filter(
|
||||
function (TransactionJournal $journal) {
|
||||
foreach ($journal->transactions as $t) {
|
||||
if ($t->budgets->count() === 0) {
|
||||
return $journal;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
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
|
||||
{
|
||||
$set = $this->journalsInPeriod($budgets, $accounts, $start, $end);
|
||||
Log::debug('spentInPeriod set count is ' . $set->count());
|
||||
$sum = '0';
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($set as $journal) {
|
||||
$sum = bcadd($sum, TransactionJournal::amount($journal));
|
||||
}
|
||||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
|
||||
@@ -16,6 +16,13 @@ use Illuminate\Support\Collection;
|
||||
interface BudgetRepositoryInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function destroy(Budget $budget): bool;
|
||||
|
||||
// /**
|
||||
// *
|
||||
// * Same as ::spentInPeriod but corrects journals for a set of accounts
|
||||
@@ -35,11 +42,13 @@ interface BudgetRepositoryInterface
|
||||
// public function cleanupBudgets(): bool;
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
* Find a budget.
|
||||
*
|
||||
* @return bool
|
||||
* @param int $budgetId
|
||||
*
|
||||
* @return Budget
|
||||
*/
|
||||
public function destroy(Budget $budget): bool;
|
||||
public function find(int $budgetId): Budget;
|
||||
|
||||
// /**
|
||||
// * @param Budget $budget
|
||||
@@ -52,13 +61,9 @@ interface BudgetRepositoryInterface
|
||||
// public function expensesSplit(Budget $budget, Account $account, Carbon $start, Carbon $end): Collection;
|
||||
|
||||
/**
|
||||
* Find a budget.
|
||||
*
|
||||
* @param int $budgetId
|
||||
*
|
||||
* @return Budget
|
||||
* @return Collection
|
||||
*/
|
||||
public function find(int $budgetId): Budget;
|
||||
public function getActiveBudgets(): Collection;
|
||||
|
||||
// /**
|
||||
// * @param Budget $budget
|
||||
@@ -67,11 +72,6 @@ interface BudgetRepositoryInterface
|
||||
// */
|
||||
// public function firstActivity(Budget $budget): Carbon;
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getActiveBudgets(): Collection;
|
||||
|
||||
/**
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
@@ -80,6 +80,11 @@ interface BudgetRepositoryInterface
|
||||
*/
|
||||
public function getAllBudgetLimitRepetitions(Carbon $start, Carbon $end): Collection;
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getBudgets(): Collection;
|
||||
|
||||
// /**
|
||||
// * @param Account $account
|
||||
// * @param Collection $accounts
|
||||
@@ -104,7 +109,7 @@ interface BudgetRepositoryInterface
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getBudgets(): Collection;
|
||||
public function getInactiveBudgets(): Collection;
|
||||
|
||||
// /**
|
||||
// * Returns an array with every budget in it and the expenses for each budget
|
||||
@@ -186,9 +191,33 @@ interface BudgetRepositoryInterface
|
||||
// public function getFirstBudgetLimitDate(Budget $budget):Carbon;
|
||||
|
||||
/**
|
||||
* @param Collection $budgets
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getInactiveBudgets(): Collection;
|
||||
public function journalsInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): Collection;
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function journalsInPeriodWithoutBudget(Collection $accounts, Carbon $start, Carbon $end): Collection;
|
||||
|
||||
/**
|
||||
* @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;
|
||||
|
||||
// /**
|
||||
// * Returns all the transaction journals for a limit, possibly limited by a limit repetition.
|
||||
|
||||
Reference in New Issue
Block a user