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);
use Rose::Object::MakeMethods::Generic
(
- scalar => [ qw(type profile all_profiles all_charsets sep_char all_sep_chars quote_char all_quote_chars escape_char all_escape_chars) ],
+ 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');
$self->redirect_to(action => 'new', 'profile.type' => $self->type);
}
+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, name => $file_name);
+}
+
#
# filters
#
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});
}
$self->$sub(($char_map{$type}->{$char} || [])->[0] || $char);
}
- if ($self->type eq 'customers_vendors') {
- $self->render('csv_import/form_customers_vendors', title => $::locale->text('CSV import: customers and vendors'));
+ $self->file(SL::SessionFile->new($self->csv_file_name));
- } elsif ($self->type eq 'addresses') {
- $self->render('csv_import/form_addresses', title => $::locale->text('CSV import: shipping addresses'));
+ 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')
+ : $self->type eq 'parts' ? $::locale->text('CSV import: parts and services')
+ : $self->type eq 'projects' ? $::locale->text('CSV import: projects')
+ : die;
- } elsif ($self->type eq 'contacts') {
- $self->render('csv_import/form_contacts', title => $::locale->text('CSV import: contacts'));
+ 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);
+ }
- } elsif ($self->type eq 'parts') {
- $self->render('csv_import/form_parts', title => $::locale->text('CSV import: parts, services and assemblies'));
+ $self->setup_help;
- } else {
- die;
- }
+ $self->render('csv_import/form', title => $title);
}
sub test_and_import {
$self->profile_from_form;
- # do the import thingy...
+ if ($::form->{file}) {
+ my $file = SL::SessionFile->new($self->csv_file_name, mode => '>');
+ $file->fh->print($::form->{file});
+ $file->fh->close;
+ }
+
+ my $file = SL::SessionFile->new($self->csv_file_name, mode => '<', encoding => $self->profile->get('charset'));
+ if (!$file->fh) {
+ flash('error', $::locale->text('No file has been uploaded yet.'));
+ return $self->action_new;
+ }
+
+ $self->file($file);
+
+ my $worker = $self->worker();
+
+ $worker->run;
+
+ $self->num_imported(0);
+ $worker->save_objects if !$params{test};
+
+ $self->num_importable(scalar grep { !$_ } map { scalar @{ $_->{errors} } } @{ $self->data || [] });
+ $self->import_status($params{test} ? 'tested' : 'imported');
+
+ flash('info', $::locale->text('Objects have been imported.')) if !$params{test};
+
$self->action_new;
}
push @settings, { key => "${type}_char", value => $char };
}
+ if ($self->type eq 'parts') {
+ $::form->{settings}->{sellprice_adjustment} = $::form->parse_amount(\%::myconfig, $::form->{settings}->{sellprice_adjustment});
+ }
+
delete $::form->{profile}->{id};
$self->profile($existing_profile || SL::DB::CsvImportProfile->new);
$self->profile->assign_attributes(%{ $::form->{profile} });
);
}
+sub csv_file_name {
+ my ($self) = @_;
+ return "csv-import-" . $self->type . ".csv";
+}
+
+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(@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->worker->setup_displayable_columns;
+}
+
+
1;