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

This commit is contained in:
James Cole
2017-07-08 06:53:06 +02:00
parent f747f3bd10
commit 952328d55c
2 changed files with 57 additions and 16 deletions

View File

@@ -67,6 +67,22 @@ class ImportAccount
return $this->account; 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 * @param array $accountIban
*/ */
@@ -99,14 +115,6 @@ class ImportAccount
$this->accountNumber = $accountNumber; $this->accountNumber = $accountNumber;
} }
/**
* @param string $expectedType
*/
public function setExpectedType(string $expectedType)
{
$this->expectedType = $expectedType;
}
/** /**
* @param User $user * @param User $user
*/ */
@@ -241,6 +249,16 @@ class ImportAccount
$search = intval($array['mapped']); $search = intval($array['mapped']);
$account = $this->repository->find($search); $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)); Log::debug(sprintf('Found account! #%d ("%s"). Return it', $account->id, $account->name));
return $account; return $account;

View File

@@ -27,6 +27,7 @@ use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency; use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType; use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Rules\Processor; use FireflyIII\Rules\Processor;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Log; use Log;
@@ -43,6 +44,8 @@ class ImportStorage
/** @var Collection */ /** @var Collection */
public $errors; public $errors;
public $journals; public $journals;
/** @var CurrencyRepositoryInterface */
private $currencyRepository;
/** @var string */ /** @var string */
private $dateFormat = 'Ymd'; private $dateFormat = 'Ymd';
/** @var TransactionCurrency */ /** @var TransactionCurrency */
@@ -77,8 +80,11 @@ class ImportStorage
*/ */
public function setJob(ImportJob $job) public function setJob(ImportJob $job)
{ {
$this->job = $job; $this->job = $job;
$this->rules = $this->getUserRules(); $repository = app(CurrencyRepositoryInterface::class);
$repository->setUser($job->user);
$this->currencyRepository = $repository;
$this->rules = $this->getUserRules();
} }
/** /**
@@ -168,13 +174,23 @@ class ImportStorage
* *
* @return TransactionCurrency * @return TransactionCurrency
*/ */
private function getCurrency(ImportJournal $importJournal): TransactionCurrency private function getCurrency(ImportJournal $importJournal, Account $account): TransactionCurrency
{ {
$currency = $importJournal->getCurrency()->getTransactionCurrency(); // start with currency pref of account, if any:
if (is_null($currency->id)) { $currency = $this->currencyRepository->find(intval($account->getMeta('currency_id')));
$currency = $this->defaultCurrency; if (!is_null($currency->id)) {
return $currency;
} }
// use given currency
$currency = $importJournal->getCurrency()->getTransactionCurrency();
if (!is_null($currency->id)) {
return $currency;
}
// backup to default
$currency = $this->defaultCurrency;
return $currency; return $currency;
} }
@@ -196,7 +212,9 @@ class ImportStorage
// amount is positive, it's a deposit, opposing is an revenue: // amount is positive, it's a deposit, opposing is an revenue:
$account->setExpectedType(AccountType::REVENUE); $account->setExpectedType(AccountType::REVENUE);
return $account->getAccount(); $databaseAccount = $account->getAccount();
return $databaseAccount;
} }
@@ -286,7 +304,7 @@ class ImportStorage
$asset = $importJournal->asset->getAccount(); $asset = $importJournal->asset->getAccount();
$amount = $importJournal->getAmount(); $amount = $importJournal->getAmount();
$currency = $this->getCurrency($importJournal); $currency = $this->getCurrency($importJournal, $asset);
$date = $importJournal->getDate($this->dateFormat); $date = $importJournal->getDate($this->dateFormat);
$transactionType = $this->getTransactionType($amount); $transactionType = $this->getTransactionType($amount);
$opposing = $this->getOpposingAccount($importJournal->opposing, $amount); $opposing = $this->getOpposingAccount($importJournal->opposing, $amount);
@@ -297,6 +315,11 @@ class ImportStorage
$transactionType = TransactionType::whereType(TransactionType::TRANSFER)->first(); $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! */ /*** First step done! */
$this->job->addStepsDone(1); $this->job->addStepsDone(1);