Skip to content

Firefly III: a personal finances manager

Implemented option to choose field delimiter in CSV import

Robert Horlings61a703e6052f
diff --git a/app/Helpers/Csv/Data.php b/app/Helpers/Csv/Data.php
index f77391cb37..f0271091aa 100644
--- a/app/Helpers/Csv/Data.php
+++ b/app/Helpers/Csv/Data.php
@@ -1,5 +1,4 @@
<?php
-
namespace FireflyIII\Helpers\Csv;
use Crypt;
@@ -17,19 +16,27 @@ class Data
/** @var string */
protected $csvFileContent;
+ /** @var string */
+ protected $delimiter;
+
/** @var string */
protected $csvFileLocation;
+
/** @var string */
protected $dateFormat;
+
/** @var bool */
protected $hasHeaders;
/** @var array */
protected $map = [];
+
/** @var array */
protected $mapped = [];
+
/** @var Reader */
protected $reader;
+
/** @var array */
protected $roles = [];
@@ -40,7 +47,6 @@ class Data
protected $importAccount = 0;
/**
- *
*/
public function __construct()
{
@@ -52,12 +58,13 @@ class Data
$this->sessionMapped();
$this->sessionSpecifix();
$this->sessionImportAccount();
+ $this->sessionDelimiter();
}
protected function sessionHasHeaders()
{
if (Session::has('csv-has-headers')) {
- $this->hasHeaders = (bool)Session::get('csv-has-headers');
+ $this->hasHeaders = (bool) Session::get('csv-has-headers');
}
}
@@ -71,46 +78,54 @@ class Data
protected function sessionDateFormat()
{
if (Session::has('csv-date-format')) {
- $this->dateFormat = (string)Session::get('csv-date-format');
+ $this->dateFormat = (string) Session::get('csv-date-format');
}
}
protected function sessionCsvFileLocation()
{
if (Session::has('csv-file')) {
- $this->csvFileLocation = (string)Session::get('csv-file');
+ $this->csvFileLocation = (string) Session::get('csv-file');
}
}
protected function sessionMap()
{
if (Session::has('csv-map')) {
- $this->map = (array)Session::get('csv-map');
+ $this->map = (array) Session::get('csv-map');
}
}
protected function sessionRoles()
{
if (Session::has('csv-roles')) {
- $this->roles = (array)Session::get('csv-roles');
+ $this->roles = (array) Session::get('csv-roles');
}
}
protected function sessionMapped()
{
if (Session::has('csv-mapped')) {
- $this->mapped = (array)Session::get('csv-mapped');
+ $this->mapped = (array) Session::get('csv-mapped');
}
}
protected function sessionSpecifix()
{
if (Session::has('csv-specifix')) {
- $this->specifix = (array)Session::get('csv-specifix');
+ $this->specifix = (array) Session::get('csv-specifix');
+ }
+ }
+
+ protected function sessionDelimiter()
+ {
+ if (Session::has('csv-delimiter')) {
+ $this->delimiter = Session::get('csv-delimiter');
}
}
/**
+ *
* @return string
*/
public function getDateFormat()
@@ -119,7 +134,8 @@ class Data
}
/**
- * @param mixed $dateFormat
+ *
+ * @param mixed $dateFormat
*/
public function setDateFormat($dateFormat)
{
@@ -128,7 +144,8 @@ class Data
}
/**
- * @param int $importAccount
+ *
+ * @param int $importAccount
*/
public function setImportAccount($importAccount)
{
@@ -137,6 +154,7 @@ class Data
}
/**
+ *
* @return bool
*/
public function hasHeaders()
@@ -145,7 +163,8 @@ class Data
}
/**
- * @param bool $hasHeaders
+ *
+ * @param bool $hasHeaders
*/
public function setHasHeaders($hasHeaders)
{
@@ -154,6 +173,7 @@ class Data
}
/**
+ *
* @return array
*/
public function getMap()
@@ -162,7 +182,8 @@ class Data
}
/**
- * @param array $map
+ *
+ * @param array $map
*/
public function setMap(array $map)
{
@@ -171,6 +192,7 @@ class Data
}
/**
+ *
* @return array
*/
public function getMapped()
@@ -179,7 +201,8 @@ class Data
}
/**
- * @param array $mapped
+ *
+ * @param array $mapped
*/
public function setMapped(array $mapped)
{
@@ -188,31 +211,33 @@ class Data
}
/**
+ *
* @return Reader
*/
public function getReader()
{
-
if (strlen($this->csvFileContent) === 0) {
$this->loadCsvFile();
}
-
+
if (is_null($this->reader)) {
$this->reader = Reader::createFromString($this->getCsvFileContent());
+ $this->reader->setDelimiter($this->delimiter);
}
-
+
return $this->reader;
}
protected function loadCsvFile()
{
- $file = $this->getCsvFileLocation();
- $content = file_get_contents($file);
+ $file = $this->getCsvFileLocation();
+ $content = file_get_contents($file);
$contentDecrypted = Crypt::decrypt($content);
$this->setCsvFileContent($contentDecrypted);
}
/**
+ *
* @return string
*/
public function getCsvFileLocation()
@@ -221,7 +246,8 @@ class Data
}
/**
- * @param string $csvFileLocation
+ *
+ * @param string $csvFileLocation
*/
public function setCsvFileLocation($csvFileLocation)
{
@@ -230,6 +256,7 @@ class Data
}
/**
+ *
* @return string
*/
public function getCsvFileContent()
@@ -238,7 +265,8 @@ class Data
}
/**
- * @param string $csvFileContent
+ *
+ * @param string $csvFileContent
*/
public function setCsvFileContent($csvFileContent)
{
@@ -246,6 +274,7 @@ class Data
}
/**
+ *
* @return array
*/
public function getRoles()
@@ -254,7 +283,8 @@ class Data
}
/**
- * @param array $roles
+ *
+ * @param array $roles
*/
public function setRoles(array $roles)
{
@@ -263,6 +293,7 @@ class Data
}
/**
+ *
* @return array
*/
public function getSpecifix()
@@ -271,7 +302,8 @@ class Data
}
/**
- * @param array $specifix
+ *
+ * @param array $specifix
*/
public function setSpecifix($specifix)
{
@@ -279,5 +311,22 @@ class Data
$this->specifix = $specifix;
}
+ /**
+ *
+ * @return string
+ */
+ public function getDelimiter()
+ {
+ return $this->delimiter;
+ }
+ /**
+ *
+ * @param string $delimiter
+ */
+ public function setDelimiter($delimiter)
+ {
+ Session::put('csv-delimiter', $delimiter);
+ $this->delimiter = $delimiter;
+ }
}
diff --git a/app/Http/Controllers/CsvController.php b/app/Http/Controllers/CsvController.php
index bde7d5b7e0..5adf0a8bba 100644
--- a/app/Http/Controllers/CsvController.php
+++ b/app/Http/Controllers/CsvController.php
@@ -385,6 +385,13 @@ class CsvController extends Controller
$settings['has-headers'] = intval(Input::get('has_headers')) === 1;
$settings['specifix'] = Input::get('specifix');
$settings['import-account'] = intval(Input::get('csv_import_account'));
+ $settings['delimiter'] = Input::get('csv_delimiter', ',');
+
+ // A tab character cannot be used itself as option value in HTML
+ // See http://stackoverflow.com/questions/6064135/valid-characters-in-option-value
+ if( $settings[ 'delimiter' ] == 'tab' )
+ $settings[ 'delimiter' ] = "\t";
+
$settings['map'] = [];
$settings['mapped'] = [];
$settings['roles'] = [];
@@ -405,7 +412,8 @@ class CsvController extends Controller
$this->data->setRoles($settings['roles']);
$this->data->setSpecifix($settings['specifix']);
$this->data->setImportAccount($settings['import-account']);
-
+ $this->data->setDelimiter($settings['delimiter']);
+
return redirect(route('csv.column-roles'));
}
diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php
index 9beff0f8e6..fa7ef1932c 100644
--- a/resources/lang/en_US/firefly.php
+++ b/resources/lang/en_US/firefly.php
@@ -282,6 +282,7 @@ return [
'csv_specifix_RabobankDescription' => 'Select this when you\'re importing Rabobank CSV export files.',
'csv_specifix_Dummy' => 'Checking this has no effect whatsoever.',
'csv_import_account_help' => 'If your CSV file does NOT contain information about your asset account(s), use this dropdown to select to which account the transactions in the CSV belong to.',
+ 'csv_delimiter_help' => 'Choose the field delimiter that is used in your input file. If not sure, comma is the safest option.',
'csv_date_parse_error' => 'Could not parse a valid date from ":value", using the format ":format". Are you sure your CSV is correct?',
// create new stuff:
diff --git a/resources/lang/en_US/form.php b/resources/lang/en_US/form.php
index ec273e435e..5c3f6a4b27 100644
--- a/resources/lang/en_US/form.php
+++ b/resources/lang/en_US/form.php
@@ -53,6 +53,7 @@ return [
'csv_config' => 'CSV import configuration',
'specifix' => 'Bank- or file specific fixes',
'csv_import_account' => 'Default import account',
+ 'csv_delimiter' => 'CSV field delimiter',
'attachments[]' => 'Attachments',
'store_new_withdrawal' => 'Store new withdrawal',
'store_new_deposit' => 'Store new deposit',
diff --git a/resources/lang/nl_NL/firefly.php b/resources/lang/nl_NL/firefly.php
index 7644571945..e42436e6b4 100755
--- a/resources/lang/nl_NL/firefly.php
+++ b/resources/lang/nl_NL/firefly.php
@@ -282,6 +282,7 @@ return [
'csv_specifix_RabobankDescription' => 'Vink dit aan als je Rabobank bestanden importeert.',
'csv_specifix_Dummy' => 'Dit vinkje doet niks (dummy).',
'csv_import_account_help' => 'Als jouw CSV bestand geen referenties bevat naar jouw rekening(en), geef dan hier aan om welke rekening het gaat.',
+ 'csv_delimiter_help' => 'Kies het veld scheidingsteken dat in het invoerbestand is gebruikt. Bij twijfel is de komma de veiligste optie.',
'csv_date_parse_error' => 'Firefly kan van ":value" geen datum maken, gegeven het formaat ":format". Weet je zeker dat je CSV goed is?',
// create new stuff:
diff --git a/resources/lang/nl_NL/form.php b/resources/lang/nl_NL/form.php
index d075a7693f..93eb8a75a0 100755
--- a/resources/lang/nl_NL/form.php
+++ b/resources/lang/nl_NL/form.php
@@ -53,6 +53,7 @@ return [
'csv_config' => 'Configuratiebestand',
'specifix' => 'Bank- or of bestandsspecifieke opties',
'csv_import_account' => 'Standaard rekening voor importeren',
+ 'csv_delimiter' => 'CSV scheidingsteken',
'attachments[]' => 'Bijlagen',
'store_new_withdrawal' => 'Nieuwe uitgave opslaan',
'store_new_deposit' => 'Nieuwe inkomsten opslaan',
diff --git a/resources/views/csv/index.twig b/resources/views/csv/index.twig
index 4bd88729ce..4e2756dcb0 100644
--- a/resources/views/csv/index.twig
+++ b/resources/views/csv/index.twig
@@ -63,6 +63,8 @@
{{ ExpandedForm.select('csv_import_account', accounts, 0, {helpText: 'csv_import_account_help'|_} ) }}
+ {{ ExpandedForm.select('csv_delimiter', { ',': ', (comma)', 'tab': '(tab)', ';': '; (semicolon)'}, 0, {helpText: 'csv_delimiter_help'|_} ) }}
+
{{ ExpandedForm.multiCheckbox('specifix', specifix) }}