action = $action; } public function actOnArray(array $journal): bool { $this->refreshNotes($journal); // not on slpit transactions $groupCount = TransactionJournal::where('transaction_group_id', $journal['transaction_group_id'])->count(); if ($groupCount > 1) { app('log')->error(sprintf('Group #%d has more than one transaction in it, cannot convert to transfer.', $journal['transaction_group_id'])); event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.split_group'))); return false; } $value = $this->action->getValue($journal); if (!is_numeric($value) || 0 === bccomp($value, '0')) { app('log')->debug(sprintf('RuleAction SetAmount, amount "%s" is not a number or is zero, will not continue.', $value)); event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.journal_invalid_amount', ['amount' => $value]))); return false; } /** @var TransactionJournal $object */ $object = TransactionJournal::where('user_id', $journal['user_id'])->find($journal['transaction_journal_id']); $positive = app('steam')->positive($value); $negative = app('steam')->negative($value); $this->updatePositive($object, $positive); $this->updateNegative($object, $negative); $object->transactionGroup->touch(); // event for audit log entry event(new TriggeredAuditLog( $this->action->rule, $object, 'update_amount', [ 'currency_symbol' => $object->transactionCurrency->symbol, 'decimal_places' => $object->transactionCurrency->decimal_places, 'amount' => $journal['amount'], ], [ 'currency_symbol' => $object->transactionCurrency->symbol, 'decimal_places' => $object->transactionCurrency->decimal_places, 'amount' => $value, ] )); return true; } private function updatePositive(TransactionJournal $object, string $amount): void { /** @var null|Transaction $transaction */ $transaction = $object->transactions()->where('amount', '>', 0)->first(); if (null === $transaction) { return; } $this->updateAmount($transaction, $amount); } private function updateAmount(Transaction $transaction, string $amount): void { $transaction->amount = $amount; $transaction->save(); $transaction->transactionJournal->touch(); } private function updateNegative(TransactionJournal $object, string $amount): void { /** @var null|Transaction $transaction */ $transaction = $object->transactions()->where('amount', '<', 0)->first(); if (null === $transaction) { return; } $this->updateAmount($transaction, $amount); } }