Skip to content

Firefly III: a personal finances manager

Reinstate support class.

James Coleaaeae992e184
diff --git a/app/Generator/Report/Budget/MonthReportGenerator.php b/app/Generator/Report/Budget/MonthReportGenerator.php
index a79e69517a..07d34f9524 100644
--- a/app/Generator/Report/Budget/MonthReportGenerator.php
+++ b/app/Generator/Report/Budget/MonthReportGenerator.php
@@ -16,6 +16,7 @@ namespace FireflyIII\Generator\Report\Budget;
use Carbon\Carbon;
use FireflyIII\Generator\Report\ReportGeneratorInterface;
+use FireflyIII\Generator\Report\Support;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Helpers\Filter\OpposingAccountFilter;
use FireflyIII\Helpers\Filter\PositiveAmountFilter;
@@ -30,7 +31,7 @@ use Log;
*
* @package FireflyIII\Generator\Report\Budget
*/
-class MonthReportGenerator implements ReportGeneratorInterface
+class MonthReportGenerator extends Support implements ReportGeneratorInterface
{
/** @var Collection */
private $accounts;
diff --git a/app/Generator/Report/Category/MonthReportGenerator.php b/app/Generator/Report/Category/MonthReportGenerator.php
index 5537ef1e27..654d819417 100644
--- a/app/Generator/Report/Category/MonthReportGenerator.php
+++ b/app/Generator/Report/Category/MonthReportGenerator.php
@@ -16,6 +16,7 @@ namespace FireflyIII\Generator\Report\Category;
use Carbon\Carbon;
use FireflyIII\Generator\Report\ReportGeneratorInterface;
+use FireflyIII\Generator\Report\Support;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Helpers\Filter\NegativeAmountFilter;
use FireflyIII\Helpers\Filter\OpposingAccountFilter;
@@ -31,7 +32,7 @@ use Log;
*
* @package FireflyIII\Generator\Report\Category
*/
-class MonthReportGenerator implements ReportGeneratorInterface
+class MonthReportGenerator extends Support implements ReportGeneratorInterface
{
/** @var Collection */
private $accounts;
diff --git a/app/Generator/Report/Support.php b/app/Generator/Report/Support.php
new file mode 100644
index 0000000000..563e72f12b
--- /dev/null
+++ b/app/Generator/Report/Support.php
@@ -0,0 +1,148 @@
+<?php
+/**
+ * Support.php
+ * Copyright (C) 2016 [email protected]
+ *
+ * This software may be modified and distributed under the terms of the
+ * Creative Commons Attribution-ShareAlike 4.0 International License.
+ *
+ * See the LICENSE file for details.
+ */
+
+declare(strict_types=1);
+
+namespace FireflyIII\Generator\Report;
+
+use FireflyIII\Models\Transaction;
+use Illuminate\Support\Collection;
+use Log;
+
+
+/**
+ * Class Support
+ *
+ * @package FireflyIII\Generator\Report\Category
+ */
+class Support
+{
+ /**
+ * @return Collection
+ */
+ public function getTopExpenses(): Collection
+ {
+ $transactions = $this->getExpenses()->sortBy('transaction_amount');
+
+ return $transactions;
+ }
+
+ /**
+ * @return Collection
+ */
+ public function getTopIncome(): Collection
+ {
+ $transactions = $this->getIncome()->sortByDesc('transaction_amount');
+
+ return $transactions;
+ }
+
+ /**
+ * @param Collection $collection
+ * @param int $sortFlag
+ *
+ * @return array
+ */
+ protected function getAverages(Collection $collection, int $sortFlag): array
+ {
+ $result = [];
+ /** @var Transaction $transaction */
+ foreach ($collection as $transaction) {
+ // opposing name and ID:
+ $opposingId = $transaction->opposing_account_id;
+
+ // is not set?
+ if (!isset($result[$opposingId])) {
+ $name = $transaction->opposing_account_name;
+ $result[$opposingId] = [
+ 'name' => $name,
+ 'count' => 1,
+ 'id' => $opposingId,
+ 'average' => $transaction->transaction_amount,
+ 'sum' => $transaction->transaction_amount,
+ ];
+ continue;
+ }
+ $result[$opposingId]['count']++;
+ $result[$opposingId]['sum'] = bcadd($result[$opposingId]['sum'], $transaction->transaction_amount);
+ $result[$opposingId]['average'] = bcdiv($result[$opposingId]['sum'], strval($result[$opposingId]['count']));
+ }
+
+ // sort result by average:
+ $average = [];
+ foreach ($result as $key => $row) {
+ $average[$key] = floatval($row['average']);
+ }
+
+ array_multisort($average, $sortFlag, $result);
+
+ return $result;
+ }
+
+ /**
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's exactly five.
+ * @param array $spent
+ * @param array $earned
+ *
+ * @return array
+ */
+ protected function getObjectSummary(array $spent, array $earned): array
+ {
+ $return = [];
+
+ /**
+ * @var int $accountId
+ * @var string $entry
+ */
+ foreach ($spent as $objectId => $entry) {
+ if (!isset($return[$objectId])) {
+ $return[$objectId] = ['spent' => 0, 'earned' => 0];
+ }
+
+ $return[$objectId]['spent'] = $entry;
+ }
+ unset($entry);
+
+ /**
+ * @var int $accountId
+ * @var string $entry
+ */
+ foreach ($earned as $objectId => $entry) {
+ if (!isset($return[$objectId])) {
+ $return[$objectId] = ['spent' => 0, 'earned' => 0];
+ }
+
+ $return[$objectId]['earned'] = $entry;
+ }
+
+
+ return $return;
+ }
+
+ /**
+ * @param Collection $collection
+ *
+ * @return array
+ */
+ protected function summarizeByAccount(Collection $collection): array
+ {
+ $result = [];
+ /** @var Transaction $transaction */
+ foreach ($collection as $transaction) {
+ $accountId = $transaction->account_id;
+ $result[$accountId] = $result[$accountId] ?? '0';
+ $result[$accountId] = bcadd($transaction->transaction_amount, $result[$accountId]);
+ }
+
+ return $result;
+ }
+
+}