X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FController%2FCsvImport.pm;h=34b794fdbe1fcbb98455e490d0a58742b0ef463e;hb=4b26e2437f1ea0d13980a1847cbfbfbd133cae2f;hp=9549ae2a7dd352b1dbc6f8e42ce1060cca1899f9;hpb=d582b83d62fea84292843a34dcee163929a4384e;p=kivitendo-erp.git diff --git a/SL/Controller/CsvImport.pm b/SL/Controller/CsvImport.pm index 9549ae2a7..34b794fdb 100644 --- a/SL/Controller/CsvImport.pm +++ b/SL/Controller/CsvImport.pm @@ -4,12 +4,14 @@ use strict; use SL::DB::Buchungsgruppe; use SL::DB::CsvImportProfile; +use SL::DB::Unit; use SL::Helper::Flash; use SL::SessionFile; use SL::Controller::CsvImport::Contact; use SL::Controller::CsvImport::CustomerVendor; use SL::Controller::CsvImport::Part; use SL::Controller::CsvImport::Shipto; +use SL::Controller::CsvImport::Project; use List::MoreUtils qw(none); @@ -17,8 +19,9 @@ use parent qw(SL::Controller::Base); use Rose::Object::MakeMethods::Generic ( - scalar => [ qw(type profile file all_profiles all_charsets sep_char all_sep_chars quote_char all_quote_chars escape_char all_escape_chars all_buchungsgruppen - import_status errors headers raw_data_headers info_headers data num_imported num_importable displayable_columns) ], + scalar => [ qw(type profile file all_profiles all_charsets sep_char all_sep_chars quote_char all_quote_chars escape_char all_escape_chars all_buchungsgruppen all_units + import_status errors headers raw_data_headers info_headers data num_imported num_importable displayable_columns file) ], + 'scalar --get_set_init' => [ qw(worker) ] ); __PACKAGE__->run_before('check_auth'); @@ -50,7 +53,7 @@ sub action_import { sub action_save { my ($self) = @_; - $self->profile_from_form(SL::DB::Manager::CsvImportProfile->find_by(name => $::form->{profile}->{name})); + $self->profile_from_form(SL::DB::Manager::CsvImportProfile->find_by(name => $::form->{profile}->{name}, login => $::myconfig{login})); $self->profile->save; flash_later('info', $::locale->text("The profile has been saved under the name '#1'.", $self->profile->name)); @@ -60,7 +63,7 @@ sub action_save { sub action_destroy { my $self = shift; - my $profile = SL::DB::CsvImportProfile->new(id => $::form->{profile}->{id}); + my $profile = SL::DB::CsvImportProfile->new(id => $::form->{profile}->{id}, login => $::myconfig{login}); $profile->delete(cascade => 1); flash_later('info', $::locale->text('The profile \'#1\' has been deleted.', $profile->name)); @@ -84,7 +87,7 @@ sub action_download_sample { $file->fh->close; - $self->send_file($file->file_name); + $self->send_file($file->file_name, name => $file_name); } # @@ -98,7 +101,7 @@ sub check_auth { sub check_type { my ($self) = @_; - die "Invalid CSV import type" if none { $_ eq $::form->{profile}->{type} } qw(parts customers_vendors addresses contacts); + die "Invalid CSV import type" if none { $_ eq $::form->{profile}->{type} } qw(parts customers_vendors addresses contacts projects); $self->type($::form->{profile}->{type}); } @@ -140,10 +143,12 @@ sub render_inputs { : $self->type eq 'addresses' ? $::locale->text('CSV import: shipping addresses') : $self->type eq 'contacts' ? $::locale->text('CSV import: contacts') : $self->type eq 'parts' ? $::locale->text('CSV import: parts and services') + : $self->type eq 'projects' ? $::locale->text('CSV import: projects') : die; if ($self->{type} eq 'parts') { $self->all_buchungsgruppen(SL::DB::Manager::Buchungsgruppe->get_all_sorted); + $self->all_units(SL::DB::Manager::Unit->get_all_sorted); } $self->setup_help; @@ -168,7 +173,10 @@ sub test_and_import { return $self->action_new; } - my $worker = $self->create_worker($file); + $self->file($file); + + my $worker = $self->worker(); + $worker->run; $self->num_imported(0); @@ -185,21 +193,26 @@ sub test_and_import { sub load_default_profile { my ($self) = @_; + my $profile; if ($::form->{profile}->{id}) { - $self->profile(SL::DB::CsvImportProfile->new(id => $::form->{profile}->{id})->load); - - } else { - $self->profile(SL::DB::Manager::CsvImportProfile->find_by(type => $self->{type}, is_default => 1)); - $self->profile(SL::DB::CsvImportProfile->new(type => $self->{type})) unless $self->profile; + $profile = SL::DB::Manager::CsvImportProfile->find_by(id => $::form->{profile}->{id}, login => $::myconfig{login}); } + $profile ||= SL::DB::Manager::CsvImportProfile->find_by(type => $self->{type}, is_default => 1, login => $::myconfig{login}); + $profile ||= SL::DB::CsvImportProfile->new(type => $self->{type}, login => $::myconfig{login}); + $self->profile($profile); $self->profile->set_defaults; } sub load_all_profiles { my ($self, %params) = @_; - $self->all_profiles(SL::DB::Manager::CsvImportProfile->get_all(where => [ type => $self->type ], sort_by => 'name')); + $self->all_profiles(SL::DB::Manager::CsvImportProfile->get_all( + where => [ + type => $self->type, + login => $::myconfig{login}, + ], + sort_by => 'name')); } sub profile_from_form { @@ -222,7 +235,7 @@ sub profile_from_form { } delete $::form->{profile}->{id}; - $self->profile($existing_profile || SL::DB::CsvImportProfile->new); + $self->profile($existing_profile || SL::DB::CsvImportProfile->new(login => $::myconfig{login})); $self->profile->assign_attributes(%{ $::form->{profile} }); $self->profile->settings(map({ { key => $_, value => $::form->{settings}->{$_} } } keys %{ $::form->{settings} }), @settings); @@ -249,20 +262,27 @@ sub csv_file_name { return "csv-import-" . $self->type . ".csv"; } -sub create_worker { - my ($self, $file) = @_; +sub init_worker { + my $self = shift; + + my @args = (controller => $self); + + if ( $self->file() ) { + push(@args, file => $self->file()); + } - return $self->{type} eq 'customers_vendors' ? SL::Controller::CsvImport::CustomerVendor->new(controller => $self, file => $file) - : $self->{type} eq 'contacts' ? SL::Controller::CsvImport::Contact->new( controller => $self, file => $file) - : $self->{type} eq 'addresses' ? SL::Controller::CsvImport::Shipto->new( controller => $self, file => $file) - : $self->{type} eq 'parts' ? SL::Controller::CsvImport::Part->new( controller => $self, file => $file) + return $self->{type} eq 'customers_vendors' ? SL::Controller::CsvImport::CustomerVendor->new(@args) + : $self->{type} eq 'contacts' ? SL::Controller::CsvImport::Contact->new(@args) + : $self->{type} eq 'addresses' ? SL::Controller::CsvImport::Shipto->new(@args) + : $self->{type} eq 'parts' ? SL::Controller::CsvImport::Part->new(@args) + : $self->{type} eq 'projects' ? SL::Controller::CsvImport::Project->new(@args) : die "Program logic error"; } sub setup_help { my ($self) = @_; - $self->create_worker->setup_displayable_columns; + $self->worker->setup_displayable_columns; }