Fix check for column roles.

This commit is contained in:
James Cole
2018-01-10 19:06:27 +01:00
parent 91178d2604
commit 61f5ed3874
6 changed files with 106 additions and 58 deletions

View File

@@ -143,14 +143,13 @@ class Roles implements ConfigurationInterface
$this->saveConfig($config);
$this->ignoreUnmappableColumns();
$this->setRolesComplete();
$config = $this->getConfig();
$config['stage'] = 'map';
$this->saveConfig($config);
$this->isMappingNecessary();
$res = $this->isRolesComplete();
if ($res === true) {
$config = $this->getConfig();
$config['stage'] = 'map';
$this->saveConfig($config);
$this->isMappingNecessary();
}
return true;
}
@@ -225,6 +224,56 @@ class Roles implements ConfigurationInterface
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.
*/
@@ -284,36 +333,6 @@ class Roles implements ConfigurationInterface
$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
*/