Fehler korrekt weiterleiten
[kivitendo-erp.git] / SL / BackgroundJob / CsvImport.pm
1 package SL::BackgroundJob::CsvImport;
2
3 use strict;
4
5 use parent qw(SL::BackgroundJob::Base);
6
7 use YAML ();
8 use SL::Controller::CsvImport;
9 use SL::DB::CsvImportProfile;
10 use SL::SessionFile::Random;
11
12 sub create_job {
13   my ($self_or_class, %params) = @_;
14
15   my $package       = ref($self_or_class) || $self_or_class;
16   $package          =~ s/SL::BackgroundJob:://;
17
18   my $profile = delete $params{profile} || SL::DB::CsvImportProfile->new;
19   my $new_profile = $profile->clone_and_reset_deep;
20   $new_profile->save;
21
22   my %data = (
23     %params,
24     profile_id => $new_profile->id,
25     session_id => $::auth->get_session_id,
26   );
27
28   my $job = SL::DB::BackgroundJob->new(
29     type         => 'once',
30     active       => 1,
31     package_name => $package,
32     data         => YAML::Dump(\%data),
33   );
34
35   return $job;
36 }
37
38 sub profile {
39   my ($self) = @_;
40
41   if (!$self->{profile}) {
42     my $data = YAML::Load($self->{db_obj}->data);
43     $self->{profile} = SL::DB::Manager::CsvImportProfile->find_by(id => $data->{profile_id});
44   }
45
46   return $self->{profile};
47 }
48
49 sub run {
50   my $self        = shift;
51   $self->{db_obj} = shift;
52
53   $self->do_import;
54
55   $self->cleanup;
56 }
57
58 sub do_import {
59   my ($self) = @_;
60
61   my $c = SL::Controller::CsvImport->new;
62   my $job = $self->{db_obj};
63
64   $c->profile($self->profile);
65   $c->type($job->data_as_hash->{type});
66
67   my $test = $job->data_as_hash->{test};
68
69
70   # $::locale->text('parsing csv')
71   # $::locale->text('building data')
72   # $::locale->text('saving data')
73   # $::locale->text('building report')
74   $self->track_progress(
75     progress => 0,
76     plan => {
77       'parsing csv'     => 1,
78       'building data'   => 2,
79     ( 'saving data'     => 3, )x!!$test,
80       'building report' => ($test ? 3 : 4),
81     },
82     num_phases => ($test ? 3 : 4),
83   );
84   $c->add_progress_tracker($self);
85
86
87   $c->test_and_import(test => 1, session_id => $job->data_as_hash->{session_id});
88
89   if ($c->errors) {
90     $job->set_data(
91       errors   => $c->errors,
92     )->save;
93   } else {
94
95     my $report_id = $c->save_report;
96     $job->set_data(report_id => $report_id)->save;
97
98     $c->track_progress(finished => 1);
99   }
100 }
101
102 sub track_progress {
103   my ($self, %params) = @_;
104
105   my $data = $self->{db_obj}->data_as_hash;
106   my $progress = $data->{progress} || {};
107
108   $progress->{$_} = $params{$_} for keys %params;
109   $self->{db_obj}->set_data(progress => $progress);
110   $self->{db_obj}->save;
111 }
112
113 sub cleanup {
114
115 }
116
117 1;
118
119 __END__
120
121 =encoding utf-8
122
123 =head1 NAME
124
125 SL::Background::CsvImport - backend for automatic imports of csv data
126
127 =head1 SYNOPSIS
128
129
130 use SL::BackgroundJob::CsvImport;
131
132
133 From a controller or external source:
134
135   my $job = SL::BackgroundJob::CsvImport->create_job(
136     file => $file,
137     %import_options
138   );
139
140 =head1 DESCRIPTION
141
142 =head1 FUNCTIONS
143
144 =head1 BUGS
145
146 =head1 AUTHOR
147
148 Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>
149
150 =cut