12 |
12 |
use SL::Helper::Flash;
|
13 |
13 |
use SL::Locale::String;
|
14 |
14 |
use SL::SessionFile;
|
|
15 |
use SL::SessionFile::Random;
|
15 |
16 |
use SL::Controller::CsvImport::Contact;
|
16 |
17 |
use SL::Controller::CsvImport::CustomerVendor;
|
17 |
18 |
use SL::Controller::CsvImport::Part;
|
... | ... | |
228 |
229 |
sub action_add_mapping_from_upload {
|
229 |
230 |
my ($self) = @_;
|
230 |
231 |
|
231 |
|
$self->profile_from_form;
|
|
232 |
if ($::form->{tmp_profile_id}) {
|
|
233 |
$self->profile_from_form(SL::DB::CsvImportProfile->new(id => $::form->{tmp_profile_id})->load);
|
|
234 |
} else {
|
|
235 |
$self->profile_from_form;
|
|
236 |
}
|
232 |
237 |
$self->setup_help;
|
233 |
238 |
|
234 |
|
my $file = SL::SessionFile->new($self->csv_file_name, mode => '<', encoding => $self->profile->get('charset'));
|
|
239 |
my $file_name;
|
|
240 |
if ($self->profile->get('file_name')) {
|
|
241 |
$file_name = $self->profile->get('file_name');
|
|
242 |
} else {
|
|
243 |
$self->js
|
|
244 |
->flash('error', t8('No file has been uploaded yet.'))
|
|
245 |
->render;
|
|
246 |
return;
|
|
247 |
}
|
|
248 |
|
|
249 |
my $file = SL::SessionFile->new($file_name, mode => '<', encoding => $self->profile->get('charset'));
|
235 |
250 |
if (!$file->fh) {
|
236 |
251 |
$self->js
|
237 |
252 |
->flash('error', t8('No file has been uploaded yet.'))
|
... | ... | |
309 |
324 |
$self->$sub(($char_map{$type}->{$char} || [])->[0] || $char);
|
310 |
325 |
}
|
311 |
326 |
|
312 |
|
$self->file(SL::SessionFile->new($self->csv_file_name));
|
|
327 |
if ($self->profile->get('file_name')) {
|
|
328 |
$self->file(SL::SessionFile->new($self->profile->get('file_name')));
|
|
329 |
}
|
313 |
330 |
|
314 |
331 |
my $title = $self->type eq 'customers_vendors' ? $::locale->text('CSV import: customers and vendors')
|
315 |
332 |
: $self->type eq 'addresses' ? $::locale->text('CSV import: shipping addresses')
|
... | ... | |
341 |
358 |
sub test_and_import_deferred {
|
342 |
359 |
my ($self, %params) = @_;
|
343 |
360 |
|
344 |
|
if ( $::form->{force_profile} && $::form->{profile}->{id} ) {
|
|
361 |
if ( $::form->{force_profile} && ($::form->{tmp_profile_id} || $::form->{profile}->{id}) ) {
|
|
362 |
$::form->{profile}->{id} = $::form->{tmp_profile_id} if $::form->{tmp_profile_id};
|
345 |
363 |
$self->load_default_profile;
|
346 |
|
} else {
|
|
364 |
} elsif ($::form->{tmp_profile_id}) {
|
|
365 |
$self->profile_from_form(SL::DB::CsvImportProfile->new(id => $::form->{tmp_profile_id})->load);
|
|
366 |
} else {
|
347 |
367 |
$self->profile_from_form;
|
348 |
368 |
};
|
349 |
369 |
|
|
370 |
my $file_name;
|
350 |
371 |
if ($::form->{file}) {
|
351 |
|
my $file = SL::SessionFile->new($self->csv_file_name, mode => '>');
|
|
372 |
my $file = SL::SessionFile::Random->new(mode => '>');
|
352 |
373 |
$file->fh->print($::form->{file});
|
353 |
374 |
$file->fh->close;
|
|
375 |
$file_name = $file->file_name;
|
|
376 |
$self->profile->set('file_name', $file_name);
|
|
377 |
} elsif ($self->profile->get('file_name')) {
|
|
378 |
$file_name = $self->profile->get('file_name');
|
|
379 |
} else {
|
|
380 |
flash('error', $::locale->text('No file has been uploaded yet.'));
|
|
381 |
return $self->action_new;
|
354 |
382 |
}
|
355 |
383 |
|
356 |
|
my $file = SL::SessionFile->new($self->csv_file_name, mode => '<', encoding => $self->profile->get('charset'));
|
|
384 |
my $file = SL::SessionFile->new($file_name, mode => '<', encoding => $self->profile->get('charset'));
|
357 |
385 |
if (!$file->fh) {
|
358 |
386 |
flash('error', $::locale->text('No file has been uploaded yet.'));
|
359 |
387 |
return $self->action_new;
|
... | ... | |
363 |
391 |
$self->profile($self->profile->clone_and_reset_deep)->save;
|
364 |
392 |
|
365 |
393 |
$self->{background_job} = SL::BackgroundJob::CsvImport->create_job(
|
366 |
|
file => $self->csv_file_name,
|
367 |
394 |
profile_id => $self->profile->id,
|
368 |
395 |
type => $self->profile->type,
|
369 |
396 |
test => $params{test},
|
... | ... | |
391 |
418 |
my ($self, %params) = @_;
|
392 |
419 |
|
393 |
420 |
my $file = SL::SessionFile->new(
|
394 |
|
$self->csv_file_name,
|
|
421 |
$self->profile->get('file_name'),
|
395 |
422 |
mode => '<',
|
396 |
423 |
encoding => $self->profile->get('charset'),
|
397 |
424 |
session_id => $params{session_id}
|
... | ... | |
467 |
494 |
|
468 |
495 |
$self->profile($existing_profile || SL::DB::CsvImportProfile->new(login => $::myconfig{login}));
|
469 |
496 |
$self->profile->assign_attributes(%{ $::form->{profile} });
|
|
497 |
|
|
498 |
# save settings for file_name, as this is not in form, but maybe in existing_profile
|
|
499 |
push @settings, { key => 'file_name', value => $self->profile->get('file_name') } if $self->profile->get('file_name');
|
|
500 |
|
470 |
501 |
$self->profile->settings(map({ { key => $_, value => $::form->{settings}->{$_} } } keys %{ $::form->{settings} }),
|
471 |
502 |
@settings);
|
472 |
|
|
473 |
503 |
$self->profile->set('json_mappings', JSON::to_json($self->mappings));
|
474 |
|
|
475 |
504 |
$self->profile->set_defaults;
|
476 |
505 |
}
|
477 |
506 |
|
... | ... | |
658 |
687 |
return $report->id;
|
659 |
688 |
}
|
660 |
689 |
|
661 |
|
sub csv_file_name {
|
662 |
|
my ($self) = @_;
|
663 |
|
return "csv-import-" . $self->type . ".csv";
|
664 |
|
}
|
665 |
|
|
666 |
690 |
sub init_worker {
|
667 |
691 |
my $self = shift;
|
668 |
692 |
|
CsvImport: zufälligen Dateinamen für jeden Import. Ablegen in temporärem Profil
Damit können auch mehrere gleiche Imports von einer Session aus gleichzeitig
aufgerufen werden.