From 6d944ec98faa348d6df7973d8c4a67714bcc0395 Mon Sep 17 00:00:00 2001 From: James Cole Date: Fri, 6 May 2016 06:15:46 +0200 Subject: [PATCH] More cleanup for budgets. --- app/Helpers/Report/BalanceReportHelper.php | 6 +- app/Helpers/Report/BudgetReportHelper.php | 40 +- app/Http/Controllers/BudgetController.php | 87 +- .../Controllers/Popup/ReportController.php | 13 +- app/Models/LimitRepetition.php | 24 + app/Models/TransactionJournal.php | 4 + app/Repositories/Budget/BudgetRepository.php | 843 ++++++++++-------- .../Budget/BudgetRepositoryInterface.php | 61 +- .../Journal/JournalRepository.php | 4 +- 9 files changed, 641 insertions(+), 441 deletions(-) diff --git a/app/Helpers/Report/BalanceReportHelper.php b/app/Helpers/Report/BalanceReportHelper.php index 472ab2dc88..d36bc46025 100644 --- a/app/Helpers/Report/BalanceReportHelper.php +++ b/app/Helpers/Report/BalanceReportHelper.php @@ -68,8 +68,10 @@ class BalanceReportHelper implements BalanceReportHelperInterface // build a balance header: $header = new BalanceHeader; - $budgets = new Collection;// $this->budgetRepository->getBudgetsAndLimitsInRange($start, $end); // TODO BUDGET getBudgets - $spentData = new Collection; // $this->budgetRepository->spentPerBudgetPerAccount($budgets, $accounts, $start, $end); TODO BUDGET journalsInPeriod + // new Collection;// $this->budgetRepository->getBudgetsAndLimitsInRange($start, $end); // TO DO BUDGET getBudgets + $budgets = $this->budgetRepository->getBudgets(); + // new Collection; // $this->budgetRepository->spentPerBudgetPerAccount($budgets, $accounts, $start, $end); TO DO BUDGET journalsInPeriod + $spentData = $this->budgetRepository->journalsInPeriod($budgets, $accounts, $start, $end); foreach ($accounts as $account) { $header->addAccount($account); } diff --git a/app/Helpers/Report/BudgetReportHelper.php b/app/Helpers/Report/BudgetReportHelper.php index 04124aace4..6f72f68777 100644 --- a/app/Helpers/Report/BudgetReportHelper.php +++ b/app/Helpers/Report/BudgetReportHelper.php @@ -16,8 +16,10 @@ use FireflyIII\Helpers\Collection\Budget as BudgetCollection; use FireflyIII\Helpers\Collection\BudgetLine; use FireflyIII\Models\Budget; use FireflyIII\Models\LimitRepetition; +use FireflyIII\Models\TransactionJournal; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use Illuminate\Support\Collection; +use Log; /** * Class BudgetReportHelper @@ -38,24 +40,21 @@ class BudgetReportHelper implements BudgetReportHelperInterface { $object = new BudgetCollection; /** @var BudgetRepositoryInterface $repository */ - $repository = app(BudgetRepositoryInterface::class); - $set = $repository->getBudgets(); - $allRepetitions = $repository->getAllBudgetLimitRepetitions($start, $end); - $allTotalSpent = '0'; //$repository->spentAllPerDayForAccounts($accounts, $start, $end);// TODO BUDGET MASSIVELY STUPID SPECIFIC METHOD + $repository = app(BudgetRepositoryInterface::class); + $set = $repository->getBudgets(); + /** @var Budget $budget */ foreach ($set as $budget) { - - $repetitions = $allRepetitions->filter( - function (LimitRepetition $rep) use ($budget) { - return $rep->budget_id == $budget->id; - } - ); - $totalSpent = $allTotalSpent[$budget->id] ?? []; + Log::debug('Now at budget #' . $budget->id . ' (' . $budget->name . ')'); + $repetitions = $budget->limitrepetitions()->before($end)->after($start)->get(); // no repetition(s) for this budget: if ($repetitions->count() == 0) { + Log::debug('Found zero repetitions.'); + // spent for budget in time range: + $spent = $repository->spentInPeriod(new Collection([$budget]), $accounts, $start, $end); - $spent = array_sum($totalSpent); + // $spent = array_sum($totalSpent); if ($spent > 0) { $budgetLine = new BudgetLine; $budgetLine->setBudget($budget); @@ -65,18 +64,16 @@ class BudgetReportHelper implements BudgetReportHelperInterface } continue; } - + Log::debug('Found ' . $repetitions->count() . ' repetitions.'); // one or more repetitions for budget: /** @var LimitRepetition $repetition */ foreach ($repetitions as $repetition) { + $budgetLine = new BudgetLine; $budgetLine->setBudget($budget); $budgetLine->setRepetition($repetition); - $expenses = $this->getSumOfRange($repetition->startdate, $repetition->enddate, $totalSpent); - - // 200 en -100 is 100, vergeleken met 0 === 1 - // 200 en -200 is 0, vergeleken met 0 === 0 - // 200 en -300 is -100, vergeleken met 0 === -1 + $expenses = $repository->spentInPeriod(new Collection([$budget]), $accounts, $repetition->startdate, $repetition->enddate); + Log::debug('Spent in p. [' . $repetition->startdate->format('Y-m-d') . '] to [' . $repetition->enddate->format('Y-m-d') . '] is ' . $expenses); $left = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? bcadd($repetition->amount, $expenses) : '0'; $spent = bccomp(bcadd($repetition->amount, $expenses), '0') === 1 ? $expenses : '0'; @@ -98,7 +95,12 @@ class BudgetReportHelper implements BudgetReportHelperInterface } // stuff outside of budgets: - $noBudget = '0'; //$repository->getWithoutBudgetSum($accounts, $start, $end); // TODO BUDGET journalsInPeriodWithoutBudget + $outsideBudget = $repository->journalsInPeriodWithoutBudget($accounts, $start, $end); + $noBudget = '0'; + /** @var TransactionJournal $journal */ + foreach ($outsideBudget as $journal) { + $noBudget = bcadd($noBudget, TransactionJournal::amount($journal)); + } $budgetLine = new BudgetLine; $budgetLine->setOverspent($noBudget); $budgetLine->setSpent($noBudget); diff --git a/app/Http/Controllers/BudgetController.php b/app/Http/Controllers/BudgetController.php index 0436135375..6188775438 100644 --- a/app/Http/Controllers/BudgetController.php +++ b/app/Http/Controllers/BudgetController.php @@ -13,6 +13,7 @@ use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; use Input; +use Log; use Navigation; use Preferences; use Response; @@ -174,6 +175,8 @@ class BudgetController extends Controller $periodStart = $start->formatLocalized($this->monthAndDayFormat); $periodEnd = $end->formatLocalized($this->monthAndDayFormat); $accounts = $accountRepository->getAccounts(['Default account', 'Asset account', 'Cash account']); + $startAsString = $start->format('Y-m-d'); + $endAsString = $end->format('Y-m-d'); /** * Do some cleanup: @@ -183,12 +186,28 @@ class BudgetController extends Controller // loop the budgets: /** @var Budget $budget */ foreach ($budgets as $budget) { - $budget->spent = '0';//$repository->balanceInPeriod($budget, $start, $end, $accounts); // TODO BUDGET spentInPeriod - $budget->currentRep = new LimitRepetition( - ); // $repository->getCurrentRepetition($budget, $repeatFreq, $start, $end); // TODO BUDGET getBudgetLimitRepetitions - $budget->otherRepetitions = new Collection( - );//$repository->getValidRepetitions($budget, $start, $end, $budget->currentRep); // TODO BUDGET getBudgetLimitRepetitions - if (!is_null($budget->currentRep->id)) { + $budget->spent = $repository->spentInPeriod(new Collection([$budget]), $accounts, $start, $end); + $allRepetitions = $repository->getAllBudgetLimitRepetitions($start, $end); + $otherRepetitions = new Collection; + + /** @var LimitRepetition $repetition */ + foreach ($allRepetitions as $repetition) { + if ($repetition->budget_id == $budget->id) { + Log::debug('Repetition #' . $repetition->id . ' with budget #' . $repetition->budget_id . ' (current = ' . $budget->id . ')'); + if ($repetition->budgetLimit->repeat_freq == $repeatFreq + && $repetition->startdate->format('Y-m-d') == $startAsString + && $repetition->enddate->format('Y-m-d') == $endAsString + ) { + // do something + $budget->currentRep = $repetition; + continue; + } + $otherRepetitions->push($repetition); + } + } + $budget->otherRepetitions = $otherRepetitions; + + if (!is_null($budget->currentRep) && !is_null($budget->currentRep->id)) { $budgeted = bcadd($budgeted, $budget->currentRep->amount); } $spent = bcadd($spent, $budget->spent); @@ -223,7 +242,11 @@ class BudgetController extends Controller $page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page')); $pageSize = Preferences::get('transactionPageSize', 50)->data; - $list = new LengthAwarePaginator([], 0, $pageSize); // $repository->getWithoutBudget($start, $end, $page, $pageSize); // TODO BUDGET journalsInPeriodWithoutBudget + $offset = ($page - 1) * $pageSize; + $journals = $repository->journalsInPeriodWithoutBudget(new Collection, $start, $end); + $count = $journals->count(); + $journals = $journals->slice($offset, $pageSize); + $list = new LengthAwarePaginator($journals, $count, $pageSize); $subTitle = trans( 'firefly.without_budget_between', ['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)] @@ -260,25 +283,30 @@ class BudgetController extends Controller */ public function show(BudgetRepositoryInterface $repository, Budget $budget) { - $pageSize = Preferences::get('transactionPageSize', 50)->data; - $journals = new LengthAwarePaginator( - [], 0, $pageSize - ); //$repository->getJournals($budget, new LimitRepetition, $pageSize); // TODO BUDGET journalsInPeriod - $start = new Carbon; //$repository->firstActivity($budget); // TODO BUDGET getOldestJournal + /** @var Carbon $start */ + $start = session('first', Carbon::create()->startOfYear()); $end = new Carbon; + $page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page')); + $pageSize = Preferences::get('transactionPageSize', 50)->data; + $offset = ($page - 1) * $pageSize; + $journals = $repository->journalsInPeriodWithoutBudget(new Collection, $start, $end); + $count = $journals->count(); + $journals = $journals->slice($offset, $pageSize); + $journals = new LengthAwarePaginator($journals, $count, $pageSize); + + $journals->setPath('/budgets/show/' . $budget->id); + + $set = $budget->limitrepetitions()->orderBy('startdate', 'DESC')->get(); $subTitle = e($budget->name); - $journals->setPath('/budgets/show/' . $budget->id); - $spentArray = []; //$repository->spentPerDay($budget, $start, $end, new Collection); // TODO BUDGET spentInPeriod - $limits = new Collection(); + $limits = new Collection(); /** @var LimitRepetition $entry */ foreach ($set as $entry) { - $entry->spent = $this->getSumOfRange($entry->startdate, $entry->enddate, $spentArray); + $entry->spent = $repository->spentInPeriod(new Collection([$budget]), new Collection, $entry->startdate, $entry->enddate); $limits->push($entry); } - return view('budgets.show', compact('limits', 'budget', 'repetition', 'journals', 'subTitle')); } @@ -295,23 +323,24 @@ class BudgetController extends Controller if ($repetition->budgetLimit->budget->id != $budget->id) { throw new FireflyException('This budget limit is not part of this budget.'); } - - $pageSize = Preferences::get('transactionPageSize', 50)->data; - $journals = new LengthAwarePaginator([], 0, $pageSize); // $repository->getJournals($budget, $repetition, $pageSize); // TODO BUDGET journalsInPeriod $start = $repetition->startdate; $end = $repetition->enddate; - $set = new Collection([$repetition]); - $subTitle = trans('firefly.budget_in_month', ['name' => $budget->name, 'month' => $repetition->startdate->formatLocalized($this->monthFormat)]); + $page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page')); + $pageSize = Preferences::get('transactionPageSize', 50)->data; + $offset = ($page - 1) * $pageSize; + $journals = $repository->journalsInPeriodWithoutBudget(new Collection, $start, $end); + $count = $journals->count(); + $journals = $journals->slice($offset, $pageSize); + $journals = new LengthAwarePaginator($journals, $count, $pageSize); + $journals->setPath('/budgets/show/' . $budget->id . '/' . $repetition->id); - $spentArray = []; //$repository->spentPerDay($budget, $start, $end, new Collection); // TODO BUDGET spentInPeriod - $limits = new Collection(); - /** @var LimitRepetition $entry */ - foreach ($set as $entry) { - $entry->spent = $this->getSumOfRange($entry->startdate, $entry->enddate, $spentArray); - $limits->push($entry); - } + $subTitle = trans( + 'firefly.budget_in_month', ['name' => $budget->name, 'month' => $repetition->startdate->formatLocalized($this->monthFormat)] + ); + $repetition->spent = $repository->spentInPeriod(new Collection([$budget]), new Collection, $repetition->startdate, $repetition->enddate); + $limits = new Collection([$repetition]); return view('budgets.show', compact('limits', 'budget', 'repetition', 'journals', 'subTitle')); diff --git a/app/Http/Controllers/Popup/ReportController.php b/app/Http/Controllers/Popup/ReportController.php index 048043c060..cbf4bc3010 100644 --- a/app/Http/Controllers/Popup/ReportController.php +++ b/app/Http/Controllers/Popup/ReportController.php @@ -14,7 +14,6 @@ namespace FireflyIII\Http\Controllers\Popup; use Carbon\Carbon; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Helpers\Collection\BalanceLine; -use FireflyIII\Helpers\Csv\Mapper\Budget; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Models\TransactionJournal; use FireflyIII\Repositories\Account\AccountRepositoryInterface; @@ -94,15 +93,17 @@ class ReportController extends Controller switch (true) { case ($role === BalanceLine::ROLE_DEFAULTROLE && !is_null($budget->id)): - $journals = new Collection;// $budgetRepository->expensesSplit($budget, $account, $attributes['startDate'], $attributes['endDate']);// TODO BUDGET journalsInPeriod + $journals = $budgetRepository->journalsInPeriod( + new Collection([$budget]), new Collection([$account]), $attributes['startDate'], $attributes['endDate'] + ); break; case ($role === BalanceLine::ROLE_DEFAULTROLE && is_null($budget->id)): $budget->name = strval(trans('firefly.no_budget')); - $journals = new Collection;// $budgetRepository->getAllWithoutBudget($account, $attributes['accounts'], $attributes['startDate'], $attributes['endDate']); // TODO BUDGET journalsInPeriodWithoutBudget + $journals = $budgetRepository->journalsInPeriodWithoutBudget($attributes['accounts'], $attributes['startDate'], $attributes['endDate']); break; case ($role === BalanceLine::ROLE_DIFFROLE): // journals no budget, not corrected by a tag. - $journals = new Collection; //$budgetRepository->getAllWithoutBudget($account, $attributes['accounts'], $attributes['startDate'], $attributes['endDate']); // TODO BUDGET journalsInPeriodWithoutBudget + $journals = $budgetRepository->journalsInPeriodWithoutBudget($attributes['accounts'], $attributes['startDate'], $attributes['endDate']); $budget->name = strval(trans('firefly.leftUnbalanced')); $journals = $journals->filter( function (TransactionJournal $journal) { @@ -138,10 +139,10 @@ class ReportController extends Controller $repository = app(BudgetRepositoryInterface::class); $budget = $repository->find(intval($attributes['budgetId'])); if (is_null($budget->id)) { - $journals = new Collection;// $repository->getWithoutBudgetForAccounts($attributes['accounts'], $attributes['startDate'], $attributes['endDate']); // TODO BUDGET journalsInPeriodWithoutBudget + $journals = $repository->journalsInPeriodWithoutBudget($attributes['accounts'], $attributes['startDate'], $attributes['endDate']); } else { // get all expenses in budget in period: - $journals = new Collection; //$repository->getExpenses($budget, $attributes['accounts'], $attributes['startDate'], $attributes['endDate']); // TODO BUDGET journalsInPeriod + $journals = $repository->journalsInPeriod(new Collection([$budget]), $attributes['accounts'], $attributes['startDate'], $attributes['endDate']); } $view = view('popup.report.budget-spent-amount', compact('journals', 'budget'))->render(); diff --git a/app/Models/LimitRepetition.php b/app/Models/LimitRepetition.php index 7257b67689..590e32fca5 100644 --- a/app/Models/LimitRepetition.php +++ b/app/Models/LimitRepetition.php @@ -1,6 +1,8 @@ belongsTo('FireflyIII\Models\BudgetLimit'); } + /** + * + * @param Builder $query + * @param Carbon $date + * + */ + public function scopeAfter(Builder $query, Carbon $date) + { + $query->where('limit_repetitions.startdate', '>=', $date->format('Y-m-d 00:00:00')); + } + + /** + * + * @param Builder $query + * @param Carbon $date + * + */ + public function scopeBefore(Builder $query, Carbon $date) + { + $query->where('limit_repetitions.enddate', '<=', $date->format('Y-m-d 00:00:00')); + } + /** * @param $value */ diff --git a/app/Models/TransactionJournal.php b/app/Models/TransactionJournal.php index 6a16338f95..2e9fe0011f 100644 --- a/app/Models/TransactionJournal.php +++ b/app/Models/TransactionJournal.php @@ -338,6 +338,10 @@ class TransactionJournal extends TransactionJournalSupport $join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '>', 0); } ); + + // order + $query->orderBy('transaction_journals.date', 'DESC')->orderBy('transaction_journals.order', 'ASC')->orderBy('transaction_journals.id', 'DESC'); + $query->groupBy('transaction_journals.id'); $query->with(['categories', 'budgets', 'attachments', 'bill','transactions']); } diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index f55774f5c8..ecc53cdeb8 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -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: -// * -// * That array contains: -// * -// * budgetid: -// * -// * Where yyyy-mm-dd is the date and 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: + // * + // * That array contains: + // * + // * budgetid: + // * + // * Where yyyy-mm-dd is the date and 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: -// * -// * Where yyyy-mm-dd is the date and 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: + // * + // * Where yyyy-mm-dd is the date and 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 diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php index 0ec995f41b..78d35421af 100644 --- a/app/Repositories/Budget/BudgetRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetRepositoryInterface.php @@ -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. diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php index d9ddb99c54..af03b870b3 100644 --- a/app/Repositories/Journal/JournalRepository.php +++ b/app/Repositories/Journal/JournalRepository.php @@ -132,8 +132,8 @@ class JournalRepository implements JournalRepositoryInterface if (count($types) > 0) { $query->transactionTypes($types); } - - $count = $query->count(); + $count = $this->user->transactionJournals()->transactionTypes($types)->count(); + Log::debug('getJournals() count: ' . $count); $set = $query->take($pageSize)->offset($offset)->get(TransactionJournal::queryFields()); $journals = new LengthAwarePaginator($set, $count, $pageSize, $page);