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::Contact;
17 use SL::Controller::CsvImport::CustomerVendor;
18 use SL::Controller::CsvImport::Part;
19 use SL::Controller::CsvImport::Inventory;
20 use SL::Controller::CsvImport::Shipto;
21 use SL::Controller::CsvImport::Project;
22 use SL::Controller::CsvImport::Order;
23 use SL::Controller::CsvImport::DeliveryOrder;
24 use SL::Controller::CsvImport::ARTransaction;
26 use SL::Controller::CsvImport::BankTransaction;
27 use SL::BackgroundJob::CsvImport;
28 use SL::System::TaskServer;
30 use List::MoreUtils qw(any none);
31 use List::Util qw(min);
33 use parent qw(SL::Controller::Base);
35 use Rose::Object::MakeMethods::Generic
37 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
38 import_status errors headers raw_data_headers info_headers data num_importable displayable_columns file all_taxzones) ],
39 'scalar --get_set_init' => [ qw(worker task_server num_imported mappings) ],
41 progress_tracker => { },
42 add_progress_tracker => { interface => 'add', hash_key => 'progress_tracker' },
46 __PACKAGE__->run_before('check_auth', except => [ qw(report) ]);
47 __PACKAGE__->run_before('ensure_form_structure');
48 __PACKAGE__->run_before('check_type', except => [ qw(report) ]);
49 __PACKAGE__->run_before('load_all_profiles');
50 __PACKAGE__->run_before('check_task_server');
52 __PACKAGE__->run_after('cleanup_reports');
61 $self->load_default_profile unless $self->profile;
67 $self->test_and_import_deferred(test => 1);
72 $self->test_and_import_deferred(test => 0);
78 $self->profile_from_form(SL::DB::Manager::CsvImportProfile->find_by(name => $::form->{profile}->{name}, login => $::myconfig{login}));
81 flash_later('info', $::locale->text("The profile has been saved under the name '#1'.", $self->profile->name));
82 $self->redirect_to(action => 'new', 'profile.type' => $self->type, 'profile.id' => $self->profile->id);
88 my $profile = SL::DB::CsvImportProfile->new(id => $::form->{profile}->{id}, login => $::myconfig{login});
89 $profile->delete(cascade => 1);
91 flash_later('info', $::locale->text('The profile \'#1\' has been deleted.', $profile->name));
92 $self->redirect_to(action => 'new', 'profile.type' => $self->type);
99 $self->{background_job} = SL::DB::Manager::BackgroundJob->find_by(id => $::form->{job});
101 my $data = $self->{background_job}->data_as_hash;
103 my $profile = SL::DB::Manager::CsvImportProfile->find_by(id => $data->{profile_id});
104 $self->profile($profile);
106 if ($data->{errors} and my $first_error = $data->{errors}->[0]) {
107 flash('error', $::locale->text('There was an error parsing the csv file: #1 in line #2: #3', $first_error->[2], $first_error->[0], $first_error->[1]));
110 if ($data->{progress}{finished} || $data->{errors}) {
111 $self->render('csv_import/_deferred_report', { layout => 0 });
113 if (!$self->task_server->is_running) {
114 $self->task_server->start;
115 $self->{status_text} = t8('Task Server is not running, starting it now. If this does not change, please check your task server config');
116 } elsif (my $phase = $data->{progress}{phase}) {
117 $self->{status_text} = "$data->{progress}{plan}{$phase} / $data->{progress}{num_phases} " . t8($phase);
119 $self->{status_text} = t8('Import not started yet, please wait...');
122 $self->render('csv_import/_deferred_results', { layout => 0 });
126 sub action_download_sample {
129 $self->profile_from_form;
132 my $file_name = 'csv_import_sample_' . $self->type . '.csv';
133 my $file = SL::SessionFile->new($file_name, mode => '>', encoding => $self->profile->get('charset'));
134 my $csv = Text::CSV_XS->new({ binary => 1, map { ( $_ => $self->profile->get($_) ) } qw(sep_char escape_char quote_char),});
136 if ($self->worker->is_multiplexed) {
137 foreach my $p (@{ $self->worker->profile }) {
138 $csv->print($file->fh, [ map { $_->{name} } @{ $self->displayable_columns->{$p->{row_ident}} } ]);
139 $file->fh->print("\r\n");
141 foreach my $p (@{ $self->worker->profile }) {
142 $csv->print($file->fh, [ map { $_->{description} } @{ $self->displayable_columns->{$p->{row_ident}} } ]);
143 $file->fh->print("\r\n");
146 $csv->print($file->fh, [ map { $_->{name} } @{ $self->displayable_columns } ]);
147 $file->fh->print("\r\n");
148 $csv->print($file->fh, [ map { $_->{description} } @{ $self->displayable_columns } ]);
149 $file->fh->print("\r\n");
154 $self->send_file($file->file_name, name => $file_name);
158 my ($self, %params) = @_;
160 my $report_id = $params{report_id} || $::form->{id};
161 $self->{report} = SL::DB::Manager::CsvImportReport->find_by(id => $report_id);
163 if (!$self->{report}) {
164 $::form->error(t8('No report with id #1', $report_id));
167 my $show_info_err = ($self->{report}->profile->get('full_preview', 0) == 1);
168 my $show_first_20 = ($self->{report}->profile->get('full_preview', 0) == 2);
171 if ($show_first_20) {
172 $num_rows = min($self->{report}->numrows, 20);
173 } elsif ($show_info_err) {
174 # count each status row only once
175 $num_rows = SL::DB::Manager::CsvImportReportStatus->get_all_count(query => [csv_import_report_id => $report_id],
180 $num_rows = $self->{report}->numrows;
183 # manual paginating, yuck
184 my $page = $::form->{page} || 1;
186 $pages->{per_page} = $::form->{per_page} || 20;
187 $pages->{max} = SL::DB::Helper::Paginated::ceil($num_rows, $pages->{per_page}) || 1;
188 $pages->{page} = $page < 1 ? 1
189 : $page > $pages->{max} ? $pages->{max}
191 $pages->{common} = [ grep { $_->{visible} } @{ SL::DB::Helper::Paginated::make_common_pages($pages->{page}, $pages->{max}) } ];
193 $self->{report_numheaders} = $self->{report}->numheaders;
194 my $first_row_header = 0;
195 my $last_row_header = $self->{report_numheaders} - 1;
196 my $first_row_data = $pages->{per_page} * ($pages->{page}-1) + $self->{report_numheaders};
197 my $last_row_data = min($pages->{per_page} * $pages->{page}, $num_rows) + $self->{report_numheaders} - 1;
200 $self->{display_rows} = [];
201 if ($show_info_err) {
202 my $limit = $last_row_data - $first_row_data + 1;
203 my $offset = $first_row_data - $self->{report_numheaders};
204 my @err_rows = map { $_->row } @{SL::DB::Manager::CsvImportReportStatus->get_all(query => [csv_import_report_id => $report_id],
210 $self->{display_rows} = [ $first_row_header .. $last_row_header,
215 $self->{display_rows} = [ $first_row_header .. $last_row_header,
216 $first_row_data .. $last_row_data ];
220 row => $self->{display_rows},
221 csv_import_report_id => $report_id,
224 my $rows = SL::DB::Manager::CsvImportReportRow ->get_all(query => \@query, sort_by => 'row');
225 my $status = SL::DB::Manager::CsvImportReportStatus->get_all(query => \@query, sort_by => 'row');
226 $self->{num_errors} = SL::DB::Manager::CsvImportReportStatus->get_all_count(query => [csv_import_report_id => $report_id, type => 'errors']);
228 $self->{report_rows} = $self->{report}->folded_rows(rows => $rows);
229 $self->{report_status} = $self->{report}->folded_status(status => $status);
230 $self->{pages} = $pages;
231 $self->{base_url} = $self->url_for(action => 'report', id => $report_id, no_layout => $params{no_layout} || $::form->{no_layout} );
233 $self->render('csv_import/report', { layout => !($params{no_layout} || $::form->{no_layout}) });
236 sub action_add_empty_mapping_line {
239 $self->profile_from_form;
243 ->append('#csv_import_mappings', $self->render('csv_import/_mapping_item', { layout => 0, output => 0 }))
244 ->hide('#mapping_empty')
248 sub action_add_mapping_from_upload {
251 if ($::form->{tmp_profile_id}) {
252 $self->profile_from_form(SL::DB::CsvImportProfile->new(id => $::form->{tmp_profile_id})->load);
254 $self->profile_from_form;
259 if ($self->profile->get('file_name')) {
260 $file_name = $self->profile->get('file_name');
263 ->flash('error', t8('No file has been uploaded yet.'))
268 my $file = SL::SessionFile->new($file_name, mode => '<', encoding => $self->profile->get('charset'));
271 ->flash('error', t8('No file has been uploaded yet.'))
276 my $csv = SL::Helper::Csv->new(
277 file => $file->file_name,
278 map { $_ => $self->profile->get($_) } qw(sep_char escape_char quote_char),
282 my $header = $csv->check_header;
284 for my $field (@$header) {
285 next if $self->mappings_for_profile->{$field};
287 '#csv_import_mappings',
288 $self->render('csv_import/_mapping_item', { layout => 0, output => 0 }, item => { from => $field }),
293 ->hide('#mapping_empty')
304 $_[0]->worker->check_auth;
310 die "Invalid CSV import type" if none { $_ eq $::form->{profile}->{type} } qw(parts inventories customers_vendors addresses contacts projects orders delivery_orders bank_transactions ar_transactions);
311 $self->type($::form->{profile}->{type});
314 sub ensure_form_structure {
315 my ($self, %params) = @_;
317 $::form->{profile} = {} unless ref $::form->{profile} eq 'HASH';
318 $::form->{settings} = {} unless ref $::form->{settings} eq 'HASH';
326 my ($self, %params) = @_;
328 $self->all_charsets([ [ 'UTF-8', 'UTF-8' ],
329 [ 'ISO-8859-1', 'ISO-8859-1 (Latin 1)' ],
330 [ 'ISO-8859-15', 'ISO-8859-15 (Latin 9)' ],
331 [ 'CP850', 'CP850 (DOS/ANSI)' ],
332 [ 'CP1252', 'CP1252 (Windows)' ],
335 my %char_map = $self->char_map;
337 foreach my $type (qw(sep quote escape)) {
338 my $sub = "all_${type}_chars";
339 $self->$sub([ sort { $a->[0] cmp $b->[0] } values %{ $char_map{$type} } ]);
341 my $char = $self->profile->get($type . '_char');
342 $sub = "${type}_char";
343 $self->$sub(($char_map{$type}->{$char} || [])->[0] || $char);
346 if ($self->profile->get('file_name')) {
347 $self->file(SL::SessionFile->new($self->profile->get('file_name')));
350 my $title = $self->type eq 'customers_vendors' ? $::locale->text('CSV import: customers and vendors')
351 : $self->type eq 'addresses' ? $::locale->text('CSV import: shipping addresses')
352 : $self->type eq 'contacts' ? $::locale->text('CSV import: contacts')
353 : $self->type eq 'parts' ? $::locale->text('CSV import: parts and services')
354 : $self->type eq 'inventories' ? $::locale->text('CSV import: inventories')
355 : $self->type eq 'projects' ? $::locale->text('CSV import: projects')
356 : $self->type eq 'orders' ? $::locale->text('CSV import: orders')
357 : $self->type eq 'delivery_orders' ? $::locale->text('CSV import: delivery orders')
358 : $self->type eq 'bank_transactions' ? $::locale->text('CSV import: bank transactions')
359 : $self->type eq 'ar_transactions' ? $::locale->text('CSV import: ar transactions')
362 if ( any { $_ eq $self->{type} } qw(customers_vendors orders delivery_orders ar_transactions) ) {
363 $self->all_taxzones(SL::DB::Manager::TaxZone->get_all_sorted(query => [ obsolete => 0 ]));
366 if ($self->{type} eq 'parts') {
367 $self->all_buchungsgruppen(SL::DB::Manager::Buchungsgruppe->get_all_sorted);
368 $self->all_units(SL::DB::Manager::Unit->get_all_sorted);
373 $self->setup_render_inputs_action_bar;
375 $self->render('csv_import/form', title => $title);
378 sub test_and_import_deferred {
379 my ($self, %params) = @_;
381 if ( $::form->{force_profile} && ($::form->{tmp_profile_id} || $::form->{profile}->{id}) ) {
382 $::form->{profile}->{id} = $::form->{tmp_profile_id} if $::form->{tmp_profile_id};
383 $self->load_default_profile;
384 } elsif ($::form->{tmp_profile_id}) {
385 $self->profile_from_form(SL::DB::CsvImportProfile->new(id => $::form->{tmp_profile_id})->load);
387 $self->profile_from_form;
391 if ($::form->{file}) {
392 my $file = SL::SessionFile::Random->new(mode => '>');
393 $file->fh->print($::form->{file});
395 $file_name = $file->file_name;
396 $self->profile->set('file_name', $file_name);
397 } elsif ($self->profile->get('file_name')) {
398 $file_name = $self->profile->get('file_name');
400 flash('error', $::locale->text('No file has been uploaded yet.'));
401 return $self->action_new;
404 my $file = SL::SessionFile->new($file_name, mode => '<', encoding => $self->profile->get('charset'));
406 flash('error', $::locale->text('No file has been uploaded yet.'));
407 return $self->action_new;
410 # save tempory profile
411 $self->profile($self->profile->clone_and_reset_deep)->save;
413 $self->{background_job} = SL::BackgroundJob::CsvImport->create_job(
414 profile_id => $self->profile->id,
415 type => $self->profile->type,
416 test => $params{test},
417 employee_id => SL::DB::Manager::Employee->current->id,
420 if ($self->task_server->is_running) {
421 $self->task_server->wake_up;
423 $self->task_server->start;
426 flash('info', $::locale->text('Your import is being processed.'));
428 $self->{deferred} = 1;
430 if ($::request->type eq 'json') {
431 $self->render(\ SL::JSON::to_json($self->{background_job}->as_tree), { type => 'json' })
433 $self->render_inputs;
437 sub test_and_import {
438 my ($self, %params) = @_;
440 my $file = SL::SessionFile->new(
441 $self->profile->get('file_name'),
443 encoding => $self->profile->get('charset'),
444 session_id => $params{session_id}
449 my $worker = $self->worker();
451 $worker->run(%params);
453 return if $self->errors;
455 $self->num_imported(0);
456 $worker->save_objects if !$params{test};
458 $self->num_importable(scalar grep { !$_ } map { scalar @{ $_->{errors} } } @{ $self->data || [] });
459 $self->import_status($params{test} ? 'tested' : 'imported');
461 # flash('info', $::locale->text('Objects have been imported.')) if !$params{test};
464 sub load_default_profile {
468 if ($::form->{profile}->{id}) {
469 $profile = SL::DB::Manager::CsvImportProfile->find_by(id => $::form->{profile}->{id});
471 $profile ||= SL::DB::Manager::CsvImportProfile->find_by(type => $self->{type}, is_default => 1, login => $::myconfig{login});
472 $profile ||= SL::DB::CsvImportProfile->new(type => $self->{type}, login => $::myconfig{login});
474 $self->profile($profile);
475 $self->mappings(SL::JSON::from_json($self->profile->get('json_mappings'))) if $self->profile->get('json_mappings');
476 $self->worker->set_profile_defaults;
477 $self->profile->set_defaults;
480 sub load_all_profiles {
481 my ($self, %params) = @_;
483 $self->all_profiles(SL::DB::Manager::CsvImportProfile->get_all(
486 login => $::myconfig{login},
492 sub profile_from_form {
493 my ($self, $existing_profile) = @_;
495 delete $::form->{profile}->{id};
497 my %char_map = $self->char_map;
500 foreach my $type (qw(sep quote escape)) {
501 my %rev_chars = map { $char_map{$type}->{$_}->[0] => $_ } keys %{ $char_map{$type} };
502 my $char = $::form->{"${type}_char"} eq 'custom' ? $::form->{"custom_${type}_char"} : $rev_chars{ $::form->{"${type}_char"} };
504 push @settings, { key => "${type}_char", value => $char };
507 if ($self->type eq 'parts') {
508 $::form->{settings}->{sellprice_adjustment} = $::form->parse_amount(\%::myconfig, $::form->{settings}->{sellprice_adjustment});
511 if ($self->type eq 'orders' or $self->{type} eq 'ar_transactions') {
512 $::form->{settings}->{max_amount_diff} = $::form->parse_amount(\%::myconfig, $::form->{settings}->{max_amount_diff});
515 $self->profile($existing_profile || SL::DB::CsvImportProfile->new(login => $::myconfig{login}));
516 $self->profile->assign_attributes(%{ $::form->{profile} });
518 # save settings for file_name, as this is not in form, but maybe in existing_profile
519 push @settings, { key => 'file_name', value => $self->profile->get('file_name') } if $self->profile->get('file_name');
521 $self->profile->settings(map({ { key => $_, value => $::form->{settings}->{$_} } } keys %{ $::form->{settings} }),
523 $self->profile->set('json_mappings', JSON::to_json($self->mappings));
524 $self->profile->set_defaults;
528 return ( sep => { ',' => [ 'comma', $::locale->text('Comma') ],
529 ';' => [ 'semicolon', $::locale->text('Semicolon') ],
530 "\t" => [ 'tab', $::locale->text('Tab') ],
531 ' ' => [ 'space', $::locale->text('Space') ],
533 quote => { '"' => [ 'quote', $::locale->text('Quotes') ],
534 "'" => [ 'singlequote', $::locale->text('Single quotes') ],
536 escape => { '"' => [ 'quote', $::locale->text('Quotes') ],
537 "'" => [ 'singlequote', $::locale->text('Single quotes') ],
543 my ($self, %params) = @_;
545 if ($self->worker->is_multiplexed) {
546 return $self->save_report_multi(%params);
548 return $self->save_report_single(%params);
552 sub save_report_single {
553 my ($self, %params) = @_;
555 $self->track_progress(phase => 'building report', progress => 0);
557 my $report = SL::DB::CsvImportReport->new(
558 session_id => $params{session_id},
559 profile_id => $self->profile->id,
562 numrows => scalar @{ $self->data },
564 test_mode => $params{test} ? 1 : 0,
567 $report->save(cascade => 1) or die $report->db->error;
569 SL::DB->client->with_transaction(sub {
570 my $dbh = SL::DB->client->dbh;
572 my $query = 'INSERT INTO csv_import_report_rows (csv_import_report_id, col, row, value) VALUES (?, ?, ?, ?)';
573 my $query2 = 'INSERT INTO csv_import_report_status (csv_import_report_id, row, type, value) VALUES (?, ?, ?, ?)';
575 my $sth = $dbh->prepare($query);
576 my $sth2 = $dbh->prepare($query2);
579 my (@headers, @info_methods, @raw_methods, @methods);
581 for my $i (0 .. $#{ $self->info_headers->{headers} }) {
582 next unless $self->info_headers->{used}->{ $self->info_headers->{methods}->[$i] };
583 push @headers, $self->info_headers->{headers}->[$i];
584 push @info_methods, $self->info_headers->{methods}->[$i];
586 for my $i (0 .. $#{ $self->headers->{headers} }) {
587 next unless $self->headers->{used}->{ $self->headers->{headers}->[$i] };
588 push @headers, $self->headers->{headers}->[$i];
589 push @methods, $self->headers->{methods}->[$i];
591 for my $i (0 .. $#{ $self->raw_data_headers->{headers} }) {
592 next unless $self->raw_data_headers->{used}->{ $self->raw_data_headers->{headers}->[$i] };
593 push @headers, $self->raw_data_headers->{headers}->[$i];
594 push @raw_methods, $self->raw_data_headers->{headers}->[$i];
597 do_statement($::form, $sth, $query, $report->id, $_, 0, $headers[$_]) for 0 .. $#headers;
600 my $o1 = @info_methods;
601 my $o2 = $o1 + @methods;
603 for my $row (0 .. $#{ $self->data }) {
604 $self->track_progress(progress => $row / @{ $self->data } * 100) if $row % 1000 == 0;
605 my $data_row = $self->{data}[$row];
607 do_statement($::form, $sth, $query, $report->id, $_, $row + 1, $data_row->{info_data}{ $info_methods[$_] }) for 0 .. $#info_methods;
608 do_statement($::form, $sth, $query, $report->id, $o1 + $_, $row + 1, $data_row->{object}->${ \ $methods[$_] }) for 0 .. $#methods;
609 do_statement($::form, $sth, $query, $report->id, $o2 + $_, $row + 1, $data_row->{raw_data}{ $raw_methods[$_] }) for 0 .. $#raw_methods;
611 do_statement($::form, $sth2, $query2, $report->id, $row + 1, 'information', $_) for @{ $data_row->{information} || [] };
612 do_statement($::form, $sth2, $query2, $report->id, $row + 1, 'errors', $_) for @{ $data_row->{errors} || [] };
615 }) or do { die SL::DB->client->error };
620 sub save_report_multi {
621 my ($self, %params) = @_;
623 $self->track_progress(phase => 'building report', progress => 0);
625 my $report = SL::DB::CsvImportReport->new(
626 session_id => $params{session_id},
627 profile_id => $self->profile->id,
630 numrows => scalar @{ $self->data },
631 numheaders => scalar @{ $self->worker->profile },
632 test_mode => $params{test} ? 1 : 0,
635 $report->save(cascade => 1) or die $report->db->error;
637 SL::DB->client->with_transaction(sub {
638 my $dbh = SL::DB->client->dbh;
640 my $query = 'INSERT INTO csv_import_report_rows (csv_import_report_id, col, row, value) VALUES (?, ?, ?, ?)';
641 my $query2 = 'INSERT INTO csv_import_report_status (csv_import_report_id, row, type, value) VALUES (?, ?, ?, ?)';
643 my $sth = $dbh->prepare($query);
644 my $sth2 = $dbh->prepare($query2);
647 my ($headers, $info_methods, $raw_methods, $methods);
649 for my $i (0 .. $#{ $self->worker->profile }) {
650 my $row_ident = $self->worker->profile->[$i]->{row_ident};
652 for my $i (0 .. $#{ $self->info_headers->{$row_ident}->{headers} }) {
653 next unless $self->info_headers->{$row_ident}->{used}->{ $self->info_headers->{$row_ident}->{methods}->[$i] };
654 push @{ $headers->{$row_ident} }, $self->info_headers->{$row_ident}->{headers}->[$i];
655 push @{ $info_methods->{$row_ident} }, $self->info_headers->{$row_ident}->{methods}->[$i];
657 for my $i (0 .. $#{ $self->headers->{$row_ident}->{headers} }) {
658 next unless $self->headers->{$row_ident}->{used}->{ $self->headers->{$row_ident}->{headers}->[$i] };
659 push @{ $headers->{$row_ident} }, $self->headers->{$row_ident}->{headers}->[$i];
660 push @{ $methods->{$row_ident} }, $self->headers->{$row_ident}->{methods}->[$i];
663 for my $i (0 .. $#{ $self->raw_data_headers->{$row_ident}->{headers} }) {
664 next unless $self->raw_data_headers->{$row_ident}->{used}->{ $self->raw_data_headers->{$row_ident}->{headers}->[$i] };
665 push @{ $headers->{$row_ident} }, $self->raw_data_headers->{$row_ident}->{headers}->[$i];
666 push @{ $raw_methods->{$row_ident} }, $self->raw_data_headers->{$row_ident}->{headers}->[$i];
671 for my $i (0 .. $#{ $self->worker->profile }) {
672 my $row_ident = $self->worker->profile->[$i]->{row_ident};
673 do_statement($::form, $sth, $query, $report->id, $_, $i, $headers->{$row_ident}->[$_]) for 0 .. $#{ $headers->{$row_ident} };
678 for my $i (0 .. $#{ $self->worker->profile }) {
679 my $row_ident = $self->worker->profile->[$i]->{row_ident};
680 my $n_info_methods = $info_methods->{$row_ident} ? scalar @{ $info_methods->{$row_ident} } : 0;
681 my $n_methods = $methods->{$row_ident} ? scalar @{ $methods->{$row_ident} } : 0;
683 $off1->{$row_ident} = $n_info_methods;
684 $off2->{$row_ident} = $off1->{$row_ident} + $n_methods;
687 my $n_header_rows = scalar @{ $self->worker->profile };
689 for my $row (0 .. $#{ $self->data }) {
690 $self->track_progress(progress => $row / @{ $self->data } * 100) if $row % 1000 == 0;
691 my $data_row = $self->{data}[$row];
692 my $row_ident = $data_row->{raw_data}{datatype};
694 my $o1 = $off1->{$row_ident};
695 my $o2 = $off2->{$row_ident};
697 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} };
698 do_statement($::form, $sth, $query, $report->id, $o1 + $_, $row + $n_header_rows, $data_row->{object}->${ \ $methods->{$row_ident}->[$_] }) for 0 .. $#{ $methods->{$row_ident} };
699 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} };
701 do_statement($::form, $sth2, $query2, $report->id, $row + $n_header_rows, 'information', $_) for @{ $data_row->{information} || [] };
702 do_statement($::form, $sth2, $query2, $report->id, $row + $n_header_rows, 'errors', $_) for @{ $data_row->{errors} || [] };
705 }) or do { die SL::DB->client->error };
713 my @args = (controller => $self);
715 if ( $self->file() ) {
716 push(@args, file => $self->file());
719 return $self->{type} eq 'customers_vendors' ? SL::Controller::CsvImport::CustomerVendor->new(@args)
720 : $self->{type} eq 'contacts' ? SL::Controller::CsvImport::Contact->new(@args)
721 : $self->{type} eq 'addresses' ? SL::Controller::CsvImport::Shipto->new(@args)
722 : $self->{type} eq 'parts' ? SL::Controller::CsvImport::Part->new(@args)
723 : $self->{type} eq 'inventories' ? SL::Controller::CsvImport::Inventory->new(@args)
724 : $self->{type} eq 'projects' ? SL::Controller::CsvImport::Project->new(@args)
725 : $self->{type} eq 'orders' ? SL::Controller::CsvImport::Order->new(@args)
726 : $self->{type} eq 'delivery_orders' ? SL::Controller::CsvImport::DeliveryOrder->new(@args)
727 : $self->{type} eq 'bank_transactions' ? SL::Controller::CsvImport::BankTransaction->new(@args)
728 : $self->{type} eq 'ar_transactions' ? SL::Controller::CsvImport::ARTransaction->new(@args)
729 : die "Program logic error";
732 sub init_num_imported { 0 }
737 $self->worker->setup_displayable_columns;
741 my ($self, %params) = @_;
743 for my $tracker ($self->progress_tracker) {
744 $tracker->track_progress(%params);
748 sub init_task_server {
749 SL::System::TaskServer->new;
752 sub cleanup_reports {
753 SL::DB::Manager::CsvImportReport->cleanup;
756 sub check_task_server {
757 if (!$::auth->client->{task_server_user_id}) {
758 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}));
761 return 1 if $_[0]->task_server->is_running;
763 flash('warning', t8('The task server is not running at the moment but needed for this module'));
768 sub mappings_for_profile {
769 +{ map { $_->{from} => $_->{to} } @{ $_[0]->mappings } }
773 [ grep { $_->{from} } @{ $::form->{mappings} || [] } ]
776 sub setup_render_inputs_action_bar {
777 my ($self, %params) = @_;
779 for my $bar ($::request->layout->get('actionbar')) {
783 submit => [ '#form', { action => 'CsvImport/test' } ],
784 accesskey => 'enter',
785 not_if => ($self->profile && $self->profile->get('dont_edit_profile')),
789 submit => [ '#form', { action => 'CsvImport/import' } ],
790 disabled => t8('The test import has not been executed yet.'),
791 id => 'action_import',