From bad889d450918b7611b2765061cb34e1f2493b98 Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 12 Dec 2017 20:53:16 +0100 Subject: [PATCH] Update ING description for #1015 --- app/Import/FileProcessor/CsvProcessor.php | 8 +++++--- app/Import/Specifics/IngDescription.php | 17 +++++++++++++++-- app/Support/Import/Configuration/Csv/Map.php | 6 ++++-- app/Support/Import/Configuration/Csv/Roles.php | 14 ++++++++------ 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/app/Import/FileProcessor/CsvProcessor.php b/app/Import/FileProcessor/CsvProcessor.php index 0b547eb026..9a21cfc9da 100644 --- a/app/Import/FileProcessor/CsvProcessor.php +++ b/app/Import/FileProcessor/CsvProcessor.php @@ -169,9 +169,11 @@ class CsvProcessor implements FileProcessorInterface $delimiter = "\t"; } $reader->setDelimiter($delimiter); - $start = $config['has-headers'] ? 1 : 0; - $results = $reader->setOffset($start)->fetch(); - Log::debug(sprintf('Created a CSV reader starting at offset %d', $start)); + if($config['has-headers']) { + $reader->setHeaderOffset(0); + } + $results = $reader->getRecords(); + Log::debug('Created a CSV reader.'); return $results; } diff --git a/app/Import/Specifics/IngDescription.php b/app/Import/Specifics/IngDescription.php index 010b93801d..c34cf61cde 100644 --- a/app/Import/Specifics/IngDescription.php +++ b/app/Import/Specifics/IngDescription.php @@ -60,7 +60,7 @@ class IngDescription implements SpecificInterface */ public function run(array $row): array { - $this->row = $row; + $this->row = array_values($row); if (count($this->row) >= 8) { // check if the array is correct switch ($this->row[4]) { // Get value for the mutation type case 'GT': // InternetBankieren @@ -69,6 +69,8 @@ class IngDescription implements SpecificInterface case 'IC': // Incasso $this->removeIBANIngDescription(); $this->removeNameIngDescription(); + // if "tegenrekening" empty, copy the description. Primitive, but it works. + $this->copyDescriptionToOpposite(); break; case 'BA': // Betaalautomaat $this->addNameIngDescription(); @@ -113,9 +115,20 @@ class IngDescription implements SpecificInterface */ protected function removeNameIngDescription() { - // Try remove everything bevore the 'Omschrijving' + // Try remove everything before the 'Omschrijving' $this->row[8] = preg_replace('/.+Omschrijving: /', '', $this->row[8]); return true; } + + /** + * + */ + private function copyDescriptionToOpposite(): void + { + $search = ['Naar Oranje Spaarrekening ', 'Afschrijvingen']; + if (strlen($this->row[3]) === 0) { + $this->row[3] = trim(str_ireplace($search, '', $this->row[8])); + } + } } diff --git a/app/Support/Import/Configuration/Csv/Map.php b/app/Support/Import/Configuration/Csv/Map.php index 7aa35f62b0..449124bbb8 100644 --- a/app/Support/Import/Configuration/Csv/Map.php +++ b/app/Support/Import/Configuration/Csv/Map.php @@ -60,8 +60,10 @@ class Map implements ConfigurationInterface /** @var Reader $reader */ $reader = Reader::createFromString($content); $reader->setDelimiter($this->configuration['delimiter']); - $offset = $this->configuration['has-headers'] ? 1 : 0; - $results = $reader->setOffset($offset)->fetch(); + if($this->configuration['has-headers']) { + $reader->setHeaderOffset(0); + } + $results = $reader->getRecords(); $this->validSpecifics = array_keys(config('csv.import_specifics')); $indexes = array_keys($this->data); $rowIndex = 0; diff --git a/app/Support/Import/Configuration/Csv/Roles.php b/app/Support/Import/Configuration/Csv/Roles.php index 97160b430e..f0d1f206c5 100644 --- a/app/Support/Import/Configuration/Csv/Roles.php +++ b/app/Support/Import/Configuration/Csv/Roles.php @@ -26,6 +26,7 @@ use FireflyIII\Import\Specifics\SpecificInterface; use FireflyIII\Models\ImportJob; use FireflyIII\Support\Import\Configuration\ConfigurationInterface; use League\Csv\Reader; +use League\Csv\Statement; use Log; /** @@ -53,9 +54,10 @@ class Roles implements ConfigurationInterface // create CSV reader. $reader = Reader::createFromString($content); $reader->setDelimiter($config['delimiter']); - $start = $config['has-headers'] ? 1 : 0; - $end = $start + config('csv.example_rows'); - + if ($config['has-headers']) { + $reader->setHeaderOffset(0); + } + $stmt = (new Statement)->limit(intval(config('csv.example_rows', 5))); // set data: $roles = $this->getRoles(); asort($roles); @@ -66,13 +68,13 @@ class Roles implements ConfigurationInterface 'headers' => $config['has-headers'] ? $reader->fetchOne(0) : [], ]; - while ($start < $end) { - $row = $reader->fetchOne($start); + + $records = $stmt->process($reader); + foreach ($records as $row) { $row = $this->processSpecifics($row); $count = count($row); $this->data['total'] = $count > $this->data['total'] ? $count : $this->data['total']; $this->processRow($row); - ++$start; } $this->updateColumCount();