mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-13 16:00:13 +00:00
Moved more stuff to the journal collector.
This commit is contained in:
@@ -232,12 +232,7 @@ class JournalCollector
|
|||||||
*/
|
*/
|
||||||
public function setBudget(Budget $budget): JournalCollector
|
public function setBudget(Budget $budget): JournalCollector
|
||||||
{
|
{
|
||||||
if (!$this->joinedBudget) {
|
$this->joinBudgetTables();
|
||||||
// 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->query->where(
|
$this->query->where(
|
||||||
function (EloquentBuilder $q) use ($budget) {
|
function (EloquentBuilder $q) use ($budget) {
|
||||||
@@ -256,12 +251,7 @@ class JournalCollector
|
|||||||
*/
|
*/
|
||||||
public function setCategory(Category $category): JournalCollector
|
public function setCategory(Category $category): JournalCollector
|
||||||
{
|
{
|
||||||
if (!$this->joinedCategory) {
|
$this->joinCategoryTables();
|
||||||
// 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->query->where(
|
$this->query->where(
|
||||||
function (EloquentBuilder $q) use ($category) {
|
function (EloquentBuilder $q) use ($category) {
|
||||||
@@ -354,17 +344,29 @@ class JournalCollector
|
|||||||
return $this;
|
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
|
* @return JournalCollector
|
||||||
*/
|
*/
|
||||||
public function withoutCategory(): JournalCollector
|
public function withoutCategory(): JournalCollector
|
||||||
{
|
{
|
||||||
if (!$this->joinedCategory) {
|
$this->joinCategoryTables();
|
||||||
// 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->query->where(
|
$this->query->where(
|
||||||
function (EloquentBuilder $q) {
|
function (EloquentBuilder $q) {
|
||||||
@@ -376,6 +378,32 @@ class JournalCollector
|
|||||||
return $this;
|
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
|
* @return EloquentBuilder
|
||||||
*/
|
*/
|
||||||
|
@@ -248,31 +248,28 @@ class BudgetController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param BudgetRepositoryInterface $repository
|
|
||||||
*
|
|
||||||
* @return View
|
* @return View
|
||||||
*/
|
*/
|
||||||
public function noBudget(BudgetRepositoryInterface $repository)
|
public function noBudget()
|
||||||
{
|
{
|
||||||
/** @var Carbon $start */
|
/** @var Carbon $start */
|
||||||
$start = session('start', Carbon::now()->startOfMonth());
|
$start = session('start', Carbon::now()->startOfMonth());
|
||||||
/** @var Carbon $end */
|
/** @var Carbon $end */
|
||||||
$end = session('end', Carbon::now()->endOfMonth());
|
$end = session('end', Carbon::now()->endOfMonth());
|
||||||
|
|
||||||
$page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page'));
|
$page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page'));
|
||||||
$pageSize = Preferences::get('transactionPageSize', 50)->data;
|
$pageSize = intval(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);
|
|
||||||
$subTitle = trans(
|
$subTitle = trans(
|
||||||
'firefly.without_budget_between',
|
'firefly.without_budget_between',
|
||||||
['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
|
['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'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -15,10 +15,12 @@ namespace FireflyIII\Http\Controllers\Chart;
|
|||||||
|
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use FireflyIII\Generator\Chart\Budget\BudgetChartGeneratorInterface;
|
use FireflyIII\Generator\Chart\Budget\BudgetChartGeneratorInterface;
|
||||||
|
use FireflyIII\Helpers\Collector\JournalCollector;
|
||||||
use FireflyIII\Http\Controllers\Controller;
|
use FireflyIII\Http\Controllers\Controller;
|
||||||
use FireflyIII\Models\Budget;
|
use FireflyIII\Models\Budget;
|
||||||
use FireflyIII\Models\LimitRepetition;
|
use FireflyIII\Models\LimitRepetition;
|
||||||
use FireflyIII\Models\TransactionJournal;
|
use FireflyIII\Models\Transaction;
|
||||||
|
use FireflyIII\Models\TransactionType;
|
||||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||||
use FireflyIII\Support\CacheProperties;
|
use FireflyIII\Support\CacheProperties;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
@@ -175,7 +177,7 @@ class BudgetController extends Controller
|
|||||||
$allEntries = $allEntries->merge($collection);
|
$allEntries = $allEntries->merge($collection);
|
||||||
|
|
||||||
}
|
}
|
||||||
$entry = $this->spentInPeriodWithout($repository, $start, $end);
|
$entry = $this->spentInPeriodWithout($start, $end);
|
||||||
$allEntries->push($entry);
|
$allEntries->push($entry);
|
||||||
$data = $this->generator->frontpage($allEntries);
|
$data = $this->generator->frontpage($allEntries);
|
||||||
$cache->store($data);
|
$cache->store($data);
|
||||||
@@ -319,19 +321,22 @@ class BudgetController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param BudgetRepositoryInterface $repository
|
|
||||||
* @param Carbon $start
|
* @param Carbon $start
|
||||||
* @param Carbon $end
|
* @param Carbon $end
|
||||||
*
|
*
|
||||||
* @return array
|
* @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
|
// collector
|
||||||
|
$collector = new JournalCollector(auth()->user());
|
||||||
|
$types = [TransactionType::WITHDRAWAL];
|
||||||
|
$collector->setAllAssetAccounts()->setTypes($types)->setRange($start, $end)->withoutBudget();
|
||||||
|
$journals = $collector->getJournals();
|
||||||
$sum = '0';
|
$sum = '0';
|
||||||
/** @var TransactionJournal $entry */
|
/** @var Transaction $entry */
|
||||||
foreach ($list as $entry) {
|
foreach ($journals as $entry) {
|
||||||
$sum = bcadd(TransactionJournal::amount($entry), $sum);
|
$sum = bcadd($entry->transaction_amount, $sum);
|
||||||
}
|
}
|
||||||
|
|
||||||
return [trans('firefly.no_budget'), '0', '0', $sum, '0', '0'];
|
return [trans('firefly.no_budget'), '0', '0', $sum, '0', '0'];
|
||||||
|
@@ -98,6 +98,7 @@ class ReportController extends Controller
|
|||||||
$repository = app(AccountRepositoryInterface::class);
|
$repository = app(AccountRepositoryInterface::class);
|
||||||
|
|
||||||
$account = $repository->find(intval($attributes['accountId']));
|
$account = $repository->find(intval($attributes['accountId']));
|
||||||
|
$types = [TransactionType::WITHDRAWAL];
|
||||||
|
|
||||||
switch (true) {
|
switch (true) {
|
||||||
case ($role === BalanceLine::ROLE_DEFAULTROLE && !is_null($budget->id)):
|
case ($role === BalanceLine::ROLE_DEFAULTROLE && !is_null($budget->id)):
|
||||||
@@ -111,11 +112,25 @@ class ReportController extends Controller
|
|||||||
break;
|
break;
|
||||||
case ($role === BalanceLine::ROLE_DEFAULTROLE && is_null($budget->id)):
|
case ($role === BalanceLine::ROLE_DEFAULTROLE && is_null($budget->id)):
|
||||||
$budget->name = strval(trans('firefly.no_budget'));
|
$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;
|
break;
|
||||||
case ($role === BalanceLine::ROLE_DIFFROLE):
|
case ($role === BalanceLine::ROLE_DIFFROLE):
|
||||||
// journals no budget, not corrected by a tag.
|
// 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'));
|
$budget->name = strval(trans('firefly.leftUnbalanced'));
|
||||||
$journals = $journals->filter(
|
$journals = $journals->filter(
|
||||||
function (TransactionJournal $journal) {
|
function (TransactionJournal $journal) {
|
||||||
@@ -152,21 +167,20 @@ class ReportController extends Controller
|
|||||||
/** @var BudgetRepositoryInterface $repository */
|
/** @var BudgetRepositoryInterface $repository */
|
||||||
$repository = app(BudgetRepositoryInterface::class);
|
$repository = app(BudgetRepositoryInterface::class);
|
||||||
$budget = $repository->find(intval($attributes['budgetId']));
|
$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)) {
|
if (is_null($budget->id)) {
|
||||||
$journals = $repository->journalsInPeriodWithoutBudget($attributes['accounts'], $attributes['startDate'], $attributes['endDate']);
|
$collector->setTypes([TransactionType::WITHDRAWAL])->withoutBudget();
|
||||||
}
|
}
|
||||||
if (!is_null($budget->id)) {
|
if (!is_null($budget->id)) {
|
||||||
// get all expenses in budget in period:
|
// get all expenses in budget in period:
|
||||||
$collector = new JournalCollector(auth()->user());
|
$collector->setBudget($budget);
|
||||||
$collector
|
|
||||||
->setAccounts($attributes['accounts'])
|
|
||||||
->setRange($attributes['startDate'], $attributes['endDate'])
|
|
||||||
->setBudget($budget);
|
|
||||||
$journals = $collector->getJournals();
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
$journals = $collector->getJournals();
|
||||||
$view = view('popup.report.budget-spent-amount', compact('journals', 'budget'))->render();
|
$view = view('popup.report.budget-spent-amount', compact('journals', 'budget'))->render();
|
||||||
|
|
||||||
return $view;
|
return $view;
|
||||||
|
@@ -210,65 +210,6 @@ class BudgetRepository implements BudgetRepositoryInterface
|
|||||||
return $set;
|
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 $budgets
|
||||||
* @param Collection $accounts
|
* @param Collection $accounts
|
||||||
|
@@ -89,15 +89,6 @@ interface BudgetRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function getInactiveBudgets(): Collection;
|
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 $budgets
|
||||||
* @param Collection $accounts
|
* @param Collection $accounts
|
||||||
|
@@ -73,7 +73,7 @@ return [
|
|||||||
'user_id_is' => 'Your user id is <strong>:user</strong>',
|
'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>.',
|
'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>.',
|
'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 frequencies:
|
||||||
'repeat_freq_yearly' => 'yearly',
|
'repeat_freq_yearly' => 'yearly',
|
||||||
|
@@ -12,7 +12,7 @@
|
|||||||
<h3 class="box-title">{{ subTitle }}</h3>
|
<h3 class="box-title">{{ subTitle }}</h3>
|
||||||
</div>
|
</div>
|
||||||
<div class="box-body">
|
<div class="box-body">
|
||||||
{% include 'list.journals' with {'journals': list} %}
|
{% include 'list.journals-tasker' with {'journals': journals} %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
Reference in New Issue
Block a user