From ccb40ac450d3094ebc5b544e75ccacdc27f47aee Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bernd=20Ble=C3=9Fmann?= Date: Mon, 20 Jan 2014 12:32:19 +0100 Subject: [PATCH] CSV-Import mit Multiplex-Daten: Die Spalte datatype muss nicht an der ersten Position sein. --- SL/Helper/Csv.pm | 34 ++++++++++++++--- locale/de/all | 2 +- locale/en/all | 2 +- t/helper/csv.t | 51 ++++++++++++++++++++++++- templates/webpages/csv_import/form.html | 2 +- 5 files changed, 81 insertions(+), 10 deletions(-) diff --git a/SL/Helper/Csv.pm b/SL/Helper/Csv.pm index 2730502b3..c4a8aefa2 100644 --- a/SL/Helper/Csv.pm +++ b/SL/Helper/Csv.pm @@ -7,12 +7,13 @@ use version 0.77; use Carp; use IO::File; use Params::Validate qw(:all); -use List::MoreUtils qw(all pairwise); +use List::MoreUtils qw(all pairwise firstidx); use Text::CSV_XS; use Rose::Object::MakeMethods::Generic scalar => [ qw( file encoding sep_char quote_char escape_char header profile numberformat dateformat ignore_unknown_columns strict_profile is_multiplexed _row_header _io _csv _objects _parsed _data _errors all_cvar_configs case_insensitive_header + _multiplex_datatype_position ) ]; use SL::Helper::Csv::Dispatcher; @@ -59,6 +60,7 @@ sub parse { $self->_open_file; return if ! $self->_check_multiplexed; return if ! $self->_check_header; + return if ! $self->_check_multiplex_datatype_position; return if ! $self->dispatcher->parse_profile; return if ! $self->_parse_data; @@ -216,6 +218,25 @@ sub _check_header { return $self->header($header); } +sub _check_multiplex_datatype_position { + my ($self) = @_; + + return 1 if !$self->is_multiplexed; # ok if if not multiplexed + + my @positions = map { firstidx { 'datatype' eq lc($_) } @{ $_ } } @{ $self->header }; + my $first_pos = $positions[0]; + if (all { $first_pos == $_ } @positions) { + $self->_multiplex_datatype_position($first_pos); + return 1; + } else { + $self->_push_error([0, + "datatype field must be at the same position for all datatypes for multiplexed data", + 0, + 0]); + return 0; + } +} + sub _parse_data { my ($self, %params) = @_; my (@data, @errors); @@ -261,7 +282,7 @@ sub _header_by_row { } if ($self->is_multiplexed) { - return $self->_row_header->{$row->[0]} + return $self->_row_header->{$row->[$self->_multiplex_datatype_position]} } else { return $self->header; } @@ -380,7 +401,7 @@ nothing more. This module can handle multiplexed data of different class types. In that case multiple profiles with classes and row identifiers must be given. Multiple headers may also be given or read from csv data. Data must contain the row -identifier in the first column and it's field name must be 'datatype'. +identifier in the column named 'datatype'. =back @@ -446,15 +467,16 @@ in objects. If not given, headers are taken from the first n lines of data, where n is the number of different class types. -In case of multiplexed data the first column must be named 'datatype'. This -name must be given in the header. +In case of multiplexed data there must be a column named 'datatype'. This +column must be given in each header and must be at the same position in each +header. Examples: classic data of one type: [ 'name', 'street', 'zipcode', 'city' ] - multiplexed data with two different types + multiplexed data with two different types: [ [ 'datatype', 'ordernumber', 'customer', 'transdate' ], [ 'datatype', 'partnumber', 'qty', 'sellprice' ] ] diff --git a/locale/de/all b/locale/de/all index 4568b0202..ac67a872c 100755 --- a/locale/de/all +++ b/locale/de/all @@ -2116,7 +2116,7 @@ $self->{texts} = { 'The client has been created.' => 'Der Mandant wurde angelegt.', 'The client has been deleted.' => 'Der Mandant wurde gelöscht.', 'The client has been saved.' => 'Der Mandant wurde gespeichert.', - 'The column "datatype" must be present and must be the first column. The values must be the row names (see settings) for order and item data respectively.' => 'Die Spalte "datatype" muss vorhanden sein und sie muss die erste Spalte sein. Die Werte in dieser Spalte müssen die Namen der Auftrag-/Positions-Zeilen (siehe Einstellungen) sein.', + 'The column "datatype" must be present and must be at the same position / column in each data set. The values must be the row names (see settings) for order and item data respectively.' => 'Die Spalte "datatype" muss vorhanden sein und sie muss in jedem Datensatz an der gleichen Stelle / Spalte sein. Die Werte in dieser Spalte müssen die Namen der Auftrag-/Positions-Zeilen (siehe Einstellungen) sein.', 'The column "make_X" can contain either a vendor\'s database ID, a vendor number or a vendor\'s name.' => 'Die Spalte "make_X" can entweder die Datenbank-ID des Lieferanten, eine Lieferantennummer oder einen Lieferantennamen enthalten.', 'The column triplets can occur multiple times with different numbers "X" each time (e.g. "make_1", "model_1", "lastcost_1", "make_2", "model_2", "lastcost_2", "make_3", "model_3", "lastcost_3" etc).' => 'Die Spalten-Dreiergruppen können mehrfach auftreten, sofern sie unterschiedliche Nummern "X" verwenden (z.B. "make_1", "model_1", "lastcost_1", "make_2", "model_2", "lastcost_2", "make_3", "model_3", "lastcost_3" etc).', 'The columns "Dunning Duedate", "Total Fees" and "Interest" show data for the previous dunning created for this invoice.' => 'Die Spalten "Zahlbar bis", "Kumulierte Gebühren" und "Zinsen" zeigen Daten der letzten für diese Rechnung erzeugten Mahnung.', diff --git a/locale/en/all b/locale/en/all index b5faacac2..6f86703b7 100644 --- a/locale/en/all +++ b/locale/en/all @@ -2079,7 +2079,7 @@ $self->{texts} = { 'The client has been created.' => '', 'The client has been deleted.' => '', 'The client has been saved.' => '', - 'The column "datatype" must be present and must be the first column. The values must be the row names (see settings) for order and item data respectively.' => '', + 'The column "datatype" must be present and must be at the same position / column in each data set. The values must be the row names (see settings) for order and item data respectively.' => '', 'The column "make_X" can contain either a vendor\'s database ID, a vendor number or a vendor\'s name.' => '', 'The column triplets can occur multiple times with different numbers "X" each time (e.g. "make_1", "model_1", "lastcost_1", "make_2", "model_2", "lastcost_2", "make_3", "model_3", "lastcost_3" etc).' => '', 'The columns "Dunning Duedate", "Total Fees" and "Interest" show data for the previous dunning created for this invoice.' => '', diff --git a/t/helper/csv.t b/t/helper/csv.t index 809dfb11a..3a8556cfd 100644 --- a/t/helper/csv.t +++ b/t/helper/csv.t @@ -1,4 +1,4 @@ -use Test::More tests => 71; +use Test::More tests => 75; use lib 't'; use utf8; @@ -551,6 +551,55 @@ ok !$csv->_check_multiplexed, 'multiplex check detects empty header'; ##### +$csv = SL::Helper::Csv->new( + file => \< [ + {class => 'SL::DB::Part', row_ident => 'P'}, + {class => 'SL::DB::Customer', row_ident => 'C'}, + ], + ignore_unknown_columns => 1, +); +$csv->parse; +is $csv->_multiplex_datatype_position, 2, 'multiplex check detects datatype field position right'; + +is_deeply $csv->get_data, [ { datatype => 'P', description => 'Kaffee', longdescription => 'lecker Kaffee' }, + { datatype => 'C', name => 'Meier', customernumber => 1}, + { datatype => 'P', description => 'Bier', longdescription => 'kühles Bier' }, + { datatype => 'C', name => 'Mueller', customernumber => 2} + ], + 'multiplex: datatype not at first position works'; + +##### + +$csv = SL::Helper::Csv->new( + file => \< [ + {class => 'SL::DB::Part', row_ident => 'P'}, + {class => 'SL::DB::Customer', row_ident => 'C'}, + ], + ignore_unknown_columns => 1, +); +ok !$csv->parse, 'multiplex check detects incosistent datatype field position'; +is_deeply( ($csv->errors)[0], [ 0, 'datatype field must be at the same position for all datatypes for multiplexed data', 0, 0 ], 'multiplex data with inconsistent datatype field posiotion throws error'); + +##### + $csv = SL::Helper::Csv->new( file => \"Datatype;Description\nDatatype;Name\nP;Kaffee\nC;Meier", # " # make emacs happy case_insensitive_header => 1, diff --git a/templates/webpages/csv_import/form.html b/templates/webpages/csv_import/form.html index 4a9672b8f..0f8ac959d 100644 --- a/templates/webpages/csv_import/form.html +++ b/templates/webpages/csv_import/form.html @@ -133,7 +133,7 @@ [%- ELSIF SELF.type == 'orders' %]

[1]: - [% LxERP.t8('The column "datatype" must be present and must be the first column. The values must be the row names (see settings) for order and item data respectively.') %] + [% LxERP.t8('The column "datatype" must be present and must be at the same position / column in each data set. The values must be the row names (see settings) for order and item data respectively.') %]

[2]: -- 2.20.1