More code for new importer

This commit is contained in:
James Cole
2016-08-12 09:27:09 +02:00
parent 2111873bcf
commit 28962007c1
11 changed files with 146 additions and 86 deletions

View File

@@ -30,7 +30,7 @@ class CurrencyCode extends BasicConverter implements ConverterInterface
*/
public function convert($value): TransactionCurrency
{
Log::debug('Going to convert ', ['value' => $value]);
Log::debug('Going to convert currency code', ['value' => $value]);
/** @var CurrencyRepositoryInterface $repository */
$repository = app(CurrencyRepositoryInterface::class);

View File

@@ -32,7 +32,7 @@ class Date extends BasicConverter implements ConverterInterface
*/
public function convert($value): Carbon
{
Log::debug('Going to convert ', ['value' => $value]);
Log::debug('Going to convert date', ['value' => $value]);
Log::debug('Format: ', ['format' => $this->config['date-format']]);
try {
$date = Carbon::createFromFormat($this->config['date-format'], $value);

View File

@@ -29,7 +29,7 @@ class INGDebetCredit extends BasicConverter implements ConverterInterface
*/
public function convert($value)
{
Log::debug('Going to convert ', ['value' => $value]);
Log::debug('Going to convert ing debet credit', ['value' => $value]);
if ($value === 'Af') {
Log::debug('Return -1');

View File

@@ -31,7 +31,7 @@ class OpposingAccountIban extends BasicConverter implements ConverterInterface
public function convert($value): Account
{
$value = trim($value);
Log::debug('Going to convert ', ['value' => $value]);
Log::debug('Going to convert opposing IBAN', ['value' => $value]);
if (strlen($value) === 0) {
$this->setCertainty(0);

View File

@@ -31,10 +31,11 @@ class OpposingAccountName extends BasicConverter implements ConverterInterface
public function convert($value): Account
{
$value = trim($value);
Log::debug('Going to convert ', ['value' => $value]);
Log::debug('Going to convert opposing account name', ['value' => $value]);
if (strlen($value) === 0) {
$value = '(empty account name)';
$this->setCertainty(0);
return new Account;
}
/** @var AccountCrudInterface $repository */

View File

@@ -27,12 +27,18 @@ class ImportEntry
{
/** @var array */
public $certain = [];
/** @var string */
public $externalID;
/** @var array */
public $fields = [];
/** @var User */
public $user;
/** @var bool */
public $valid = true;
/** @var int */
private $amountMultiplier = 0;
/** @var array */
private $validFields
= ['amount',
@@ -74,16 +80,13 @@ class ImportEntry
/**
* @param string $role
* @param string $value
* @param int $certainty
* @param $convertedValue
*
* @throws FireflyException
*/
public function importValue(string $role, string $value, int $certainty, $convertedValue)
public function importValue(string $role, int $certainty, $convertedValue)
{
Log::debug('Going to import', ['role' => $role, 'value' => $value, 'certainty' => $certainty]);
switch ($role) {
default:
Log::error('Import entry cannot handle object.', ['role' => $role]);
@@ -96,6 +99,8 @@ class ImportEntry
*/
$this->setFloat('amount', $convertedValue, $certainty);
$this->applyMultiplier('amount'); // if present.
return;
case 'account-id':
case 'account-iban':
@@ -139,6 +144,9 @@ class ImportEntry
case 'date-process':
$this->setDate('date-process', $convertedValue, $certainty);
break;
case 'sepa-ct-id':
case 'sepa-db':
case 'sepa-ct-op':
case'description':
$this->setAppendableString('description', $convertedValue);
break;
@@ -147,12 +155,13 @@ class ImportEntry
case 'ing-debet-credit':
case 'rabo-debet-credit':
$this->manipulateFloat('amount', 'multiply', $convertedValue);
$this->applyMultiplier('amount'); // if present.
break;
case 'tags-comma':
case 'tags-space':
$this->appendCollection('tags', $convertedValue);
case 'external-id':
// ignore for now.
$this->externalID = $convertedValue;
break;
}
@@ -178,6 +187,16 @@ class ImportEntry
$this->fields[$field] = $this->fields[$field]->merge($convertedValue);
}
/**
* @param string $field
*/
private function applyMultiplier(string $field)
{
if ($this->fields[$field] != 0 && $this->amountMultiplier != 0) {
$this->fields[$field] = $this->fields[$field] * $this->amountMultiplier;
}
}
/**
* @param string $field
* @param string $action
@@ -191,8 +210,8 @@ class ImportEntry
default:
Log::error('Cannot handle manipulateFloat', ['field' => $field, 'action' => $action]);
throw new FireflyException('Cannot manipulateFloat with action ' . $action);
case'multiply':
$this->fields[$field] = $this->fields[$field] * $convertedValue;
case 'multiply':
$this->amountMultiplier = $convertedValue;
break;
}
}

View File

@@ -11,10 +11,9 @@ declare(strict_types = 1);
namespace FireflyIII\Import\Importer;
use FireflyIII\Crud\Account\AccountCrud;
use FireflyIII\Import\Converter\ConverterInterface;
use FireflyIII\Import\ImportEntry;
use FireflyIII\Models\Account;
use FireflyIII\Import\Specifics\SpecificInterface;
use FireflyIII\Models\ImportJob;
use Illuminate\Support\Collection;
use League\Csv\Reader;
@@ -27,9 +26,19 @@ use Log;
*/
class CsvImporter implements ImporterInterface
{
/** @var Collection */
public $collection;
/** @var ImportJob */
public $job;
/**
* CsvImporter constructor.
*/
public function __construct()
{
$this->collection = new Collection;
}
/**
* Run the actual import
*
@@ -45,18 +54,17 @@ class CsvImporter implements ImporterInterface
$reader->setDelimiter($config['delimiter']);
$start = $config['has-headers'] ? 1 : 0;
$results = $reader->fetch();
$collection = new Collection;
foreach ($results as $index => $row) {
if ($index >= $start) {
Log::debug('----- import entry build start --');
Log::debug(sprintf('Now going to import row %d.', $index));
$importEntry = $this->importSingleRow($index, $row);
$collection->put($index, $importEntry);
$this->collection->put($index, $importEntry);
}
}
Log::debug(sprintf('Import collection contains %d entries', $collection->count()));
Log::debug(sprintf('Import collection contains %d entries', $this->collection->count()));
return $collection;
return $this->collection;
}
/**
@@ -75,19 +83,28 @@ class CsvImporter implements ImporterInterface
*/
private function importSingleRow(int $index, array $row): ImportEntry
{
// create import object:
// create import object. This is where each entry ends up.
$object = new ImportEntry;
// set some vars:
$object->setUser($this->job->user);
$config = $this->job->configuration;
foreach ($row as $index => $value) {
// and this is the point where the specifix go to work.
foreach ($config['specifics'] as $name => $enabled) {
/** @var SpecificInterface $specific */
$specific = app('FireflyIII\Import\Specifics\\' . $name);
// it returns the row, possibly modified:
$row = $specific->run($row);
}
foreach ($row as $rowIndex => $value) {
// find the role for this column:
$role = $config['column-roles'][$index] ?? '_ignore';
$doMap = $config['column-do-mapping'][$index] ?? false;
$role = $config['column-roles'][$rowIndex] ?? '_ignore';
$doMap = $config['column-do-mapping'][$rowIndex] ?? false;
$converterClass = config('csv.import_roles.' . $role . '.converter');
$mapping = $config['column-mapping-config'][$index] ?? [];
$mapping = $config['column-mapping-config'][$rowIndex] ?? [];
/** @var ConverterInterface $converter */
$converter = app('FireflyIII\\Import\\Converter\\' . $converterClass);
// set some useful values for the converter:
@@ -101,21 +118,15 @@ class CsvImporter implements ImporterInterface
$certainty = $converter->getCertainty();
// log it.
Log::debug('Value ', ['index' => $index, 'value' => $value, 'role' => $role]);
Log::debug('Value ', ['index' => $rowIndex, 'value' => $value, 'role' => $role]);
// store in import entry:
$object->importValue($role, $value, $certainty, $convertedValue);
Log::debug('Going to import', ['role' => $role, 'value' => $value, 'certainty' => $certainty]);
$object->importValue($role, $certainty, $convertedValue);
}
return $object;
// $result = $object->import();
// if ($result->failed()) {
// Log::error(sprintf('Import of row %d has failed.', $index), $result->errors->toArray());
// }
//
// exit;
//
// return true;
}
}

View File

@@ -34,4 +34,14 @@ class AbnAmroDescription implements SpecificInterface
{
return 'Fixes possible problems with ABN Amro descriptions.';
}
/**
* @param array $row
*
* @return array
*/
public function run(array $row): array
{
return $row;
}
}

View File

@@ -11,6 +11,8 @@ declare(strict_types = 1);
namespace FireflyIII\Import\Specifics;
use Log;
/**
* Class RabobankDescription
*
@@ -18,6 +20,14 @@ namespace FireflyIII\Import\Specifics;
*/
class RabobankDescription implements SpecificInterface
{
/**
* @return string
*/
static public function getDescription(): string
{
return 'Fixes possible problems with Rabobank descriptions.';
}
/**
* @return string
*/
@@ -27,10 +37,28 @@ class RabobankDescription implements SpecificInterface
}
/**
* @return string
* @param array $row
*
* @return array
*/
static public function getDescription(): string
public function run(array $row): array
{
return 'Fixes possible problems with Rabobank descriptions.';
$oppositeAccount = trim($row[5]);
$oppositeName = trim($row[6]);
$alternateName = trim($row[10]);
if (strlen($oppositeAccount) < 1 && strlen($oppositeName) < 1) {
Log::debug(
sprintf(
'Rabobank specific: Opposite account and opposite name are' .
' both empty. Will use "%s" (from description) instead', $alternateName
)
);
$row[6] = $alternateName;
$row[10] = '';
} else {
Log::debug('Rabobank specific: either opposite account or name are filled.');
}
return $row;
}
}

View File

@@ -28,4 +28,11 @@ interface SpecificInterface
*/
static public function getDescription(): string;
/**
* @param array $row
*
* @return array
*/
public function run(array $row): array;
}

View File

@@ -100,63 +100,40 @@
"has-headers": false,
"date-format": "Ymd",
"delimiter": ",",
"import-account": 0,
"import-account": 1,
"specifics": {
"RabobankDescription": 1,
"AbnAmroDescription": 1
"RabobankDescription": 1
},
"column-count": 30,
"column-count": 19,
"column-roles": [
"amount",
"account-id",
"account-iban",
"account-name",
"opposing-number",
"bill-id",
"bill-name",
"budget-id",
"budget-name",
"category-id",
"category-name",
"currency-code",
"currency-id",
"currency-symbol",
"currency-name",
"date-transaction",
"date-interest",
"rabo-debet-credit",
"amount",
"opposing-iban",
"opposing-name",
"date-book",
"description",
"_ignore",
"ing-debet-credit",
"opposing-iban",
"opposing-id",
"opposing-name",
"opposing-number",
"rabo-debet-credit",
"tags-comma",
"tags-space",
"date-interest",
"date-book",
"date-process",
"external-id"
"description",
"description",
"description",
"description",
"description",
"description",
"sepa-ct-id",
"sepa-ct-op",
"sepa-db"
],
"column-do-mapping": [
true,
true,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
true,
true,
false,
false,
false,
@@ -171,7 +148,14 @@
false
],
"column-roles-complete": false,
"column-mapping-config": {},
"column-mapping-config": {
"0": [],
"1": {
"EUR": 1
},
"5": [],
"6": []
},
"column-mapping-complete": false
}
}