diff --git a/app/Import/Object/ImportAccount.php b/app/Import/Object/ImportAccount.php index 334709d8bb..8ab5c77077 100644 --- a/app/Import/Object/ImportAccount.php +++ b/app/Import/Object/ImportAccount.php @@ -37,6 +37,8 @@ class ImportAccount private $accountName = []; /** @var array */ private $accountNumber = []; + /** @var int */ + private $defaultAccountId = 0; /** @var string */ private $expectedType = ''; /** @var AccountRepositoryInterface */ @@ -115,6 +117,14 @@ class ImportAccount $this->accountNumber = $accountNumber; } + /** + * @param int $defaultAccountId + */ + public function setDefaultAccountId(int $defaultAccountId) + { + $this->defaultAccountId = $defaultAccountId; + } + /** * @param User $user */ @@ -249,6 +259,12 @@ class ImportAccount $search = intval($array['mapped']); $account = $this->repository->find($search); + if (is_null($account->id)) { + Log::error(sprintf('There is no account with id #%d. Invalid mapping will be ignored!', $search)); + + return new Account; + } + if ($account->accountType->type !== $this->expectedType) { Log::error( sprintf( @@ -256,6 +272,7 @@ class ImportAccount $this->expectedType ) ); + return new Account; } @@ -297,6 +314,14 @@ class ImportAccount } $this->expectedType = $oldExpectedType; + // if search for an asset account, fall back to given "default account" (mandatory) + if ($this->expectedType === AccountType::ASSET) { + $this->account = $this->repository->find($this->defaultAccountId); + Log::debug(sprintf('Fall back to default account #%d "%s"', $this->account->id, $this->account->name)); + + return true; + } + Log::debug(sprintf('Found no account of type %s so must create one ourselves.', $this->expectedType)); $data = [ diff --git a/app/Import/Object/ImportBill.php b/app/Import/Object/ImportBill.php index 729a290d3b..b8bbb18161 100644 --- a/app/Import/Object/ImportBill.php +++ b/app/Import/Object/ImportBill.php @@ -181,11 +181,18 @@ class ImportBill Log::debug('Finding a mapped bill based on', $array); $search = intval($array['mapped']); - $account = $this->repository->find($search); + $bill = $this->repository->find($search); - Log::debug(sprintf('Found bill! #%d ("%s"). Return it', $account->id, $account->name)); + if (is_null($bill->id)) { + Log::error(sprintf('There is no bill with id #%d. Invalid mapping will be ignored!', $search)); - return $account; + return new Bill; + } + + + Log::debug(sprintf('Found bill! #%d ("%s"). Return it', $bill->id, $bill->name)); + + return $bill; } /** diff --git a/app/Import/Object/ImportBudget.php b/app/Import/Object/ImportBudget.php index 011fef8c8e..cbc1e05460 100644 --- a/app/Import/Object/ImportBudget.php +++ b/app/Import/Object/ImportBudget.php @@ -181,11 +181,17 @@ class ImportBudget Log::debug('Finding a mapped budget based on', $array); $search = intval($array['mapped']); - $account = $this->repository->find($search); + $budget = $this->repository->find($search); - Log::debug(sprintf('Found budget! #%d ("%s"). Return it', $account->id, $account->name)); + if (is_null($budget->id)) { + Log::error(sprintf('There is no budget with id #%d. Invalid mapping will be ignored!', $search)); - return $account; + return new Budget; + } + + Log::debug(sprintf('Found budget! #%d ("%s"). Return it', $budget->id, $budget->name)); + + return $budget; } /** diff --git a/app/Import/Object/ImportCategory.php b/app/Import/Object/ImportCategory.php index 69265fcd3a..b86a73d6ec 100644 --- a/app/Import/Object/ImportCategory.php +++ b/app/Import/Object/ImportCategory.php @@ -175,11 +175,17 @@ class ImportCategory Log::debug('Finding a mapped category based on', $array); $search = intval($array['mapped']); - $account = $this->repository->find($search); + $category = $this->repository->find($search); - Log::debug(sprintf('Found category! #%d ("%s"). Return it', $account->id, $account->name)); + if (is_null($category->id)) { + Log::error(sprintf('There is no category with id #%d. Invalid mapping will be ignored!', $search)); - return $account; + return new Category; + } + + Log::debug(sprintf('Found category! #%d ("%s"). Return it', $category->id, $category->name)); + + return $category; } /** diff --git a/app/Import/Object/ImportCurrency.php b/app/Import/Object/ImportCurrency.php index 983c2591ae..e282e29228 100644 --- a/app/Import/Object/ImportCurrency.php +++ b/app/Import/Object/ImportCurrency.php @@ -209,6 +209,13 @@ class ImportCurrency $search = intval($array['mapped']); $currency = $this->repository->find($search); + + if (is_null($currency->id)) { + Log::error(sprintf('There is no currency with id #%d. Invalid mapping will be ignored!', $search)); + + return new TransactionCurrency; + } + Log::debug(sprintf('Found currency! #%d ("%s"). Return it', $currency->id, $currency->name)); return $currency; diff --git a/app/Import/Storage/ImportStorage.php b/app/Import/Storage/ImportStorage.php index 4986bfbb29..8bdb6685fe 100644 --- a/app/Import/Storage/ImportStorage.php +++ b/app/Import/Storage/ImportStorage.php @@ -301,7 +301,7 @@ class ImportStorage private function storeImportJournal(int $index, ImportJournal $importJournal): bool { Log::debug(sprintf('Going to store object #%d with description "%s"', $index, $importJournal->description)); - + $importJournal->asset->setDefaultAccountId($this->job->configuration['import-account']); $asset = $importJournal->asset->getAccount(); $amount = $importJournal->getAmount(); $currency = $this->getCurrency($importJournal, $asset);