diff --git a/app/Helpers/Report/BalanceReportHelper.php b/app/Helpers/Report/BalanceReportHelper.php index 5c9f08a90a..472ab2dc88 100644 --- a/app/Helpers/Report/BalanceReportHelper.php +++ b/app/Helpers/Report/BalanceReportHelper.php @@ -68,8 +68,8 @@ class BalanceReportHelper implements BalanceReportHelperInterface // build a balance header: $header = new BalanceHeader; - $budgets = $this->budgetRepository->getBudgetsAndLimitsInRange($start, $end); - $spentData = $this->budgetRepository->spentPerBudgetPerAccount($budgets, $accounts, $start, $end); + $budgets = new Collection;// $this->budgetRepository->getBudgetsAndLimitsInRange($start, $end); // TODO BUDGET getBudgets + $spentData = new Collection; // $this->budgetRepository->spentPerBudgetPerAccount($budgets, $accounts, $start, $end); TODO BUDGET journalsInPeriod foreach ($accounts as $account) { $header->addAccount($account); } diff --git a/app/Helpers/Report/BudgetReportHelper.php b/app/Helpers/Report/BudgetReportHelper.php index 8a9de7eaec..04124aace4 100644 --- a/app/Helpers/Report/BudgetReportHelper.php +++ b/app/Helpers/Report/BudgetReportHelper.php @@ -41,7 +41,7 @@ class BudgetReportHelper implements BudgetReportHelperInterface $repository = app(BudgetRepositoryInterface::class); $set = $repository->getBudgets(); $allRepetitions = $repository->getAllBudgetLimitRepetitions($start, $end); - $allTotalSpent = $repository->spentAllPerDayForAccounts($accounts, $start, $end); + $allTotalSpent = '0'; //$repository->spentAllPerDayForAccounts($accounts, $start, $end);// TODO BUDGET MASSIVELY STUPID SPECIFIC METHOD foreach ($set as $budget) { @@ -98,7 +98,7 @@ class BudgetReportHelper implements BudgetReportHelperInterface } // stuff outside of budgets: - $noBudget = $repository->getWithoutBudgetSum($accounts, $start, $end); + $noBudget = '0'; //$repository->getWithoutBudgetSum($accounts, $start, $end); // TODO BUDGET journalsInPeriodWithoutBudget $budgetLine = new BudgetLine; $budgetLine->setOverspent($noBudget); $budgetLine->setSpent($noBudget); @@ -124,7 +124,7 @@ class BudgetReportHelper implements BudgetReportHelperInterface $set = new Collection; /** @var Budget $budget */ foreach ($budgets as $budget) { - $expenses = $repository->spentPerDay($budget, $start, $end, $accounts); + $expenses = [0]; // $repository->spentPerDay($budget, $start, $end, $accounts); // TODO BUDGET spentInPeriod $total = strval(array_sum($expenses)); if (bccomp($total, '0') === -1) { $set->push($budget); diff --git a/app/Http/Controllers/BudgetController.php b/app/Http/Controllers/BudgetController.php index 0fb3acae7c..0436135375 100644 --- a/app/Http/Controllers/BudgetController.php +++ b/app/Http/Controllers/BudgetController.php @@ -10,6 +10,7 @@ use FireflyIII\Models\Budget; use FireflyIII\Models\LimitRepetition; use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; +use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; use Input; use Navigation; @@ -177,14 +178,16 @@ class BudgetController extends Controller /** * Do some cleanup: */ - $repository->cleanupBudgets(); + // $repository->cleanupBudgets(); // loop the budgets: /** @var Budget $budget */ foreach ($budgets as $budget) { - $budget->spent = $repository->balanceInPeriod($budget, $start, $end, $accounts); - $budget->currentRep = $repository->getCurrentRepetition($budget, $repeatFreq, $start, $end); - $budget->otherRepetitions = $repository->getValidRepetitions($budget, $start, $end, $budget->currentRep); + $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)) { $budgeted = bcadd($budgeted, $budget->currentRep->amount); } @@ -220,7 +223,7 @@ class BudgetController extends Controller $page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page')); $pageSize = Preferences::get('transactionPageSize', 50)->data; - $list = $repository->getWithoutBudget($start, $end, $page, $pageSize); + $list = new LengthAwarePaginator([], 0, $pageSize); // $repository->getWithoutBudget($start, $end, $page, $pageSize); // TODO BUDGET journalsInPeriodWithoutBudget $subTitle = trans( 'firefly.without_budget_between', ['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)] @@ -258,13 +261,15 @@ class BudgetController extends Controller public function show(BudgetRepositoryInterface $repository, Budget $budget) { $pageSize = Preferences::get('transactionPageSize', 50)->data; - $journals = $repository->getJournals($budget, new LimitRepetition, $pageSize); - $start = $repository->firstActivity($budget); + $journals = new LengthAwarePaginator( + [], 0, $pageSize + ); //$repository->getJournals($budget, new LimitRepetition, $pageSize); // TODO BUDGET journalsInPeriod + $start = new Carbon; //$repository->firstActivity($budget); // TODO BUDGET getOldestJournal $end = new Carbon; $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); + $spentArray = []; //$repository->spentPerDay($budget, $start, $end, new Collection); // TODO BUDGET spentInPeriod $limits = new Collection(); /** @var LimitRepetition $entry */ @@ -292,13 +297,13 @@ class BudgetController extends Controller } $pageSize = Preferences::get('transactionPageSize', 50)->data; - $journals = $repository->getJournals($budget, $repetition, $pageSize); + $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)]); $journals->setPath('/budgets/show/' . $budget->id . '/' . $repetition->id); - $spentArray = $repository->spentPerDay($budget, $start, $end, new Collection); + $spentArray = []; //$repository->spentPerDay($budget, $start, $end, new Collection); // TODO BUDGET spentInPeriod $limits = new Collection(); /** @var LimitRepetition $entry */ diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php index 66814507ed..f2ed25a9b3 100644 --- a/app/Http/Controllers/Chart/BudgetController.php +++ b/app/Http/Controllers/Chart/BudgetController.php @@ -8,14 +8,9 @@ use FireflyIII\Generator\Chart\Budget\BudgetChartGeneratorInterface; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Models\Budget; use FireflyIII\Models\LimitRepetition; -use FireflyIII\Models\TransactionJournal; use FireflyIII\Repositories\Account\AccountRepositoryInterface as ARI; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; -use FireflyIII\Support\CacheProperties; use Illuminate\Support\Collection; -use Navigation; -use Preferences; -use Response; /** * Class BudgetController @@ -48,45 +43,46 @@ class BudgetController extends Controller */ public function budget(BudgetRepositoryInterface $repository, Budget $budget) { - - // dates and times - $first = $repository->getFirstBudgetLimitDate($budget); - $range = Preferences::get('viewRange', '1M')->data; - $last = session('end', new Carbon); - - // chart properties for cache: - $cache = new CacheProperties(); - $cache->addProperty($first); - $cache->addProperty($last); - $cache->addProperty('budget'); - if ($cache->has()) { - - //return Response::json($cache->get()); - } - - $final = clone $last; - $final->addYears(2); - $last = Navigation::endOfX($last, $range, $final); - $entries = new Collection; - // get all expenses: - $spentArray = $repository->spentPerDay($budget, $first, $last, new Collection); - - while ($first < $last) { - - // periodspecific dates: - $currentStart = Navigation::startOfPeriod($first, $range); - $currentEnd = Navigation::endOfPeriod($first, $range); - $spent = $this->getSumOfRange($currentStart, $currentEnd, $spentArray); - $entry = [$first, ($spent * -1)]; - - $entries->push($entry); - $first = Navigation::addPeriod($first, $range, 0); - } - - $data = $this->generator->budgetLimit($entries, 'month'); - $cache->store($data); - - return Response::json($data); + /** + * // dates and times + * $first = $repository->getFirstBudgetLimitDate($budget); + * $range = Preferences::get('viewRange', '1M')->data; + * $last = session('end', new Carbon); + * + * // chart properties for cache: + * $cache = new CacheProperties(); + * $cache->addProperty($first); + * $cache->addProperty($last); + * $cache->addProperty('budget'); + * if ($cache->has()) { + * + * //return Response::json($cache->get()); + * } + * + * $final = clone $last; + * $final->addYears(2); + * $last = Navigation::endOfX($last, $range, $final); + * $entries = new Collection; + * // get all expenses: + * $spentArray = $repository->spentPerDay($budget, $first, $last, new Collection); + * + * while ($first < $last) { + * + * // periodspecific dates: + * $currentStart = Navigation::startOfPeriod($first, $range); + * $currentEnd = Navigation::endOfPeriod($first, $range); + * $spent = $this->getSumOfRange($currentStart, $currentEnd, $spentArray); + * $entry = [$first, ($spent * -1)]; + * + * $entries->push($entry); + * $first = Navigation::addPeriod($first, $range, 0); + * } + * + * $data = $this->generator->budgetLimit($entries, 'month'); + * $cache->store($data); + * + * return Response::json($data); + * **/ } /** @@ -100,42 +96,42 @@ class BudgetController extends Controller */ public function budgetLimit(BudgetRepositoryInterface $repository, Budget $budget, LimitRepetition $repetition) { - $start = clone $repetition->startdate; - $end = $repetition->enddate; - - // chart properties for cache: - $cache = new CacheProperties(); - $cache->addProperty($start); - $cache->addProperty($end); - $cache->addProperty('budget'); - $cache->addProperty('limit'); - $cache->addProperty($budget->id); - $cache->addProperty($repetition->id); - if ($cache->has()) { - return Response::json($cache->get()); - } - - $set = $repository->spentPerDay($budget, $start, $end, new Collection); - $entries = new Collection; - $amount = $repetition->amount; - - // get sum (har har)! - while ($start <= $end) { - $formatted = $start->format('Y-m-d'); - $sum = $set[$formatted] ?? '0'; - - /* - * Sum of expenses on this day: - */ - $amount = round(bcadd(strval($amount), $sum), 2); - $entries->push([clone $start, $amount]); - $start->addDay(); - } - - $data = $this->generator->budgetLimit($entries, 'monthAndDay'); - $cache->store($data); - - return Response::json($data); + /** + * $start = clone $repetition->startdate; + * $end = $repetition->enddate; + * + * // chart properties for cache: + * $cache = new CacheProperties(); + * $cache->addProperty($start); + * $cache->addProperty($end); + * $cache->addProperty('budget'); + * $cache->addProperty('limit'); + * $cache->addProperty($budget->id); + * $cache->addProperty($repetition->id); + * if ($cache->has()) { + * return Response::json($cache->get()); + * } + * + * $set = $repository->spentPerDay($budget, $start, $end, new Collection); + * $entries = new Collection; + * $amount = $repetition->amount; + * + * // get sum (har har)! + * while ($start <= $end) { + * $formatted = $start->format('Y-m-d'); + * $sum = $set[$formatted] ?? '0'; + * + * // Sum of expenses on this day: + * $amount = round(bcadd(strval($amount), $sum), 2); + * $entries->push([clone $start, $amount]); + * $start->addDay(); + * } + * + * $data = $this->generator->budgetLimit($entries, 'monthAndDay'); + * $cache->store($data); + * + * return Response::json($data); + **/ } @@ -150,75 +146,77 @@ class BudgetController extends Controller */ public function frontpage(BudgetRepositoryInterface $repository, ARI $accountRepository) { - $start = session('start', Carbon::now()->startOfMonth()); - $end = session('end', Carbon::now()->endOfMonth()); - - // chart properties for cache: - $cache = new CacheProperties(); - $cache->addProperty($start); - $cache->addProperty($end); - $cache->addProperty('budget'); - $cache->addProperty('all'); - if ($cache->has()) { - return Response::json($cache->get()); - } - - $budgets = $repository->getBudgetsAndLimitsInRange($start, $end); - $allEntries = new Collection; - $accounts = $accountRepository->getAccounts(['Default account', 'Asset account', 'Cash account']); - $format = strval(trans('config.month_and_day')); - - - /** @var Budget $budget */ - foreach ($budgets as $budget) { - // we already have amount, startdate and enddate. - // if this "is" a limit repetition (as opposed to a budget without one entirely) - // depends on whether startdate and enddate are null. - $name = $budget->name; - if (is_null($budget->startdate) && is_null($budget->enddate)) { - $currentStart = clone $start; - $currentEnd = clone $end; - $expenses = $repository->balanceInPeriod($budget, $currentStart, $currentEnd, $accounts); - $amount = '0'; - $left = '0'; - $spent = $expenses; - $overspent = '0'; - } else { - - // update the display name if the range - // of the limit repetition does not match - // the session's range (for clarity). - if ( - ($start->format('Y-m-d') != $budget->startdate->format('Y-m-d')) - || ($end->format('Y-m-d') != $budget->enddate->format('Y-m-d')) - ) { - $name .= ' ' . trans( - 'firefly.between_dates', - [ - 'start' => $budget->startdate->formatLocalized($format), - 'end' => $budget->startdate->formatLocalized($format), - ] - ); - } - $currentStart = clone $budget->startdate; - $currentEnd = clone $budget->enddate; - $expenses = $repository->balanceInPeriod($budget, $currentStart, $currentEnd, $accounts); - $amount = $budget->amount; - // smaller than 1 means spent MORE than budget allows. - $left = bccomp(bcadd($budget->amount, $expenses), '0') < 1 ? '0' : bcadd($budget->amount, $expenses); - $spent = bccomp(bcadd($budget->amount, $expenses), '0') < 1 ? bcmul($amount, '-1') : $expenses; - $overspent = bccomp(bcadd($budget->amount, $expenses), '0') < 1 ? bcadd($budget->amount, $expenses) : '0'; - } - - $allEntries->push([$name, $left, $spent, $overspent, $amount, $expenses]); - } - - $noBudgetExpenses = $repository->getWithoutBudgetSum($accounts, $start, $end); - $allEntries->push([trans('firefly.no_budget'), '0', '0', $noBudgetExpenses, '0', '0']); - $data = $this->generator->frontpage($allEntries); - $cache->store($data); - - return Response::json($data); + /** + * $start = session('start', Carbon::now()->startOfMonth()); + * $end = session('end', Carbon::now()->endOfMonth()); + * + * // chart properties for cache: + * $cache = new CacheProperties(); + * $cache->addProperty($start); + * $cache->addProperty($end); + * $cache->addProperty('budget'); + * $cache->addProperty('all'); + * if ($cache->has()) { + * return Response::json($cache->get()); + * } + * + * $budgets = $repository->getBudgetsAndLimitsInRange($start, $end); + * $allEntries = new Collection; + * $accounts = $accountRepository->getAccounts(['Default account', 'Asset account', 'Cash account']); + * $format = strval(trans('config.month_and_day')); + * + * + * // @var Budget $budget + * foreach ($budgets as $budget) { + * // we already have amount, startdate and enddate. + * // if this "is" a limit repetition (as opposed to a budget without one entirely) + * // depends on whether startdate and enddate are null. + * $name = $budget->name; + * if (is_null($budget->startdate) && is_null($budget->enddate)) { + * $currentStart = clone $start; + * $currentEnd = clone $end; + * $expenses = $repository->balanceInPeriod($budget, $currentStart, $currentEnd, $accounts); + * $amount = '0'; + * $left = '0'; + * $spent = $expenses; + * $overspent = '0'; + * } else { + * + * // update the display name if the range + * // of the limit repetition does not match + * // the session's range (for clarity). + * if ( + * ($start->format('Y-m-d') != $budget->startdate->format('Y-m-d')) + * || ($end->format('Y-m-d') != $budget->enddate->format('Y-m-d')) + * ) { + * $name .= ' ' . trans( + * 'firefly.between_dates', + * [ + * 'start' => $budget->startdate->formatLocalized($format), + * 'end' => $budget->startdate->formatLocalized($format), + * ] + * ); + * } + * $currentStart = clone $budget->startdate; + * $currentEnd = clone $budget->enddate; + * $expenses = $repository->balanceInPeriod($budget, $currentStart, $currentEnd, $accounts); + * $amount = $budget->amount; + * // smaller than 1 means spent MORE than budget allows. + * $left = bccomp(bcadd($budget->amount, $expenses), '0') < 1 ? '0' : bcadd($budget->amount, $expenses); + * $spent = bccomp(bcadd($budget->amount, $expenses), '0') < 1 ? bcmul($amount, '-1') : $expenses; + * $overspent = bccomp(bcadd($budget->amount, $expenses), '0') < 1 ? bcadd($budget->amount, $expenses) : '0'; + * } + * + * $allEntries->push([$name, $left, $spent, $overspent, $amount, $expenses]); + * } + * + * $noBudgetExpenses = $repository->getWithoutBudgetSum($accounts, $start, $end); + * $allEntries->push([trans('firefly.no_budget'), '0', '0', $noBudgetExpenses, '0', '0']); + * $data = $this->generator->frontpage($allEntries); + * $cache->store($data); + * + * return Response::json($data); + **/ } /** @@ -235,73 +233,72 @@ class BudgetController extends Controller */ public function multiYear(BudgetRepositoryInterface $repository, string $reportType, Carbon $start, Carbon $end, Collection $accounts, Collection $budgets) { - // chart properties for cache: - $cache = new CacheProperties(); - $cache->addProperty($reportType); - $cache->addProperty($start); - $cache->addProperty($end); - $cache->addProperty($accounts); - $cache->addProperty($budgets); - $cache->addProperty('multiYearBudget'); - - if ($cache->has()) { - return Response::json($cache->get()); - } - - /* - * Get the budgeted amounts for each budgets in each year. - */ - $budgetedSet = $repository->getBudgetedPerYear($budgets, $start, $end); - $budgetedArray = []; - /** @var Budget $entry */ - foreach ($budgetedSet as $entry) { - $budgetedArray[$entry->id][$entry->dateFormatted] = $entry->budgeted; - } - - $set = $repository->getBudgetsAndExpensesPerYear($budgets, $accounts, $start, $end); - $entries = new Collection; - // go by budget, not by year. - /** @var Budget $budget */ - foreach ($budgets as $budget) { - $entry = ['name' => '', 'spent' => [], 'budgeted' => []]; - $id = $budget->id; - $currentStart = clone $start; - while ($currentStart < $end) { - // fix the date: - $currentEnd = clone $currentStart; - $currentEnd->endOfYear(); - - // basic information: - $year = $currentStart->year; - $entry['name'] = $budget->name ?? (string)trans('firefly.no_budget'); - $spent = 0; - // this might be a good moment to collect no budget stuff. - if (is_null($budget->id)) { - // get without budget sum in range: - $spent = $repository->getWithoutBudgetSum($accounts, $currentStart, $currentEnd) * -1; - } else { - if (isset($set[$id]['entries'][$year])) { - $spent = $set[$id]['entries'][$year] * -1; - } - } - - $budgeted = $budgetedArray[$id][$year] ?? '0'; - $entry['spent'][$year] = $spent; - $entry['budgeted'][$year] = round($budgeted, 2); - - - // jump to next year. - $currentStart = clone $currentEnd; - $currentStart->addDay(); - } - $entries->push($entry); - } - // generate chart with data: - $data = $this->generator->multiYear($entries); - $cache->store($data); - - return Response::json($data); - + /** + * // chart properties for cache: + * $cache = new CacheProperties(); + * $cache->addProperty($reportType); + * $cache->addProperty($start); + * $cache->addProperty($end); + * $cache->addProperty($accounts); + * $cache->addProperty($budgets); + * $cache->addProperty('multiYearBudget'); + * + * if ($cache->has()) { + * return Response::json($cache->get()); + * } + * + * // Get the budgeted amounts for each budgets in each year. + * $budgetedSet = $repository->getBudgetedPerYear($budgets, $start, $end); + * $budgetedArray = []; + * // @var Budget $entry + * foreach ($budgetedSet as $entry) { + * $budgetedArray[$entry->id][$entry->dateFormatted] = $entry->budgeted; + * } + * + * $set = $repository->getBudgetsAndExpensesPerYear($budgets, $accounts, $start, $end); + * $entries = new Collection; + * // go by budget, not by year. + * // @var Budget $budget + * foreach ($budgets as $budget) { + * $entry = ['name' => '', 'spent' => [], 'budgeted' => []]; + * $id = $budget->id; + * $currentStart = clone $start; + * while ($currentStart < $end) { + * // fix the date: + * $currentEnd = clone $currentStart; + * $currentEnd->endOfYear(); + * + * // basic information: + * $year = $currentStart->year; + * $entry['name'] = $budget->name ?? (string)trans('firefly.no_budget'); + * $spent = 0; + * // this might be a good moment to collect no budget stuff. + * if (is_null($budget->id)) { + * // get without budget sum in range: + * $spent = $repository->getWithoutBudgetSum($accounts, $currentStart, $currentEnd) * -1; + * } else { + * if (isset($set[$id]['entries'][$year])) { + * $spent = $set[$id]['entries'][$year] * -1; + * } + * } + * + * $budgeted = $budgetedArray[$id][$year] ?? '0'; + * $entry['spent'][$year] = $spent; + * $entry['budgeted'][$year] = round($budgeted, 2); + * + * + * // jump to next year. + * $currentStart = clone $currentEnd; + * $currentStart->addDay(); + * } + * $entries->push($entry); + * } + * // generate chart with data: + * $data = $this->generator->multiYear($entries); + * $cache->store($data); + * + * return Response::json($data); + **/ } /** @@ -315,58 +312,60 @@ class BudgetController extends Controller */ public function period(Budget $budget, string $reportType, Carbon $start, Carbon $end, Collection $accounts) { - // chart properties for cache: - $cache = new CacheProperties(); - $cache->addProperty($start); - $cache->addProperty($end); - $cache->addProperty($reportType); - $cache->addProperty($accounts); - $cache->addProperty($budget->id); - $cache->addProperty('budget'); - $cache->addProperty('period'); - if ($cache->has()) { - return Response::json($cache->get()); - } - - /** @var BudgetRepositoryInterface $repository */ - $repository = app(BudgetRepositoryInterface::class); - // loop over period, add by users range: - $current = clone $start; - $viewRange = Preferences::get('viewRange', '1M')->data; - $set = new Collection; - while ($current < $end) { - $currentStart = clone $current; - $currentEnd = Navigation::endOfPeriod($currentStart, $viewRange); - - // get all budget limits and their repetitions. - $reps = $repository->getAllBudgetLimitRepetitions($currentStart, $currentEnd, $budget); - $budgeted = $reps->sum('amount'); - $perBudget = $repository->spentPerBudgetPerAccount(new Collection([$budget]), $accounts, $currentStart, $currentEnd); - // includes null, so filter! - $perBudget = $perBudget->filter( - function (TransactionJournal $journal) use ($budget) { - if (intval($journal->budget_id) === $budget->id) { - return $journal; - } - } - ); - - - $spent = $perBudget->sum('spent'); - - $entry = [ - 'date' => clone $currentStart, - 'budgeted' => $budgeted, - 'spent' => $spent, - ]; - $set->push($entry); - $currentEnd->addDay(); - $current = clone $currentEnd; - } - $data = $this->generator->period($set, $viewRange); - $cache->store($data); - - return Response::json($data); + /** + * // chart properties for cache: + * $cache = new CacheProperties(); + * $cache->addProperty($start); + * $cache->addProperty($end); + * $cache->addProperty($reportType); + * $cache->addProperty($accounts); + * $cache->addProperty($budget->id); + * $cache->addProperty('budget'); + * $cache->addProperty('period'); + * if ($cache->has()) { + * return Response::json($cache->get()); + * } + * + * // @var BudgetRepositoryInterface $repository + * $repository = app(BudgetRepositoryInterface::class); + * // loop over period, add by users range: + * $current = clone $start; + * $viewRange = Preferences::get('viewRange', '1M')->data; + * $set = new Collection; + * while ($current < $end) { + * $currentStart = clone $current; + * $currentEnd = Navigation::endOfPeriod($currentStart, $viewRange); + * + * // get all budget limits and their repetitions. + * $reps = $repository->getAllBudgetLimitRepetitions($currentStart, $currentEnd, $budget); + * $budgeted = $reps->sum('amount'); + * $perBudget = $repository->spentPerBudgetPerAccount(new Collection([$budget]), $accounts, $currentStart, $currentEnd); + * // includes null, so filter! + * $perBudget = $perBudget->filter( + * function (TransactionJournal $journal) use ($budget) { + * if (intval($journal->budget_id) === $budget->id) { + * return $journal; + * } + * } + * ); + * + * + * $spent = $perBudget->sum('spent'); + * + * $entry = [ + * 'date' => clone $currentStart, + * 'budgeted' => $budgeted, + * 'spent' => $spent, + * ]; + * $set->push($entry); + * $currentEnd->addDay(); + * $current = clone $currentEnd; + * } + * $data = $this->generator->period($set, $viewRange); + * $cache->store($data); + * + * return Response::json($data); + */ } @@ -382,52 +381,54 @@ class BudgetController extends Controller */ public function year(BudgetRepositoryInterface $repository, string $reportType, Carbon $start, Carbon $end, Collection $accounts) { - // chart properties for cache: - $cache = new CacheProperties(); - $cache->addProperty($start); - $cache->addProperty($end); - $cache->addProperty($reportType); - $cache->addProperty($accounts); - $cache->addProperty('budget'); - $cache->addProperty('year'); - if ($cache->has()) { - return Response::json($cache->get()); - } - - $budgetInformation = $repository->getBudgetsAndExpensesPerMonth($accounts, $start, $end); - $budgets = new Collection; - $entries = new Collection; - - /** @var array $row */ - foreach ($budgetInformation as $row) { - $budgets->push($row['budget']); - } - while ($start < $end) { - // month is the current end of the period: - $month = clone $start; - $month->endOfMonth(); - $row = [clone $start]; - $dateFormatted = $start->format('Y-m'); - - // each budget, check if there is an entry for this month: - /** @var array $row */ - foreach ($budgetInformation as $budgetRow) { - $spent = 0; // nothing spent. - if (isset($budgetRow['entries'][$dateFormatted])) { - $spent = $budgetRow['entries'][$dateFormatted] * -1; // to fit array - } - $row[] = $spent; - } - - // add "no budget" thing. - $row[] = round(bcmul($repository->getWithoutBudgetSum($accounts, $start, $month), '-1'), 4); - - $entries->push($row); - $start->endOfMonth()->addDay(); - } - $data = $this->generator->year($budgets, $entries); - $cache->store($data); - - return Response::json($data); + /** + * // chart properties for cache: + * $cache = new CacheProperties(); + * $cache->addProperty($start); + * $cache->addProperty($end); + * $cache->addProperty($reportType); + * $cache->addProperty($accounts); + * $cache->addProperty('budget'); + * $cache->addProperty('year'); + * if ($cache->has()) { + * return Response::json($cache->get()); + * } + * + * $budgetInformation = $repository->getBudgetsAndExpensesPerMonth($accounts, $start, $end); + * $budgets = new Collection; + * $entries = new Collection; + * + * // @var array $row + * foreach ($budgetInformation as $row) { + * $budgets->push($row['budget']); + * } + * while ($start < $end) { + * // month is the current end of the period: + * $month = clone $start; + * $month->endOfMonth(); + * $row = [clone $start]; + * $dateFormatted = $start->format('Y-m'); + * + * // each budget, check if there is an entry for this month: + * // @var array $row + * foreach ($budgetInformation as $budgetRow) { + * $spent = 0; // nothing spent. + * if (isset($budgetRow['entries'][$dateFormatted])) { + * $spent = $budgetRow['entries'][$dateFormatted] * -1; // to fit array + * } + * $row[] = $spent; + * } + * + * // add "no budget" thing. + * $row[] = round(bcmul($repository->getWithoutBudgetSum($accounts, $start, $month), '-1'), 4); + * + * $entries->push($row); + * $start->endOfMonth()->addDay(); + * } + * $data = $this->generator->year($budgets, $entries); + * $cache->store($data); + * + * return Response::json($data); + */ } } diff --git a/app/Http/Controllers/Popup/ReportController.php b/app/Http/Controllers/Popup/ReportController.php index 13edc40765..048043c060 100644 --- a/app/Http/Controllers/Popup/ReportController.php +++ b/app/Http/Controllers/Popup/ReportController.php @@ -22,6 +22,7 @@ use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Category\SingleCategoryRepositoryInterface; use FireflyIII\Support\Binder\AccountList; use Illuminate\Http\Request; +use Illuminate\Support\Collection; use InvalidArgumentException; use Response; use View; @@ -93,15 +94,15 @@ class ReportController extends Controller switch (true) { case ($role === BalanceLine::ROLE_DEFAULTROLE && !is_null($budget->id)): - $journals = $budgetRepository->expensesSplit($budget, $account, $attributes['startDate'], $attributes['endDate']); + $journals = new Collection;// $budgetRepository->expensesSplit($budget, $account, $attributes['startDate'], $attributes['endDate']);// TODO BUDGET journalsInPeriod break; case ($role === BalanceLine::ROLE_DEFAULTROLE && is_null($budget->id)): $budget->name = strval(trans('firefly.no_budget')); - $journals = $budgetRepository->getAllWithoutBudget($account, $attributes['accounts'], $attributes['startDate'], $attributes['endDate']); + $journals = new Collection;// $budgetRepository->getAllWithoutBudget($account, $attributes['accounts'], $attributes['startDate'], $attributes['endDate']); // TODO BUDGET journalsInPeriodWithoutBudget break; case ($role === BalanceLine::ROLE_DIFFROLE): // journals no budget, not corrected by a tag. - $journals = $budgetRepository->getAllWithoutBudget($account, $attributes['accounts'], $attributes['startDate'], $attributes['endDate']); + $journals = new Collection; //$budgetRepository->getAllWithoutBudget($account, $attributes['accounts'], $attributes['startDate'], $attributes['endDate']); // TODO BUDGET journalsInPeriodWithoutBudget $budget->name = strval(trans('firefly.leftUnbalanced')); $journals = $journals->filter( function (TransactionJournal $journal) { @@ -137,10 +138,10 @@ class ReportController extends Controller $repository = app(BudgetRepositoryInterface::class); $budget = $repository->find(intval($attributes['budgetId'])); if (is_null($budget->id)) { - $journals = $repository->getWithoutBudgetForAccounts($attributes['accounts'], $attributes['startDate'], $attributes['endDate']); + $journals = new Collection;// $repository->getWithoutBudgetForAccounts($attributes['accounts'], $attributes['startDate'], $attributes['endDate']); // TODO BUDGET journalsInPeriodWithoutBudget } else { // get all expenses in budget in period: - $journals = $repository->getExpenses($budget, $attributes['accounts'], $attributes['startDate'], $attributes['endDate']); + $journals = new Collection; //$repository->getExpenses($budget, $attributes['accounts'], $attributes['startDate'], $attributes['endDate']); // TODO BUDGET journalsInPeriod } $view = view('popup.report.budget-spent-amount', compact('journals', 'budget'))->render(); diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php index c097f4b230..f55774f5c8 100644 --- a/app/Repositories/Budget/BudgetRepository.php +++ b/app/Repositories/Budget/BudgetRepository.php @@ -7,7 +7,6 @@ use Carbon\Carbon; use DB; use FireflyIII\Events\BudgetLimitStored; use FireflyIII\Events\BudgetLimitUpdated; -use FireflyIII\Models\Account; use FireflyIII\Models\Budget; use FireflyIII\Models\BudgetLimit; use FireflyIII\Models\LimitRepetition; @@ -21,7 +20,6 @@ use Illuminate\Database\Query\JoinClause; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; use Input; -use Log; /** * Class BudgetRepository @@ -43,30 +41,30 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn $this->user = $user; } - /** - * @param Budget $budget - * @param Carbon $start - * @param Carbon $end - * @param Collection $accounts - * - * @return string - */ - public function balanceInPeriod(Budget $budget, Carbon $start, Carbon $end, Collection $accounts): string - { - return $this->commonBalanceInPeriod($budget, $start, $end, $accounts); - } + // /** + // * @param Budget $budget + // * @param Carbon $start + // * @param Carbon $end + // * @param Collection $accounts + // * + // * @return string + // */ + // public function balanceInPeriod(Budget $budget, Carbon $start, Carbon $end, Collection $accounts): string + // { + // return $this->commonBalanceInPeriod($budget, $start, $end, $accounts); + // } - /** - * @return bool - */ - public function cleanupBudgets(): bool - { - // delete limits with amount 0: - BudgetLimit::where('amount', 0)->delete(); - - return true; - - } + // /** + // * @return bool + // */ + // public function cleanupBudgets(): bool + // { + // // delete limits with amount 0: + // BudgetLimit::where('amount', 0)->delete(); + // + // return true; + // + // } /** * @param Budget $budget @@ -80,22 +78,22 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn return true; } - /** - * @param Budget $budget - * @param Account $account - * @param Carbon $start - * @param Carbon $end - * - * @return Collection - */ - public function expensesSplit(Budget $budget, Account $account, Carbon $start, Carbon $end): Collection - { - return $budget->transactionjournals()->expanded() - ->before($end) - ->after($start) - ->where('source_account.id', $account->id) - ->get(TransactionJournal::queryFields()); - } + // /** + // * @param Budget $budget + // * @param Account $account + // * @param Carbon $start + // * @param Carbon $end + // * + // * @return Collection + // */ + // public function expensesSplit(Budget $budget, Account $account, Carbon $start, Carbon $end): Collection + // { + // return $budget->transactionjournals()->expanded() + // ->before($end) + // ->after($start) + // ->where('source_account.id', $account->id) + // ->get(TransactionJournal::queryFields()); + // } /** * Find a budget. @@ -114,20 +112,20 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn return $budget; } - /** - * @param Budget $budget - * - * @return Carbon - */ - public function firstActivity(Budget $budget): Carbon - { - $first = $budget->transactionjournals()->orderBy('date', 'ASC')->first(); - if ($first) { - return $first->date; - } - - return new Carbon; - } + // /** + // * @param Budget $budget + // * + // * @return Carbon + // */ + // public function firstActivity(Budget $budget): Carbon + // { + // $first = $budget->transactionjournals()->orderBy('date', 'ASC')->first(); + // if ($first) { + // return $first->date; + // } + // + // return new Carbon; + // } /** * @return Collection @@ -149,11 +147,10 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn /** * @param Carbon $start * @param Carbon $end - * @param Budget $budget * * @return Collection */ - public function getAllBudgetLimitRepetitions(Carbon $start, Carbon $end, Budget $budget = null): Collection + public function getAllBudgetLimitRepetitions(Carbon $start, Carbon $end): Collection { $query = LimitRepetition:: leftJoin('budget_limits', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id') @@ -162,70 +159,66 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn ->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d 00:00:00')) ->where('budgets.user_id', $this->user->id); - if (!is_null($budget)) { - $query->where('budgets.id', $budget->id); - } - $set = $query->get(['limit_repetitions.*', 'budget_limits.budget_id']); return $set; } - /** - * @param Account $account - * @param Carbon $start - * @param Carbon $end - * @param Collection $accounts - * - * @return Collection - */ - public function getAllWithoutBudget(Account $account, Collection $accounts, Carbon $start, Carbon $end): Collection - { - $ids = $accounts->pluck('id')->toArray(); + // /** + // * @param Account $account + // * @param Carbon $start + // * @param Carbon $end + // * @param Collection $accounts + // * + // * @return Collection + // */ + // public function getAllWithoutBudget(Account $account, Collection $accounts, Carbon $start, Carbon $end): Collection + // { + // $ids = $accounts->pluck('id')->toArray(); + // + // return $this->user + // ->transactionjournals() + // ->expanded() + // ->where('source_account.id', $account->id) + // ->whereNotIn('destination_account.id', $ids) + // ->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') + // ->whereNull('budget_transaction_journal.id') + // ->before($end) + // ->after($start) + // ->get(TransactionJournal::queryFields()); + // } - return $this->user - ->transactionjournals() - ->expanded() - ->where('source_account.id', $account->id) - ->whereNotIn('destination_account.id', $ids) - ->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id') - ->whereNull('budget_transaction_journal.id') - ->before($end) - ->after($start) - ->get(TransactionJournal::queryFields()); - } - - /** - * Get the budgeted amounts for each budgets in each year. - * - * @param Collection $budgets - * @param Carbon $start - * @param Carbon $end - * - * @return Collection - */ - public function getBudgetedPerYear(Collection $budgets, Carbon $start, Carbon $end): Collection - { - $budgetIds = $budgets->pluck('id')->toArray(); - - $set = $this->user->budgets() - ->leftJoin('budget_limits', 'budgets.id', '=', 'budget_limits.budget_id') - ->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id') - ->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d')) - ->where('limit_repetitions.enddate', '<=', $end->format('Y-m-d')) - ->groupBy('budgets.id') - ->groupBy('dateFormatted') - ->whereIn('budgets.id', $budgetIds) - ->get( - [ - 'budgets.*', - DB::raw('DATE_FORMAT(`limit_repetitions`.`startdate`,"%Y") as `dateFormatted`'), - DB::raw('SUM(`limit_repetitions`.`amount`) as `budgeted`'), - ] - ); - - return $set; - } + // /** + // * Get the budgeted amounts for each budgets in each year. + // * + // * @param Collection $budgets + // * @param Carbon $start + // * @param Carbon $end + // * + // * @return Collection + // */ + // public function getBudgetedPerYear(Collection $budgets, Carbon $start, Carbon $end): Collection + // { + // $budgetIds = $budgets->pluck('id')->toArray(); + // + // $set = $this->user->budgets() + // ->leftJoin('budget_limits', 'budgets.id', '=', 'budget_limits.budget_id') + // ->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id') + // ->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d')) + // ->where('limit_repetitions.enddate', '<=', $end->format('Y-m-d')) + // ->groupBy('budgets.id') + // ->groupBy('dateFormatted') + // ->whereIn('budgets.id', $budgetIds) + // ->get( + // [ + // 'budgets.*', + // DB::raw('DATE_FORMAT(`limit_repetitions`.`startdate`,"%Y") as `dateFormatted`'), + // DB::raw('SUM(`limit_repetitions`.`amount`) as `budgeted`'), + // ] + // ); + // + // return $set; + // } /** * @return Collection @@ -244,274 +237,274 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn return $set; } - /** - * Returns an array with every budget in it and the expenses for each budget - * per month. - * - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return array - */ - public function getBudgetsAndExpensesPerMonth(Collection $accounts, Carbon $start, Carbon $end): array - { - $ids = $accounts->pluck('id')->toArray(); + // /** + // * Returns an array with every budget in it and the expenses for each budget + // * per month. + // * + // * @param Collection $accounts + // * @param Carbon $start + // * @param Carbon $end + // * + // * @return array + // */ + // public function getBudgetsAndExpensesPerMonth(Collection $accounts, Carbon $start, Carbon $end): array + // { + // $ids = $accounts->pluck('id')->toArray(); + // + // /** @var Collection $set */ + // $set = $this->user->budgets() + // ->leftJoin('budget_transaction_journal', 'budgets.id', '=', 'budget_transaction_journal.budget_id') + // ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id') + // ->leftJoin( + // 'transactions', function (JoinClause $join) { + // $join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0); + // } + // ) + // ->groupBy('budgets.id') + // ->groupBy('dateFormatted') + // ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) + // ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) + // ->whereIn('transactions.account_id', $ids) + // ->get( + // [ + // 'budgets.*', + // DB::raw('DATE_FORMAT(`transaction_journals`.`date`, "%Y-%m") AS `dateFormatted`'), + // DB::raw('SUM(`transactions`.`amount`) AS `sumAmount`'), + // ] + // ); + // + // $set = $set->sortBy( + // function (Budget $budget) { + // return strtolower($budget->name); + // } + // ); + // + // $return = []; + // foreach ($set as $budget) { + // $id = $budget->id; + // if (!isset($return[$id])) { + // $return[$id] = [ + // 'budget' => $budget, + // 'entries' => [], + // ]; + // } + // // store each entry: + // $return[$id]['entries'][$budget->dateFormatted] = $budget->sumAmount; + // } + // + // return $return; + // } - /** @var Collection $set */ - $set = $this->user->budgets() - ->leftJoin('budget_transaction_journal', 'budgets.id', '=', 'budget_transaction_journal.budget_id') - ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id') - ->leftJoin( - 'transactions', function (JoinClause $join) { - $join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0); - } - ) - ->groupBy('budgets.id') - ->groupBy('dateFormatted') - ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) - ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) - ->whereIn('transactions.account_id', $ids) - ->get( - [ - 'budgets.*', - DB::raw('DATE_FORMAT(`transaction_journals`.`date`, "%Y-%m") AS `dateFormatted`'), - DB::raw('SUM(`transactions`.`amount`) AS `sumAmount`'), - ] - ); + // /** + // * Returns an array with every budget in it and the expenses for each budget + // * per year for. + // * + // * @param Collection $budgets + // * @param Collection $accounts + // * @param Carbon $start + // * @param Carbon $end + // * + // * @deprecated + // * + // * @return array + // */ + // public function getBudgetsAndExpensesPerYear(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array + // { + // // get budgets, + // $ids = $accounts->pluck('id')->toArray(); + // $budgetIds = $budgets->pluck('id')->toArray(); + // + // /** @var Collection $set */ + // $set = $this->user->budgets() + // ->join('budget_transaction_journal', 'budgets.id', '=', 'budget_transaction_journal.budget_id') + // ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id') + // ->leftJoin( + // 'transactions', function (JoinClause $join) { + // $join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0); + // } + // ) + // ->groupBy('budgets.id') + // ->groupBy('dateFormatted') + // ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) + // ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) + // ->whereIn('transactions.account_id', $ids) + // ->whereIn('budgets.id', $budgetIds) + // ->get( + // [ + // 'budgets.*', + // DB::raw('DATE_FORMAT(`transaction_journals`.`date`, "%Y") AS `dateFormatted`'), + // DB::raw('SUM(`transactions`.`amount`) AS `sumAmount`'), + // ] + // ); + // + // // run it again, for transactions this time. + // /** @var Collection $secondSet */ + // $secondSet = $this->user->budgets() + // ->join('budget_transaction', 'budgets.id', '=', 'budget_transaction.budget_id') + // ->leftJoin('transactions', 'transactions.id', '=', 'budget_transaction.transaction_id') + // ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') + // ->where('transactions.amount', '<', 0) + // ->groupBy('budgets.id') + // ->groupBy('dateFormatted') + // ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) + // ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) + // ->whereIn('transactions.account_id', $ids) + // ->whereIn('budgets.id', $budgetIds) + // ->get( + // [ + // 'budgets.*', + // DB::raw('DATE_FORMAT(`transaction_journals`.`date`, "%Y") AS `dateFormatted`'), + // DB::raw('SUM(`transactions`.`amount`) AS `sumAmount`'), + // ] + // ); + // + // $set = $set->sortBy( + // function (Budget $budget) { + // return strtolower($budget->name); + // } + // ); + // + // $return = []; + // foreach ($set as $budget) { + // Log::debug('First set, budget #' . $budget->id . ' (' . $budget->name . ')'); + // $id = $budget->id; + // if (!isset($return[$id])) { + // Log::debug('$return[$id] is not set, now created.'); + // $return[$id] = [ + // 'budget' => $budget, + // 'entries' => [], + // ]; + // } + // Log::debug('Add new entry to entries, for ' . $budget->dateFormatted . ' and amount ' . $budget->sumAmount); + // // store each entry: + // $return[$id]['entries'][$budget->dateFormatted] = $budget->sumAmount; + // } + // unset($budget); + // + // // run the second set: + // foreach ($secondSet as $entry) { + // $id = $entry->id; + // // create it if it still does not exist (not really likely) + // if (!isset($return[$id])) { + // $return[$id] = [ + // 'budget' => $entry, + // 'entries' => [], + // ]; + // } + // // this one might be filled too: + // $startAmount = $return[$id]['entries'][$entry->dateFormatted] ?? '0'; + // // store each entry: + // $return[$id]['entries'][$entry->dateFormatted] = bcadd($startAmount, $entry->sumAmount); + // } + // + // return $return; + // } - $set = $set->sortBy( - function (Budget $budget) { - return strtolower($budget->name); - } - ); + // /** + // * Returns a list of budgets, budget limits and limit repetitions + // * (doubling any of them in a left join) + // * + // * @param Carbon $start + // * @param Carbon $end + // * + // * @return Collection + // */ + // public function getBudgetsAndLimitsInRange(Carbon $start, Carbon $end): Collection + // { + // /** @var Collection $set */ + // $set = $this->user + // ->budgets() + // ->leftJoin('budget_limits', 'budget_limits.budget_id', '=', 'budgets.id') + // ->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id') + // ->where( + // function (Builder $query) use ($start, $end) { + // $query->where( + // function (Builder $query) use ($start, $end) { + // $query->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d')); + // $query->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d')); + // } + // ); + // $query->orWhere( + // function (Builder $query) { + // $query->whereNull('limit_repetitions.startdate'); + // $query->whereNull('limit_repetitions.enddate'); + // } + // ); + // } + // ) + // ->orderBy('budgets.id', 'budget_limits.startdate', 'limit_repetitions.enddate') + // ->get(['budgets.*', 'limit_repetitions.startdate', 'limit_repetitions.enddate', 'limit_repetitions.amount']); + // + // $set = $set->sortBy( + // function (Budget $budget) { + // return strtolower($budget->name); + // } + // ); + // + // return $set; + // + // } - $return = []; - foreach ($set as $budget) { - $id = $budget->id; - if (!isset($return[$id])) { - $return[$id] = [ - 'budget' => $budget, - 'entries' => [], - ]; - } - // store each entry: - $return[$id]['entries'][$budget->dateFormatted] = $budget->sumAmount; - } +// /** +// * @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; +// } - return $return; - } +// /** +// * 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 an array with every budget in it and the expenses for each budget - * per year for. - * - * @param Collection $budgets - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @deprecated - * - * @return array - */ - public function getBudgetsAndExpensesPerYear(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array - { - // get budgets, - $ids = $accounts->pluck('id')->toArray(); - $budgetIds = $budgets->pluck('id')->toArray(); - - /** @var Collection $set */ - $set = $this->user->budgets() - ->join('budget_transaction_journal', 'budgets.id', '=', 'budget_transaction_journal.budget_id') - ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'budget_transaction_journal.transaction_journal_id') - ->leftJoin( - 'transactions', function (JoinClause $join) { - $join->on('transactions.transaction_journal_id', '=', 'transaction_journals.id')->where('transactions.amount', '<', 0); - } - ) - ->groupBy('budgets.id') - ->groupBy('dateFormatted') - ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) - ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) - ->whereIn('transactions.account_id', $ids) - ->whereIn('budgets.id', $budgetIds) - ->get( - [ - 'budgets.*', - DB::raw('DATE_FORMAT(`transaction_journals`.`date`, "%Y") AS `dateFormatted`'), - DB::raw('SUM(`transactions`.`amount`) AS `sumAmount`'), - ] - ); - - // run it again, for transactions this time. - /** @var Collection $secondSet */ - $secondSet = $this->user->budgets() - ->join('budget_transaction', 'budgets.id', '=', 'budget_transaction.budget_id') - ->leftJoin('transactions', 'transactions.id', '=', 'budget_transaction.transaction_id') - ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') - ->where('transactions.amount', '<', 0) - ->groupBy('budgets.id') - ->groupBy('dateFormatted') - ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) - ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) - ->whereIn('transactions.account_id', $ids) - ->whereIn('budgets.id', $budgetIds) - ->get( - [ - 'budgets.*', - DB::raw('DATE_FORMAT(`transaction_journals`.`date`, "%Y") AS `dateFormatted`'), - DB::raw('SUM(`transactions`.`amount`) AS `sumAmount`'), - ] - ); - - $set = $set->sortBy( - function (Budget $budget) { - return strtolower($budget->name); - } - ); - - $return = []; - foreach ($set as $budget) { - Log::debug('First set, budget #' . $budget->id . ' (' . $budget->name . ')'); - $id = $budget->id; - if (!isset($return[$id])) { - Log::debug('$return[$id] is not set, now created.'); - $return[$id] = [ - 'budget' => $budget, - 'entries' => [], - ]; - } - Log::debug('Add new entry to entries, for ' . $budget->dateFormatted . ' and amount ' . $budget->sumAmount); - // store each entry: - $return[$id]['entries'][$budget->dateFormatted] = $budget->sumAmount; - } - unset($budget); - - // run the second set: - foreach ($secondSet as $entry) { - $id = $entry->id; - // create it if it still does not exist (not really likely) - if (!isset($return[$id])) { - $return[$id] = [ - 'budget' => $entry, - 'entries' => [], - ]; - } - // this one might be filled too: - $startAmount = $return[$id]['entries'][$entry->dateFormatted] ?? '0'; - // store each entry: - $return[$id]['entries'][$entry->dateFormatted] = bcadd($startAmount, $entry->sumAmount); - } - - return $return; - } - - /** - * Returns a list of budgets, budget limits and limit repetitions - * (doubling any of them in a left join) - * - * @param Carbon $start - * @param Carbon $end - * - * @return Collection - */ - public function getBudgetsAndLimitsInRange(Carbon $start, Carbon $end): Collection - { - /** @var Collection $set */ - $set = $this->user - ->budgets() - ->leftJoin('budget_limits', 'budget_limits.budget_id', '=', 'budgets.id') - ->leftJoin('limit_repetitions', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id') - ->where( - function (Builder $query) use ($start, $end) { - $query->where( - function (Builder $query) use ($start, $end) { - $query->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d')); - $query->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d')); - } - ); - $query->orWhere( - function (Builder $query) { - $query->whereNull('limit_repetitions.startdate'); - $query->whereNull('limit_repetitions.enddate'); - } - ); - } - ) - ->orderBy('budgets.id', 'budget_limits.startdate', 'limit_repetitions.enddate') - ->get(['budgets.*', 'limit_repetitions.startdate', 'limit_repetitions.enddate', 'limit_repetitions.amount']); - - $set = $set->sortBy( - function (Budget $budget) { - return strtolower($budget->name); - } - ); - - return $set; - - } - - /** - * @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; - } - - /** - * @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 @@ -530,315 +523,315 @@ 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(); +// /** +// * 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; +// } - if (!is_null($repetition->id)) { - $setQuery->after($repetition->startdate)->before($repetition->enddate); - $countQuery->after($repetition->startdate)->before($repetition->enddate); - } +// /** +// * @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()); +// } - $set = $setQuery->get(TransactionJournal::queryFields()); - $count = $countQuery->count(); +// /** +// * @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; +// } - $paginator = new LengthAwarePaginator($set, $count, $take, $offset); +// /** +// * 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; +// +// } - 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; - } - - /** - * @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 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 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 array $data diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php index 577b5e18aa..0ec995f41b 100644 --- a/app/Repositories/Budget/BudgetRepositoryInterface.php +++ b/app/Repositories/Budget/BudgetRepositoryInterface.php @@ -4,11 +4,8 @@ declare(strict_types = 1); namespace FireflyIII\Repositories\Budget; use Carbon\Carbon; -use FireflyIII\Models\Account; use FireflyIII\Models\Budget; use FireflyIII\Models\BudgetLimit; -use FireflyIII\Models\LimitRepetition; -use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; /** @@ -19,23 +16,23 @@ use Illuminate\Support\Collection; interface BudgetRepositoryInterface { - /** - * - * Same as ::spentInPeriod but corrects journals for a set of accounts - * - * @param Budget $budget - * @param Carbon $start - * @param Carbon $end - * @param Collection $accounts - * - * @return string - */ - public function balanceInPeriod(Budget $budget, Carbon $start, Carbon $end, Collection $accounts); + // /** + // * + // * Same as ::spentInPeriod but corrects journals for a set of accounts + // * + // * @param Budget $budget + // * @param Carbon $start + // * @param Carbon $end + // * @param Collection $accounts + // * + // * @return string + // */ + // public function balanceInPeriod(Budget $budget, Carbon $start, Carbon $end, Collection $accounts); - /** - * @return bool - */ - public function cleanupBudgets(): bool; + // /** + // * @return bool + // */ + // public function cleanupBudgets(): bool; /** * @param Budget $budget @@ -44,15 +41,15 @@ interface BudgetRepositoryInterface */ public function destroy(Budget $budget): bool; - /** - * @param Budget $budget - * @param Account $account - * @param Carbon $start - * @param Carbon $end - * - * @return Collection - */ - public function expensesSplit(Budget $budget, Account $account, Carbon $start, Carbon $end): Collection; + // /** + // * @param Budget $budget + // * @param Account $account + // * @param Carbon $start + // * @param Carbon $end + // * + // * @return Collection + // */ + // public function expensesSplit(Budget $budget, Account $account, Carbon $start, Carbon $end): Collection; /** * Find a budget. @@ -63,12 +60,12 @@ interface BudgetRepositoryInterface */ public function find(int $budgetId): Budget; - /** - * @param Budget $budget - * - * @return Carbon - */ - public function firstActivity(Budget $budget): Carbon; + // /** + // * @param Budget $budget + // * + // * @return Carbon + // */ + // public function firstActivity(Budget $budget): Carbon; /** * @return Collection @@ -78,209 +75,208 @@ interface BudgetRepositoryInterface /** * @param Carbon $start * @param Carbon $end - * @param Budget $budget * * @return Collection */ - public function getAllBudgetLimitRepetitions(Carbon $start, Carbon $end, Budget $budget = null): Collection; + public function getAllBudgetLimitRepetitions(Carbon $start, Carbon $end): Collection; - /** - * @param Account $account - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return Collection - */ - public function getAllWithoutBudget(Account $account, Collection $accounts, Carbon $start, Carbon $end): Collection; + // /** + // * @param Account $account + // * @param Collection $accounts + // * @param Carbon $start + // * @param Carbon $end + // * + // * @return Collection + // */ + // public function getAllWithoutBudget(Account $account, Collection $accounts, Carbon $start, Carbon $end): Collection; - /** - * Get the budgeted amounts for each budgets in each year. - * - * @param Collection $budgets - * @param Carbon $start - * @param Carbon $end - * - * @return Collection - */ - public function getBudgetedPerYear(Collection $budgets, Carbon $start, Carbon $end): Collection; + // /** + // * Get the budgeted amounts for each budgets in each year. + // * + // * @param Collection $budgets + // * @param Carbon $start + // * @param Carbon $end + // * + // * @return Collection + // */ + // public function getBudgetedPerYear(Collection $budgets, Carbon $start, Carbon $end): Collection; /** * @return Collection */ public function getBudgets(): Collection; - /** - * Returns an array with every budget in it and the expenses for each budget - * per month. - * - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return array - */ - public function getBudgetsAndExpensesPerMonth(Collection $accounts, Carbon $start, Carbon $end): array; + // /** + // * Returns an array with every budget in it and the expenses for each budget + // * per month. + // * + // * @param Collection $accounts + // * @param Carbon $start + // * @param Carbon $end + // * + // * @return array + // */ + // public function getBudgetsAndExpensesPerMonth(Collection $accounts, Carbon $start, Carbon $end): array; - /** - * Returns an array with every budget in it and the expenses for each budget - * per year for. - * - * @param Collection $budgets - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @deprecated - * - * @return array - */ - public function getBudgetsAndExpensesPerYear(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array; + // /** + // * Returns an array with every budget in it and the expenses for each budget + // * per year for. + // * + // * @param Collection $budgets + // * @param Collection $accounts + // * @param Carbon $start + // * @param Carbon $end + // * + // * @deprecated + // * + // * @return array + // */ + // public function getBudgetsAndExpensesPerYear(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): array; - /** - * Returns a list of budgets, budget limits and limit repetitions - * (doubling any of them in a left join) - * - * @param Carbon $start - * @param Carbon $end - * - * @return Collection - */ - public function getBudgetsAndLimitsInRange(Carbon $start, Carbon $end): Collection; + // /** + // * Returns a list of budgets, budget limits and limit repetitions + // * (doubling any of them in a left join) + // * + // * @param Carbon $start + // * @param Carbon $end + // * + // * @return Collection + // */ + // public function getBudgetsAndLimitsInRange(Carbon $start, Carbon $end): Collection; - /** - * Returns a list of budget limits that are valid in the current given range. - * - * @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; + // /** + // * Returns a list of budget limits that are valid in the current given range. + // * + // * @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; - /** - * @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; + // /** + // * @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; - /** - * 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; + // /** + // * 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; - /** - * @param Budget $budget - * - * @return Carbon - */ - public function getFirstBudgetLimitDate(Budget $budget):Carbon; + // /** + // * @param Budget $budget + // * + // * @return Carbon + // */ + // public function getFirstBudgetLimitDate(Budget $budget):Carbon; /** * @return Collection */ public function getInactiveBudgets(): Collection; - /** - * 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; + // /** + // * 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; - /** - * @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; + // /** + // * @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; - /** - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return Collection - */ - public function getWithoutBudgetForAccounts(Collection $accounts, Carbon $start, Carbon $end): Collection; + // /** + // * @param Collection $accounts + // * @param Carbon $start + // * @param Carbon $end + // * + // * @return Collection + // */ + // public function getWithoutBudgetForAccounts(Collection $accounts, Carbon $start, Carbon $end): Collection; - /** - * @param Collection $accounts - * @param Carbon $start - * @param Carbon $end - * - * @return string - */ - public function getWithoutBudgetSum(Collection $accounts, Carbon $start, Carbon $end): string; + // /** + // * @param Collection $accounts + // * @param Carbon $start + // * @param Carbon $end + // * + // * @return string + // */ + // public function getWithoutBudgetSum(Collection $accounts, Carbon $start, Carbon $end): string; - /** - * 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; + // /** + // * 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; - /** - * 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; + // /** + // * 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; - /** - * 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 WITHDRAWALS 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; + // /** + // * 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 WITHDRAWALS 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; /** * @param array $data