Files
firefly-iii/app/Generator/Report/Support.php

213 lines
5.9 KiB
PHP
Raw Normal View History

2017-04-29 08:55:50 +02:00
<?php
/**
* Support.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2017-04-29 08:55:50 +02:00
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
2017-04-29 08:55:50 +02:00
*
2017-10-21 08:40:00 +02:00
* 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
2017-12-17 14:41:58 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-04-29 08:55:50 +02:00
*/
/** @noinspection PhpUndefinedMethodInspection */
/** @noinspection PhpUndefinedMethodInspection */
2017-04-29 08:55:50 +02:00
declare(strict_types=1);
namespace FireflyIII\Generator\Report;
2019-05-30 06:23:25 +02:00
use FireflyIII\Models\TransactionType;
2017-04-29 08:55:50 +02:00
/**
2017-11-15 12:25:49 +01:00
* Class Support.
* @method array getExpenses()
* @method array getIncome()
2019-02-13 17:38:41 +01:00
*
2018-08-24 07:18:33 +02:00
* @codeCoverageIgnore
2017-04-29 08:55:50 +02:00
*/
class Support
{
/**
* Get the top expenses.
*
* @return array
2017-04-29 08:55:50 +02:00
*/
public function getTopExpenses(): array
2017-04-29 08:55:50 +02:00
{
$expenses = $this->getExpenses();
usort($expenses, function ($a, $b) {
return $a['amount'] <=> $b['amount'];
});
return $expenses;
2017-04-29 08:55:50 +02:00
}
/**
* Get the top income.
*
* @return array
2017-04-29 08:55:50 +02:00
*/
public function getTopIncome(): array
2017-04-29 08:55:50 +02:00
{
$income = $this->getIncome();
usort($income, function ($a, $b) {
return $b['amount'] <=> $a['amount'];
});
return $income;
2017-04-29 08:55:50 +02:00
}
/**
* Get averages from a collection.
*
* @param array $array
* @param int $sortFlag
2017-04-29 08:55:50 +02:00
*
* @return array
*/
protected function getAverages(array $array, int $sortFlag): array
2017-04-29 08:55:50 +02:00
{
$result = [];
/** @var array $journal */
foreach ($array as $journal) {
2017-04-29 08:55:50 +02:00
// opposing name and ID:
$opposingId = $journal['destination_account_id'];
2017-04-29 08:55:50 +02:00
// is not set?
if (!isset($result[$opposingId])) {
$name = $journal['destination_account_name'];
2017-04-29 08:55:50 +02:00
$result[$opposingId] = [
'name' => $name,
'count' => 1,
'id' => $opposingId,
'average' => $journal['amount'],
'sum' => $journal['amount'],
2017-04-29 08:55:50 +02:00
];
continue;
}
2017-11-15 12:25:49 +01:00
++$result[$opposingId]['count'];
$result[$opposingId]['sum'] = bcadd($result[$opposingId]['sum'], $journal['amount']);
2018-04-02 14:42:07 +02:00
$result[$opposingId]['average'] = bcdiv($result[$opposingId]['sum'], (string)$result[$opposingId]['count']);
2017-04-29 08:55:50 +02:00
}
// sort result by average:
$average = [];
foreach ($result as $key => $row) {
2018-04-02 14:42:07 +02:00
$average[$key] = (float)$row['average'];
2017-04-29 08:55:50 +02:00
}
array_multisort($average, $sortFlag, $result);
return $result;
}
/**
* Summarize collection by earned and spent data.
*
2017-04-29 08:55:50 +02:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's exactly five.
2017-11-15 12:25:49 +01:00
*
2017-04-29 08:55:50 +02:00
* @param array $spent
* @param array $earned
*
* @return array
*/
protected function getObjectSummary(array $spent, array $earned): array
{
2018-01-12 20:53:18 +01:00
$return = [
'sum' => [
'spent' => '0',
'earned' => '0',
],
];
2017-04-29 08:55:50 +02:00
/**
2017-11-15 12:25:49 +01:00
* @var int
2017-04-29 08:55:50 +02:00
* @var string $entry
*/
foreach ($spent as $objectId => $entry) {
if (!isset($return[$objectId])) {
2018-01-12 20:53:18 +01:00
$return[$objectId] = ['spent' => '0', 'earned' => '0'];
2017-04-29 08:55:50 +02:00
}
$return[$objectId]['spent'] = $entry;
2018-01-12 20:53:18 +01:00
$return['sum']['spent'] = bcadd($return['sum']['spent'], $entry);
2017-04-29 08:55:50 +02:00
}
/**
2017-11-15 12:25:49 +01:00
* @var int
2017-04-29 08:55:50 +02:00
* @var string $entry
*/
foreach ($earned as $objectId => $entry) {
2019-05-30 06:23:25 +02:00
$entry = bcmul($entry, '-1');
2017-04-29 08:55:50 +02:00
if (!isset($return[$objectId])) {
2018-01-12 20:53:18 +01:00
$return[$objectId] = ['spent' => '0', 'earned' => '0'];
2017-04-29 08:55:50 +02:00
}
$return[$objectId]['earned'] = $entry;
2018-01-12 21:08:59 +01:00
$return['sum']['earned'] = bcadd($return['sum']['earned'], $entry);
2017-04-29 08:55:50 +02:00
}
return $return;
}
/**
* Summarize the data by account.
*
* @param array $array
2017-04-29 08:55:50 +02:00
*
* @return array
*/
protected function summarizeByAccount(array $array): array
2017-04-29 08:55:50 +02:00
{
2018-03-09 05:52:52 +01:00
$result = [];
/** @var array $journal */
foreach ($array as $journal) {
$accountId = $journal['source_account_id'] ?? 0;
2017-04-29 08:55:50 +02:00
$result[$accountId] = $result[$accountId] ?? '0';
$result[$accountId] = bcadd($journal['amount'], $result[$accountId]);
2017-04-29 08:55:50 +02:00
}
return $result;
}
2019-05-30 06:23:25 +02:00
/**
* 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;
}
2017-04-29 08:55:50 +02:00
}