X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FController%2FCsvImport%2FBase.pm;h=97126979053938ef43f41ee43bb1b37a8f5ddfcb;hb=d402ad9bc8991a0631206930c9c5b238e5586931;hp=f8e9b591c6f2e3f08773db4da467215688646070;hpb=e0e3eee4eb59b7d6fbe17fe9ad12167b6b8a388b;p=kivitendo-erp.git diff --git a/SL/Controller/CsvImport/Base.pm b/SL/Controller/CsvImport/Base.pm index f8e9b591c..971269790 100644 --- a/SL/Controller/CsvImport/Base.pm +++ b/SL/Controller/CsvImport/Base.pm @@ -2,6 +2,7 @@ package SL::Controller::CsvImport::Base; use strict; +use English qw(-no_match_vars); use List::MoreUtils qw(pairwise any); use SL::Helper::Csv; @@ -18,13 +19,13 @@ use parent qw(Rose::Object); use Rose::Object::MakeMethods::Generic ( scalar => [ qw(controller file csv test_run save_with_cascade) ], - 'scalar --get_set_init' => [ qw(profile displayable_columns existing_objects class manager_class cvar_columns all_cvar_configs all_languages payment_terms_by delivery_terms_by all_vc vc_by) ], + 'scalar --get_set_init' => [ qw(profile displayable_columns existing_objects class manager_class cvar_columns all_cvar_configs all_languages payment_terms_by delivery_terms_by all_vc vc_by clone_methods) ], ); 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); @@ -58,7 +59,7 @@ sub run { $self->controller->raw_data_headers({ used => { }, headers => [ ] }); $self->controller->info_headers({ used => { }, headers => [ ] }); - my @objects = $self->csv->get_objects; + my $objects = $self->csv->get_objects; $self->controller->track_progress(progress => 70); @@ -66,7 +67,7 @@ sub run { $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); @@ -161,6 +162,10 @@ sub init_all_vc { vendors => SL::DB::Manager::Vendor->get_all }; } +sub init_clone_methods { + {} +} + sub force_allow_columns { return (); } @@ -217,15 +222,29 @@ sub handle_cvars { bool => 'bool_value' ); my @cvars; + my %changed_cvars; foreach my $config (@{ $self->all_cvar_configs }) { 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 => ''); + my $new_cvar = SL::DB::CustomVariable->new(config_id => $config->id, $column => $value, sub_module => ''); + + push @cvars, $new_cvar; + $changed_cvars{$config->name} = $new_cvar; } - $entry->{object}->custom_variables(\@cvars); + # merge existing with new cvars. swap every existing with the imported one, push the rest + if (@cvars) { + my @orig_cvars = ($entry->{object_to_save} || $entry->{object})->custom_variables; + for (@orig_cvars) { + $_ = $changed_cvars{ $_->config->name } if $changed_cvars{ $_->config->name }; + delete $changed_cvars{ $_->config->name }; + } + push @orig_cvars, values %changed_cvars; + + $entry->{object}->custom_variables(\@orig_cvars); + } } sub init_profile { @@ -385,6 +404,9 @@ sub check_payment { } $object->payment_id($terms->id); + + # register payment_id for method copying later + $self->clone_methods->{payment_id} = 1; } return 1; @@ -411,6 +433,9 @@ sub check_delivery_term { } $object->delivery_term_id($terms->id); + + # register delivery_term_id for method copying later + $self->clone_methods->{delivery_term_id} = 1; } return 1; @@ -437,9 +462,9 @@ sub save_objects { my $ret; if (!eval { $ret = $object->save(cascade => !!$self->save_with_cascade()); 1 }) { - push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $@); + push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $EVAL_ERROR); } elsif ( !$ret ) { - push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $entry->{object}->db->error); + push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $object->db->error); } else { $self->_save_history($object); $self->controller->num_imported($self->controller->num_imported + 1); @@ -455,7 +480,9 @@ sub save_objects { } sub field_lengths { - return (); + my ($self) = @_; + + return map { $_->name => $_->length } grep { $_->type eq 'varchar' } @{$self->class->meta->columns}; } sub fix_field_lengths {