mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Fix queries.
This commit is contained in:
parent
9f8faf15f1
commit
dc825d5a9c
@ -126,7 +126,9 @@ class VerifyDatabase extends Command
|
||||
::leftJoin('budget_transaction_journal', 'budgets.id', '=', 'budget_transaction_journal.budget_id')
|
||||
->leftJoin('users', 'budgets.user_id', '=', 'users.id')
|
||||
->distinct()
|
||||
->get(['budgets.id', 'budgets.name', 'budget_transaction_journal.budget_id', 'budgets.user_id', 'users.email']);
|
||||
->whereNull('budget_transaction_journal.budget_id')
|
||||
->whereNull('budgets.deleted_at')
|
||||
->get(['budgets.id', 'budgets.name', 'budgets.user_id', 'users.email']);
|
||||
|
||||
/** @var stdClass $entry */
|
||||
foreach ($set as $entry) {
|
||||
@ -145,7 +147,9 @@ class VerifyDatabase extends Command
|
||||
::leftJoin('category_transaction_journal', 'categories.id', '=', 'category_transaction_journal.category_id')
|
||||
->leftJoin('users', 'categories.user_id', '=', 'users.id')
|
||||
->distinct()
|
||||
->get(['categories.id', 'categories.name', 'category_transaction_journal.category_id', 'categories.user_id', 'users.email']);
|
||||
->whereNull('category_transaction_journal.category_id')
|
||||
->whereNull('categories.deleted_at')
|
||||
->get(['categories.id', 'categories.name', 'categories.user_id', 'users.email']);
|
||||
|
||||
/** @var stdClass $entry */
|
||||
foreach ($set as $entry) {
|
||||
@ -263,7 +267,9 @@ having transaction_count = 0
|
||||
::leftJoin('tag_transaction_journal', 'tags.id', '=', 'tag_transaction_journal.tag_id')
|
||||
->leftJoin('users', 'tags.user_id', '=', 'users.id')
|
||||
->distinct()
|
||||
->get(['tags.id', 'tags.tag', 'tag_transaction_journal.tag_id', 'tags.user_id', 'users.email']);
|
||||
->whereNull('tag_transaction_journal.tag_id')
|
||||
->whereNull('tags.deleted_at')
|
||||
->get(['tags.id', 'tags.tag', 'tags.user_id', 'users.email']);
|
||||
|
||||
/** @var stdClass $entry */
|
||||
foreach ($set as $entry) {
|
||||
|
@ -20,6 +20,7 @@ use FireflyIII\Helpers\Collection\BalanceLine;
|
||||
use FireflyIII\Models\Account;
|
||||
use FireflyIII\Models\Budget;
|
||||
use FireflyIII\Models\Budget as BudgetModel;
|
||||
use FireflyIII\Models\LimitRepetition;
|
||||
use FireflyIII\Models\Tag;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
@ -27,6 +28,7 @@ use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
|
||||
use Illuminate\Database\Query\JoinClause;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class BalanceReportHelper
|
||||
@ -65,20 +67,22 @@ class BalanceReportHelper implements BalanceReportHelperInterface
|
||||
public function getBalanceReport(Carbon $start, Carbon $end, Collection $accounts): Balance
|
||||
{
|
||||
$balance = new Balance;
|
||||
|
||||
Log::debug('Build new report.');
|
||||
// build a balance header:
|
||||
$header = new BalanceHeader;
|
||||
// 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);
|
||||
$header = new BalanceHeader;
|
||||
$budgets = $this->budgetRepository->getBudgets();
|
||||
$limitRepetitions = $this->budgetRepository->getAllBudgetLimitRepetitions($start, $end);
|
||||
$spentData = $this->budgetRepository->journalsInPeriod($budgets, $accounts, $start, $end);
|
||||
foreach ($accounts as $account) {
|
||||
$header->addAccount($account);
|
||||
Log::debug('Add account #' . $account->id . ' to header.');
|
||||
}
|
||||
|
||||
/** @var BudgetModel $budget */
|
||||
foreach ($budgets as $budget) {
|
||||
$balance->addBalanceLine($this->createBalanceLine($budget, $accounts, $spentData));
|
||||
/** @var LimitRepetition $repetition */
|
||||
foreach ($limitRepetitions as $repetition) {
|
||||
$budget = $this->budgetRepository->find($repetition->budget_id);
|
||||
Log::debug('Create and add balance line for budget #' . $budget->id);
|
||||
$balance->addBalanceLine($this->createBalanceLine($budget, $repetition, $accounts));
|
||||
}
|
||||
|
||||
$balance->addBalanceLine($this->createEmptyBalanceLine($accounts, $spentData));
|
||||
@ -142,34 +146,30 @@ class BalanceReportHelper implements BalanceReportHelperInterface
|
||||
|
||||
|
||||
/**
|
||||
* @param Budget $budget
|
||||
* @param Collection $accounts
|
||||
* @param Collection $spentData
|
||||
* @param Budget $budget
|
||||
* @param LimitRepetition $repetition
|
||||
* @param Collection $accounts
|
||||
*
|
||||
* @return BalanceLine
|
||||
*/
|
||||
private function createBalanceLine(BudgetModel $budget, Collection $accounts, Collection $spentData): BalanceLine
|
||||
private function createBalanceLine(BudgetModel $budget, LimitRepetition $repetition, Collection $accounts): BalanceLine
|
||||
{
|
||||
Log::debug('Create line for budget #' . $budget->id . ' and repetition #' . $repetition->id);
|
||||
$line = new BalanceLine;
|
||||
$budget->amount = $repetition->amount;
|
||||
$line->setBudget($budget);
|
||||
$line->setStartDate($budget->startdate); // returned by getBudgetsAndLimitsInRange()
|
||||
$line->setEndDate($budget->enddate); // returned by getBudgetsAndLimitsInRange()
|
||||
|
||||
|
||||
$line->setStartDate($repetition->startdate);
|
||||
$line->setEndDate($repetition->enddate);
|
||||
|
||||
// loop accounts:
|
||||
foreach ($accounts as $account) {
|
||||
$balanceEntry = new BalanceEntry;
|
||||
$balanceEntry->setAccount($account);
|
||||
|
||||
// get spent:
|
||||
$entry = $spentData->filter(
|
||||
function (TransactionJournal $model) use ($budget, $account) {
|
||||
return $model->account_id == $account->id && $model->budget_id == $budget->id;
|
||||
}
|
||||
$spent = $this->budgetRepository->spentInPeriod(
|
||||
new Collection([$budget]), new Collection([$account]), $repetition->startdate, $repetition->enddate
|
||||
);
|
||||
$spent = '0';
|
||||
if (!is_null($entry->first())) {
|
||||
$spent = $entry->first()->spent;
|
||||
}
|
||||
$balanceEntry->setSpent($spent);
|
||||
$line->addBalanceEntry($balanceEntry);
|
||||
}
|
||||
@ -334,7 +334,6 @@ class BalanceReportHelper implements BalanceReportHelperInterface
|
||||
$newSet->push($entry);
|
||||
}
|
||||
|
||||
|
||||
$balance->setBalanceLines($newSet);
|
||||
|
||||
return $balance;
|
||||
|
@ -868,6 +868,9 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
{
|
||||
$return = new Collection;
|
||||
$accountIds = [];
|
||||
// expand the number of grabbed fields:
|
||||
$fields = TransactionJournal::queryFields();
|
||||
$fields[] = 'source.account_id';
|
||||
if ($accounts->count() > 0) {
|
||||
$accountIds = $accounts->pluck('id')->toArray();
|
||||
}
|
||||
@ -878,16 +881,16 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
->before($end)
|
||||
->sortCorrectly()
|
||||
->after($start)
|
||||
->leftJoin('transactions as source', 'source.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->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());
|
||||
//Log::debug('journalsInPeriod journal count is ' . $journals->count());
|
||||
|
||||
// then get transactions themselves.
|
||||
$transactionQuery = $this->user->transactionjournals()
|
||||
@ -897,13 +900,14 @@ class BudgetRepository implements BudgetRepositoryInterface
|
||||
->after($start)
|
||||
->leftJoin('transactions as related', 'related.transaction_journal_id', '=', 'transaction_journals.id')
|
||||
->leftJoin('budget_transaction', 'budget_transaction.transaction_id', '=', 'related.id')
|
||||
->leftJoin('transactions as source', 'source.transaction_journal_id', '=', 'transaction_journals.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());
|
||||
|
||||
$transactions = $transactionQuery->get($fields);
|
||||
|
||||
// return complete set:
|
||||
$return = $return->merge($transactions);
|
||||
|
Loading…
Reference in New Issue
Block a user