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