Modify search for #963

This commit is contained in:
James Cole
2017-10-30 17:28:43 +01:00
parent 0bb46dd184
commit 5722622251
5 changed files with 188 additions and 40 deletions

View File

@@ -61,24 +61,8 @@ class Modifier
*/
public static function apply(array $modifier, Transaction $transaction): bool
{
$res = true;
switch ($modifier['type']) {
default:
throw new FireflyException(sprintf('Search modifier "%s" is not (yet) supported. Sorry!', $modifier['type']));
case 'amount':
case 'amount_is':
$res = self::amountCompare($transaction, $modifier['value'], 0);
Log::debug(sprintf('Amount is %s? %s', $modifier['value'], var_export($res, true)));
break;
case 'amount_min':
case 'amount_less':
$res = self::amountCompare($transaction, $modifier['value'], 1);
Log::debug(sprintf('Amount less than %s? %s', $modifier['value'], var_export($res, true)));
break;
case 'amount_max':
case 'amount_more':
$res = self::amountCompare($transaction, $modifier['value'], -1);
Log::debug(sprintf('Amount more than %s? %s', $modifier['value'], var_export($res, true)));
break;
case 'source':
$res = self::stringCompare($transaction->account_name, $modifier['value']);
Log::debug(sprintf('Source is %s? %s', $modifier['value'], var_export($res, true)));
@@ -99,25 +83,6 @@ class Modifier
$res = self::stringCompare(strval($transaction->bill_name), $modifier['value']);
Log::debug(sprintf('Bill is %s? %s', $modifier['value'], var_export($res, true)));
break;
case 'type':
$res = self::stringCompare($transaction->transaction_type_type, $modifier['value']);
Log::debug(sprintf('Transaction type is %s? %s', $modifier['value'], var_export($res, true)));
break;
case 'date':
case 'on':
$res = self::sameDate($transaction->date, $modifier['value']);
Log::debug(sprintf('Date same as %s? %s', $modifier['value'], var_export($res, true)));
break;
case 'date_before':
case 'before':
$res = self::dateBefore($transaction->date, $modifier['value']);
Log::debug(sprintf('Date before %s? %s', $modifier['value'], var_export($res, true)));
break;
case 'date_after':
case 'after':
$res = self::dateAfter($transaction->date, $modifier['value']);
Log::debug(sprintf('Date before %s? %s', $modifier['value'], var_export($res, true)));
break;
}
return $res;

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Support\Search;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Helpers\Filter\InternalTransferFilter;
@@ -121,6 +122,10 @@ class Search implements SearchInterface
if ($this->hasModifiers()) {
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
}
// some modifiers can be applied to the collector directly.
$collector = $this->applyModifiers($collector);
$collector->removeFilter(InternalTransferFilter::class);
$set = $collector->getPaginatedJournals()->getCollection();
@@ -186,6 +191,50 @@ class Search implements SearchInterface
$this->user = $user;
}
private function applyModifiers(JournalCollectorInterface $collector): JournalCollectorInterface
{
foreach ($this->modifiers as $modifier) {
switch ($modifier['type']) {
case 'amount_is':
case 'amount':
$amount = app('steam')->positive(strval($modifier['value']));
$collector->amountIs($amount);
break;
case 'amount_min':
case 'amount_less':
$amount = app('steam')->positive(strval($modifier['value']));
$collector->amountLess($amount);
break;
case 'amount_max':
case 'amount_more':
$amount = app('steam')->positive(strval($modifier['value']));
$collector->amountMore($amount);
break;
case 'type':
$collector->setTypes([ucfirst($modifier['value'])]);
break;
case 'date':
case 'on':
$start = new Carbon($modifier['value']);
$collector->setRange($start, $start);
break;
case 'date_before':
case 'before':
$before = new Carbon($modifier['value']);
$collector->setBefore($before);
break;
case 'date_after':
case 'after':
$after = new Carbon($modifier['value']);
$collector->setBefore($after);
break;
}
}
return $collector;
}
/**
* @param string $string
*/