This commit is contained in:
James Cole
2020-08-26 20:18:27 +02:00
parent c985683ee3
commit cd156d6991
7 changed files with 91 additions and 50 deletions

View File

@@ -72,15 +72,17 @@ class RuleController extends Controller
*/ */
public function trigger(Request $request): JsonResponse public function trigger(Request $request): JsonResponse
{ {
$count = (int) $request->get('count') > 0 ? (int) $request->get('count') : 1; $count = (int) $request->get('count') > 0 ? (int) $request->get('count') : 1;
$keys = array_keys(config('firefly.search.operators')); $operators = config('firefly.search.operators');
$triggers = []; $triggers = [];
foreach ($keys as $key) { foreach ($operators as $key => $operator) {
if ('user_action' !== $key) { if ('user_action' !== $key && false === $operator['alias']) {
$triggers[$key] = (string) trans('firefly.rule_trigger_' . $key . '_choice');
$triggers[$key] = (string) trans(sprintf('firefly.rule_trigger_%s_choice', $key));
} }
} }
asort($triggers); asort($triggers);
var_dump($triggers);exit;
try { try {
$view = view('rules.partials.trigger', compact('triggers', 'count'))->render(); $view = view('rules.partials.trigger', compact('triggers', 'count'))->render();
@@ -91,6 +93,7 @@ class RuleController extends Controller
} }
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
echo $view;exit;
return response()->json(['html' => $view]); return response()->json(['html' => $view]);
} }

View File

@@ -347,6 +347,21 @@ class OperatorQuerySearch implements SearchInterface
} }
break; break;
// //
// cash account
//
case 'source_is_cash':
$account = $this->getCashAccount();
$this->collector->setSourceAccounts(new Collection([$account]));
break;
case 'destination_is_cash':
$account = $this->getCashAccount();
$this->collector->setDestinationAccounts(new Collection([$account]));
break;
case 'account_is_cash':
$account = $this->getCashAccount();
$this->collector->setAccounts(new Collection([$account]));
break;
//
// description // description
// //
case 'description_starts': case 'description_starts':
@@ -713,4 +728,12 @@ class OperatorQuerySearch implements SearchInterface
{ {
$this->limit = $limit; $this->limit = $limit;
} }
/**
* @return Account
*/
private function getCashAccount(): Account
{
return $this->accountRepository->getCashAccount();
}
} }

View File

@@ -76,7 +76,7 @@ class FireflyValidator extends Validator
{ {
$field = $parameters[1] ?? 'id'; $field = $parameters[1] ?? 'id';
if (0 === (int)$value) { if (0 === (int) $value) {
return true; return true;
} }
$count = DB::table($parameters[0])->where('user_id', auth()->user()->id)->where($field, $value)->count(); $count = DB::table($parameters[0])->where('user_id', auth()->user()->id)->where($field, $value)->count();
@@ -179,7 +179,7 @@ class FireflyValidator extends Validator
$iban = str_replace($search, $replace, $iban); $iban = str_replace($search, $replace, $iban);
$checksum = bcmod($iban, '97'); $checksum = bcmod($iban, '97');
return 1 === (int)$checksum; return 1 === (int) $checksum;
} }
/** /**
@@ -193,7 +193,7 @@ class FireflyValidator extends Validator
/** @var mixed $compare */ /** @var mixed $compare */
$compare = $parameters[0] ?? '0'; $compare = $parameters[0] ?? '0';
return bccomp((string)$value, (string)$compare) < 0; return bccomp((string) $value, (string) $compare) < 0;
} }
/** /**
@@ -207,7 +207,7 @@ class FireflyValidator extends Validator
/** @var mixed $compare */ /** @var mixed $compare */
$compare = $parameters[0] ?? '0'; $compare = $parameters[0] ?? '0';
return bccomp((string)$value, (string)$compare) > 0; return bccomp((string) $value, (string) $compare) > 0;
} }
/** /**
@@ -221,7 +221,7 @@ class FireflyValidator extends Validator
{ {
$field = $parameters[1] ?? 'id'; $field = $parameters[1] ?? 'id';
if (0 === (int)$value) { if (0 === (int) $value) {
return true; return true;
} }
$count = DB::table($parameters[0])->where($field, $value)->count(); $count = DB::table($parameters[0])->where($field, $value)->count();
@@ -241,7 +241,7 @@ class FireflyValidator extends Validator
// first, get the index from this string: // first, get the index from this string:
$value = $value ?? ''; $value = $value ?? '';
$parts = explode('.', $attribute); $parts = explode('.', $attribute);
$index = (int)($parts[1] ?? '0'); $index = (int) ($parts[1] ?? '0');
// get the name of the trigger from the data array: // get the name of the trigger from the data array:
$actionType = $this->data['actions'][$index]['type'] ?? 'invalid'; $actionType = $this->data['actions'][$index]['type'] ?? 'invalid';
@@ -289,6 +289,12 @@ class FireflyValidator extends Validator
return null !== $account; return null !== $account;
} }
// must be numeric for piggy bank things:
if ('update_piggy' === $actionType) {
$value = str_replace(',', '.', $value);
return '' !== $value && is_numeric($value);
}
// return true for the rest. // return true for the rest.
return true; return true;
} }
@@ -305,7 +311,7 @@ class FireflyValidator extends Validator
{ {
// first, get the index from this string: // first, get the index from this string:
$parts = explode('.', $attribute); $parts = explode('.', $attribute);
$index = (int)($parts[1] ?? '0'); $index = (int) ($parts[1] ?? '0');
// get the name of the trigger from the data array: // get the name of the trigger from the data array:
$triggerType = $this->data['triggers'][$index]['type'] ?? 'invalid'; $triggerType = $this->data['triggers'][$index]['type'] ?? 'invalid';
@@ -337,7 +343,7 @@ class FireflyValidator extends Validator
} }
// if the type is date, the simply try to parse it and throw error when it's bad. // if the type is date, the simply try to parse it and throw error when it's bad.
if (in_array($triggerType, ['date_is'], true)) { if (in_array($triggerType, ['date_is', 'created_on', 'updated_on', 'date_before', 'date_after'], true)) {
/** @var ParseDateString $parser */ /** @var ParseDateString $parser */
$parser = app(ParseDateString::class); $parser = app(ParseDateString::class);
try { try {
@@ -371,7 +377,7 @@ class FireflyValidator extends Validator
{ {
$verify = false; $verify = false;
if (isset($this->data['verify_password'])) { if (isset($this->data['verify_password'])) {
$verify = 1 === (int)$this->data['verify_password']; $verify = 1 === (int) $this->data['verify_password'];
} }
if ($verify) { if ($verify) {
/** @var Verifier $service */ /** @var Verifier $service */
@@ -409,7 +415,7 @@ class FireflyValidator extends Validator
} }
$parameterId = $parameters[0] ?? null; $parameterId = $parameters[0] ?? null;
if (null !== $parameterId) { if (null !== $parameterId) {
return $this->validateByParameterId((int)$parameterId, $value); return $this->validateByParameterId((int) $parameterId, $value);
} }
if (isset($this->data['id'])) { if (isset($this->data['id'])) {
return $this->validateByAccountId($value); return $this->validateByAccountId($value);
@@ -428,9 +434,9 @@ class FireflyValidator extends Validator
*/ */
public function validateUniqueAccountNumberForUser($attribute, $value, $parameters): bool public function validateUniqueAccountNumberForUser($attribute, $value, $parameters): bool
{ {
$accountId = (int)($this->data['id'] ?? 0.0); $accountId = (int) ($this->data['id'] ?? 0.0);
if (0 === $accountId) { if (0 === $accountId) {
$accountId = (int)($parameters[0] ?? 0.0); $accountId = (int) ($parameters[0] ?? 0.0);
} }
$query = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id') $query = AccountMeta::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id')
@@ -471,15 +477,15 @@ class FireflyValidator extends Validator
public function validateUniqueObjectForUser($attribute, $value, $parameters): bool public function validateUniqueObjectForUser($attribute, $value, $parameters): bool
{ {
[$table, $field] = $parameters; [$table, $field] = $parameters;
$exclude = (int)($parameters[2] ?? 0.0); $exclude = (int) ($parameters[2] ?? 0.0);
/* /*
* If other data (in $this->getData()) contains * If other data (in $this->getData()) contains
* ID field, set that field to be the $exclude. * ID field, set that field to be the $exclude.
*/ */
$data = $this->getData(); $data = $this->getData();
if (!isset($parameters[2]) && isset($data['id']) && (int)$data['id'] > 0) { if (!isset($parameters[2]) && isset($data['id']) && (int) $data['id'] > 0) {
$exclude = (int)$data['id']; $exclude = (int) $data['id'];
} }
@@ -535,7 +541,7 @@ class FireflyValidator extends Validator
$query = DB::table('piggy_banks')->whereNull('piggy_banks.deleted_at') $query = DB::table('piggy_banks')->whereNull('piggy_banks.deleted_at')
->leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')->where('accounts.user_id', auth()->user()->id); ->leftJoin('accounts', 'accounts.id', '=', 'piggy_banks.account_id')->where('accounts.user_id', auth()->user()->id);
if (null !== $exclude) { if (null !== $exclude) {
$query->where('piggy_banks.id', '!=', (int)$exclude); $query->where('piggy_banks.id', '!=', (int) $exclude);
} }
$set = $query->get(['piggy_banks.*']); $set = $query->get(['piggy_banks.*']);
@@ -628,7 +634,7 @@ class FireflyValidator extends Validator
private function validateByAccountTypeId($value, $parameters): bool private function validateByAccountTypeId($value, $parameters): bool
{ {
$type = AccountType::find($this->data['account_type_id'])->first(); $type = AccountType::find($this->data['account_type_id'])->first();
$ignore = (int)($parameters[0] ?? 0.0); $ignore = (int) ($parameters[0] ?? 0.0);
/** @var Collection $set */ /** @var Collection $set */
$set = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)->get(); $set = auth()->user()->accounts()->where('account_type_id', $type->id)->where('id', '!=', $ignore)->get();
@@ -646,7 +652,7 @@ class FireflyValidator extends Validator
/** /**
* @param string $value * @param string $value
* @param array $parameters * @param array $parameters
* @param string $type * @param string $type
* *
* @return bool * @return bool
@@ -662,7 +668,7 @@ class FireflyValidator extends Validator
/** @var Collection $accountTypes */ /** @var Collection $accountTypes */
$accountTypes = AccountType::whereIn('type', $search)->get(); $accountTypes = AccountType::whereIn('type', $search)->get();
$ignore = (int)($parameters[0] ?? 0.0); $ignore = (int) ($parameters[0] ?? 0.0);
$accountTypeIds = $accountTypes->pluck('id')->toArray(); $accountTypeIds = $accountTypes->pluck('id')->toArray();
/** @var Collection $set */ /** @var Collection $set */
$set = auth()->user()->accounts()->whereIn('account_type_id', $accountTypeIds)->where('id', '!=', $ignore)->get(); $set = auth()->user()->accounts()->whereIn('account_type_id', $accountTypeIds)->where('id', '!=', $ignore)->get();

View File

@@ -435,8 +435,8 @@ return [
'append_notes', 'append_notes',
'prepend_notes', 'prepend_notes',
'link_to_bill', 'link_to_bill',
//'convert_withdrawal', // 'convert_withdrawal',
//'convert_deposit', // 'convert_deposit',
'convert_transfer', 'convert_transfer',
], ],
@@ -491,6 +491,11 @@ return [
'source_account_is' => ['alias' => false, 'needs_context' => true,], 'source_account_is' => ['alias' => false, 'needs_context' => true,],
'from_account_is' => ['alias' => true, 'alias_for' => 'source_account_is', 'needs_context' => true,], 'from_account_is' => ['alias' => true, 'alias_for' => 'source_account_is', 'needs_context' => true,],
// source or dest is cash account?
'source_is_cash' => ['alias' => false, 'needs_context' => false],
'destination_is_cash' => ['alias' => false, 'needs_context' => false],
'account_is_cash' => ['alias' => false, 'needs_context' => false],
// source account name contains + alias // source account name contains + alias
'source_account_contains' => ['alias' => false, 'needs_context' => true,], 'source_account_contains' => ['alias' => false, 'needs_context' => true,],
'from_account_contains' => ['alias' => true, 'alias_for' => 'source_account_contains', 'needs_context' => true,], 'from_account_contains' => ['alias' => true, 'alias_for' => 'source_account_contains', 'needs_context' => true,],
@@ -511,7 +516,7 @@ return [
// source account number is // source account number is
'source_account_nr_is' => ['alias' => false, 'needs_context' => true,], 'source_account_nr_is' => ['alias' => false, 'needs_context' => true,],
'from_account_nr_is' => ['alias' => true, 'needs_context' => true,], 'from_account_nr_is' => ['alias' => true, 'alias_for' => 'source_account_nr_is', 'needs_context' => true,],
// source account number contains // source account number contains
'source_account_nr_contains' => ['alias' => false, 'needs_context' => true,], 'source_account_nr_contains' => ['alias' => false, 'needs_context' => true,],

View File

@@ -4,7 +4,7 @@
</td> </td>
<td style="width:30%;"> <td style="width:30%;">
<select name="triggers[{{ count }}][type]" class="form-control"> <select name="triggers[{{ count }}][type]" class="form-control">
{% for key,type in allRuleTriggers() %} {% for key,type in triggers %}
<option value="{{ key }}" label="{{ type }}" <option value="{{ key }}" label="{{ type }}"
{% if key == oldTrigger %} {% if key == oldTrigger %}
selected selected

View File

@@ -1,3 +1,5 @@
{% set bcArgs = [objectType] %}
{% extends "./layout/default" %} {% extends "./layout/default" %}
{% block content %} {% block content %}
<div id="accounts"></div> <div id="accounts"></div>

View File

@@ -22,12 +22,13 @@
namespace Tests\Unit\Support\Search; namespace Tests\Unit\Support\Search;
use DB;
use FireflyIII\Exceptions\FireflyException; use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account; use FireflyIII\Models\Account;
use FireflyIII\Support\Search\OperatorQuerySearch; use FireflyIII\Support\Search\OperatorQuerySearch;
use Log; use Log;
use Tests\TestCase; use Tests\TestCase;
use DB;
/** /**
* Test: * Test:
* *
@@ -52,8 +53,6 @@ class OperatorQuerySearchTest extends TestCase
*/ */
public function testParseQuery(): void public function testParseQuery(): void
{ {
$this->assertTrue(true);
return;
$this->be($this->user()); $this->be($this->user());
// mock some of the used classes to verify results. // mock some of the used classes to verify results.
@@ -75,6 +74,9 @@ class OperatorQuerySearchTest extends TestCase
'from' => 'test', 'from' => 'test',
'source_account_id' => '1', 'source_account_id' => '1',
'from_account_id' => '1', 'from_account_id' => '1',
'source_is_cash' => true,
'destination_is_cash' => true,
'account_is_cash' => true,
// source account nr // source account nr
'from_account_nr_starts' => 'test', 'from_account_nr_starts' => 'test',
@@ -138,7 +140,6 @@ class OperatorQuerySearchTest extends TestCase
'any_notes' => 'test', 'any_notes' => 'test',
// exact amount // exact amount
'amount_exactly' => '0', 'amount_exactly' => '0',
'amount_is' => '0', 'amount_is' => '0',
@@ -182,6 +183,8 @@ class OperatorQuerySearchTest extends TestCase
'tag' => 'abc', 'tag' => 'abc',
'created_on' => '2020-01-01', 'created_on' => '2020-01-01',
'updated_on' => '2020-01-01', 'updated_on' => '2020-01-01',
'created_at' => '2020-01-01',
'updated_at' => '2020-01-01',
'external_id' => 'abc', 'external_id' => 'abc',
'internal_reference' => 'def', 'internal_reference' => 'def',
]; ];
@@ -424,7 +427,6 @@ class OperatorQuerySearchTest extends TestCase
} }
/** /**
* @covers \FireflyIII\Support\Search\OperatorQuerySearch * @covers \FireflyIII\Support\Search\OperatorQuerySearch
*/ */
@@ -547,7 +549,7 @@ class OperatorQuerySearchTest extends TestCase
$this->be($this->user()); $this->be($this->user());
// update one journal to have a very specific created_on date: // update one journal to have a very specific created_on date:
DB::table('transaction_journals')->where('id',1)->update(['created_at' => '2020-08-12 00:00:00']); DB::table('transaction_journals')->where('id', 1)->update(['created_at' => '2020-08-12 00:00:00']);
$object = new OperatorQuerySearch; $object = new OperatorQuerySearch;
$object->setUser($this->user()); $object->setUser($this->user());
@@ -589,7 +591,7 @@ class OperatorQuerySearchTest extends TestCase
$this->be($this->user()); $this->be($this->user());
// update one journal to have a very specific created_on date: // update one journal to have a very specific created_on date:
DB::table('transaction_journals')->where('id',1)->update(['updated_at' => '2020-08-12 00:00:00']); DB::table('transaction_journals')->where('id', 1)->update(['updated_at' => '2020-08-12 00:00:00']);
$object = new OperatorQuerySearch; $object = new OperatorQuerySearch;
$object->setUser($this->user()); $object->setUser($this->user());
@@ -630,7 +632,7 @@ class OperatorQuerySearchTest extends TestCase
$this->be($this->user()); $this->be($this->user());
// update one journal to have a very specific created_on date: // update one journal to have a very specific created_on date:
DB::table('transaction_journals')->where('id',1)->update(['updated_at' => '2020-08-12 00:00:00']); DB::table('transaction_journals')->where('id', 1)->update(['updated_at' => '2020-08-12 00:00:00']);
$object = new OperatorQuerySearch; $object = new OperatorQuerySearch;
$object->setUser($this->user()); $object->setUser($this->user());
@@ -702,7 +704,7 @@ class OperatorQuerySearchTest extends TestCase
$this->be($this->user()); $this->be($this->user());
// update one journal to have a very specific created_on date: // update one journal to have a very specific created_on date:
DB::table('transaction_journals')->where('id',1)->update(['updated_at' => '2020-08-12 00:00:00']); DB::table('transaction_journals')->where('id', 1)->update(['updated_at' => '2020-08-12 00:00:00']);
$object = new OperatorQuerySearch; $object = new OperatorQuerySearch;
$object->setUser($this->user()); $object->setUser($this->user());
@@ -1027,8 +1029,8 @@ class OperatorQuerySearchTest extends TestCase
// the first one should say "Groceries". // the first one should say "Groceries".
$transaction = array_shift($result->first()['transactions']); $transaction = array_shift($result->first()['transactions']);
$tags = $transaction['tags'] ?? []; $tags = $transaction['tags'] ?? [];
$singleTag= array_shift($tags); $singleTag = array_shift($tags);
$this->assertEquals('searchTestTag', $singleTag['name'] ?? ''); $this->assertEquals('searchTestTag', $singleTag['name'] ?? '');
} }
@@ -1099,7 +1101,7 @@ class OperatorQuerySearchTest extends TestCase
} }
// many results, tricky to verify. // many results, tricky to verify.
$this->assertCount(1,$result); $this->assertCount(1, $result);
// the first one should say "Groceries". // the first one should say "Groceries".
$transaction = array_shift($result->first()['transactions']); $transaction = array_shift($result->first()['transactions']);
@@ -1138,12 +1140,12 @@ class OperatorQuerySearchTest extends TestCase
} }
// many results, tricky to verify. // many results, tricky to verify.
$this->assertCount(1,$result); $this->assertCount(1, $result);
// the first one should hav this tag. // the first one should hav this tag.
$transaction = array_shift($result->first()['transactions']); $transaction = array_shift($result->first()['transactions']);
$tags = $transaction['tags'] ?? []; $tags = $transaction['tags'] ?? [];
$singleTag= array_shift($tags); $singleTag = array_shift($tags);
$this->assertEquals('searchTestTag', $singleTag['name'] ?? ''); $this->assertEquals('searchTestTag', $singleTag['name'] ?? '');
} }
@@ -1178,7 +1180,7 @@ class OperatorQuerySearchTest extends TestCase
} }
// many results, tricky to verify. // many results, tricky to verify.
$this->assertCount(1,$result); $this->assertCount(1, $result);
// the first one should say "Groceries". // the first one should say "Groceries".
$transaction = array_shift($result->first()['transactions']); $transaction = array_shift($result->first()['transactions']);
@@ -1217,7 +1219,7 @@ class OperatorQuerySearchTest extends TestCase
} }
// many results, tricky to verify. // many results, tricky to verify.
$this->assertCount(1,$result); $this->assertCount(1, $result);
// the first one should say "Groceries". // the first one should say "Groceries".
$transaction = array_shift($result->first()['transactions']); $transaction = array_shift($result->first()['transactions']);
@@ -1331,7 +1333,7 @@ class OperatorQuerySearchTest extends TestCase
} }
// could have many results, grab first transaction: // could have many results, grab first transaction:
$this->assertTrue( count($result) > 1); $this->assertTrue(count($result) > 1);
// todo better verification // todo better verification
} }
@@ -1346,7 +1348,7 @@ class OperatorQuerySearchTest extends TestCase
/** @var Account $account */ /** @var Account $account */
$account = $this->user()->accounts()->where('name', 'Dest2Acct3Test4Thing')->first(); $account = $this->user()->accounts()->where('name', 'Dest2Acct3Test4Thing')->first();
$accountId = (int) $account->id; $accountId = (int) $account->id;
$object = new OperatorQuerySearch; $object = new OperatorQuerySearch;
$object->setUser($this->user()); $object->setUser($this->user());
$object->setPage(1); $object->setPage(1);
$query = sprintf('destination_account_id:%d', $accountId); $query = sprintf('destination_account_id:%d', $accountId);
@@ -1388,7 +1390,7 @@ class OperatorQuerySearchTest extends TestCase
/** @var Account $account */ /** @var Account $account */
$account = $this->user()->accounts()->where('name', 'from_acct_NL30ABNA_test')->first(); $account = $this->user()->accounts()->where('name', 'from_acct_NL30ABNA_test')->first();
$accountId = (int) $account->id; $accountId = (int) $account->id;
$object = new OperatorQuerySearch; $object = new OperatorQuerySearch;
$object->setUser($this->user()); $object->setUser($this->user());
$object->setPage(1); $object->setPage(1);
$query = sprintf('source_account_id:%d', $accountId); $query = sprintf('source_account_id:%d', $accountId);
@@ -1429,7 +1431,7 @@ class OperatorQuerySearchTest extends TestCase
/** @var Account $account */ /** @var Account $account */
$account = $this->user()->accounts()->where('name', 'from_acct_NL30ABNA_test')->first(); $account = $this->user()->accounts()->where('name', 'from_acct_NL30ABNA_test')->first();
$accountId = (int) $account->id; $accountId = (int) $account->id;
$object = new OperatorQuerySearch; $object = new OperatorQuerySearch;
$object->setUser($this->user()); $object->setUser($this->user());
$object->setPage(1); $object->setPage(1);
$query = sprintf('account_id:%d', $accountId); $query = sprintf('account_id:%d', $accountId);