Skip to content

Firefly III: a personal finances manager

Prevent asset account from being found by opposing account. #719

James Cole0375f77b73df
diff --git a/app/Import/Object/ImportAccount.php b/app/Import/Object/ImportAccount.php
index ca5dba85d5..a4c3d966e1 100644
--- a/app/Import/Object/ImportAccount.php
+++ b/app/Import/Object/ImportAccount.php
@@ -20,6 +20,7 @@ use Illuminate\Support\Collection;
use Log;
/**
+ *
* Class ImportAccount
*
* @package FireflyIII\Import\Object
@@ -41,6 +42,14 @@ class ImportAccount
private $defaultAccountId = 0;
/** @var string */
private $expectedType = '';
+ /**
+ * This value is used to indicate the other account ID (the opposing transaction's account),
+ * if it is know. If so, this particular importaccount may never return an Account with this ID.
+ * If it would, this would result in a transaction from-to the same account.
+ *
+ * @var int
+ */
+ private $forbiddenAccountId = 0;
/** @var AccountRepositoryInterface */
private $repository;
/** @var User */
@@ -125,6 +134,14 @@ class ImportAccount
$this->defaultAccountId = $defaultAccountId;
}
+ /**
+ * @param int $forbiddenAccountId
+ */
+ public function setForbiddenAccountId(int $forbiddenAccountId)
+ {
+ $this->forbiddenAccountId = $forbiddenAccountId;
+ }
+
/**
* @param User $user
*/
@@ -148,7 +165,9 @@ class ImportAccount
if (count($this->accountId) === 3) {
Log::debug(sprintf('Finding account of type %d and ID %d', $accountType->id, $this->accountId['value']));
/** @var Account $account */
- $account = $this->user->accounts()->where('account_type_id', $accountType->id)->where('id', $this->accountId['value'])->first();
+ $account = $this->user->accounts()->where('id', '!=', $this->forbiddenAccountId)->where('account_type_id', $accountType->id)->where(
+ 'id', $this->accountId['value']
+ )->first();
if (!is_null($account)) {
Log::debug(sprintf('Found unmapped %s account by ID (#%d): %s', $this->expectedType, $account->id, $account->name));
@@ -164,7 +183,7 @@ class ImportAccount
Log::debug(sprintf('Finding account of type %d and IBAN %s', $accountType->id, $iban));
$filtered = $accounts->filter(
function (Account $account) use ($iban) {
- if ($account->iban === $iban) {
+ if ($account->iban === $iban && $account->id !== $this->forbiddenAccountId) {
Log::debug(
sprintf('Found unmapped %s account by IBAN (#%d): %s (%s)', $this->expectedType, $account->id, $account->name, $account->iban)
);
@@ -187,7 +206,7 @@ class ImportAccount
Log::debug(sprintf('Finding account of type %d and name %s', $accountType->id, $name));
$filtered = $accounts->filter(
function (Account $account) use ($name) {
- if ($account->name === $name) {
+ if ($account->name === $name && $account->id !== $this->forbiddenAccountId) {
Log::debug(sprintf('Found unmapped %s account by name (#%d): %s', $this->expectedType, $account->id, $account->name));
return $account;
diff --git a/app/Import/Storage/ImportStorage.php b/app/Import/Storage/ImportStorage.php
index 8e417efc61..5e917bdab8 100644
--- a/app/Import/Storage/ImportStorage.php
+++ b/app/Import/Storage/ImportStorage.php
@@ -188,7 +188,7 @@ class ImportStorage
{
$amount = Steam::positive($importJournal->getAmount());
$asset = $importJournal->asset->getAccount();
- $opposing = $this->getOpposingAccount($importJournal->opposing, $amount);
+ $opposing = $this->getOpposingAccount($importJournal->opposing,$asset->id, $amount);
$description = $importJournal->getDescription();
$filtered = $set->filter(
@@ -288,8 +288,9 @@ class ImportStorage
*
* @return Account
*/
- private function getOpposingAccount(ImportAccount $account, $amount): Account
+ private function getOpposingAccount(ImportAccount $account, int $forbiddenAccount, string $amount): Account
{
+ $account->setForbiddenAccountId($forbiddenAccount);
if (bccomp($amount, '0') === -1) {
Log::debug(sprintf('%s is negative, create opposing expense account.', $amount));
$account->setExpectedType(AccountType::EXPENSE);