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

This commit is contained in:
James Cole 2017-08-12 13:49:40 +02:00
parent 9803932324
commit 0375f77b73
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
2 changed files with 25 additions and 5 deletions

View File

@ -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;

View File

@ -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);