mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-04 19:53:44 +00:00
Revamped tag report as well.
This commit is contained in:
@@ -39,7 +39,7 @@ use Throwable;
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class MonthReportGenerator extends Support implements ReportGeneratorInterface
|
||||
class MonthReportGenerator implements ReportGeneratorInterface
|
||||
{
|
||||
/** @var Collection The accounts in the report. */
|
||||
private $accounts;
|
||||
|
@@ -39,7 +39,7 @@ use Throwable;
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class MonthReportGenerator extends Support implements ReportGeneratorInterface
|
||||
class MonthReportGenerator implements ReportGeneratorInterface
|
||||
{
|
||||
/** @var Collection The included accounts */
|
||||
private $accounts;
|
||||
|
@@ -1,211 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Support.php
|
||||
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/** @noinspection PhpUndefinedMethodInspection */
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Generator\Report;
|
||||
|
||||
use FireflyIII\Models\TransactionType;
|
||||
|
||||
/**
|
||||
* Class Support.
|
||||
* @method array getExpenses()
|
||||
* @method array getIncome()
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class Support
|
||||
{
|
||||
/**
|
||||
* Get the top expenses.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTopExpenses(): array
|
||||
{
|
||||
$expenses = $this->getExpenses();
|
||||
usort($expenses, function ($a, $b) {
|
||||
return $a['amount'] <=> $b['amount'];
|
||||
});
|
||||
|
||||
return $expenses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the top income.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTopIncome(): array
|
||||
{
|
||||
$income = $this->getIncome();
|
||||
usort($income, function ($a, $b) {
|
||||
return $b['amount'] <=> $a['amount'];
|
||||
});
|
||||
|
||||
return $income;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get averages from a collection.
|
||||
*
|
||||
* @param array $array
|
||||
* @param int $sortFlag
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getAverages(array $array, int $sortFlag): array
|
||||
{
|
||||
$result = [];
|
||||
/** @var array $journal */
|
||||
foreach ($array as $journal) {
|
||||
// opposing name and ID:
|
||||
$opposingId = $journal['destination_account_id'];
|
||||
|
||||
// is not set?
|
||||
if (!isset($result[$opposingId])) {
|
||||
$name = $journal['destination_account_name'];
|
||||
$result[$opposingId] = [
|
||||
'name' => $name,
|
||||
'count' => 1,
|
||||
'id' => $opposingId,
|
||||
'average' => $journal['amount'],
|
||||
'sum' => $journal['amount'],
|
||||
];
|
||||
continue;
|
||||
}
|
||||
++$result[$opposingId]['count'];
|
||||
$result[$opposingId]['sum'] = bcadd($result[$opposingId]['sum'], $journal['amount']);
|
||||
$result[$opposingId]['average'] = bcdiv($result[$opposingId]['sum'], (string)$result[$opposingId]['count']);
|
||||
}
|
||||
|
||||
// sort result by average:
|
||||
$average = [];
|
||||
foreach ($result as $key => $row) {
|
||||
$average[$key] = (float)$row['average'];
|
||||
}
|
||||
|
||||
array_multisort($average, $sortFlag, $result);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Summarize collection by earned and spent data.
|
||||
*
|
||||
* // it's exactly five.
|
||||
*
|
||||
* @param array $spent
|
||||
* @param array $earned
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getObjectSummary(array $spent, array $earned): array
|
||||
{
|
||||
$return = [
|
||||
'sum' => [
|
||||
'spent' => '0',
|
||||
'earned' => '0',
|
||||
],
|
||||
];
|
||||
|
||||
/**
|
||||
* @var int
|
||||
* @var string $entry
|
||||
*/
|
||||
foreach ($spent as $objectId => $entry) {
|
||||
if (!isset($return[$objectId])) {
|
||||
$return[$objectId] = ['spent' => '0', 'earned' => '0'];
|
||||
}
|
||||
|
||||
$return[$objectId]['spent'] = $entry;
|
||||
$return['sum']['spent'] = bcadd($return['sum']['spent'], $entry);
|
||||
}
|
||||
|
||||
/**
|
||||
* @var int
|
||||
* @var string $entry
|
||||
*/
|
||||
foreach ($earned as $objectId => $entry) {
|
||||
$entry = bcmul($entry, '-1');
|
||||
if (!isset($return[$objectId])) {
|
||||
$return[$objectId] = ['spent' => '0', 'earned' => '0'];
|
||||
}
|
||||
|
||||
$return[$objectId]['earned'] = $entry;
|
||||
$return['sum']['earned'] = bcadd($return['sum']['earned'], $entry);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Summarize the data by account.
|
||||
*
|
||||
* @param array $array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function summarizeByAccount(array $array): array
|
||||
{
|
||||
$result = [];
|
||||
/** @var array $journal */
|
||||
foreach ($array as $journal) {
|
||||
$accountId = $journal['source_account_id'] ?? 0;
|
||||
$result[$accountId] = $result[$accountId] ?? '0';
|
||||
$result[$accountId] = bcadd($journal['amount'], $result[$accountId]);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Summarize the data by the asset account or liability, depending on the type.
|
||||
*
|
||||
* In case of transfers, it will choose the source account.
|
||||
*
|
||||
* @param array $array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function summarizeByAssetAccount(array $array): array
|
||||
{
|
||||
$result = [];
|
||||
/** @var array $journal */
|
||||
foreach ($array as $journal) {
|
||||
$accountId = 0;
|
||||
switch ($journal['transaction_type_type']) {
|
||||
case TransactionType::WITHDRAWAL:
|
||||
case TransactionType::TRANSFER:
|
||||
$accountId = $journal['source_account_id'] ?? 0;
|
||||
break;
|
||||
case TransactionType::DEPOSIT:
|
||||
$accountId = $journal['destination_account_id'] ?? 0;
|
||||
break;
|
||||
}
|
||||
|
||||
$result[$accountId] = $result[$accountId] ?? '0';
|
||||
$result[$accountId] = bcadd($journal['amount'], $result[$accountId]);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
@@ -28,8 +28,6 @@ namespace FireflyIII\Generator\Report\Tag;
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Generator\Report\ReportGeneratorInterface;
|
||||
use FireflyIII\Generator\Report\Support;
|
||||
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use Illuminate\Support\Collection;
|
||||
use Log;
|
||||
use Throwable;
|
||||
@@ -39,7 +37,7 @@ use Throwable;
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
class MonthReportGenerator extends Support implements ReportGeneratorInterface
|
||||
class MonthReportGenerator implements ReportGeneratorInterface
|
||||
{
|
||||
/** @var Collection The accounts involved */
|
||||
private $accounts;
|
||||
@@ -71,30 +69,18 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
|
||||
*/
|
||||
public function generate(): string
|
||||
{
|
||||
$accountIds = implode(',', $this->accounts->pluck('id')->toArray());
|
||||
$tagTags = implode(',', $this->tags->pluck('tag')->toArray());
|
||||
$tagIds = implode(',', $this->tags->pluck('id')->toArray());
|
||||
$reportType = 'tag';
|
||||
$expenses = $this->getExpenses();
|
||||
$income = $this->getIncome();
|
||||
$accountSummary = $this->getObjectSummary($this->summarizeByAssetAccount($expenses), $this->summarizeByAssetAccount($income));
|
||||
$tagSummary = $this->getObjectSummary($this->summarizeByTag($expenses), $this->summarizeByTag($income));
|
||||
$averageExpenses = $this->getAverages($expenses, SORT_ASC);
|
||||
$averageIncome = $this->getAverages($income, SORT_DESC);
|
||||
$topExpenses = $this->getTopExpenses();
|
||||
$topIncome = $this->getTopIncome();
|
||||
$accountIds = implode(',', $this->accounts->pluck('id')->toArray());
|
||||
$tagIds = implode(',', $this->tags->pluck('id')->toArray());
|
||||
$reportType = 'tag';
|
||||
|
||||
// render!
|
||||
try {
|
||||
$result = view(
|
||||
'reports.tag.month', compact(
|
||||
'accountIds', 'tagTags', 'reportType', 'accountSummary', 'tagSummary', 'averageExpenses', 'averageIncome', 'topIncome',
|
||||
'topExpenses', 'tagIds'
|
||||
)
|
||||
'reports.tag.month', compact('accountIds', 'reportType', 'tagIds')
|
||||
)->with('start', $this->start)->with('end', $this->end)->with('tags', $this->tags)->with('accounts', $this->accounts)->render();
|
||||
} catch (Throwable $e) {
|
||||
Log::error(sprintf('Cannot render reports.tag.month: %s', $e->getMessage()));
|
||||
$result = 'Could not render report view.';
|
||||
$result = sprintf('Could not render report view: %s', $e->getMessage());
|
||||
}
|
||||
|
||||
return $result;
|
||||
@@ -192,79 +178,5 @@ class MonthReportGenerator extends Support implements ReportGeneratorInterface
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get expense collection for report.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getExpenses(): array
|
||||
{
|
||||
if (count($this->expenses) > 0) {
|
||||
Log::debug('Return previous set of expenses.');
|
||||
|
||||
return $this->expenses;
|
||||
}
|
||||
|
||||
/** @var GroupCollectorInterface $collector */
|
||||
$collector = app(GroupCollectorInterface::class);
|
||||
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)
|
||||
->setTypes([TransactionType::WITHDRAWAL, TransactionType::TRANSFER])
|
||||
->setTags($this->tags)->withAccountInformation();
|
||||
|
||||
$journals = $collector->getExtractedJournals();
|
||||
$this->expenses = $journals;
|
||||
|
||||
return $journals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the income for this report.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getIncome(): array
|
||||
{
|
||||
if (count($this->income) > 0) {
|
||||
return $this->income;
|
||||
}
|
||||
|
||||
/** @var GroupCollectorInterface $collector */
|
||||
$collector = app(GroupCollectorInterface::class);
|
||||
$collector->setAccounts($this->accounts)->setRange($this->start, $this->end)
|
||||
->setTypes([TransactionType::DEPOSIT, TransactionType::TRANSFER])
|
||||
->setTags($this->tags)->withAccountInformation();
|
||||
|
||||
$journals = $collector->getExtractedJournals();
|
||||
$this->income = $journals;
|
||||
|
||||
return $journals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Summarize by tag.
|
||||
*
|
||||
* @param array $array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function summarizeByTag(array $array): array
|
||||
{
|
||||
$tagIds = array_map('\intval', $this->tags->pluck('id')->toArray());
|
||||
$result = [];
|
||||
/** @var array $journal */
|
||||
foreach ($array as $journal) {
|
||||
/**
|
||||
* @var int $id
|
||||
* @var array $tag
|
||||
*/
|
||||
foreach ($journal['tags'] as $id => $tag) {
|
||||
if (in_array($id, $tagIds, true)) {
|
||||
$result[$id] = $result[$id] ?? '0';
|
||||
$result[$id] = bcadd($journal['amount'], $result[$id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user