This commit is contained in:
James Cole
2025-05-22 06:06:30 +02:00
parent 2a17ec9280
commit 1261122a57
2 changed files with 21 additions and 9 deletions

View File

@@ -48,10 +48,10 @@ class SetDestinationAccount implements ActionInterface
public function actOnArray(array $journal): bool public function actOnArray(array $journal): bool
{ {
$accountName = $this->action->getValue($journal); $accountName = $this->action->getValue($journal);
/** @var User $user */ /** @var User $user */
$user = User::find($journal['user_id']); $user = User::find($journal['user_id']);
/** @var null|TransactionJournal $object */ /** @var null|TransactionJournal $object */
$object = $user->transactionJournals()->find((int) $journal['transaction_journal_id']); $object = $user->transactionJournals()->find((int) $journal['transaction_journal_id']);
@@ -63,11 +63,11 @@ class SetDestinationAccount implements ActionInterface
return false; return false;
} }
$type = $object->transactionType->type; $type = $object->transactionType->type;
$this->repository->setUser($user); $this->repository->setUser($user);
// if this is a transfer or a deposit, the new destination account must be an asset account or a default account, and it MUST exist: // if this is a transfer or a deposit, the new destination account must be an asset account or a default account, and it MUST exist:
$newAccount = $this->findAssetAccount($type, $accountName); $newAccount = $this->findAssetAccount($type, $accountName);
if ((TransactionTypeEnum::DEPOSIT->value === $type || TransactionTypeEnum::TRANSFER->value === $type) && null === $newAccount) { if ((TransactionTypeEnum::DEPOSIT->value === $type || TransactionTypeEnum::TRANSFER->value === $type) && null === $newAccount) {
app('log')->error( app('log')->error(
sprintf( sprintf(
@@ -83,7 +83,7 @@ class SetDestinationAccount implements ActionInterface
// new destination account must be different from the current source account: // new destination account must be different from the current source account:
/** @var null|Transaction $source */ /** @var null|Transaction $source */
$source = $object->transactions()->where('amount', '<', 0)->first(); $source = $object->transactions()->where('amount', '<', 0)->first();
if (null === $source) { if (null === $source) {
app('log')->error('Could not find source transaction.'); app('log')->error('Could not find source transaction.');
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_source_transaction'))); event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.cannot_find_source_transaction')));
@@ -116,6 +116,18 @@ class SetDestinationAccount implements ActionInterface
if (TransactionTypeEnum::WITHDRAWAL->value === $type) { if (TransactionTypeEnum::WITHDRAWAL->value === $type) {
$newAccount = $this->findWithdrawalDestinationAccount($accountName); $newAccount = $this->findWithdrawalDestinationAccount($accountName);
} }
if (null === $newAccount) {
app('log')->error(
sprintf(
'No destination account found for name "%s".',
$accountName
)
);
event(new RuleActionFailedOnArray($this->action, $journal, trans('rules.no_destination', ['name' => $accountName])));
return false;
}
app('log')->debug(sprintf('New destination account is #%d ("%s").', $newAccount->id, $newAccount->name)); app('log')->debug(sprintf('New destination account is #%d ("%s").', $newAccount->id, $newAccount->name));
@@ -123,10 +135,9 @@ class SetDestinationAccount implements ActionInterface
// update destination transaction with new destination account: // update destination transaction with new destination account:
DB::table('transactions') DB::table('transactions')
->where('transaction_journal_id', '=', $object->id) ->where('transaction_journal_id', '=', $object->id)
->where('amount', '>', 0) ->where('amount', '>', 0)
->update(['account_id' => $newAccount->id]) ->update(['account_id' => $newAccount->id]);
;
app('log')->debug(sprintf('Updated journal #%d (group #%d) and gave it new destination account ID.', $object->id, $object->transaction_group_id)); app('log')->debug(sprintf('Updated journal #%d (group #%d) and gave it new destination account ID.', $object->id, $object->transaction_group_id));

View File

@@ -38,6 +38,7 @@ return [
'is_already_withdrawal' => 'This transaction is already a withdrawal', 'is_already_withdrawal' => 'This transaction is already a withdrawal',
'is_already_deposit' => 'This transaction is already a deposit', 'is_already_deposit' => 'This transaction is already a deposit',
'is_already_transfer' => 'This transaction is already a transfer', 'is_already_transfer' => 'This transaction is already a transfer',
'no_destination' => 'Could not find or create destination account ":name"',
'is_not_transfer' => 'This transaction is not a transfer', 'is_not_transfer' => 'This transaction is not a transfer',
'complex_error' => 'Something complicated went wrong. Sorry about that. Please inspect the logs of Firefly III', 'complex_error' => 'Something complicated went wrong. Sorry about that. Please inspect the logs of Firefly III',
'no_valid_opposing' => 'Conversion failed because there is no valid account named ":account"', 'no_valid_opposing' => 'Conversion failed because there is no valid account named ":account"',