Skip to content

Firefly III: a personal finances manager

Routine to make sure imported entries have the correct opposing account and the correct amount.

James Cole952328d55cee
diff --git a/app/Import/Object/ImportAccount.php b/app/Import/Object/ImportAccount.php
index 22295cae12..334709d8bb 100644
--- a/app/Import/Object/ImportAccount.php
+++ b/app/Import/Object/ImportAccount.php
@@ -67,6 +67,22 @@ class ImportAccount
return $this->account;
}
+ /**
+ * @return string
+ */
+ public function getExpectedType(): string
+ {
+ return $this->expectedType;
+ }
+
+ /**
+ * @param string $expectedType
+ */
+ public function setExpectedType(string $expectedType)
+ {
+ $this->expectedType = $expectedType;
+ }
+
/**
* @param array $accountIban
*/
@@ -99,14 +115,6 @@ class ImportAccount
$this->accountNumber = $accountNumber;
}
- /**
- * @param string $expectedType
- */
- public function setExpectedType(string $expectedType)
- {
- $this->expectedType = $expectedType;
- }
-
/**
* @param User $user
*/
@@ -241,6 +249,16 @@ class ImportAccount
$search = intval($array['mapped']);
$account = $this->repository->find($search);
+ if ($account->accountType->type !== $this->expectedType) {
+ Log::error(
+ sprintf(
+ 'Mapped account #%d is of type "%s" but we expect a "%s"-account. Mapping will be ignored.', $account->id, $account->accountType->type,
+ $this->expectedType
+ )
+ );
+ return new Account;
+ }
+
Log::debug(sprintf('Found account! #%d ("%s"). Return it', $account->id, $account->name));
return $account;
diff --git a/app/Import/Storage/ImportStorage.php b/app/Import/Storage/ImportStorage.php
index b366aeb8c7..4986bfbb29 100644
--- a/app/Import/Storage/ImportStorage.php
+++ b/app/Import/Storage/ImportStorage.php
@@ -27,6 +27,7 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
+use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Rules\Processor;
use Illuminate\Support\Collection;
use Log;
@@ -43,6 +44,8 @@ class ImportStorage
/** @var Collection */
public $errors;
public $journals;
+ /** @var CurrencyRepositoryInterface */
+ private $currencyRepository;
/** @var string */
private $dateFormat = 'Ymd';
/** @var TransactionCurrency */
@@ -77,8 +80,11 @@ class ImportStorage
*/
public function setJob(ImportJob $job)
{
- $this->job = $job;
- $this->rules = $this->getUserRules();
+ $this->job = $job;
+ $repository = app(CurrencyRepositoryInterface::class);
+ $repository->setUser($job->user);
+ $this->currencyRepository = $repository;
+ $this->rules = $this->getUserRules();
}
/**
@@ -168,13 +174,23 @@ class ImportStorage
*
* @return TransactionCurrency
*/
- private function getCurrency(ImportJournal $importJournal): TransactionCurrency
+ private function getCurrency(ImportJournal $importJournal, Account $account): TransactionCurrency
{
+ // start with currency pref of account, if any:
+ $currency = $this->currencyRepository->find(intval($account->getMeta('currency_id')));
+ if (!is_null($currency->id)) {
+ return $currency;
+ }
+
+ // use given currency
$currency = $importJournal->getCurrency()->getTransactionCurrency();
- if (is_null($currency->id)) {
- $currency = $this->defaultCurrency;
+ if (!is_null($currency->id)) {
+ return $currency;
}
+ // backup to default
+ $currency = $this->defaultCurrency;
+
return $currency;
}
@@ -196,7 +212,9 @@ class ImportStorage
// amount is positive, it's a deposit, opposing is an revenue:
$account->setExpectedType(AccountType::REVENUE);
- return $account->getAccount();
+ $databaseAccount = $account->getAccount();
+
+ return $databaseAccount;
}
@@ -286,7 +304,7 @@ class ImportStorage
$asset = $importJournal->asset->getAccount();
$amount = $importJournal->getAmount();
- $currency = $this->getCurrency($importJournal);
+ $currency = $this->getCurrency($importJournal, $asset);
$date = $importJournal->getDate($this->dateFormat);
$transactionType = $this->getTransactionType($amount);
$opposing = $this->getOpposingAccount($importJournal->opposing, $amount);
@@ -297,6 +315,11 @@ class ImportStorage
$transactionType = TransactionType::whereType(TransactionType::TRANSFER)->first();
}
+ // verify that opposing account is of the correct type:
+ if ($opposing->accountType->type === AccountType::EXPENSE && $transactionType->type !== TransactionType::WITHDRAWAL) {
+ Log::error(sprintf('Row #%d is imported as a %s but opposing is an expense account. This cannot be!', $index, $transactionType->type));
+ }
+
/*** First step done! */
$this->job->addStepsDone(1);