mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-13 07:53:16 +00:00
More cleanup for budgets.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
@@ -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
|
||||
@@ -40,22 +42,19 @@ class BudgetReportHelper implements BudgetReportHelperInterface
|
||||
/** @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
|
||||
|
||||
/** @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);
|
||||
|
@@ -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();
|
||||
|
||||
/** @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'));
|
||||
|
||||
|
@@ -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();
|
||||
|
@@ -1,6 +1,8 @@
|
||||
<?php namespace FireflyIII\Models;
|
||||
|
||||
use Auth;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
@@ -59,6 +61,28 @@ class LimitRepetition extends Model
|
||||
return $this->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
|
||||
*/
|
||||
|
@@ -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']);
|
||||
}
|
||||
|
@@ -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;
|
||||
@@ -833,6 +829,119 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
|
||||
// return $return;
|
||||
// }
|
||||
|
||||
/**
|
||||
* @param Collection $budgets
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function journalsInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): Collection
|
||||
{
|
||||
$return = new Collection;
|
||||
$accountIds = [];
|
||||
if ($accounts->count() > 0) {
|
||||
$accountIds = $accounts->pluck('id')->toArray();
|
||||
}
|
||||
|
||||
// first get all journals for all budget(s):
|
||||
$journalQuery = $this->user->transactionjournals()
|
||||
->expanded()
|
||||
->before($end)
|
||||
->after($start)
|
||||
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->whereIn('budget_transaction_journal.budget_id', $budgets->pluck('id')->toArray());
|
||||
// add account id's, if relevant:
|
||||
if (count($accountIds) > 0) {
|
||||
$journalQuery->leftJoin('transactions as source', 'source.transaction_journal_id', '=', 'transaction_journals.id');
|
||||
$journalQuery->whereIn('source.account_id', $accountIds);
|
||||
}
|
||||
// get them:
|
||||
$journals = $journalQuery->get(TransactionJournal::queryFields());
|
||||
Log::debug('journalsInPeriod journal count is ' . $journals->count());
|
||||
|
||||
// then get transactions themselves.
|
||||
$transactionQuery = $this->user->transactionjournals()
|
||||
->expanded()
|
||||
->before($end)
|
||||
->after($start)
|
||||
->leftJoin('transactions as related', 'related.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->leftJoin('budget_transaction', 'budget_transaction.transaction_id', '=', 'related.id')
|
||||
->whereIn('budget_transaction.budget_id', $budgets->pluck('id')->toArray());
|
||||
|
||||
if (count($accountIds) > 0) {
|
||||
$transactionQuery->leftJoin('transactions as source', 'source.transaction_journal_id', '=', 'transaction_journals.id');
|
||||
$transactionQuery->whereIn('source.account_id', $accountIds);
|
||||
}
|
||||
$transactions = $transactionQuery->get(TransactionJournal::queryFields());
|
||||
|
||||
// return complete set:
|
||||
$return = $return->merge($transactions);
|
||||
$return = $return->merge($journals);
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function journalsInPeriodWithoutBudget(Collection $accounts, Carbon $start, Carbon $end): Collection
|
||||
{
|
||||
/** @var Collection $set */
|
||||
$set = $this->user
|
||||
->transactionjournals()
|
||||
->expanded()
|
||||
->transactionTypes([TransactionType::WITHDRAWAL])
|
||||
->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->whereNull('budget_transaction_journal.id')
|
||||
->before($end)
|
||||
->after($start)->with(
|
||||
[
|
||||
'transactions' => function (HasMany $query) {
|
||||
$query->where('transactions.amount', '<', 0);
|
||||
},
|
||||
'transactions.budgets',
|
||||
]
|
||||
)->get(TransactionJournal::queryFields());
|
||||
|
||||
$set = $set->filter(
|
||||
function (TransactionJournal $journal) {
|
||||
foreach ($journal->transactions as $t) {
|
||||
if ($t->budgets->count() === 0) {
|
||||
return $journal;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
return $set;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection $budgets
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function spentInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end) : string
|
||||
{
|
||||
$set = $this->journalsInPeriod($budgets, $accounts, $start, $end);
|
||||
Log::debug('spentInPeriod set count is ' . $set->count());
|
||||
$sum = '0';
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($set as $journal) {
|
||||
$sum = bcadd($sum, TransactionJournal::amount($journal));
|
||||
}
|
||||
|
||||
return $sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
|
@@ -16,6 +16,13 @@ use Illuminate\Support\Collection;
|
||||
interface BudgetRepositoryInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function destroy(Budget $budget): bool;
|
||||
|
||||
// /**
|
||||
// *
|
||||
// * Same as ::spentInPeriod but corrects journals for a set of accounts
|
||||
@@ -35,11 +42,13 @@ interface BudgetRepositoryInterface
|
||||
// public function cleanupBudgets(): bool;
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
* Find a budget.
|
||||
*
|
||||
* @return bool
|
||||
* @param int $budgetId
|
||||
*
|
||||
* @return Budget
|
||||
*/
|
||||
public function destroy(Budget $budget): bool;
|
||||
public function find(int $budgetId): Budget;
|
||||
|
||||
// /**
|
||||
// * @param Budget $budget
|
||||
@@ -52,13 +61,9 @@ interface BudgetRepositoryInterface
|
||||
// public function expensesSplit(Budget $budget, Account $account, Carbon $start, Carbon $end): Collection;
|
||||
|
||||
/**
|
||||
* Find a budget.
|
||||
*
|
||||
* @param int $budgetId
|
||||
*
|
||||
* @return Budget
|
||||
* @return Collection
|
||||
*/
|
||||
public function find(int $budgetId): Budget;
|
||||
public function getActiveBudgets(): Collection;
|
||||
|
||||
// /**
|
||||
// * @param Budget $budget
|
||||
@@ -67,11 +72,6 @@ interface BudgetRepositoryInterface
|
||||
// */
|
||||
// public function firstActivity(Budget $budget): Carbon;
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getActiveBudgets(): Collection;
|
||||
|
||||
/**
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
@@ -80,6 +80,11 @@ interface BudgetRepositoryInterface
|
||||
*/
|
||||
public function getAllBudgetLimitRepetitions(Carbon $start, Carbon $end): Collection;
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getBudgets(): Collection;
|
||||
|
||||
// /**
|
||||
// * @param Account $account
|
||||
// * @param Collection $accounts
|
||||
@@ -104,7 +109,7 @@ interface BudgetRepositoryInterface
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getBudgets(): Collection;
|
||||
public function getInactiveBudgets(): Collection;
|
||||
|
||||
// /**
|
||||
// * Returns an array with every budget in it and the expenses for each budget
|
||||
@@ -186,9 +191,33 @@ interface BudgetRepositoryInterface
|
||||
// public function getFirstBudgetLimitDate(Budget $budget):Carbon;
|
||||
|
||||
/**
|
||||
* @param Collection $budgets
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getInactiveBudgets(): Collection;
|
||||
public function journalsInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): Collection;
|
||||
|
||||
/**
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function journalsInPeriodWithoutBudget(Collection $accounts, Carbon $start, Carbon $end): Collection;
|
||||
|
||||
/**
|
||||
* @param Collection $budgets
|
||||
* @param Collection $accounts
|
||||
* @param Carbon $start
|
||||
* @param Carbon $end
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function spentInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end) : string;
|
||||
|
||||
// /**
|
||||
// * Returns all the transaction journals for a limit, possibly limited by a limit repetition.
|
||||
|
@@ -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);
|
||||
|
||||
|
Reference in New Issue
Block a user