mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 23:45:10 +00:00
Some optimisations.
This commit is contained in:
@@ -232,6 +232,69 @@ class ReportQuery implements ReportQueryInterface
|
|||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method returns all "expense" journals in a certain period, which are both transfers to a shared account
|
||||||
|
* and "ordinary" withdrawals. The query used is almost equal to ReportQueryInterface::journalsByRevenueAccount but it does
|
||||||
|
* not group and returns different fields.
|
||||||
|
*
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
* @param bool $includeShared
|
||||||
|
*
|
||||||
|
* @return Collection
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function expenseInPeriod(Carbon $start, Carbon $end, $includeShared = false) {
|
||||||
|
$query = $this->queryJournalsWithTransactions($start, $end);
|
||||||
|
if ($includeShared === false) {
|
||||||
|
// only get withdrawals not from a shared account
|
||||||
|
// and transfers from a shared account.
|
||||||
|
$query->where(
|
||||||
|
function (Builder $query) {
|
||||||
|
$query->where(
|
||||||
|
function (Builder $q) {
|
||||||
|
$q->where('transaction_types.type', 'Withdrawal');
|
||||||
|
$q->where('acm_from.data', '!=', '"sharedAsset"');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
$query->orWhere(
|
||||||
|
function (Builder $q) {
|
||||||
|
$q->where('transaction_types.type', 'Transfer');
|
||||||
|
$q->where('acm_to.data', '=', '"sharedAsset"');
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// any withdrawal is fine.
|
||||||
|
$query->where('transaction_types.type', 'Withdrawal');
|
||||||
|
}
|
||||||
|
$query->groupBy('transaction_journals.id')->orderBy('transaction_journals.date');
|
||||||
|
|
||||||
|
// get everything, decrypt and return
|
||||||
|
$data = $query->get(
|
||||||
|
['transaction_journals.id',
|
||||||
|
'transaction_journals.description',
|
||||||
|
'transaction_journals.encrypted',
|
||||||
|
'transaction_types.type',
|
||||||
|
DB::Raw('SUM(`t_from`.`amount`) as `queryAmount`'),
|
||||||
|
'transaction_journals.date',
|
||||||
|
't_to.account_id as account_id',
|
||||||
|
'ac_to.name as name',
|
||||||
|
'ac_to.encrypted as account_encrypted'
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
$data->each(
|
||||||
|
function (Model $object) {
|
||||||
|
$object->name = intval($object->account_encrypted) == 1 ? Crypt::decrypt($object->name) : $object->name;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
$data->sortByDesc('queryAmount');
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a list of expenses grouped by the budget they were filed under.
|
* Gets a list of expenses grouped by the budget they were filed under.
|
||||||
*
|
*
|
||||||
|
@@ -86,6 +86,20 @@ interface ReportQueryInterface
|
|||||||
*/
|
*/
|
||||||
public function incomeInPeriod(Carbon $start, Carbon $end, $includeShared = false);
|
public function incomeInPeriod(Carbon $start, Carbon $end, $includeShared = false);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method returns all "expense" journals in a certain period, which are both transfers to a shared account
|
||||||
|
* and "ordinary" withdrawals. The query used is almost equal to ReportQueryInterface::journalsByRevenueAccount but it does
|
||||||
|
* not group and returns different fields.
|
||||||
|
*
|
||||||
|
* @param Carbon $start
|
||||||
|
* @param Carbon $end
|
||||||
|
* @param bool $includeShared
|
||||||
|
*
|
||||||
|
* @return Collection
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function expenseInPeriod(Carbon $start, Carbon $end, $includeShared = false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a list of expenses grouped by the budget they were filed under.
|
* Gets a list of expenses grouped by the budget they were filed under.
|
||||||
*
|
*
|
||||||
|
@@ -266,8 +266,8 @@ class ReportController extends Controller
|
|||||||
$subTitleIcon = 'fa-bar-chart';
|
$subTitleIcon = 'fa-bar-chart';
|
||||||
$totalExpense = 0;
|
$totalExpense = 0;
|
||||||
$totalIncome = 0;
|
$totalIncome = 0;
|
||||||
$incomeTopLength = 5;
|
$incomeTopLength = 8;
|
||||||
$expenseTopLength = 10;
|
$expenseTopLength = 8;
|
||||||
|
|
||||||
if ($shared == 'shared') {
|
if ($shared == 'shared') {
|
||||||
$shared = true;
|
$shared = true;
|
||||||
@@ -327,10 +327,35 @@ class ReportController extends Controller
|
|||||||
* GET ALL EXPENSES
|
* GET ALL EXPENSES
|
||||||
* Summarized.
|
* Summarized.
|
||||||
*/
|
*/
|
||||||
$expenses = $this->query->journalsByExpenseAccount($date, $end, $shared);
|
$set = $this->query->expenseInPeriod($date, $end, $shared);
|
||||||
foreach ($expenses as $expense) {
|
// group, sort and sum:
|
||||||
$totalExpense += floatval($expense->queryAmount);
|
$expenses = [];
|
||||||
|
foreach ($set as $entry) {
|
||||||
|
$id = $entry->account_id;
|
||||||
|
$totalExpense += floatval($entry->queryAmount);
|
||||||
|
if (isset($expenses[$id])) {
|
||||||
|
$expenses[$id]['amount'] += floatval($entry->queryAmount);
|
||||||
|
$expenses[$id]['count']++;
|
||||||
|
} else {
|
||||||
|
$expenses[$id] = [
|
||||||
|
'amount' => floatval($entry->queryAmount),
|
||||||
|
'name' => $entry->name,
|
||||||
|
'count' => 1,
|
||||||
|
'id' => $id,
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// sort with callback:
|
||||||
|
uasort(
|
||||||
|
$expenses, function ($a, $b) {
|
||||||
|
if ($a['amount'] == $b['amount']) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ($a['amount'] < $b['amount']) ? -1 : 1;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
unset($set, $id);
|
||||||
|
|
||||||
return view(
|
return view(
|
||||||
'reports.year',
|
'reports.year',
|
||||||
|
@@ -125,16 +125,21 @@
|
|||||||
</div>
|
</div>
|
||||||
<table class="table">
|
<table class="table">
|
||||||
{% set sum =0 %}
|
{% set sum =0 %}
|
||||||
{% for expense in expenses %}
|
{% for row in expenses %}
|
||||||
{% if loop.index > expenseTopLength %}
|
{% if loop.index > expenseTopLength %}
|
||||||
<tr class="collapse out expenseCollapsed">
|
<tr class="collapse out expenseCollapsed">
|
||||||
{% else %}
|
{% else %}
|
||||||
<tr>
|
<tr>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<td><a href="{{ route('accounts.show',expense.id) }}">{{ expense.name }}</a></td>
|
<td>
|
||||||
<td><span class="text-danger">{{ expense.queryAmount|formatAmountPlain }}</span></td>
|
<a href="{{ route('accounts.show',row.id) }}">{{ row.name }}</a>
|
||||||
|
{% if row.count > 1 %}
|
||||||
|
<br /><small>{{ row.count }} {{ 'transactions'|_|lower }}</small>
|
||||||
|
{% endif %}
|
||||||
|
</td>
|
||||||
|
<td><span class="text-danger">{{ (row.amount*-1)|formatAmountPlain }}</span></td>
|
||||||
</tr>
|
</tr>
|
||||||
{% set sum = sum + (expense.queryAmount * -1) %}
|
{% set sum = sum + row.amount %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% if expenses|length > expenseTopLength %}
|
{% if expenses|length > expenseTopLength %}
|
||||||
<tr>
|
<tr>
|
||||||
|
Reference in New Issue
Block a user