Fix check for column roles.

This commit is contained in:
James Cole 2018-01-10 19:06:27 +01:00
parent 91178d2604
commit 61f5ed3874
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
6 changed files with 106 additions and 58 deletions

View File

@ -48,6 +48,8 @@ class ImportJournal
public $currency; public $currency;
/** @var string */ /** @var string */
public $description = ''; public $description = '';
/** @var ImportCurrency */
public $foreignCurrency;
/** @var string */ /** @var string */
public $hash; public $hash;
/** @var array */ /** @var array */
@ -71,6 +73,8 @@ class ImportJournal
/** @var string */ /** @var string */
private $externalId = ''; private $externalId = '';
/** @var array */ /** @var array */
private $foreignAmount;
/** @var array */
private $modifiers = []; private $modifiers = [];
/** @var User */ /** @var User */
private $user; private $user;
@ -86,6 +90,7 @@ class ImportJournal
$this->category = new ImportCategory; $this->category = new ImportCategory;
$this->budget = new ImportBudget; $this->budget = new ImportBudget;
$this->currency = new ImportCurrency; $this->currency = new ImportCurrency;
$this->foreignCurrency = new ImportCurrency;
} }
/** /**
@ -190,6 +195,12 @@ class ImportJournal
case 'amount': case 'amount':
$this->amount = $array; $this->amount = $array;
break; break;
case 'amount_foreign':
$this->foreignAmount = $array;
break;
case 'foreign-currency-code':
$this->foreignCurrency->setId($array);
break;
case 'amount_debit': case 'amount_debit':
$this->amountDebit = $array; $this->amountDebit = $array;
break; break;

View File

@ -412,8 +412,27 @@ class SpectreRoutine implements RoutineInterface
$notes = ''; $notes = '';
$notes .= strval(trans('import.imported_from_account', ['account' => $account->getName()])) . ' ' $notes .= strval(trans('import.imported_from_account', ['account' => $account->getName()])) . ' '
. "\n"; // double space for newline in Markdown. . "\n"; // double space for newline in Markdown.
foreach ($extra as $key => $value) { foreach ($extra as $key => $value) {
switch ($key) { switch ($key) {
case 'account_number':
$importJournal->setValue(['role' => 'account-number', 'value' => $value]);
break;
case 'original_category':
case 'original_subcategory':
case 'customer_category_code':
case 'customer_category_name':
$tags[] = $value;
break;
case 'payee':
$importJournal->setValue(['role' => 'opposing-name', 'value' => $value]);
break;
case 'original_amount':
$importJournal->setValue(['role' => 'amount_foreign', 'value' => $value]);
break;
case 'original_currency_code':
$importJournal->setValue(['role' => 'foreign-currency-code', 'value' => $value]);
break;
default: default:
$notes .= $key . ': ' . $value . ' '; // for newline in Markdown. $notes .= $key . ': ' . $value . ' '; // for newline in Markdown.
} }
@ -494,7 +513,8 @@ class SpectreRoutine implements RoutineInterface
*/ */
private function runStageHaveMapping() private function runStageHaveMapping()
{ {
$accounts = $this->getConfig()['accounts'] ?? []; $config = $this->getConfig();
$accounts = $config['accounts'] ?? [];
$all = []; $all = [];
$count = 0; $count = 0;
/** @var array $accountArray */ /** @var array $accountArray */
@ -503,7 +523,7 @@ class SpectreRoutine implements RoutineInterface
$importId = intval($config['accounts-mapped'][$account->getid()] ?? 0); $importId = intval($config['accounts-mapped'][$account->getid()] ?? 0);
$doImport = $importId !== 0 ? true : false; $doImport = $importId !== 0 ? true : false;
if (!$doImport) { if (!$doImport) {
Log::debug('Will NOT import from Spectre account #%d ("%s")', $account->getId(), $account->getName()); Log::debug(sprintf('Will NOT import from Spectre account #%d ("%s")', $account->getId(), $account->getName()));
continue; continue;
} }
// grab all transactions // grab all transactions

View File

@ -98,13 +98,13 @@ class ImportStorage
$this->repository->setUser($job->user); $this->repository->setUser($job->user);
$config = $this->repository->getConfiguration($job); $config = $this->repository->getConfiguration($job);
$currency = app('amount')->getDefaultCurrencyByUser($this->job->user); $currency = app('amount')->getDefaultCurrencyByUser($job->user);
$this->defaultCurrencyId = $currency->id; $this->defaultCurrencyId = $currency->id;
$this->job = $job;
$this->transfers = $this->getTransfers(); $this->transfers = $this->getTransfers();
$this->applyRules = $config['apply-rules'] ?? false; $this->applyRules = $config['apply-rules'] ?? false;
$this->matchBills = $config['match-bills'] ?? false; $this->matchBills = $config['match-bills'] ?? false;
if (true === $this->applyRules) { if (true === $this->applyRules) {
Log::debug('applyRules seems to be true, get the rules.'); Log::debug('applyRules seems to be true, get the rules.');
$this->rules = $this->getRules(); $this->rules = $this->getRules();
@ -119,8 +119,6 @@ class ImportStorage
Log::debug(sprintf('Value of match bills is %s', var_export($this->matchBills, true))); Log::debug(sprintf('Value of match bills is %s', var_export($this->matchBills, true)));
$this->job = $job;
} }
/** /**

View File

@ -69,7 +69,7 @@ class ImportJobRepository implements ImportJobRepositoryInterface
$status['done'] += $steps; $status['done'] += $steps;
Log::debug(sprintf('Add %d to steps done for job "%s" making steps done %d', $steps, $job->key, $status['done'])); Log::debug(sprintf('Add %d to steps done for job "%s" making steps done %d', $steps, $job->key, $status['done']));
return $this->setExtendedStatus($status); return $this->setExtendedStatus($job, $status);
} }
/** /**

View File

@ -143,14 +143,13 @@ class Roles implements ConfigurationInterface
$this->saveConfig($config); $this->saveConfig($config);
$this->ignoreUnmappableColumns(); $this->ignoreUnmappableColumns();
$this->setRolesComplete(); $res = $this->isRolesComplete();
if ($res === true) {
$config = $this->getConfig(); $config = $this->getConfig();
$config['stage'] = 'map'; $config['stage'] = 'map';
$this->saveConfig($config); $this->saveConfig($config);
$this->isMappingNecessary(); $this->isMappingNecessary();
}
return true; return true;
} }
@ -225,6 +224,56 @@ class Roles implements ConfigurationInterface
return true; return true;
} }
/**
* @return bool
*/
private function isRolesComplete(): bool
{
$config = $this->getConfig();
$count = $config['column-count'];
$assigned = 0;
// check if data actually contains amount column (foreign amount does not count)
$hasAmount = false;
$hasForeignAmount = false;
$hasForeignCode = false;
for ($i = 0; $i < $count; ++$i) {
$role = $config['column-roles'][$i] ?? '_ignore';
if ('_ignore' !== $role) {
++$assigned;
}
if (in_array($role, ['amount', 'amount_credit', 'amount_debit'])) {
$hasAmount = true;
}
if ($role === 'foreign-currency-code') {
$hasForeignCode = true;
}
if ($role === 'amount_foreign') {
$hasForeignAmount = true;
}
}
if ($assigned > 0 && $hasAmount && ($hasForeignCode === false && $hasForeignAmount === false)) {
$this->warning = '';
$this->saveConfig($config);
return true;
}
// warn if has foreign amount but no currency code:
if ($hasForeignAmount && !$hasForeignCode) {
$this->warning = strval(trans('import.foreign_amount_warning'));
return false;
}
if (0 === $assigned || !$hasAmount) {
$this->warning = strval(trans('import.roles_warning'));
return false;
}
return false;
}
/** /**
* make unique example data. * make unique example data.
*/ */
@ -284,36 +333,6 @@ class Roles implements ConfigurationInterface
$this->repository->setConfiguration($this->job, $array); $this->repository->setConfiguration($this->job, $array);
} }
/**
* @return bool
*/
private function setRolesComplete(): bool
{
$config = $this->getConfig();
$count = $config['column-count'];
$assigned = 0;
$hasAmount = false;
for ($i = 0; $i < $count; ++$i) {
$role = $config['column-roles'][$i] ?? '_ignore';
if ('_ignore' !== $role) {
++$assigned;
}
if (in_array($role, ['amount', 'amount_credit', 'amount_debit'])) {
$hasAmount = true;
}
}
if ($assigned > 0 && $hasAmount) {
$config['column-roles-complete'] = true;
$this->warning = '';
}
if (0 === $assigned || !$hasAmount) {
$this->warning = strval(trans('import.roles_warning'));
}
$this->saveConfig($config);
return true;
}
/** /**
* @return bool * @return bool
*/ */