mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-31 18:54:58 +00:00 
			
		
		
		
	Prevent asset account from being found by opposing account. #719
This commit is contained in:
		| @@ -20,6 +20,7 @@ use Illuminate\Support\Collection; | |||||||
| use Log; | use Log; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  |  * | ||||||
|  * Class ImportAccount |  * Class ImportAccount | ||||||
|  * |  * | ||||||
|  * @package FireflyIII\Import\Object |  * @package FireflyIII\Import\Object | ||||||
| @@ -41,6 +42,14 @@ class ImportAccount | |||||||
|     private $defaultAccountId = 0; |     private $defaultAccountId = 0; | ||||||
|     /** @var string */ |     /** @var string */ | ||||||
|     private $expectedType = ''; |     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 */ |     /** @var  AccountRepositoryInterface */ | ||||||
|     private $repository; |     private $repository; | ||||||
|     /** @var  User */ |     /** @var  User */ | ||||||
| @@ -125,6 +134,14 @@ class ImportAccount | |||||||
|         $this->defaultAccountId = $defaultAccountId; |         $this->defaultAccountId = $defaultAccountId; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * @param int $forbiddenAccountId | ||||||
|  |      */ | ||||||
|  |     public function setForbiddenAccountId(int $forbiddenAccountId) | ||||||
|  |     { | ||||||
|  |         $this->forbiddenAccountId = $forbiddenAccountId; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @param User $user |      * @param User $user | ||||||
|      */ |      */ | ||||||
| @@ -148,7 +165,9 @@ class ImportAccount | |||||||
|         if (count($this->accountId) === 3) { |         if (count($this->accountId) === 3) { | ||||||
|             Log::debug(sprintf('Finding account of type %d and ID %d', $accountType->id, $this->accountId['value'])); |             Log::debug(sprintf('Finding account of type %d and ID %d', $accountType->id, $this->accountId['value'])); | ||||||
|             /** @var Account $account */ |             /** @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)) { |             if (!is_null($account)) { | ||||||
|                 Log::debug(sprintf('Found unmapped %s account by ID (#%d): %s', $this->expectedType, $account->id, $account->name)); |                 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)); |             Log::debug(sprintf('Finding account of type %d and IBAN %s', $accountType->id, $iban)); | ||||||
|             $filtered = $accounts->filter( |             $filtered = $accounts->filter( | ||||||
|                 function (Account $account) use ($iban) { |                 function (Account $account) use ($iban) { | ||||||
|                     if ($account->iban === $iban) { |                     if ($account->iban === $iban && $account->id !== $this->forbiddenAccountId) { | ||||||
|                         Log::debug( |                         Log::debug( | ||||||
|                             sprintf('Found unmapped %s account by IBAN (#%d): %s (%s)', $this->expectedType, $account->id, $account->name, $account->iban) |                             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)); |             Log::debug(sprintf('Finding account of type %d and name %s', $accountType->id, $name)); | ||||||
|             $filtered = $accounts->filter( |             $filtered = $accounts->filter( | ||||||
|                 function (Account $account) use ($name) { |                 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)); |                         Log::debug(sprintf('Found unmapped %s account by name (#%d): %s', $this->expectedType, $account->id, $account->name)); | ||||||
|  |  | ||||||
|                         return $account; |                         return $account; | ||||||
|   | |||||||
| @@ -188,7 +188,7 @@ class ImportStorage | |||||||
|     { |     { | ||||||
|         $amount      = Steam::positive($importJournal->getAmount()); |         $amount      = Steam::positive($importJournal->getAmount()); | ||||||
|         $asset       = $importJournal->asset->getAccount(); |         $asset       = $importJournal->asset->getAccount(); | ||||||
|         $opposing    = $this->getOpposingAccount($importJournal->opposing, $amount); |         $opposing    = $this->getOpposingAccount($importJournal->opposing,$asset->id, $amount); | ||||||
|         $description = $importJournal->getDescription(); |         $description = $importJournal->getDescription(); | ||||||
|  |  | ||||||
|         $filtered = $set->filter( |         $filtered = $set->filter( | ||||||
| @@ -288,8 +288,9 @@ class ImportStorage | |||||||
|      * |      * | ||||||
|      * @return Account |      * @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) { |         if (bccomp($amount, '0') === -1) { | ||||||
|             Log::debug(sprintf('%s is negative, create opposing expense account.', $amount)); |             Log::debug(sprintf('%s is negative, create opposing expense account.', $amount)); | ||||||
|             $account->setExpectedType(AccountType::EXPENSE); |             $account->setExpectedType(AccountType::EXPENSE); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user