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 public function convert($value): TransactionCurrency
{ {
Log::debug('Going to convert ', ['value' => $value]); Log::debug('Going to convert currency code', ['value' => $value]);
/** @var CurrencyRepositoryInterface $repository */ /** @var CurrencyRepositoryInterface $repository */
$repository = app(CurrencyRepositoryInterface::class); $repository = app(CurrencyRepositoryInterface::class);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -11,10 +11,9 @@ declare(strict_types = 1);
namespace FireflyIII\Import\Importer; namespace FireflyIII\Import\Importer;
use FireflyIII\Crud\Account\AccountCrud;
use FireflyIII\Import\Converter\ConverterInterface; use FireflyIII\Import\Converter\ConverterInterface;
use FireflyIII\Import\ImportEntry; use FireflyIII\Import\ImportEntry;
use FireflyIII\Models\Account; use FireflyIII\Import\Specifics\SpecificInterface;
use FireflyIII\Models\ImportJob; use FireflyIII\Models\ImportJob;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use League\Csv\Reader; use League\Csv\Reader;
@@ -27,9 +26,19 @@ use Log;
*/ */
class CsvImporter implements ImporterInterface class CsvImporter implements ImporterInterface
{ {
/** @var Collection */
public $collection;
/** @var ImportJob */ /** @var ImportJob */
public $job; public $job;
/**
* CsvImporter constructor.
*/
public function __construct()
{
$this->collection = new Collection;
}
/** /**
* Run the actual import * Run the actual import
* *
@@ -41,22 +50,21 @@ class CsvImporter implements ImporterInterface
$content = $this->job->uploadFileContents(); $content = $this->job->uploadFileContents();
// create CSV reader. // create CSV reader.
$reader = Reader::createFromString($content); $reader = Reader::createFromString($content);
$reader->setDelimiter($config['delimiter']); $reader->setDelimiter($config['delimiter']);
$start = $config['has-headers'] ? 1 : 0; $start = $config['has-headers'] ? 1 : 0;
$results = $reader->fetch(); $results = $reader->fetch();
$collection = new Collection;
foreach ($results as $index => $row) { foreach ($results as $index => $row) {
if ($index >= $start) { if ($index >= $start) {
Log::debug('----- import entry build start --'); Log::debug('----- import entry build start --');
Log::debug(sprintf('Now going to import row %d.', $index)); Log::debug(sprintf('Now going to import row %d.', $index));
$importEntry = $this->importSingleRow($index, $row); $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 private function importSingleRow(int $index, array $row): ImportEntry
{ {
// create import object: // create import object. This is where each entry ends up.
$object = new ImportEntry; $object = new ImportEntry;
// set some vars: // set some vars:
$object->setUser($this->job->user); $object->setUser($this->job->user);
$config = $this->job->configuration; $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: // find the role for this column:
$role = $config['column-roles'][$index] ?? '_ignore'; $role = $config['column-roles'][$rowIndex] ?? '_ignore';
$doMap = $config['column-do-mapping'][$index] ?? false; $doMap = $config['column-do-mapping'][$rowIndex] ?? false;
$converterClass = config('csv.import_roles.' . $role . '.converter'); $converterClass = config('csv.import_roles.' . $role . '.converter');
$mapping = $config['column-mapping-config'][$index] ?? []; $mapping = $config['column-mapping-config'][$rowIndex] ?? [];
/** @var ConverterInterface $converter */ /** @var ConverterInterface $converter */
$converter = app('FireflyIII\\Import\\Converter\\' . $converterClass); $converter = app('FireflyIII\\Import\\Converter\\' . $converterClass);
// set some useful values for the converter: // set some useful values for the converter:
@@ -101,21 +118,15 @@ class CsvImporter implements ImporterInterface
$certainty = $converter->getCertainty(); $certainty = $converter->getCertainty();
// log it. // log it.
Log::debug('Value ', ['index' => $index, 'value' => $value, 'role' => $role]); Log::debug('Value ', ['index' => $rowIndex, 'value' => $value, 'role' => $role]);
// store in import entry: // 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; 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.'; 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; namespace FireflyIII\Import\Specifics;
use Log;
/** /**
* Class RabobankDescription * Class RabobankDescription
* *
@@ -18,6 +20,14 @@ namespace FireflyIII\Import\Specifics;
*/ */
class RabobankDescription implements SpecificInterface class RabobankDescription implements SpecificInterface
{ {
/**
* @return string
*/
static public function getDescription(): string
{
return 'Fixes possible problems with Rabobank descriptions.';
}
/** /**
* @return string * @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; 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, "has-headers": false,
"date-format": "Ymd", "date-format": "Ymd",
"delimiter": ",", "delimiter": ",",
"import-account": 0, "import-account": 1,
"specifics": { "specifics": {
"RabobankDescription": 1, "RabobankDescription": 1
"AbnAmroDescription": 1
}, },
"column-count": 30, "column-count": 19,
"column-roles": [ "column-roles": [
"amount",
"account-id",
"account-iban", "account-iban",
"account-name",
"opposing-number",
"bill-id",
"bill-name",
"budget-id",
"budget-name",
"category-id",
"category-name",
"currency-code", "currency-code",
"currency-id", "date-interest",
"currency-symbol", "rabo-debet-credit",
"currency-name", "amount",
"date-transaction", "opposing-iban",
"opposing-name",
"date-book",
"description", "description",
"_ignore", "_ignore",
"ing-debet-credit", "description",
"opposing-iban", "description",
"opposing-id", "description",
"opposing-name", "description",
"opposing-number", "description",
"rabo-debet-credit", "description",
"tags-comma", "sepa-ct-id",
"tags-space", "sepa-ct-op",
"date-interest", "sepa-db"
"date-book",
"date-process",
"external-id"
], ],
"column-do-mapping": [ "column-do-mapping": [
true,
true,
false, false,
false, false,
false, false,
false, true,
false, true,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false,
false, false,
false, false,
false, false,
@@ -171,7 +148,14 @@
false false
], ],
"column-roles-complete": false, "column-roles-complete": false,
"column-mapping-config": {}, "column-mapping-config": {
"0": [],
"1": {
"EUR": 1
},
"5": [],
"6": []
},
"column-mapping-complete": false "column-mapping-complete": false
} }
} }