mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
This code makes sure the budget report also includes split expenses.
This commit is contained in:
parent
8860378757
commit
28f655dba1
@ -154,7 +154,7 @@ class JournalCollector implements JournalCollectorInterface
|
|||||||
* @return LengthAwarePaginator
|
* @return LengthAwarePaginator
|
||||||
* @throws FireflyException
|
* @throws FireflyException
|
||||||
*/
|
*/
|
||||||
public function getPaginatedJournals():LengthAwarePaginator
|
public function getPaginatedJournals(): LengthAwarePaginator
|
||||||
{
|
{
|
||||||
if ($this->run === true) {
|
if ($this->run === true) {
|
||||||
throw new FireflyException('Cannot getPaginatedJournals after run in JournalCollector.');
|
throw new FireflyException('Cannot getPaginatedJournals after run in JournalCollector.');
|
||||||
@ -244,6 +244,29 @@ class JournalCollector implements JournalCollectorInterface
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Collection $budgets
|
||||||
|
*
|
||||||
|
* @return JournalCollectorInterface
|
||||||
|
*/
|
||||||
|
public function setBudgets(Collection $budgets): JournalCollectorInterface
|
||||||
|
{
|
||||||
|
$budgetIds = $budgets->pluck('id')->toArray();
|
||||||
|
if (count($budgetIds) === 0) {
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
$this->joinBudgetTables();
|
||||||
|
|
||||||
|
$this->query->where(
|
||||||
|
function (EloquentBuilder $q) use ($budgetIds) {
|
||||||
|
$q->whereIn('budget_transaction.budget_id', $budgetIds);
|
||||||
|
$q->orWhereIn('budget_transaction_journal.budget_id', $budgetIds);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection $categories
|
* @param Collection $categories
|
||||||
*
|
*
|
||||||
@ -259,10 +282,8 @@ class JournalCollector implements JournalCollectorInterface
|
|||||||
|
|
||||||
$this->query->where(
|
$this->query->where(
|
||||||
function (EloquentBuilder $q) use ($categoryIds) {
|
function (EloquentBuilder $q) use ($categoryIds) {
|
||||||
if (count($categoryIds) > 0) {
|
$q->whereIn('category_transaction.category_id', $categoryIds);
|
||||||
$q->whereIn('category_transaction.category_id', $categoryIds);
|
$q->orWhereIn('category_transaction_journal.category_id', $categoryIds);
|
||||||
$q->orWhereIn('category_transaction_journal.category_id', $categoryIds);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -74,6 +74,14 @@ interface JournalCollectorInterface
|
|||||||
*/
|
*/
|
||||||
public function setBudget(Budget $budget): JournalCollectorInterface;
|
public function setBudget(Budget $budget): JournalCollectorInterface;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Collection $budgets
|
||||||
|
*
|
||||||
|
* @return JournalCollectorInterface
|
||||||
|
*/
|
||||||
|
public function setBudgets(Collection $budgets): JournalCollectorInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection $categories
|
* @param Collection $categories
|
||||||
*
|
*
|
||||||
|
@ -17,11 +17,12 @@ namespace FireflyIII\Helpers\Report;
|
|||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use FireflyIII\Helpers\Collection\Budget as BudgetCollection;
|
use FireflyIII\Helpers\Collection\Budget as BudgetCollection;
|
||||||
use FireflyIII\Helpers\Collection\BudgetLine;
|
use FireflyIII\Helpers\Collection\BudgetLine;
|
||||||
|
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
|
||||||
use FireflyIII\Models\Budget;
|
use FireflyIII\Models\Budget;
|
||||||
use FireflyIII\Models\LimitRepetition;
|
use FireflyIII\Models\LimitRepetition;
|
||||||
|
use FireflyIII\Models\TransactionType;
|
||||||
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Navigation;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class BudgetReportHelper
|
* Class BudgetReportHelper
|
||||||
@ -52,26 +53,66 @@ class BudgetReportHelper implements BudgetReportHelperInterface
|
|||||||
*/
|
*/
|
||||||
public function getBudgetPeriodReport(Carbon $start, Carbon $end, Collection $accounts): array
|
public function getBudgetPeriodReport(Carbon $start, Carbon $end, Collection $accounts): array
|
||||||
{
|
{
|
||||||
$budgets = $this->repository->getBudgets();
|
$budgets = $this->repository->getBudgets();
|
||||||
$queryResult = $this->repository->getBudgetPeriodReport($budgets, $accounts, $start, $end);
|
/** @var JournalCollectorInterface $collector */
|
||||||
$data = [];
|
$collector = app(JournalCollectorInterface::class);
|
||||||
$periods = Navigation::listOfPeriods($start, $end);
|
$collector->setAllAssetAccounts()->setRange($start, $end);
|
||||||
|
$collector->setBudgets($budgets);
|
||||||
|
$transactions = $collector->getJournals();
|
||||||
|
|
||||||
// do budget "zero"
|
// this is the date format we need:
|
||||||
$emptyBudget = new Budget;
|
// define period to group on:
|
||||||
$emptyBudget->id = 0;
|
$carbonFormat = 'Y-m-d';
|
||||||
$emptyBudget->name = strval(trans('firefly.no_budget'));
|
// monthly report (for year)
|
||||||
$budgets->push($emptyBudget);
|
if ($start->diffInMonths($end) > 1) {
|
||||||
|
$carbonFormat = 'Y-m';
|
||||||
// get all budgets and years.
|
|
||||||
foreach ($budgets as $budget) {
|
|
||||||
$data[$budget->id] = [
|
|
||||||
'name' => $budget->name,
|
|
||||||
'entries' => $this->repository->filterAmounts($queryResult, $budget->id, $periods),
|
|
||||||
'sum' => '0',
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
// filter out empty ones and fill sum:
|
|
||||||
|
// yearly report (for multi year)
|
||||||
|
if ($start->diffInMonths($end) > 12) {
|
||||||
|
$carbonFormat = 'Y';
|
||||||
|
}
|
||||||
|
|
||||||
|
// this is the set of transactions for this period
|
||||||
|
// in these budgets. Now they must be grouped (manually)
|
||||||
|
// id, period => amount
|
||||||
|
$data = [];
|
||||||
|
foreach ($transactions as $transaction) {
|
||||||
|
$budgetId = max(intval($transaction->transaction_journal_budget_id), intval($transaction->transaction_budget_id));
|
||||||
|
$date = $transaction->date->format($carbonFormat);
|
||||||
|
|
||||||
|
if (!isset($data[$budgetId])) {
|
||||||
|
$data[$budgetId]['name'] = $this->getBudgetName($budgetId, $budgets);
|
||||||
|
$data[$budgetId]['sum'] = '0';
|
||||||
|
$data[$budgetId]['entries'] = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isset($data[$budgetId]['entries'][$date])) {
|
||||||
|
$data[$budgetId]['entries'][$date] = '0';
|
||||||
|
}
|
||||||
|
$data[$budgetId]['entries'][$date] = bcadd($data[$budgetId]['entries'][$date], $transaction->transaction_amount);
|
||||||
|
}
|
||||||
|
// and now the same for stuff without a budget:
|
||||||
|
/** @var JournalCollectorInterface $collector */
|
||||||
|
$collector = app(JournalCollectorInterface::class);
|
||||||
|
$collector->setAllAssetAccounts()->setRange($start, $end);
|
||||||
|
$collector->setTypes([TransactionType::WITHDRAWAL]);
|
||||||
|
$collector->withoutBudget();
|
||||||
|
$transactions = $collector->getJournals();
|
||||||
|
|
||||||
|
$data[0]['entries'] = [];
|
||||||
|
$data[0]['name'] = strval(trans('firefly.no_budget'));
|
||||||
|
$data[0]['sum'] = '0';
|
||||||
|
|
||||||
|
foreach ($transactions as $transaction) {
|
||||||
|
$date = $transaction->date->format($carbonFormat);
|
||||||
|
|
||||||
|
if (!isset($data[0]['entries'][$date])) {
|
||||||
|
$data[0]['entries'][$date] = '0';
|
||||||
|
}
|
||||||
|
$data[0]['entries'][$date] = bcadd($data[0]['entries'][$date], $transaction->transaction_amount);
|
||||||
|
}
|
||||||
|
|
||||||
$data = $this->filterBudgetPeriodReport($data);
|
$data = $this->filterBudgetPeriodReport($data);
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
@ -209,4 +250,25 @@ class BudgetReportHelper implements BudgetReportHelperInterface
|
|||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $budgetId
|
||||||
|
* @param Collection $budgets
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function getBudgetName(int $budgetId, Collection $budgets): string
|
||||||
|
{
|
||||||
|
|
||||||
|
$first = $budgets->filter(
|
||||||
|
function (Budget $budget) use ($budgetId) {
|
||||||
|
return $budgetId === $budget->id;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
if (!is_null($first->first())) {
|
||||||
|
return $first->first()->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
return '(unknown)';
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ class BudgetController extends Controller
|
|||||||
* @param Carbon $end
|
* @param Carbon $end
|
||||||
* @param Collection $accounts
|
* @param Collection $accounts
|
||||||
*
|
*
|
||||||
* @return mixed|string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function budgetPeriodReport(BudgetReportHelperInterface $helper, Carbon $start, Carbon $end, Collection $accounts)
|
public function budgetPeriodReport(BudgetReportHelperInterface $helper, Carbon $start, Carbon $end, Collection $accounts)
|
||||||
{
|
{
|
||||||
@ -46,7 +46,7 @@ class BudgetController extends Controller
|
|||||||
$cache->addProperty('budget-period-report');
|
$cache->addProperty('budget-period-report');
|
||||||
$cache->addProperty($accounts->pluck('id')->toArray());
|
$cache->addProperty($accounts->pluck('id')->toArray());
|
||||||
if ($cache->has()) {
|
if ($cache->has()) {
|
||||||
return $cache->get();
|
return $cache->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
$periods = Navigation::listOfPeriods($start, $end);
|
$periods = Navigation::listOfPeriods($start, $end);
|
||||||
|
@ -14,10 +14,17 @@
|
|||||||
<td data-value="{{ info.name }}">
|
<td data-value="{{ info.name }}">
|
||||||
<a title="{{ info.name }}" href="#" data-budget="{{ id }}" class="budget-chart-activate">{{ info.name }}</a>
|
<a title="{{ info.name }}" href="#" data-budget="{{ id }}" class="budget-chart-activate">{{ info.name }}</a>
|
||||||
</td>
|
</td>
|
||||||
{% for amount in info.entries %}
|
{% for key, period in periods %}
|
||||||
<td data-value="{{ amount }}">
|
{% if(info.entries[key]) %}
|
||||||
{{ amount|formatAmount }}
|
<td data-value="{{ info.entries[key] }}">
|
||||||
</td>
|
{{ info.entries[key]|formatAmount }}
|
||||||
|
</td>
|
||||||
|
{% else %}
|
||||||
|
<td data-value="0">
|
||||||
|
{{ 0|formatAmount }}
|
||||||
|
</td>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<td data-value="{{ info.sum }}">
|
<td data-value="{{ info.sum }}">
|
||||||
{{ info.sum|formatAmount }}
|
{{ info.sum|formatAmount }}
|
||||||
|
Loading…
Reference in New Issue
Block a user