Change code for #4607

This commit is contained in:
James Cole
2021-04-05 21:53:28 +02:00
parent e4802ec958
commit d47bddde62

View File

@@ -55,32 +55,32 @@ class SetSourceAccount implements ActionInterface
*/ */
public function actOnArray(array $journal): bool public function actOnArray(array $journal): bool
{ {
$user = User::find($journal['user_id']); /** @var TransactionJournal $object */
$type = $journal['transaction_type_type'];
/** @var TransactionJournal $journal */
$journal = TransactionJournal::find((int)$journal['transaction_journal_id']);
/** @var AccountRepositoryInterface repository */ /** @var AccountRepositoryInterface repository */
/** @var Transaction $destination */
$user = User::find($journal['user_id']);
$type = $journal['transaction_type_type'];
$object = $user->transactionJournals()->find((int)$journal['transaction_journal_id']);
$this->repository = app(AccountRepositoryInterface::class); $this->repository = app(AccountRepositoryInterface::class);
$this->repository->setUser($user); $this->repository->setUser($user);
// if this is a transfer or a withdrawal, the new source account must be an asset account or a default account, and it MUST exist: // if this is a transfer or a withdrawal, the new source account must be an asset account or a default account, and it MUST exist:
$newAccount = $this->findAssetAccount($type); $newAccount = $this->findAssetAccount($type);
if ((TransactionType::WITHDRAWAL === $type || TransactionType::TRANSFER === $type) && null === $newAccount) { if ((TransactionType::WITHDRAWAL === $type || TransactionType::TRANSFER === $type) && null === $newAccount) {
Log::error( Log::error(
sprintf( sprintf('Cant change src account of journal #%d because no asset account with name "%s" exists.', $object->id, $this->action->action_value)
'Cannot change source account of journal #%d because no asset account with name "%s" exists.', $journal['transaction_journal_id'],
$this->action->action_value
)
); );
return false; return false;
} }
// new source account must be different from the current destination: // new source account must be different from the current destination:
$destinationId = (int)$journal['destination_account_id']; $destinationId = (int)$journal['destination_account_id'];
/** @var Transaction $source */ $destination = $object->transactions()->where('amount', '>', 0)->first();
$source = $journal->transactions()->where('amount', '>', 0)->first(); if (null !== $destination) {
if (null !== $source) { $destinationId = $destination->account ? (int)$destination->account->id : $destinationId;
$destinationId = $source->account ? (int)$source->account->id : $destinationId;
} }
if (TransactionType::TRANSFER === $type && null !== $newAccount && (int)$newAccount->id === $destinationId) { if (TransactionType::TRANSFER === $type && null !== $newAccount && (int)$newAccount->id === $destinationId) {
Log::error( Log::error(
@@ -92,6 +92,7 @@ class SetSourceAccount implements ActionInterface
return false; return false;
} }
// if this is a deposit, the new source account must be a revenue account and may be created: // if this is a deposit, the new source account must be a revenue account and may be created:
if (TransactionType::DEPOSIT === $type) { if (TransactionType::DEPOSIT === $type) {
$newAccount = $this->findRevenueAccount(); $newAccount = $this->findRevenueAccount();
@@ -107,11 +108,11 @@ class SetSourceAccount implements ActionInterface
// update source transaction with new source account: // update source transaction with new source account:
// get source transaction: // get source transaction:
DB::table('transactions') DB::table('transactions')
->where('transaction_journal_id', '=', $journal['transaction_journal_id']) ->where('transaction_journal_id', '=', $object->id)
->where('amount', '<', 0) ->where('amount', '<', 0)
->update(['account_id' => $newAccount->id]); ->update(['account_id' => $newAccount->id]);
Log::debug(sprintf('Updated journal #%d and gave it new source account ID.', $journal['transaction_journal_id'])); Log::debug(sprintf('Updated journal #%d (group #%d) and gave it new source account ID.', $object->id, $object->transaction_group_id));
return true; return true;
} }
@@ -141,12 +142,12 @@ class SetSourceAccount implements ActionInterface
if (null === $account) { if (null === $account) {
// create new revenue account with this name: // create new revenue account with this name:
$data = [ $data = [
'name' => $this->action->action_value, 'name' => $this->action->action_value,
'account_type' => 'revenue', 'account_type_name' => 'revenue',
'account_type_id' => null, 'account_type_id' => null,
'virtual_balance' => 0, 'virtual_balance' => 0,
'active' => true, 'active' => true,
'iban' => null, 'iban' => null,
]; ];
$account = $this->repository->store($data); $account = $this->repository->store($data);
} }