1 package SL::Controller::CsvImport;
6 use SL::DB::Buchungsgruppe;
7 use SL::DB::CsvImportProfile;
8 use SL::DB::CsvImportReport;
10 use SL::DB::Helper::Paginated ();
11 use SL::DBUtils qw(do_statement);
12 use SL::Helper::Flash;
13 use SL::Locale::String;
15 use SL::SessionFile::Random;
16 use SL::Controller::CsvImport::AdditionalBillingAddress;
17 use SL::Controller::CsvImport::Contact;
18 use SL::Controller::CsvImport::CustomerVendor;
19 use SL::Controller::CsvImport::Part;
20 use SL::Controller::CsvImport::Inventory;
21 use SL::Controller::CsvImport::Shipto;
22 use SL::Controller::CsvImport::Project;
23 use SL::Controller::CsvImport::Order;
24 use SL::Controller::CsvImport::DeliveryOrder;
25 use SL::Controller::CsvImport::ARTransaction;
27 use SL::Controller::CsvImport::BankTransaction;
28 use SL::BackgroundJob::CsvImport;
29 use SL::System::TaskServer;
31 use List::MoreUtils qw(any none);
32 use List::Util qw(min);
34 use parent qw(SL::Controller::Base);
36 use Rose::Object::MakeMethods::Generic
38 scalar => [ qw(type profile all_profiles all_charsets sep_char all_sep_chars quote_char all_quote_chars escape_char all_escape_chars all_buchungsgruppen all_units
39 import_status errors headers raw_data_headers info_headers data num_importable displayable_columns file all_taxzones) ],
40 'scalar --get_set_init' => [ qw(worker task_server num_imported mappings) ],
42 progress_tracker => { },
43 add_progress_tracker => { interface => 'add', hash_key => 'progress_tracker' },
47 __PACKAGE__->run_before('check_auth', except => [ qw(report) ]);
48 __PACKAGE__->run_before('ensure_form_structure');
49 __PACKAGE__->run_before('check_type', except => [ qw(report) ]);
50 __PACKAGE__->run_before('load_all_profiles');
51 __PACKAGE__->run_before('check_task_server');
53 __PACKAGE__->run_after('cleanup_reports');
62 $self->load_default_profile unless $self->profile;
68 $self->test_and_import_deferred(test => 1);
73 $self->test_and_import_deferred(test => 0);
79 $self->profile_from_form(SL::DB::Manager::CsvImportProfile->find_by(name => $::form->{profile}->{name}, login => $::myconfig{login}));
82 flash_later('info', $::locale->text("The profile has been saved under the name '#1'.", $self->profile->name));
83 $self->redirect_to(action => 'new', 'profile.type' => $self->type, 'profile.id' => $self->profile->id);
89 my $profile = SL::DB::CsvImportProfile->new(id => $::form->{profile}->{id}, login => $::myconfig{login});
90 $profile->delete(cascade => 1);
92 flash_later('info', $::locale->text('The profile \'#1\' has been deleted.', $profile->name));
93 $self->redirect_to(action => 'new', 'profile.type' => $self->type);
100 $self->{background_job} = SL::DB::Manager::BackgroundJob->find_by(id => $::form->{job});
102 my $data = $self->{background_job}->data_as_hash;
104 my $profile = SL::DB::Manager::CsvImportProfile->find_by(id => $data->{profile_id});
105 $self->profile($profile);
107 if ($data->{errors} and my $first_error = $data->{errors}->[0]) {
108 flash('error', $::locale->text('There was an error parsing the csv file: #1 in line #2.', $first_error->[2], $first_error->[4]));
111 if ($data->{progress}{finished} || $data->{errors}) {
112 $self->render('csv_import/_deferred_report', { layout => 0 });
114 if (!$self->task_server->is_running) {
115 $self->task_server->start;
116 $self->{status_text} = t8('Task Server is not running, starting it now. If this does not change, please check your task server config');
117 } elsif (my $phase = $data->{progress}{phase}) {
118 $self->{status_text} = "$data->{progress}{plan}{$phase} / $data->{progress}{num_phases} " . t8($phase);
120 $self->{status_text} = t8('Import not started yet, please wait...');
123 $self->render('csv_import/_deferred_results', { layout => 0 });
127 sub action_download_sample {
130 $self->profile_from_form;
133 my $file_name = 'csv_import_sample_' . $self->type . '.csv';
134 my $file = SL::SessionFile->new($file_name, mode => '>', encoding => $self->profile->get('charset'));
135 my $csv = Text::CSV_XS->new({ binary => 1, map { ( $_ => $self->profile->get($_) ) } qw(sep_char escape_char quote_char),});
137 if ($self->worker->is_multiplexed) {
138 foreach my $p (@{ $self->worker->profile }) {
139 $csv->print($file->fh, [ map { $_->{name} } @{ $self->displayable_columns->{$p->{row_ident}} } ]);
140 $file->fh->print("\r\n");
142 foreach my $p (@{ $self->worker->profile }) {
143 $csv->print($file->fh, [ map { $_->{description} } @{ $self->displayable_columns->{$p->{row_ident}} } ]);
144 $file->fh->print("\r\n");
147 $csv->print($file->fh, [ map { $_->{name} } @{ $self->displayable_columns } ]);
148 $file->fh->print("\r\n");
149 $csv->print($file->fh, [ map { $_->{description} } @{ $self->displayable_columns } ]);
150 $file->fh->print("\r\n");
155 $self->send_file($file->file_name, name => $file_name);
159 my ($self, %params) = @_;
161 my $report_id = $params{report_id} || $::form->{id};
162 $self->{report} = SL::DB::Manager::CsvImportReport->find_by(id => $report_id);
164 if (!$self->{report}) {
165 $::form->error(t8('No report with id #1', $report_id));
168 my $show_info_err = ($self->{report}->profile->get('full_preview', 0) == 1);
169 my $show_first_20 = ($self->{report}->profile->get('full_preview', 0) == 2);
172 if ($show_first_20) {
173 $num_rows = min($self->{report}->numrows, 20);
174 } elsif ($show_info_err) {
175 # count each status row only once
176 $num_rows = SL::DB::Manager::CsvImportReportStatus->get_all_count(query => [csv_import_report_id => $report_id],
181 $num_rows = $self->{report}->numrows;
184 # manual paginating, yuck
185 my $page = $::form->{page} || 1;
187 $pages->{per_page} = $::form->{per_page} || 20;
188 $pages->{max} = SL::DB::Helper::Paginated::ceil($num_rows, $pages->{per_page}) || 1;
189 $pages->{page} = $page < 1 ? 1
190 : $page > $pages->{max} ? $pages->{max}
192 $pages->{common} = [ grep { $_->{visible} } @{ SL::DB::Helper::Paginated::make_common_pages($pages->{page}, $pages->{max}) } ];
194 $self->{report_numheaders} = $self->{report}->numheaders;
195 my $first_row_header = 0;
196 my $last_row_header = $self->{report_numheaders} - 1;
197 my $first_row_data = $pages->{per_page} * ($pages->{page}-1) + $self->{report_numheaders};
198 my $last_row_data = min($pages->{per_page} * $pages->{page}, $num_rows) + $self->{report_numheaders} - 1;
201 $self->{display_rows} = [];
202 if ($show_info_err) {
203 my $limit = $last_row_data - $first_row_data + 1;
204 my $offset = $first_row_data - $self->{report_numheaders};
205 my @err_rows = map { $_->row } @{SL::DB::Manager::CsvImportReportStatus->get_all(query => [csv_import_report_id => $report_id],
211 $self->{display_rows} = [ $first_row_header .. $last_row_header,
216 $self->{display_rows} = [ $first_row_header .. $last_row_header,
217 $first_row_data .. $last_row_data ];
221 row => $self->{display_rows},
222 csv_import_report_id => $report_id,
225 my $rows = SL::DB::Manager::CsvImportReportRow ->get_all(query => \@query, sort_by => 'row');
226 my $status = SL::DB::Manager::CsvImportReportStatus->get_all(query => \@query, sort_by => 'row');
227 $self->{num_errors} = SL::DB::Manager::CsvImportReportStatus->get_all_count(query => [csv_import_report_id => $report_id, type => 'errors']);
229 $self->{report_rows} = $self->{report}->folded_rows(rows => $rows);
230 $self->{report_status} = $self->{report}->folded_status(status => $status);
231 $self->{pages} = $pages;
232 $self->{base_url} = $self->url_for(action => 'report', id => $report_id, no_layout => $params{no_layout} || $::form->{no_layout} );
234 $self->render('csv_import/report', { layout => !($params{no_layout} || $::form->{no_layout}) });
237 sub action_add_empty_mapping_line {
240 $self->profile_from_form;
244 ->append('#csv_import_mappings', $self->render('csv_import/_mapping_item', { layout => 0, output => 0 }))
245 ->hide('#mapping_empty')
249 sub action_add_mapping_from_upload {
252 if ($::form->{tmp_profile_id}) {
253 $self->profile_from_form(SL::DB::CsvImportProfile->new(id => $::form->{tmp_profile_id})->load);
255 $self->profile_from_form;
260 if ($self->profile->get('file_name')) {
261 $file_name = $self->profile->get('file_name');
264 ->flash('error', t8('No file has been uploaded yet.'))
269 my $file = SL::SessionFile->new($file_name, mode => '<', encoding => $self->profile->get('charset'));
272 ->flash('error', t8('No file has been uploaded yet.'))
277 my $csv = SL::Helper::Csv->new(
278 file => $file->file_name,
279 map { $_ => $self->profile->get($_) } qw(sep_char escape_char quote_char),
283 my $header = $csv->check_header;
285 for my $field (@$header) {
286 next if $self->mappings_for_profile->{$field};
288 '#csv_import_mappings',
289 $self->render('csv_import/_mapping_item', { layout => 0, output => 0 }, item => { from => $field }),
294 ->hide('#mapping_empty')
305 $_[0]->worker->check_auth;
311 die "Invalid CSV import type" if none { $_ eq $::form->{profile}->{type} } qw(parts inventories customers_vendors billing_addresses addresses contacts projects orders delivery_orders bank_transactions ar_transactions);
312 $self->type($::form->{profile}->{type});
315 sub ensure_form_structure {
316 my ($self, %params) = @_;
318 $::form->{profile} = {} unless ref $::form->{profile} eq 'HASH';
319 $::form->{settings} = {} unless ref $::form->{settings} eq 'HASH';
327 my ($self, %params) = @_;
329 $self->all_charsets([ [ 'UTF-8', 'UTF-8' ],
330 [ 'ISO-8859-1', 'ISO-8859-1 (Latin 1)' ],
331 [ 'ISO-8859-15', 'ISO-8859-15 (Latin 9)' ],
332 [ 'CP850', 'CP850 (DOS/ANSI)' ],
333 [ 'CP1252', 'CP1252 (Windows)' ],
336 my %char_map = $self->char_map;
338 foreach my $type (qw(sep quote escape)) {
339 my $sub = "all_${type}_chars";
340 $self->$sub([ sort { $a->[0] cmp $b->[0] } values %{ $char_map{$type} } ]);
342 my $char = $self->profile->get($type . '_char');
343 $sub = "${type}_char";
344 $self->$sub(($char_map{$type}->{$char} || [])->[0] || $char);
347 if ($self->profile->get('file_name')) {
348 $self->file(SL::SessionFile->new($self->profile->get('file_name')));
351 my $title = $self->type eq 'customers_vendors' ? $::locale->text('CSV import: customers and vendors')
352 : $self->type eq 'billing_addresses' ? $::locale->text('CSV import: additional billing addresses')
353 : $self->type eq 'addresses' ? $::locale->text('CSV import: shipping addresses')
354 : $self->type eq 'contacts' ? $::locale->text('CSV import: contacts')
355 : $self->type eq 'parts' ? $::locale->text('CSV import: parts and services')
356 : $self->type eq 'inventories' ? $::locale->text('CSV import: inventories')
357 : $self->type eq 'projects' ? $::locale->text('CSV import: projects')
358 : $self->type eq 'orders' ? $::locale->text('CSV import: orders')
359 : $self->type eq 'delivery_orders' ? $::locale->text('CSV import: delivery orders')
360 : $self->type eq 'bank_transactions' ? $::locale->text('CSV import: bank transactions')
361 : $self->type eq 'ar_transactions' ? $::locale->text('CSV import: ar transactions')
364 if ( any { $_ eq $self->{type} } qw(customers_vendors orders delivery_orders ar_transactions) ) {
365 $self->all_taxzones(SL::DB::Manager::TaxZone->get_all_sorted(query => [ obsolete => 0 ]));
368 if ($self->{type} eq 'parts') {
369 $self->all_buchungsgruppen(SL::DB::Manager::Buchungsgruppe->get_all_sorted);
370 $self->all_units(SL::DB::Manager::Unit->get_all_sorted);
375 $self->setup_render_inputs_action_bar;
377 $self->render('csv_import/form', title => $title);
380 sub test_and_import_deferred {
381 my ($self, %params) = @_;
383 if ( $::form->{force_profile} && ($::form->{tmp_profile_id} || $::form->{profile}->{id}) ) {
384 $::form->{profile}->{id} = $::form->{tmp_profile_id} if $::form->{tmp_profile_id};
385 $self->load_default_profile;
386 } elsif ($::form->{tmp_profile_id}) {
387 $self->profile_from_form(SL::DB::CsvImportProfile->new(id => $::form->{tmp_profile_id})->load);
389 $self->profile_from_form;
393 if ($::form->{file}) {
394 my $file = SL::SessionFile::Random->new(mode => '>');
395 $file->fh->print($::form->{file});
397 $file_name = $file->file_name;
398 $self->profile->set('file_name', $file_name);
399 } elsif ($self->profile->get('file_name')) {
400 $file_name = $self->profile->get('file_name');
402 flash('error', $::locale->text('No file has been uploaded yet.'));
403 return $self->action_new;
406 my $file = SL::SessionFile->new($file_name, mode => '<', encoding => $self->profile->get('charset'));
408 flash('error', $::locale->text('No file has been uploaded yet.'));
409 return $self->action_new;
412 # save tempory profile
413 $self->profile($self->profile->clone_and_reset_deep)->save;
415 $self->{background_job} = SL::BackgroundJob::CsvImport->create_job(
416 profile_id => $self->profile->id,
417 type => $self->profile->type,
418 test => $params{test},
419 employee_id => SL::DB::Manager::Employee->current->id,
422 if ($self->task_server->is_running) {
423 $self->task_server->wake_up;
425 $self->task_server->start;
428 flash('info', $::locale->text('Your import is being processed.'));
430 $self->{deferred} = 1;
432 if ($::request->type eq 'json') {
433 $self->render(\ SL::JSON::to_json($self->{background_job}->as_tree), { type => 'json' })
435 $self->render_inputs;
439 sub test_and_import {
440 my ($self, %params) = @_;
442 my $file = SL::SessionFile->new(
443 $self->profile->get('file_name'),
445 encoding => $self->profile->get('charset'),
446 session_id => $params{session_id}
451 my $worker = $self->worker();
453 $worker->run(%params);
455 return if $self->errors;
457 $self->num_imported(0);
458 $worker->save_objects if !$params{test};
460 $self->num_importable(scalar grep { !$_ } map { scalar @{ $_->{errors} } } @{ $self->data || [] });
461 $self->import_status($params{test} ? 'tested' : 'imported');
463 # flash('info', $::locale->text('Objects have been imported.')) if !$params{test};
466 sub load_default_profile {
470 if ($::form->{profile}->{id}) {
471 $profile = SL::DB::Manager::CsvImportProfile->find_by(id => $::form->{profile}->{id});
473 $profile ||= SL::DB::Manager::CsvImportProfile->find_by(type => $self->{type}, is_default => 1, login => $::myconfig{login});
474 $profile ||= SL::DB::CsvImportProfile->new(type => $self->{type}, login => $::myconfig{login});
476 $self->profile($profile);
477 $self->mappings(SL::JSON::from_json($self->profile->get('json_mappings'))) if $self->profile->get('json_mappings');
478 $self->worker->set_profile_defaults;
479 $self->profile->set_defaults;
482 sub load_all_profiles {
483 my ($self, %params) = @_;
485 $self->all_profiles(SL::DB::Manager::CsvImportProfile->get_all(
488 login => $::myconfig{login},
494 sub profile_from_form {
495 my ($self, $existing_profile) = @_;
497 delete $::form->{profile}->{id};
499 my %char_map = $self->char_map;
502 foreach my $type (qw(sep quote escape)) {
503 my %rev_chars = map { $char_map{$type}->{$_}->[0] => $_ } keys %{ $char_map{$type} };
504 my $char = $::form->{"${type}_char"} eq 'custom' ? $::form->{"custom_${type}_char"} : $rev_chars{ $::form->{"${type}_char"} };
506 push @settings, { key => "${type}_char", value => $char };
509 if ($self->type eq 'parts') {
510 $::form->{settings}->{sellprice_adjustment} = $::form->parse_amount(\%::myconfig, $::form->{settings}->{sellprice_adjustment});
513 if ($self->type eq 'orders' or $self->{type} eq 'ar_transactions') {
514 $::form->{settings}->{max_amount_diff} = $::form->parse_amount(\%::myconfig, $::form->{settings}->{max_amount_diff});
517 $self->profile($existing_profile || SL::DB::CsvImportProfile->new(login => $::myconfig{login}));
518 $self->profile->assign_attributes(%{ $::form->{profile} });
520 # save settings for file_name, as this is not in form, but maybe in existing_profile
521 push @settings, { key => 'file_name', value => $self->profile->get('file_name') } if $self->profile->get('file_name');
523 $self->profile->settings(map({ { key => $_, value => $::form->{settings}->{$_} } } keys %{ $::form->{settings} }),
525 $self->profile->set('json_mappings', JSON::to_json($self->mappings));
526 $self->profile->set_defaults;
530 return ( sep => { ',' => [ 'comma', $::locale->text('Comma') ],
531 ';' => [ 'semicolon', $::locale->text('Semicolon') ],
532 "\t" => [ 'tab', $::locale->text('Tab') ],
533 ' ' => [ 'space', $::locale->text('Space') ],
535 quote => { '"' => [ 'quote', $::locale->text('Quotes') ],
536 "'" => [ 'singlequote', $::locale->text('Single quotes') ],
538 escape => { '"' => [ 'quote', $::locale->text('Quotes') ],
539 "'" => [ 'singlequote', $::locale->text('Single quotes') ],
545 my ($self, %params) = @_;
547 if ($self->worker->is_multiplexed) {
548 return $self->save_report_multi(%params);
550 return $self->save_report_single(%params);
554 sub save_report_single {
555 my ($self, %params) = @_;
557 $self->track_progress(phase => 'building report', progress => 0);
559 my $report = SL::DB::CsvImportReport->new(
560 session_id => $params{session_id},
561 profile_id => $self->profile->id,
564 numrows => scalar @{ $self->data },
566 test_mode => $params{test} ? 1 : 0,
569 $report->save(cascade => 1) or die $report->db->error;
571 SL::DB->client->with_transaction(sub {
572 my $dbh = SL::DB->client->dbh;
574 my $query = 'INSERT INTO csv_import_report_rows (csv_import_report_id, col, row, value) VALUES (?, ?, ?, ?)';
575 my $query2 = 'INSERT INTO csv_import_report_status (csv_import_report_id, row, type, value) VALUES (?, ?, ?, ?)';
577 my $sth = $dbh->prepare($query);
578 my $sth2 = $dbh->prepare($query2);
581 my (@headers, @info_methods, @raw_methods, @methods);
583 for my $i (0 .. $#{ $self->info_headers->{headers} }) {
584 next unless $self->info_headers->{used}->{ $self->info_headers->{methods}->[$i] };
585 push @headers, $self->info_headers->{headers}->[$i];
586 push @info_methods, $self->info_headers->{methods}->[$i];
588 for my $i (0 .. $#{ $self->headers->{headers} }) {
589 next unless $self->headers->{used}->{ $self->headers->{headers}->[$i] };
590 push @headers, $self->headers->{headers}->[$i];
591 push @methods, $self->headers->{methods}->[$i];
593 for my $i (0 .. $#{ $self->raw_data_headers->{headers} }) {
594 next unless $self->raw_data_headers->{used}->{ $self->raw_data_headers->{headers}->[$i] };
595 push @headers, $self->raw_data_headers->{headers}->[$i];
596 push @raw_methods, $self->raw_data_headers->{headers}->[$i];
599 do_statement($::form, $sth, $query, $report->id, $_, 0, $headers[$_]) for 0 .. $#headers;
602 my $o1 = @info_methods;
603 my $o2 = $o1 + @methods;
605 for my $row (0 .. $#{ $self->data }) {
606 $self->track_progress(progress => $row / @{ $self->data } * 100) if $row % 1000 == 0;
607 my $data_row = $self->{data}[$row];
609 my $object = $data_row->{object_to_save} || $data_row->{object};
610 do_statement($::form, $sth, $query, $report->id, $_, $row + 1, $data_row->{info_data}{ $info_methods[$_] }) for 0 .. $#info_methods;
611 do_statement($::form, $sth, $query, $report->id, $o1 + $_, $row + 1, $object->${ \ $methods[$_] }) for 0 .. $#methods;
612 do_statement($::form, $sth, $query, $report->id, $o2 + $_, $row + 1, $data_row->{raw_data}{ $raw_methods[$_] }) for 0 .. $#raw_methods;
614 do_statement($::form, $sth2, $query2, $report->id, $row + 1, 'information', $_) for @{ $data_row->{information} || [] };
615 do_statement($::form, $sth2, $query2, $report->id, $row + 1, 'errors', $_) for @{ $data_row->{errors} || [] };
618 }) or do { die SL::DB->client->error };
623 sub save_report_multi {
624 my ($self, %params) = @_;
626 $self->track_progress(phase => 'building report', progress => 0);
628 my $report = SL::DB::CsvImportReport->new(
629 session_id => $params{session_id},
630 profile_id => $self->profile->id,
633 numrows => scalar @{ $self->data },
634 numheaders => scalar @{ $self->worker->profile },
635 test_mode => $params{test} ? 1 : 0,
638 $report->save(cascade => 1) or die $report->db->error;
640 SL::DB->client->with_transaction(sub {
641 my $dbh = SL::DB->client->dbh;
643 my $query = 'INSERT INTO csv_import_report_rows (csv_import_report_id, col, row, value) VALUES (?, ?, ?, ?)';
644 my $query2 = 'INSERT INTO csv_import_report_status (csv_import_report_id, row, type, value) VALUES (?, ?, ?, ?)';
646 my $sth = $dbh->prepare($query);
647 my $sth2 = $dbh->prepare($query2);
650 my ($headers, $info_methods, $raw_methods, $methods);
652 for my $i (0 .. $#{ $self->worker->profile }) {
653 my $row_ident = $self->worker->profile->[$i]->{row_ident};
655 for my $i (0 .. $#{ $self->info_headers->{$row_ident}->{headers} }) {
656 next unless $self->info_headers->{$row_ident}->{used}->{ $self->info_headers->{$row_ident}->{methods}->[$i] };
657 push @{ $headers->{$row_ident} }, $self->info_headers->{$row_ident}->{headers}->[$i];
658 push @{ $info_methods->{$row_ident} }, $self->info_headers->{$row_ident}->{methods}->[$i];
660 for my $i (0 .. $#{ $self->headers->{$row_ident}->{headers} }) {
661 next unless $self->headers->{$row_ident}->{used}->{ $self->headers->{$row_ident}->{headers}->[$i] };
662 push @{ $headers->{$row_ident} }, $self->headers->{$row_ident}->{headers}->[$i];
663 push @{ $methods->{$row_ident} }, $self->headers->{$row_ident}->{methods}->[$i];
666 for my $i (0 .. $#{ $self->raw_data_headers->{$row_ident}->{headers} }) {
667 next unless $self->raw_data_headers->{$row_ident}->{used}->{ $self->raw_data_headers->{$row_ident}->{headers}->[$i] };
668 push @{ $headers->{$row_ident} }, $self->raw_data_headers->{$row_ident}->{headers}->[$i];
669 push @{ $raw_methods->{$row_ident} }, $self->raw_data_headers->{$row_ident}->{headers}->[$i];
674 for my $i (0 .. $#{ $self->worker->profile }) {
675 my $row_ident = $self->worker->profile->[$i]->{row_ident};
676 do_statement($::form, $sth, $query, $report->id, $_, $i, $headers->{$row_ident}->[$_]) for 0 .. $#{ $headers->{$row_ident} };
681 for my $i (0 .. $#{ $self->worker->profile }) {
682 my $row_ident = $self->worker->profile->[$i]->{row_ident};
683 my $n_info_methods = $info_methods->{$row_ident} ? scalar @{ $info_methods->{$row_ident} } : 0;
684 my $n_methods = $methods->{$row_ident} ? scalar @{ $methods->{$row_ident} } : 0;
686 $off1->{$row_ident} = $n_info_methods;
687 $off2->{$row_ident} = $off1->{$row_ident} + $n_methods;
690 my $n_header_rows = scalar @{ $self->worker->profile };
692 for my $row (0 .. $#{ $self->data }) {
693 $self->track_progress(progress => $row / @{ $self->data } * 100) if $row % 1000 == 0;
694 my $data_row = $self->{data}[$row];
695 my $row_ident = $data_row->{raw_data}{datatype};
697 my $o1 = $off1->{$row_ident};
698 my $o2 = $off2->{$row_ident};
700 my $object = $data_row->{object_to_save} || $data_row->{object};
701 do_statement($::form, $sth, $query, $report->id, $_, $row + $n_header_rows, $data_row->{info_data}{ $info_methods->{$row_ident}->[$_] }) for 0 .. $#{ $info_methods->{$row_ident} };
702 do_statement($::form, $sth, $query, $report->id, $o1 + $_, $row + $n_header_rows, $object->${ \ $methods->{$row_ident}->[$_] }) for 0 .. $#{ $methods->{$row_ident} };
703 do_statement($::form, $sth, $query, $report->id, $o2 + $_, $row + $n_header_rows, $data_row->{raw_data}{ $raw_methods->{$row_ident}->[$_] }) for 0 .. $#{ $raw_methods->{$row_ident} };
705 do_statement($::form, $sth2, $query2, $report->id, $row + $n_header_rows, 'information', $_) for @{ $data_row->{information} || [] };
706 do_statement($::form, $sth2, $query2, $report->id, $row + $n_header_rows, 'errors', $_) for @{ $data_row->{errors} || [] };
709 }) or do { die SL::DB->client->error };
717 my @args = (controller => $self);
719 if ( $self->file() ) {
720 push(@args, file => $self->file());
723 return $self->{type} eq 'customers_vendors' ? SL::Controller::CsvImport::CustomerVendor->new(@args)
724 : $self->{type} eq 'contacts' ? SL::Controller::CsvImport::Contact->new(@args)
725 : $self->{type} eq 'billing_addresses' ? SL::Controller::CsvImport::AdditionalBillingAddress->new(@args)
726 : $self->{type} eq 'addresses' ? SL::Controller::CsvImport::Shipto->new(@args)
727 : $self->{type} eq 'parts' ? SL::Controller::CsvImport::Part->new(@args)
728 : $self->{type} eq 'inventories' ? SL::Controller::CsvImport::Inventory->new(@args)
729 : $self->{type} eq 'projects' ? SL::Controller::CsvImport::Project->new(@args)
730 : $self->{type} eq 'orders' ? SL::Controller::CsvImport::Order->new(@args)
731 : $self->{type} eq 'delivery_orders' ? SL::Controller::CsvImport::DeliveryOrder->new(@args)
732 : $self->{type} eq 'bank_transactions' ? SL::Controller::CsvImport::BankTransaction->new(@args)
733 : $self->{type} eq 'ar_transactions' ? SL::Controller::CsvImport::ARTransaction->new(@args)
734 : die "Program logic error";
737 sub init_num_imported { 0 }
742 $self->worker->setup_displayable_columns;
746 my ($self, %params) = @_;
748 for my $tracker ($self->progress_tracker) {
749 $tracker->track_progress(%params);
753 sub init_task_server {
754 SL::System::TaskServer->new;
757 sub cleanup_reports {
758 SL::DB::Manager::CsvImportReport->cleanup;
761 sub check_task_server {
762 if (!$::auth->client->{task_server_user_id}) {
763 flash('error', t8('The task server is required for this module but not enabled for the current client. Please enable it for the client "#1" in the administration section.', $::auth->client->{name}));
766 return 1 if $_[0]->task_server->is_running;
768 flash('warning', t8('The task server is not running at the moment but needed for this module'));
773 sub mappings_for_profile {
774 +{ map { $_->{from} => $_->{to} } @{ $_[0]->mappings } }
778 [ grep { $_->{from} } @{ $::form->{mappings} || [] } ]
781 sub setup_render_inputs_action_bar {
782 my ($self, %params) = @_;
784 for my $bar ($::request->layout->get('actionbar')) {
788 submit => [ '#form', { action => 'CsvImport/test' } ],
789 accesskey => 'enter',
790 not_if => ($self->profile && $self->profile->get('dont_edit_profile')),
794 submit => [ '#form', { action => 'CsvImport/import' } ],
795 disabled => t8('The test import has not been executed yet.'),
796 id => 'action_import',