mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Add ability to disable running of rules on transactions.
This commit is contained in:
@@ -297,7 +297,7 @@ class TransactionController extends Controller
|
|||||||
return response()->json($response, 422);
|
return response()->json($response, 422);
|
||||||
}
|
}
|
||||||
app('preferences')->mark();
|
app('preferences')->mark();
|
||||||
event(new StoredTransactionGroup($transactionGroup));
|
event(new StoredTransactionGroup($transactionGroup, $data['apply_rules'] ?? true));
|
||||||
|
|
||||||
$manager = $this->getManager();
|
$manager = $this->getManager();
|
||||||
/** @var User $admin */
|
/** @var User $admin */
|
||||||
@@ -341,7 +341,7 @@ class TransactionController extends Controller
|
|||||||
$manager = $this->getManager();
|
$manager = $this->getManager();
|
||||||
|
|
||||||
app('preferences')->mark();
|
app('preferences')->mark();
|
||||||
event(new UpdatedTransactionGroup($transactionGroup));
|
event(new UpdatedTransactionGroup($transactionGroup, $data['apply_rules'] ?? true));
|
||||||
|
|
||||||
/** @var User $admin */
|
/** @var User $admin */
|
||||||
$admin = auth()->user();
|
$admin = auth()->user();
|
||||||
|
@@ -47,6 +47,7 @@ class TransactionStoreRequest extends Request
|
|||||||
public function authorize(): bool
|
public function authorize(): bool
|
||||||
{
|
{
|
||||||
Log::debug('Authorize TransactionStoreRequest');
|
Log::debug('Authorize TransactionStoreRequest');
|
||||||
|
|
||||||
// Only allow authenticated users
|
// Only allow authenticated users
|
||||||
return auth()->check();
|
return auth()->check();
|
||||||
}
|
}
|
||||||
@@ -62,6 +63,7 @@ class TransactionStoreRequest extends Request
|
|||||||
$data = [
|
$data = [
|
||||||
'group_title' => $this->string('group_title'),
|
'group_title' => $this->string('group_title'),
|
||||||
'error_if_duplicate_hash' => $this->boolean('error_if_duplicate_hash'),
|
'error_if_duplicate_hash' => $this->boolean('error_if_duplicate_hash'),
|
||||||
|
'apply_rules' => $this->boolean('apply_rules', true),
|
||||||
'transactions' => $this->getTransactionData(),
|
'transactions' => $this->getTransactionData(),
|
||||||
];
|
];
|
||||||
|
|
||||||
@@ -80,6 +82,7 @@ class TransactionStoreRequest extends Request
|
|||||||
// basic fields for group:
|
// basic fields for group:
|
||||||
'group_title' => 'between:1,1000|nullable',
|
'group_title' => 'between:1,1000|nullable',
|
||||||
'error_if_duplicate_hash' => [new IsBoolean],
|
'error_if_duplicate_hash' => [new IsBoolean],
|
||||||
|
'apply_rules' => [new IsBoolean],
|
||||||
|
|
||||||
// transaction rules (in array for splits):
|
// transaction rules (in array for splits):
|
||||||
'transactions.*.type' => 'required|in:withdrawal,deposit,transfer,opening-balance,reconciliation',
|
'transactions.*.type' => 'required|in:withdrawal,deposit,transfer,opening-balance,reconciliation',
|
||||||
|
@@ -138,6 +138,7 @@ class TransactionUpdateRequest extends Request
|
|||||||
|
|
||||||
$data = [
|
$data = [
|
||||||
'transactions' => $this->getTransactionData(),
|
'transactions' => $this->getTransactionData(),
|
||||||
|
'apply_rules' => $this->boolean('apply_rules', true),
|
||||||
];
|
];
|
||||||
if ($this->has('group_title')) {
|
if ($this->has('group_title')) {
|
||||||
$data['group_title'] = $this->string('group_title');
|
$data['group_title'] = $this->string('group_title');
|
||||||
@@ -156,6 +157,7 @@ class TransactionUpdateRequest extends Request
|
|||||||
$rules = [
|
$rules = [
|
||||||
// basic fields for group:
|
// basic fields for group:
|
||||||
'group_title' => 'between:1,1000',
|
'group_title' => 'between:1,1000',
|
||||||
|
'apply_rules' => [new IsBoolean],
|
||||||
|
|
||||||
// transaction rules (in array for splits):
|
// transaction rules (in array for splits):
|
||||||
'transactions.*.type' => 'in:withdrawal,deposit,transfer,opening-balance,reconciliation',
|
'transactions.*.type' => 'in:withdrawal,deposit,transfer,opening-balance,reconciliation',
|
||||||
|
@@ -36,6 +36,7 @@ class StoredTransactionGroup extends Event
|
|||||||
{
|
{
|
||||||
use SerializesModels;
|
use SerializesModels;
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
public $applyRules;
|
public $applyRules;
|
||||||
/** @var TransactionGroup The group that was stored. */
|
/** @var TransactionGroup The group that was stored. */
|
||||||
public $transactionGroup;
|
public $transactionGroup;
|
||||||
|
@@ -37,6 +37,8 @@ class UpdatedTransactionGroup extends Event
|
|||||||
{
|
{
|
||||||
use SerializesModels;
|
use SerializesModels;
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
|
public $applyRules;
|
||||||
/** @var TransactionGroup The group that was stored. */
|
/** @var TransactionGroup The group that was stored. */
|
||||||
public $transactionGroup;
|
public $transactionGroup;
|
||||||
|
|
||||||
@@ -45,8 +47,9 @@ class UpdatedTransactionGroup extends Event
|
|||||||
*
|
*
|
||||||
* @param TransactionGroup $transactionGroup
|
* @param TransactionGroup $transactionGroup
|
||||||
*/
|
*/
|
||||||
public function __construct(TransactionGroup $transactionGroup)
|
public function __construct(TransactionGroup $transactionGroup, bool $applyRules = true)
|
||||||
{
|
{
|
||||||
$this->transactionGroup = $transactionGroup;
|
$this->transactionGroup = $transactionGroup;
|
||||||
|
$this->applyRules = $applyRules;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -35,21 +35,23 @@ class StoredGroupEventHandler
|
|||||||
/**
|
/**
|
||||||
* This method grabs all the users rules and processes them.
|
* This method grabs all the users rules and processes them.
|
||||||
*
|
*
|
||||||
* @param StoredTransactionGroup $storedJournalEvent
|
* @param StoredTransactionGroup $storedGroupEvent
|
||||||
*/
|
*/
|
||||||
public function processRules(StoredTransactionGroup $storedJournalEvent): void
|
public function processRules(StoredTransactionGroup $storedGroupEvent): void
|
||||||
{
|
{
|
||||||
if (false === $storedJournalEvent->applyRules) {
|
if (false === $storedGroupEvent->applyRules) {
|
||||||
|
Log::info(sprintf('Will not run rules on group #%d', $storedGroupEvent->transactionGroup->id));
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Log::debug('Now in StoredGroupEventHandler::processRules()');
|
Log::debug('Now in StoredGroupEventHandler::processRules()');
|
||||||
|
|
||||||
/** @var RuleEngine $ruleEngine */
|
/** @var RuleEngine $ruleEngine */
|
||||||
$ruleEngine = app(RuleEngine::class);
|
$ruleEngine = app(RuleEngine::class);
|
||||||
$ruleEngine->setUser($storedJournalEvent->transactionGroup->user);
|
$ruleEngine->setUser($storedGroupEvent->transactionGroup->user);
|
||||||
$ruleEngine->setAllRules(true);
|
$ruleEngine->setAllRules(true);
|
||||||
$ruleEngine->setTriggerMode(RuleEngine::TRIGGER_STORE);
|
$ruleEngine->setTriggerMode(RuleEngine::TRIGGER_STORE);
|
||||||
$journals = $storedJournalEvent->transactionGroup->transactionJournals;
|
$journals = $storedGroupEvent->transactionGroup->transactionJournals;
|
||||||
|
|
||||||
/** @var TransactionJournal $journal */
|
/** @var TransactionJournal $journal */
|
||||||
foreach ($journals as $journal) {
|
foreach ($journals as $journal) {
|
||||||
|
@@ -35,16 +35,22 @@ class UpdatedGroupEventHandler
|
|||||||
/**
|
/**
|
||||||
* This method will check all the rules when a journal is updated.
|
* This method will check all the rules when a journal is updated.
|
||||||
*
|
*
|
||||||
* @param UpdatedTransactionGroup $updatedJournalEvent
|
* @param UpdatedTransactionGroup $updatedGroupEvent
|
||||||
*/
|
*/
|
||||||
public function processRules(UpdatedTransactionGroup $updatedJournalEvent): void
|
public function processRules(UpdatedTransactionGroup $updatedGroupEvent): void
|
||||||
{
|
{
|
||||||
|
if (false === $updatedGroupEvent->applyRules) {
|
||||||
|
Log::info(sprintf('Will not run rules on group #%d', $updatedGroupEvent->transactionGroup->id));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/** @var RuleEngine $ruleEngine */
|
/** @var RuleEngine $ruleEngine */
|
||||||
$ruleEngine = app(RuleEngine::class);
|
$ruleEngine = app(RuleEngine::class);
|
||||||
$ruleEngine->setUser($updatedJournalEvent->transactionGroup->user);
|
$ruleEngine->setUser($updatedGroupEvent->transactionGroup->user);
|
||||||
$ruleEngine->setAllRules(true);
|
$ruleEngine->setAllRules(true);
|
||||||
$ruleEngine->setTriggerMode(RuleEngine::TRIGGER_UPDATE);
|
$ruleEngine->setTriggerMode(RuleEngine::TRIGGER_UPDATE);
|
||||||
$journals = $updatedJournalEvent->transactionGroup->transactionJournals;
|
$journals = $updatedGroupEvent->transactionGroup->transactionJournals;
|
||||||
|
|
||||||
/** @var TransactionJournal $journal */
|
/** @var TransactionJournal $journal */
|
||||||
foreach ($journals as $journal) {
|
foreach ($journals as $journal) {
|
||||||
|
Reference in New Issue
Block a user