Fix actions and associated tests.

This commit is contained in:
James Cole
2020-08-23 16:12:16 +02:00
parent 139b3ffab4
commit fecc9f7659
8 changed files with 286 additions and 318 deletions

View File

@@ -22,12 +22,13 @@ declare(strict_types=1);
namespace FireflyIII\TransactionRules\Actions;
use DB;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\User;
use Log;
/**
@@ -35,17 +36,8 @@ use Log;
*/
class SetSourceAccount implements ActionInterface
{
/** @var RuleAction The rule action */
private $action;
/** @var TransactionJournal The journal */
private $journal;
/** @var Account The new source account */
private $newSourceAccount;
/** @var AccountRepositoryInterface Account repository */
private $repository;
private RuleAction $action;
private AccountRepositoryInterface $repository;
/**
* TriggerInterface constructor.
@@ -61,85 +53,34 @@ class SetSourceAccount implements ActionInterface
* Set source account to X
*
* @param TransactionJournal $journal
*
* @return bool
* @deprecated
* @codeCoverageIgnore
*/
public function act(TransactionJournal $journal): bool
{
$this->journal = $journal;
$this->repository = app(AccountRepositoryInterface::class);
$this->repository->setUser($journal->user);
// journal type:
$type = $journal->transactionType->type;
// 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 ((TransactionType::WITHDRAWAL === $type || TransactionType::TRANSFER === $type) && !$this->findAssetAccount($type)) {
Log::error(
sprintf(
'Cannot change source account of journal #%d because no asset account with name "%s" exists.',
$journal->id,
$this->action->action_value
)
);
return false;
}
// if this is a deposit, the new source account must be a revenue account and may be created:
if (TransactionType::DEPOSIT === $type) {
$this->findRevenueAccount();
}
Log::debug(sprintf('New source account is #%d ("%s").', $this->newSourceAccount->id, $this->newSourceAccount->name));
// update source transaction with new source account:
// get source transaction:
$transaction = $journal->transactions()->where('amount', '<', 0)->first();
if (null === $transaction) {
// @codeCoverageIgnoreStart
Log::error(sprintf('Cannot change source account of journal #%d because no source transaction exists.', $journal->id));
return false;
// @codeCoverageIgnoreEnd
}
$transaction->account_id = $this->newSourceAccount->id;
$transaction->save();
$journal->touch();
Log::debug(sprintf('Updated transaction #%d and gave it new account ID.', $transaction->id));
return true;
return false;
}
/**
* @param string $type
*
* @return bool
* @return Account|null
*/
private function findAssetAccount(string $type): bool
private function findAssetAccount(string $type): ?Account
{
// switch on type:
$allowed = config(sprintf('firefly.expected_source_types.source.%s', $type));
$allowed = is_array($allowed) ? $allowed : [];
Log::debug(sprintf('Check config for expected_source_types.source.%s, result is', $type), $allowed);
$account = $this->repository->findByName($this->action->action_value, $allowed);
if (null === $account) {
Log::debug(sprintf('There is NO valid source account called "%s".', $this->action->action_value));
return false;
}
Log::debug(sprintf('There exists a valid source account called "%s". ID is #%d', $this->action->action_value, $account->id));
$this->newSourceAccount = $account;
return true;
return $this->repository->findByName($this->action->action_value, $allowed);
}
/**
*
* @return Account|null
*/
private function findRevenueAccount(): void
private function findRevenueAccount(): ?Account
{
$allowed = config('firefly.expected_source_types.source.Deposit');
$account = $this->repository->findByName($this->action->action_value, $allowed);
@@ -156,7 +97,7 @@ class SetSourceAccount implements ActionInterface
$account = $this->repository->store($data);
}
Log::debug(sprintf('Found or created revenue account #%d ("%s")', $account->id, $account->name));
$this->newSourceAccount = $account;
return $account;
}
/**
@@ -164,6 +105,39 @@ class SetSourceAccount implements ActionInterface
*/
public function actOnArray(array $journal): bool
{
// TODO: Implement actOnArray() method.
$user = User::find($journal['user_id']);
$type = $journal['transaction_type_type'];
$this->repository = app(AccountRepositoryInterface::class);
$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:
$newAccount = $this->findAssetAccount($type);
if ((TransactionType::WITHDRAWAL === $type || TransactionType::TRANSFER === $type) && null === $newAccount) {
Log::error(sprintf('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;
}
// if this is a deposit, the new source account must be a revenue account and may be created:
if (TransactionType::DEPOSIT === $type) {
$newAccount = $this->findRevenueAccount();
}
if (null === $newAccount) {
Log::error('New account is NULL');
return false;
}
Log::debug(sprintf('New source account is #%d ("%s").', $newAccount->id, $newAccount->name));
// update source transaction with new source account:
// get source transaction:
DB::table('transactions')
->where('transaction_journal_id', '=', $journal['transaction_journal_id'])
->where('amount', '<', 0)
->update(['account_id' => $newAccount->id]);
Log::debug(sprintf('Updated journal #%d and gave it new source account ID.', $journal['transaction_journal_id']));
return true;
}
}