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;
$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;
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);
}
if ($self->is_multiplexed) {
- return $self->_row_header->{$row->[0]}
+ return $self->_row_header->{$row->[$self->_multiplex_datatype_position]}
} else {
return $self->header;
}
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
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' ] ]
'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.',
'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.' => '',
-use Test::More tests => 71;
+use Test::More tests => 75;
use lib 't';
use utf8;
#####
+$csv = SL::Helper::Csv->new(
+ file => \<<EOL,
+description;longdescription;datatype
+name;customernumber;datatype
+Kaffee;"lecker Kaffee";P
+Meier;1;C
+Bier;"kühles Bier";P
+Mueller;2;C
+EOL
+# " # make emacs happy
+ profile => [
+ {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 => \<<EOL,
+datatype;description;longdescription
+name;datatype;customernumber
+P;Kaffee;"lecker Kaffee"
+Meier;C;1
+P;Bier;"kühles Bier"
+Mueller;C;2
+EOL
+# " # make emacs happy
+ profile => [
+ {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,
[%- ELSIF SELF.type == 'orders' %]
<p>
[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.') %]
</p>
<p>
[2]: