Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
New filters to clean up the journal collector.
James Cole42322055f910
diff --git a/app/Helpers/Filter/AmountFilter.php b/app/Helpers/Filter/AmountFilter.phpnew file mode 100644index 0000000000..236f114ea4--- /dev/null+++ b/app/Helpers/Filter/AmountFilter.php@@ -0,0 +1,50 @@+<?php+/**+ * AmountFilter.php+ * Copyright (c) 2017 [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\Helpers\Filter;++use FireflyIII\Models\Transaction;+use Illuminate\Support\Collection;+use Log;++/**+ * Class AmountFilter+ *+ * This filter removes transactions with either a positive amount ($parameters = 1) or a negative amount+ * ($parameter = -1). This is helpful when a Collection has you with both transactions in a journal.+ *+ * @package FireflyIII\Helpers\Filter+ */+class AmountFilter implements FilterInterface+{++ /**+ * @param Collection $set+ * @param null $parameters+ *+ * @return Collection+ */+ public function filter(Collection $set, $parameters = null): Collection+ {+ return $set->filter(+ function (Transaction $transaction) use ($parameters) {+ // remove by amount+ if (bccomp($transaction->transaction_amount, '0') === $parameters) {+ Log::debug(sprintf('Filtered #%d because amount is %f.', $transaction->id, $transaction->transaction_amount));++ return null;+ }++ return $transaction;+ }+ );+ }+}\ No newline at end of filediff --git a/app/Helpers/Filter/EmptyFilter.php b/app/Helpers/Filter/EmptyFilter.phpnew file mode 100644index 0000000000..a3c3dd20d3--- /dev/null+++ b/app/Helpers/Filter/EmptyFilter.php@@ -0,0 +1,36 @@+<?php+/**+ * EmptyFilter.php+ * Copyright (c) 2017 [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\Helpers\Filter;++use Illuminate\Support\Collection;++/**+ * Class EmptyFilter+ *+ * @package FireflyIII\Helpers\Filter+ */+class EmptyFilter implements FilterInterface+{+++ /**+ * @param Collection $set+ * @param null $parameters+ *+ * @return Collection+ */+ public function filter(Collection $set, $parameters = null): Collection+ {+ // TODO: Implement filter() method.+ throw new NotImplementedException;+ }+}\ No newline at end of filediff --git a/app/Helpers/Filter/FilterInterface.php b/app/Helpers/Filter/FilterInterface.phpnew file mode 100644index 0000000000..964d2ac7b2--- /dev/null+++ b/app/Helpers/Filter/FilterInterface.php@@ -0,0 +1,27 @@+<?php+/**+ * FilterInterface.php+ * Copyright (c) 2017 [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\Helpers\Filter;+++use Illuminate\Support\Collection;++interface FilterInterface+{+ /**+ * @param Collection $set+ * @param null $parameters+ *+ * @return Collection+ */+ public function filter(Collection $set, $parameters = null): Collection;++}\ No newline at end of filediff --git a/app/Helpers/Filter/InternalTransferFilter.php b/app/Helpers/Filter/InternalTransferFilter.phpnew file mode 100644index 0000000000..87d6d750d0--- /dev/null+++ b/app/Helpers/Filter/InternalTransferFilter.php@@ -0,0 +1,62 @@+<?php+/**+ * InternalTransferFilter.php+ * Copyright (c) 2017 [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\Helpers\Filter;++use FireflyIII\Models\Transaction;+use Illuminate\Support\Collection;+use Log;++/**+ * Class InternalTransferFilter+ *+ * This filter removes any filters that are from A to B or from B to A given a set of+ * account id's (in $parameters) where A and B are mentioned. So transfers between the mentioned+ * accounts will be removed.+ *+ * @package FireflyIII\Helpers\Filter+ */+class InternalTransferFilter implements FilterInterface+{++ /**+ * @param Collection $set+ * @param null $parameters+ *+ * @return Collection+ */+ public function filter(Collection $set, $parameters = null): Collection+ {+ return $set->filter(+ function (Transaction $transaction) use ($parameters) {+ if (is_null($transaction->opposing_account_id)) {+ return $transaction;+ }+ // both id's in $parameters?+ if (in_array($transaction->account_id, $parameters) && in_array($transaction->opposing_account_id, $parameters)) {+ Log::debug(+ sprintf(+ 'Transaction #%d has #%d and #%d in set, so removed',+ $transaction->id, $transaction->account_id, $transaction->opposing_account_id+ ), $parameters+ );++ return false;+ }++ return $transaction;++ }+ );+++ }+}\ No newline at end of filediff --git a/app/Helpers/Filter/OpposingAccountFilter.php b/app/Helpers/Filter/OpposingAccountFilter.phpnew file mode 100644index 0000000000..0d21356c1d--- /dev/null+++ b/app/Helpers/Filter/OpposingAccountFilter.php@@ -0,0 +1,52 @@+<?php+/**+ * OpposingAccountFilter.php+ * Copyright (c) 2017 [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\Helpers\Filter;+++use FireflyIII\Models\Transaction;+use Illuminate\Support\Collection;+use Log;++/**+ * Class OpposingAccountFilter+ *+ * This filter is similar to the internal transfer filter but only removes transactions when the opposing account is+ * amongst $parameters (list of account ID's).+ *+ * @package FireflyIII\Helpers\Filter+ */+class OpposingAccountFilter implements FilterInterface+{++ /**+ * @param Collection $set+ * @param null $parameters+ *+ * @return Collection+ */+ public function filter(Collection $set, $parameters = null): Collection+ {+ return $set->filter(+ function (Transaction $transaction) use ($parameters) {+ $opposing = $transaction->opposing_account_id;+ // remove internal transfer+ if (in_array($opposing, $parameters)) {+ Log::debug(sprintf('Filtered #%d because its opposite is in accounts.', $transaction->id), $parameters);++ return null;+ }++ return $transaction;+ }+ );+ }+}\ No newline at end of filediff --git a/app/Helpers/Filter/TransferFilter.php b/app/Helpers/Filter/TransferFilter.phpnew file mode 100644index 0000000000..f17ae6b399--- /dev/null+++ b/app/Helpers/Filter/TransferFilter.php@@ -0,0 +1,59 @@+<?php+/**+ * TransferFilter.php+ * Copyright (c) 2017 [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\Helpers\Filter;++use FireflyIII\Models\Transaction;+use FireflyIII\Models\TransactionType;+use Illuminate\Support\Collection;+use Steam;++/**+ * Class TransferFilter+ *+ * This filter removes any transfers that are in the collection twice (from A to B and from B to A).+ *+ * @package FireflyIII\Helpers\Filter+ */+class TransferFilter implements FilterInterface+{+ /**+ * @param Collection $set+ * @param null $parameters+ *+ * @return Collection+ */+ public function filter(Collection $set, $parameters = null): Collection+ {+ $count = [];+ $new = new Collection;+ /** @var Transaction $transaction */+ foreach ($set as $transaction) {+ if ($transaction->transaction_type_type !== TransactionType::TRANSFER) {+ $new->push($transaction);+ continue;+ }+ // make property string:+ $journalId = $transaction->transaction_journal_id;+ $amount = Steam::positive($transaction->transaction_amount);+ $accountIds = [intval($transaction->account_id), intval($transaction->opposing_account_id)];+ sort($accountIds);+ $key = $journalId . '-' . join(',', $accountIds) . '-' . $amount;+ if (!isset($count[$key])) {+ // not yet counted? add to new set and count it:+ $new->push($transaction);+ $count[$key] = 1;+ }+ }++ return $new;+ }+}\ No newline at end of file