Skip to content

Firefly III: a personal finances manager

Moved more stuff to the journal collector.

James Cole1e2124c5ed8a
diff --git a/app/Helpers/Collector/JournalCollector.php b/app/Helpers/Collector/JournalCollector.php
index d6790eb096..0179a204c0 100644
--- a/app/Helpers/Collector/JournalCollector.php
+++ b/app/Helpers/Collector/JournalCollector.php
@@ -232,12 +232,7 @@ class JournalCollector
*/
public function setBudget(Budget $budget): JournalCollector
{
- if (!$this->joinedBudget) {
- // join some extra tables:
- $this->joinedBudget = true;
- $this->query->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id');
- $this->query->leftJoin('budget_transaction', 'budget_transaction.transaction_id', '=', 'transactions.id');
- }
+ $this->joinBudgetTables();
$this->query->where(
function (EloquentBuilder $q) use ($budget) {
@@ -256,12 +251,7 @@ class JournalCollector
*/
public function setCategory(Category $category): JournalCollector
{
- if (!$this->joinedCategory) {
- // join some extra tables:
- $this->joinedCategory = true;
- $this->query->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id');
- $this->query->leftJoin('category_transaction', 'category_transaction.transaction_id', '=', 'transactions.id');
- }
+ $this->joinCategoryTables();
$this->query->where(
function (EloquentBuilder $q) use ($category) {
@@ -354,17 +344,29 @@ class JournalCollector
return $this;
}
+ /**
+ * @return JournalCollector
+ */
+ public function withoutBudget(): JournalCollector
+ {
+ $this->joinBudgetTables();
+
+ $this->query->where(
+ function (EloquentBuilder $q) {
+ $q->whereNull('budget_transaction.budget_id');
+ $q->whereNull('budget_transaction_journal.budget_id');
+ }
+ );
+
+ return $this;
+ }
+
/**
* @return JournalCollector
*/
public function withoutCategory(): JournalCollector
{
- if (!$this->joinedCategory) {
- // join some extra tables:
- $this->joinedCategory = true;
- $this->query->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id');
- $this->query->leftJoin('category_transaction', 'category_transaction.transaction_id', '=', 'transactions.id');
- }
+ $this->joinCategoryTables();
$this->query->where(
function (EloquentBuilder $q) {
@@ -376,6 +378,32 @@ class JournalCollector
return $this;
}
+ /**
+ *
+ */
+ private function joinBudgetTables()
+ {
+ if (!$this->joinedBudget) {
+ // join some extra tables:
+ $this->joinedBudget = true;
+ $this->query->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id');
+ $this->query->leftJoin('budget_transaction', 'budget_transaction.transaction_id', '=', 'transactions.id');
+ }
+ }
+
+ /**
+ *
+ */
+ private function joinCategoryTables()
+ {
+ if (!$this->joinedCategory) {
+ // join some extra tables:
+ $this->joinedCategory = true;
+ $this->query->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id');
+ $this->query->leftJoin('category_transaction', 'category_transaction.transaction_id', '=', 'transactions.id');
+ }
+ }
+
/**
* @return EloquentBuilder
*/
diff --git a/app/Http/Controllers/BudgetController.php b/app/Http/Controllers/BudgetController.php
index f9b176060a..b09e56f9c9 100644
--- a/app/Http/Controllers/BudgetController.php
+++ b/app/Http/Controllers/BudgetController.php
@@ -248,31 +248,28 @@ class BudgetController extends Controller
}
/**
- * @param BudgetRepositoryInterface $repository
- *
* @return View
*/
- public function noBudget(BudgetRepositoryInterface $repository)
+ public function noBudget()
{
/** @var Carbon $start */
$start = session('start', Carbon::now()->startOfMonth());
/** @var Carbon $end */
$end = session('end', Carbon::now()->endOfMonth());
-
$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); // budget
- $count = $journals->count();
- $journals = $journals->slice($offset, $pageSize);
- $list = new LengthAwarePaginator($journals, $count, $pageSize);
+ $pageSize = intval(Preferences::get('transactionPageSize', 50)->data);
$subTitle = trans(
'firefly.without_budget_between',
['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
);
- $list->setPath('/budgets/list/noBudget');
- return view('budgets.noBudget', compact('list', 'subTitle'));
+ // collector
+ $collector = new JournalCollector(auth()->user());
+ $collector->setAllAssetAccounts()->setRange($start, $end)->setLimit($pageSize)->setPage($page)->withoutBudget();
+ $journals = $collector->getPaginatedJournals();
+ $journals->setPath('/budgets/list/noBudget');
+
+ return view('budgets.no-budget', compact('journals', 'subTitle'));
}
/**
diff --git a/app/Http/Controllers/Chart/BudgetController.php b/app/Http/Controllers/Chart/BudgetController.php
index 3e6c5f1c83..43536e67cc 100644
--- a/app/Http/Controllers/Chart/BudgetController.php
+++ b/app/Http/Controllers/Chart/BudgetController.php
@@ -15,10 +15,12 @@ namespace FireflyIII\Http\Controllers\Chart;
use Carbon\Carbon;
use FireflyIII\Generator\Chart\Budget\BudgetChartGeneratorInterface;
+use FireflyIII\Helpers\Collector\JournalCollector;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Budget;
use FireflyIII\Models\LimitRepetition;
-use FireflyIII\Models\TransactionJournal;
+use FireflyIII\Models\Transaction;
+use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection;
@@ -175,7 +177,7 @@ class BudgetController extends Controller
$allEntries = $allEntries->merge($collection);
}
- $entry = $this->spentInPeriodWithout($repository, $start, $end);
+ $entry = $this->spentInPeriodWithout($start, $end);
$allEntries->push($entry);
$data = $this->generator->frontpage($allEntries);
$cache->store($data);
@@ -319,19 +321,22 @@ class BudgetController extends Controller
}
/**
- * @param BudgetRepositoryInterface $repository
- * @param Carbon $start
- * @param Carbon $end
+ * @param Carbon $start
+ * @param Carbon $end
*
* @return array
*/
- private function spentInPeriodWithout(BudgetRepositoryInterface $repository, Carbon $start, Carbon $end):array
+ private function spentInPeriodWithout(Carbon $start, Carbon $end):array
{
- $list = $repository->journalsInPeriodWithoutBudget(new Collection, $start, $end); // budget
- $sum = '0';
- /** @var TransactionJournal $entry */
- foreach ($list as $entry) {
- $sum = bcadd(TransactionJournal::amount($entry), $sum);
+ // collector
+ $collector = new JournalCollector(auth()->user());
+ $types = [TransactionType::WITHDRAWAL];
+ $collector->setAllAssetAccounts()->setTypes($types)->setRange($start, $end)->withoutBudget();
+ $journals = $collector->getJournals();
+ $sum = '0';
+ /** @var Transaction $entry */
+ foreach ($journals as $entry) {
+ $sum = bcadd($entry->transaction_amount, $sum);
}
return [trans('firefly.no_budget'), '0', '0', $sum, '0', '0'];
diff --git a/app/Http/Controllers/Popup/ReportController.php b/app/Http/Controllers/Popup/ReportController.php
index 89fd605502..c9fd9afe8c 100644
--- a/app/Http/Controllers/Popup/ReportController.php
+++ b/app/Http/Controllers/Popup/ReportController.php
@@ -98,6 +98,7 @@ class ReportController extends Controller
$repository = app(AccountRepositoryInterface::class);
$account = $repository->find(intval($attributes['accountId']));
+ $types = [TransactionType::WITHDRAWAL];
switch (true) {
case ($role === BalanceLine::ROLE_DEFAULTROLE && !is_null($budget->id)):
@@ -111,11 +112,25 @@ class ReportController extends Controller
break;
case ($role === BalanceLine::ROLE_DEFAULTROLE && is_null($budget->id)):
$budget->name = strval(trans('firefly.no_budget'));
- $journals = $budgetRepository->journalsInPeriodWithoutBudget($attributes['accounts'], $attributes['startDate'], $attributes['endDate']);
+ // collector
+ $collector = new JournalCollector(auth()->user());
+ $collector
+ ->setAccounts(new Collection([$account]))
+ ->setTypes($types)
+ ->setRange($attributes['startDate'], $attributes['endDate'])
+ ->withoutBudget();
+ $journals = $collector->getJournals();
break;
case ($role === BalanceLine::ROLE_DIFFROLE):
// journals no budget, not corrected by a tag.
- $journals = $budgetRepository->journalsInPeriodWithoutBudget($attributes['accounts'], $attributes['startDate'], $attributes['endDate']);
+ $collector = new JournalCollector(auth()->user());
+ $collector
+ ->setAccounts(new Collection([$account]))
+ ->setTypes($types)
+ ->setRange($attributes['startDate'], $attributes['endDate'])
+ ->withoutBudget();
+ $journals = $collector->getJournals();
+
$budget->name = strval(trans('firefly.leftUnbalanced'));
$journals = $journals->filter(
function (TransactionJournal $journal) {
@@ -152,22 +167,21 @@ class ReportController extends Controller
/** @var BudgetRepositoryInterface $repository */
$repository = app(BudgetRepositoryInterface::class);
$budget = $repository->find(intval($attributes['budgetId']));
+ $collector = new JournalCollector(auth()->user());
+
+ $collector
+ ->setAccounts($attributes['accounts'])
+ ->setRange($attributes['startDate'], $attributes['endDate']);
+
if (is_null($budget->id)) {
- $journals = $repository->journalsInPeriodWithoutBudget($attributes['accounts'], $attributes['startDate'], $attributes['endDate']);
+ $collector->setTypes([TransactionType::WITHDRAWAL])->withoutBudget();
}
if (!is_null($budget->id)) {
// get all expenses in budget in period:
- $collector = new JournalCollector(auth()->user());
- $collector
- ->setAccounts($attributes['accounts'])
- ->setRange($attributes['startDate'], $attributes['endDate'])
- ->setBudget($budget);
- $journals = $collector->getJournals();
-
-
+ $collector->setBudget($budget);
}
-
- $view = view('popup.report.budget-spent-amount', compact('journals', 'budget'))->render();
+ $journals = $collector->getJournals();
+ $view = view('popup.report.budget-spent-amount', compact('journals', 'budget'))->render();
return $view;
}
diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php
index 0e0e281dfd..b6fecfdca4 100644
--- a/app/Repositories/Budget/BudgetRepository.php
+++ b/app/Repositories/Budget/BudgetRepository.php
@@ -210,65 +210,6 @@ class BudgetRepository implements BudgetRepositoryInterface
return $set;
}
- /**
- * @param Collection $accounts
- * @param Carbon $start
- * @param Carbon $end
- *
- * @return Collection
- */
- public function journalsInPeriodWithoutBudget(Collection $accounts, Carbon $start, Carbon $end): Collection
- {
- $accountIds = [];
- if ($accounts->count() > 0) {
- $accountIds = $accounts->pluck('id')->toArray();
- }
-
- /** @var Collection $set */
- $query = $this->user
- ->transactionJournals()
- ->expanded()
- ->sortCorrectly()
- ->transactionTypes([TransactionType::WITHDRAWAL])
- ->leftJoin('budget_transaction_journal', 'budget_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id')
- ->whereNull('budget_transaction_journal.id')
- ->leftJoin(
- 'transactions as source',
- function (JoinClause $join) {
- $join->on('source.transaction_journal_id', '=', 'transaction_journals.id')->where('source.amount', '<', '0');
- }
- )
- ->before($end)
- ->after($start)->with(
- [
- 'transactions' => function (HasMany $query) {
- $query->where('transactions.amount', '<', 0);
- },
- 'transactions.budgets',
- ]
- );
-
- // add account id's, if relevant:
- if (count($accountIds) > 0) {
- $query->whereIn('source.account_id', $accountIds);
- }
-
- $set = $query->get(TransactionJournal::queryFields());
- $set = $set->filter(
- function (TransactionJournal $journal) {
- foreach ($journal->transactions as $t) {
- if ($t->budgets->count() === 0) {
- return true;
- }
- }
-
- return false;
- }
- );
-
- return $set;
- }
-
/**
* @param Collection $budgets
* @param Collection $accounts
diff --git a/app/Repositories/Budget/BudgetRepositoryInterface.php b/app/Repositories/Budget/BudgetRepositoryInterface.php
index e6c5503c1f..b7116dcbd2 100644
--- a/app/Repositories/Budget/BudgetRepositoryInterface.php
+++ b/app/Repositories/Budget/BudgetRepositoryInterface.php
@@ -89,15 +89,6 @@ interface BudgetRepositoryInterface
*/
public function getInactiveBudgets(): 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
diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php
index 22c235368b..8519610ca7 100644
--- a/resources/lang/en_US/firefly.php
+++ b/resources/lang/en_US/firefly.php
@@ -73,7 +73,7 @@ return [
'user_id_is' => 'Your user id is <strong>:user</strong>',
'field_supports_markdown' => 'This field supports <a href="https://en.support.wordpress.com/markdown-quick-reference/">Markdown</a>.',
'need_more_help' => 'If you need more help using Firefly III, please <a href="https://github.com/JC5/firefly-iii/issues">open a ticker on Github</a>.',
-
+ 'nothing_to_display' => 'There are no transactions to show you',
// repeat frequencies:
'repeat_freq_yearly' => 'yearly',
diff --git a/resources/views/budgets/noBudget.twig b/resources/views/budgets/no-budget.twig
similarity index 88%
rename from resources/views/budgets/noBudget.twig
rename to resources/views/budgets/no-budget.twig
index 6abbb397d0..33223893ba 100644
--- a/resources/views/budgets/noBudget.twig
+++ b/resources/views/budgets/no-budget.twig
@@ -12,7 +12,7 @@
<h3 class="box-title">{{ subTitle }}</h3>
</div>
<div class="box-body">
- {% include 'list.journals' with {'journals': list} %}
+ {% include 'list.journals-tasker' with {'journals': journals} %}
</div>
</div>
</div>