New filters to clean up the journal collector.

This commit is contained in:
James Cole 2017-04-28 18:02:54 +02:00
parent 5de8fce156
commit 42322055f9
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
6 changed files with 286 additions and 0 deletions

View File

@ -0,0 +1,50 @@
<?php
/**
* AmountFilter.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
* 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;
}
);
}
}

View File

@ -0,0 +1,36 @@
<?php
/**
* EmptyFilter.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
* 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;
}
}

View File

@ -0,0 +1,27 @@
<?php
/**
* FilterInterface.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
* 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;
}

View File

@ -0,0 +1,62 @@
<?php
/**
* InternalTransferFilter.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
* 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;
}
);
}
}

View File

@ -0,0 +1,52 @@
<?php
/**
* OpposingAccountFilter.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
* 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;
}
);
}
}

View File

@ -0,0 +1,59 @@
<?php
/**
* TransferFilter.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
* 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;
}
}