X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/d74921657d967900bded4aaf4805647e9cf83562..e95294b56b3e6b51c96f35add267b4dc58fe47d7:/SL/Controller/CsvImport/Base.pm diff --git a/SL/Controller/CsvImport/Base.pm b/SL/Controller/CsvImport/Base.pm index 67ca19185..5da5fc290 100644 --- a/SL/Controller/CsvImport/Base.pm +++ b/SL/Controller/CsvImport/Base.pm @@ -2,47 +2,50 @@ package SL::Controller::CsvImport::Base; use strict; -use List::MoreUtils qw(pairwise); +use List::MoreUtils qw(pairwise any); use SL::Helper::Csv; +use SL::DB::Currency; use SL::DB::Customer; use SL::DB::Language; use SL::DB::PaymentTerm; use SL::DB::Vendor; use SL::DB::Contact; +use SL::DB::History; use parent qw(Rose::Object); use Rose::Object::MakeMethods::Generic ( - scalar => [ qw(controller file csv 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 all_vc vc_by) ], + 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 all_currencies default_currency_id all_vc vc_by) ], ); sub run { - my ($self) = @_; + my ($self, %params) = @_; + + $self->test_run($params{test_run}); - $self->controller->track_progress(0); + $self->controller->track_progress(phase => 'parsing csv', progress => 0); my $profile = $self->profile; $self->csv(SL::Helper::Csv->new(file => $self->file->file_name, encoding => $self->controller->profile->get('charset'), - class => $self->class, - profile => $profile, + profile => [{ profile => $profile, class => $self->class }], ignore_unknown_columns => 1, strict_profile => 1, case_insensitive_header => 1, map { ( $_ => $self->controller->profile->get($_) ) } qw(sep_char escape_char quote_char), )); - $self->controller->track_progress(1); + $self->controller->track_progress(progress => 10); my $old_numberformat = $::myconfig{numberformat}; $::myconfig{numberformat} = $self->controller->profile->get('numberformat'); $self->csv->parse; - $self->controller->track_progress(3); + $self->controller->track_progress(progress => 50); $self->controller->errors([ $self->csv->errors ]) if $self->csv->errors; @@ -55,21 +58,17 @@ sub run { $self->controller->raw_data_headers({ used => { }, headers => [ ] }); $self->controller->info_headers({ used => { }, headers => [ ] }); - $::lxdebug->dump(0, "self", $self->controller->info_headers); - $::lxdebug->dump(0, "self", $self->controller->headers); - $::lxdebug->dump(0, "self", $self->controller->raw_data_headers); - my @objects = $self->csv->get_objects; - $self->controller->track_progress(4); + $self->controller->track_progress(progress => 70); my @raw_data = @{ $self->csv->get_data }; - $self->controller->track_progress(4.5); + $self->controller->track_progress(progress => 80); $self->controller->data([ pairwise { { object => $a, raw_data => $b, errors => [], information => [], info_data => {} } } @objects, @raw_data ]); - $self->controller->track_progress(5); + $self->controller->track_progress(progress => 90); $self->check_objects; if ( $self->controller->profile->get('duplicates', 'no_check') ne 'no_check' ) { @@ -78,7 +77,7 @@ sub run { } $self->fix_field_lengths; - $self->controller->track_progress(99); + $self->controller->track_progress(progress => 100); $::myconfig{numberformat} = $old_numberformat; } @@ -124,6 +123,11 @@ sub add_cvar_raw_data_columns { map { $self->add_raw_data_columns($_) if exists $self->controller->data->[0]->{raw_data}->{$_} } @{ $self->cvar_columns }; } +sub init_all_cvar_configs { + # Must be overridden by derived specialized importer classes. + return []; +} + sub init_cvar_columns { my ($self) = @_; @@ -136,6 +140,18 @@ sub init_all_languages { return SL::DB::Manager::Language->get_all; } +sub init_all_currencies { + my ($self) = @_; + + return SL::DB::Manager::Currency->get_all; +} + +sub init_default_currency_id { + my ($self) = @_; + + return SL::DB::Default->get->currency_id; +} + sub init_payment_terms_by { my ($self) = @_; @@ -150,6 +166,10 @@ sub init_all_vc { vendors => SL::DB::Manager::Vendor->get_all }; } +sub force_allow_columns { + return (); +} + sub init_vc_by { my ($self) = @_; @@ -193,8 +213,6 @@ sub check_vc { sub handle_cvars { my ($self, $entry) = @_; - return unless $self->can('all_cvar_configs'); - my %type_to_column = ( text => 'text_value', textfield => 'text_value', select => 'text_value', @@ -221,6 +239,8 @@ sub init_profile { eval "require " . $self->class; my %unwanted = map { ( $_ => 1 ) } (qw(itime mtime), map { $_->name } @{ $self->class->meta->primary_key_columns }); + delete $unwanted{$_} for ($self->force_allow_columns); + my %profile; for my $col ($self->class->meta->columns) { next if $unwanted{$col}; @@ -233,13 +253,9 @@ sub init_profile { $profile{$col} = $name; } - if ($self->can('all_cvar_configs')) { - for (@{ $self->all_cvar_configs }) { - $profile{ 'cvar_' . $_->name } = ''; - } - } + $profile{ 'cvar_' . $_->name } = '' for @{ $self->all_cvar_configs }; - $self->profile(\%profile); + \%profile; } sub add_displayable_columns { @@ -268,8 +284,6 @@ sub setup_displayable_columns { sub add_cvar_columns_to_displayable_columns { my ($self) = @_; - return unless $self->can('all_cvar_configs'); - $self->add_displayable_columns(map { { name => 'cvar_' . $_->name, description => $::locale->text('#1 (custom variable)', $_->description) } } @{ $self->all_cvar_configs }); @@ -293,6 +307,8 @@ sub init_manager_class { $self->manager_class("SL::DB::Manager::" . $1); } +sub is_multiplexed { 0 } + sub check_objects { } @@ -387,6 +403,8 @@ sub save_objects { return unless $data->[0]; return unless $data->[0]{object}; + $self->controller->track_progress(phase => 'saving data', progress => 0); # scale from 45..95%; + my $dbh = $data->[0]{object}->db; $dbh->begin_work; @@ -396,15 +414,19 @@ sub save_objects { my $object = $entry->{object_to_save} || $entry->{object}; - if ( !$object->save(cascade => !!$self->save_with_cascade()) ) { + my $ret; + if (!eval { $ret = $object->save(cascade => !!$self->save_with_cascade()); 1 }) { + push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $@); + } elsif ( !$ret ) { push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $entry->{object}->db->error); } else { + $self->_save_history($object); $self->controller->num_imported($self->controller->num_imported + 1); } } continue { if ($entry_index % 100 == 0) { $dbh->commit; - $self->controller->track_progress(45 + $entry_index/scalar(@$data) * 50); # scale from 45..95%; + $self->controller->track_progress(progress => $entry_index/scalar(@$data) * 100); # scale from 45..95%; $dbh->begin_work; } } @@ -425,5 +447,38 @@ sub fix_field_lengths { } } -1; +sub clean_fields { + my ($self, $illegal_chars, $object, @fields) = @_; + + my @cleaned_fields; + foreach my $field (grep { $object->can($_) } @fields) { + my $value = $object->$field; + + next unless defined($value) && ($value =~ s/$illegal_chars/ /g); + $object->$field($value); + push @cleaned_fields, $field; + } + + return @cleaned_fields; +} + +sub _save_history { + my ($self, $object) = @_; + + if (any { $_ eq $self->controller->{type} } qw(parts customers_vendors)) { + my $snumbers = $self->controller->{type} eq 'parts' ? 'partnumber_' . $object->partnumber + : $self->controller->{type} eq 'customers_vendors' ? + ($self->table eq 'customer' ? 'customernumber_' . $object->customernumber : 'vendornumber_' . $object->vendornumber) + : ''; + + SL::DB::History->new( + trans_id => $object->id, + snumbers => $snumbers, + employee_id => $self->controller->{employee_id}, + addition => 'SAVED', + )->save(); + } +} + +1;