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

View File

@@ -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 */
@@ -78,6 +81,9 @@ class ImportStorage
public function setJob(ImportJob $job)
{
$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
{
$currency = $importJournal->getCurrency()->getTransactionCurrency();
if (is_null($currency->id)) {
$currency = $this->defaultCurrency;
// 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)) {
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);