X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FController%2FCsvImport%2FBaseMulti.pm;h=973a4ae8e93fe6f8a0673e4c004301585670eace;hb=93cc0157161476f242f3304b1950f80d0ba5ef08;hp=fb2437a050e9c279f371d80cf31db53031c689ad;hpb=8c84f902c9fbd73554b80b4b6024aa81d1d4d297;p=kivitendo-erp.git diff --git a/SL/Controller/CsvImport/BaseMulti.pm b/SL/Controller/CsvImport/BaseMulti.pm index fb2437a05..973a4ae8e 100644 --- a/SL/Controller/CsvImport/BaseMulti.pm +++ b/SL/Controller/CsvImport/BaseMulti.pm @@ -5,18 +5,18 @@ use strict; use List::MoreUtils qw(pairwise); use SL::Helper::Csv; -use SL::DB::Customer; -use SL::DB::Language; -use SL::DB::PaymentTerm; -use SL::DB::Vendor; -use SL::DB::Contact; use parent qw(SL::Controller::CsvImport::Base); +use Rose::Object::MakeMethods::Generic +( +'scalar --get_set_init' => [ qw(cvar_configs_by cvar_columns_by) ], +); + sub run { my ($self, %params) = @_; - $self->test_run($params{test_run}); + $self->test_run($params{test}); $self->controller->track_progress(phase => 'parsing csv', progress => 0); @@ -40,11 +40,6 @@ sub run { $self->controller->track_progress(progress => 50); - # bb: make sanity-check of it? - #if ($self->csv->is_multiplexed != $self->is_multiplexed) { - # die "multiplex controller on simplex data or vice versa"; - #} - $self->controller->errors([ $self->csv->errors ]) if $self->csv->errors; return if ( !$self->csv->header || $self->csv->errors ); @@ -75,14 +70,15 @@ sub run { $self->controller->raw_data_headers($raw_data_headers); $self->controller->info_headers($info_headers); - my @objects = $self->csv->get_objects; + my $objects = $self->csv->get_objects; + $self->controller->track_progress(progress => 70); my @raw_data = @{ $self->csv->get_data }; $self->controller->track_progress(progress => 80); - $self->controller->data([ pairwise { { object => $a, raw_data => $b, errors => [], information => [], info_data => {} } } @objects, @raw_data ]); + $self->controller->data([ pairwise { { object => $a, raw_data => $b, errors => [], information => [], info_data => {} } } @$objects, @raw_data ]); $self->controller->track_progress(progress => 90); @@ -136,7 +132,51 @@ sub add_raw_data_columns { sub add_cvar_raw_data_columns { my ($self) = @_; - map { $self->add_raw_data_columns($_) if exists $self->controller->data->[0]->{raw_data}->{$_} } @{ $self->cvar_columns }; + foreach my $data (@{ $self->controller->data }) { + my $ri = $data->{raw_data}->{datatype}; + map { $self->add_raw_data_columns($ri, $_) if exists $data->{raw_data}->{$_} } @{ $self->cvar_columns_by->{row_ident}->{$ri} }; + } +} + +sub init_cvar_configs_by { + # Must be overridden by derived specialized importer classes. + return {}; +} + +sub init_cvar_columns_by { + my ($self) = @_; + + my $ccb; + foreach my $p (@{ $self->profile }) { + my $ri = $p->{row_ident}; + $ccb->{row_ident}->{$ri} = [ map { "cvar_" . $_->name } (@{ $self->cvar_configs_by->{row_ident}->{$ri} }) ]; + } + + return $ccb; +} + +sub handle_cvars { + my ($self, $entry, %params) = @_; + + my %type_to_column = ( text => 'text_value', + textfield => 'text_value', + select => 'text_value', + date => 'timestamp_value_as_date', + timestamp => 'timestamp_value_as_date', + number => 'number_value_as_number', + bool => 'bool_value' ); + + $params{sub_module} ||= ''; + my @cvars; + foreach my $config (@{ $self->cvar_configs_by->{row_ident}->{$entry->{raw_data}->{datatype}} }) { + next unless exists $entry->{raw_data}->{ "cvar_" . $config->name }; + my $value = $entry->{raw_data}->{ "cvar_" . $config->name }; + my $column = $type_to_column{ $config->type } || die "Program logic error: unknown custom variable storage type"; + + push @cvars, SL::DB::CustomVariable->new(config_id => $config->id, $column => $value, sub_module => $params{sub_module}); + } + + $entry->{object}->custom_variables(\@cvars) if @cvars; } sub init_profile { @@ -160,7 +200,7 @@ sub init_profile { $prof{$col} = $name; } - $prof{ 'cvar_' . $_->name } = '' for @{ $self->all_cvar_configs }; + $prof{ 'cvar_' . $_->name } = '' for @{ $self->cvar_configs_by->{class}->{$class} }; $class =~ m/^SL::DB::(.+)/; push @profile, {'profile' => \%prof, 'class' => $class, 'row_ident' => $::locale->text($1)}; @@ -202,29 +242,38 @@ sub setup_displayable_columns { } sub add_cvar_columns_to_displayable_columns { - my ($self) = @_; + my ($self, $row_ident) = @_; - $self->add_displayable_columns(map { { name => 'cvar_' . $_->name, + $self->add_displayable_columns($row_ident, + map { { name => 'cvar_' . $_->name, description => $::locale->text('#1 (custom variable)', $_->description) } } - @{ $self->all_cvar_configs }); + @{ $self->cvar_configs_by->{row_ident}->{$row_ident} }); } -sub init_existing_objects { +sub field_lengths { my ($self) = @_; - eval "require " . $self->class; - $self->existing_objects($self->manager_class->get_all); -} + my %field_lengths_by_ri = (); + + foreach my $p (@{ $self->profile }) { + my %field_lengths = map { $_->name => $_->length } grep { $_->type eq 'varchar' } @{ $p->{class}->meta->columns }; + $field_lengths_by_ri{ $p->{row_ident} } = \%field_lengths; + } -sub init_class { - die "class not set"; + return %field_lengths_by_ri; } -sub init_manager_class { +sub fix_field_lengths { my ($self) = @_; - $self->class =~ m/^SL::DB::(.+)/; - $self->manager_class("SL::DB::Manager::" . $1); + my %field_lengths_by_ri = $self->field_lengths; + foreach my $entry (@{ $self->controller->data }) { + next unless @{ $entry->{errors} }; + my %field_lengths = %{ $field_lengths_by_ri{ $entry->{raw_data}->{datatype} } }; + map { $entry->{object}->$_(substr($entry->{object}->$_, 0, $field_lengths{$_})) if $entry->{object}->$_ } keys %field_lengths; + } + + return; } sub is_multiplexed { 1 }