kivitendo/SL/Controller/CsvImport.pm @ d5a003cf
7f1f5efe | Moritz Bunkus | package SL::Controller::CsvImport;
|
||
use strict;
|
||||
1abd7845 | Moritz Bunkus | use SL::DB::Buchungsgruppe;
|
||
7f1f5efe | Moritz Bunkus | use SL::DB::CsvImportProfile;
|
||
use SL::Helper::Flash;
|
||||
29269e99 | Moritz Bunkus | use SL::SessionFile;
|
||
7b9d1666 | Moritz Bunkus | use SL::Controller::CsvImport::Contact;
|
||
f87763cd | Moritz Bunkus | use SL::Controller::CsvImport::CustomerVendor;
|
||
81379539 | Moritz Bunkus | use SL::Controller::CsvImport::Part;
|
||
9e152755 | Moritz Bunkus | use SL::Controller::CsvImport::Shipto;
|
||
7f1f5efe | Moritz Bunkus | |||
use List::MoreUtils qw(none);
|
||||
use parent qw(SL::Controller::Base);
|
||||
use Rose::Object::MakeMethods::Generic
|
||||
(
|
||||
f87763cd | Moritz Bunkus | 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
|
||
2c6717d9 | Moritz Bunkus | import_status errors headers raw_data_headers info_headers data num_imported num_importable displayable_columns) ],
|
||
7f1f5efe | Moritz Bunkus | );
|
||
__PACKAGE__->run_before('check_auth');
|
||||
__PACKAGE__->run_before('ensure_form_structure');
|
||||
__PACKAGE__->run_before('check_type');
|
||||
__PACKAGE__->run_before('load_all_profiles');
|
||||
#
|
||||
# actions
|
||||
#
|
||||
sub action_new {
|
||||
my ($self) = @_;
|
||||
$self->load_default_profile unless $self->profile;
|
||||
$self->render_inputs;
|
||||
}
|
||||
sub action_test {
|
||||
my ($self) = @_;
|
||||
$self->test_and_import(test => 1);
|
||||
}
|
||||
sub action_import {
|
||||
my $self = shift;
|
||||
$self->test_and_import(test => 0);
|
||||
}
|
||||
sub action_save {
|
||||
my ($self) = @_;
|
||||
$self->profile_from_form(SL::DB::Manager::CsvImportProfile->find_by(name => $::form->{profile}->{name}));
|
||||
$self->profile->save;
|
||||
flash_later('info', $::locale->text("The profile has been saved under the name '#1'.", $self->profile->name));
|
||||
$self->redirect_to(action => 'new', 'profile.type' => $self->type, 'profile.id' => $self->profile->id);
|
||||
}
|
||||
sub action_destroy {
|
||||
my $self = shift;
|
||||
my $profile = SL::DB::CsvImportProfile->new(id => $::form->{profile}->{id});
|
||||
$profile->delete(cascade => 1);
|
||||
flash_later('info', $::locale->text('The profile \'#1\' has been deleted.', $profile->name));
|
||||
$self->redirect_to(action => 'new', 'profile.type' => $self->type);
|
||||
}
|
||||
6759902a | Moritz Bunkus | sub action_download_sample {
|
||
my $self = shift;
|
||||
$self->profile_from_form;
|
||||
$self->setup_help;
|
||||
my $file_name = 'csv_import_sample_' . $self->type . '.csv';
|
||||
my $file = SL::SessionFile->new($file_name, mode => '>', encoding => $self->profile->get('charset'));
|
||||
my $csv = Text::CSV_XS->new({ binary => 1, map { ( $_ => $self->profile->get($_) ) } qw(sep_char escape_char quote_char),});
|
||||
$csv->print($file->fh, [ map { $_->{name} } @{ $self->displayable_columns } ]);
|
||||
$file->fh->print("\r\n");
|
||||
$csv->print($file->fh, [ map { $_->{description} } @{ $self->displayable_columns } ]);
|
||||
$file->fh->print("\r\n");
|
||||
$file->fh->close;
|
||||
$self->send_file($file->file_name);
|
||||
}
|
||||
7f1f5efe | Moritz Bunkus | #
|
||
# filters
|
||||
#
|
||||
sub check_auth {
|
||||
$::auth->assert('config');
|
||||
}
|
||||
sub check_type {
|
||||
my ($self) = @_;
|
||||
die "Invalid CSV import type" if none { $_ eq $::form->{profile}->{type} } qw(parts customers_vendors addresses contacts);
|
||||
$self->type($::form->{profile}->{type});
|
||||
}
|
||||
sub ensure_form_structure {
|
||||
my ($self, %params) = @_;
|
||||
$::form->{profile} = {} unless ref $::form->{profile} eq 'HASH';
|
||||
$::form->{settings} = {} unless ref $::form->{settings} eq 'HASH';
|
||||
}
|
||||
#
|
||||
# helpers
|
||||
#
|
||||
sub render_inputs {
|
||||
my ($self, %params) = @_;
|
||||
$self->all_charsets([ [ 'UTF-8', 'UTF-8' ],
|
||||
[ 'ISO-8859-1', 'ISO-8859-1 (Latin 1)' ],
|
||||
[ 'ISO-8859-15', 'ISO-8859-15 (Latin 9)' ],
|
||||
[ 'CP850', 'CP850 (DOS/ANSI)' ],
|
||||
[ 'CP1252', 'CP1252 (Windows)' ],
|
||||
]);
|
||||
my %char_map = $self->char_map;
|
||||
foreach my $type (qw(sep quote escape)) {
|
||||
my $sub = "all_${type}_chars";
|
||||
$self->$sub([ sort { $a->[0] cmp $b->[0] } values %{ $char_map{$type} } ]);
|
||||
my $char = $self->profile->get($type . '_char');
|
||||
$sub = "${type}_char";
|
||||
$self->$sub(($char_map{$type}->{$char} || [])->[0] || $char);
|
||||
}
|
||||
29269e99 | Moritz Bunkus | $self->file(SL::SessionFile->new($self->csv_file_name));
|
||
f5594740 | Moritz Bunkus | my $title = $self->type eq 'customers_vendors' ? $::locale->text('CSV import: customers and vendors')
|
||
: $self->type eq 'addresses' ? $::locale->text('CSV import: shipping addresses')
|
||||
: $self->type eq 'contacts' ? $::locale->text('CSV import: contacts')
|
||||
1abd7845 | Moritz Bunkus | : $self->type eq 'parts' ? $::locale->text('CSV import: parts and services')
|
||
f5594740 | Moritz Bunkus | : die;
|
||
7f1f5efe | Moritz Bunkus | |||
1abd7845 | Moritz Bunkus | $self->all_buchungsgruppen(SL::DB::Manager::Buchungsgruppe->get_all_sorted);
|
||
7caf72ff | Moritz Bunkus | $self->setup_help;
|
||
f5594740 | Moritz Bunkus | $self->render('csv_import/form', title => $title);
|
||
7f1f5efe | Moritz Bunkus | }
|
||
sub test_and_import {
|
||||
my ($self, %params) = @_;
|
||||
$self->profile_from_form;
|
||||
29269e99 | Moritz Bunkus | if ($::form->{file}) {
|
||
f87763cd | Moritz Bunkus | my $file = SL::SessionFile->new($self->csv_file_name, mode => '>');
|
||
29269e99 | Moritz Bunkus | $file->fh->print($::form->{file});
|
||
$file->fh->close;
|
||||
}
|
||||
f87763cd | Moritz Bunkus | my $file = SL::SessionFile->new($self->csv_file_name, mode => '<', encoding => $self->profile->get('charset'));
|
||
29269e99 | Moritz Bunkus | if (!$file->fh) {
|
||
flash('error', $::locale->text('No file has been uploaded yet.'));
|
||||
return $self->action_new;
|
||||
}
|
||||
81379539 | Moritz Bunkus | my $worker = $self->create_worker($file);
|
||
f87763cd | Moritz Bunkus | $worker->run;
|
||
$worker->save_objects if !$params{test};
|
||||
b9e6845d | Moritz Bunkus | $self->num_importable(scalar grep { !$_ } map { scalar @{ $_->{errors} } } @{ $self->data || [] });
|
||
f87763cd | Moritz Bunkus | $self->import_status($params{test} ? 'tested' : 'imported');
|
||
136a063c | Moritz Bunkus | flash('info', $::locale->text('Objects have been imported.')) if !$params{test};
|
||
7f1f5efe | Moritz Bunkus | $self->action_new;
|
||
}
|
||||
sub load_default_profile {
|
||||
my ($self) = @_;
|
||||
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;
|
||||
}
|
||||
$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'));
|
||||
}
|
||||
sub profile_from_form {
|
||||
my ($self, $existing_profile) = @_;
|
||||
delete $::form->{profile}->{id};
|
||||
my %char_map = $self->char_map;
|
||||
my @settings;
|
||||
foreach my $type (qw(sep quote escape)) {
|
||||
my %rev_chars = map { $char_map{$type}->{$_}->[0] => $_ } keys %{ $char_map{$type} };
|
||||
my $char = $::form->{"${type}_char"} eq 'custom' ? $::form->{"custom_${type}_char"} : $rev_chars{ $::form->{"${type}_char"} };
|
||||
push @settings, { key => "${type}_char", value => $char };
|
||||
}
|
||||
81379539 | Moritz Bunkus | if ($self->type eq 'parts') {
|
||
$::form->{settings}->{sellprice_adjustment} = $::form->parse_amount(\%::myconfig, $::form->{settings}->{sellprice_adjustment});
|
||||
}
|
||||
7f1f5efe | Moritz Bunkus | delete $::form->{profile}->{id};
|
||
$self->profile($existing_profile || SL::DB::CsvImportProfile->new);
|
||||
$self->profile->assign_attributes(%{ $::form->{profile} });
|
||||
$self->profile->settings(map({ { key => $_, value => $::form->{settings}->{$_} } } keys %{ $::form->{settings} }),
|
||||
@settings);
|
||||
$self->profile->set_defaults;
|
||||
}
|
||||
sub char_map {
|
||||
return ( sep => { ',' => [ 'comma', $::locale->text('Comma') ],
|
||||
';' => [ 'semicolon', $::locale->text('Semicolon') ],
|
||||
"\t" => [ 'tab', $::locale->text('Tab') ],
|
||||
' ' => [ 'space', $::locale->text('Space') ],
|
||||
},
|
||||
quote => { '"' => [ 'quote', $::locale->text('Quotes') ],
|
||||
"'" => [ 'singlequote', $::locale->text('Single quotes') ],
|
||||
},
|
||||
escape => { '"' => [ 'quote', $::locale->text('Quotes') ],
|
||||
"'" => [ 'singlequote', $::locale->text('Single quotes') ],
|
||||
},
|
||||
);
|
||||
}
|
||||
29269e99 | Moritz Bunkus | sub csv_file_name {
|
||
my ($self) = @_;
|
||||
return "csv-import-" . $self->type . ".csv";
|
||||
}
|
||||
81379539 | Moritz Bunkus | sub create_worker {
|
||
my ($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)
|
||||
: die "Program logic error";
|
||||
}
|
||||
7caf72ff | Moritz Bunkus | sub setup_help {
|
||
my ($self) = @_;
|
||||
$self->create_worker->setup_displayable_columns;
|
||||
}
|
||||
7f1f5efe | Moritz Bunkus | 1;
|