Revision 9ef11f3c
Von Tamino Steinert vor 11 Monaten hinzugefügt
SL/BackgroundJob/ImportPurchaseInvoiceEmails.pm | ||
---|---|---|
1 |
package SL::BackgroundJob::ImportPurchaseInvoiceEmails; |
|
2 |
|
|
3 |
use strict; |
|
4 |
use warnings; |
|
5 |
|
|
6 |
use parent qw(SL::BackgroundJob::Base); |
|
7 |
|
|
8 |
use SL::IMAPClient; |
|
9 |
use SL::DB::Manager::EmailImport; |
|
10 |
|
|
11 |
sub sync_email_folder { |
|
12 |
my ($self) = @_; |
|
13 |
my $folder = $self->{job_obj}->data_as_hash->{folder}; |
|
14 |
|
|
15 |
my $imap_client = SL::IMAPClient->new(%{$::lx_office_conf{purchase_invoice_emails_imap}}); |
|
16 |
|
|
17 |
my $email_import = $imap_client->update_emails_from_folder( |
|
18 |
$folder, |
|
19 |
{ |
|
20 |
email_journal => { |
|
21 |
extended_status => 'purchase_invoice_import', |
|
22 |
}, |
|
23 |
} |
|
24 |
); |
|
25 |
return unless $email_import; |
|
26 |
|
|
27 |
return "Created email import: " . $email_import->id; |
|
28 |
} |
|
29 |
|
|
30 |
sub delete_email_imports { |
|
31 |
my ($self) = @_; |
|
32 |
my $job_obj = $self->{job_obj}; |
|
33 |
|
|
34 |
my $email_import_ids_to_delete = |
|
35 |
$job_obj->data_as_hash->{email_import_ids_to_delete} || []; |
|
36 |
|
|
37 |
my @deleted_email_imports_ids; |
|
38 |
foreach my $email_import_id (@$email_import_ids_to_delete) { |
|
39 |
my $email_import = SL::DB::Manager::EmailImport->find_by(id => $email_import_id); |
|
40 |
next unless $email_import; |
|
41 |
$email_import->delete(cascade => 1); |
|
42 |
push @deleted_email_imports_ids, $email_import_id; |
|
43 |
} |
|
44 |
return unless @deleted_email_imports_ids; |
|
45 |
|
|
46 |
return "Deleted email import(s): " . join(', ', @deleted_email_imports_ids); |
|
47 |
} |
|
48 |
|
|
49 |
sub clean_up_imported_emails { |
|
50 |
my ($self) = @_; |
|
51 |
my $folder = $self->{job_obj}->data_as_hash->{folder}; |
|
52 |
|
|
53 |
my $imap_client = SL::IMAPClient->new(%{$::lx_office_conf{purchase_invoice_emails_imap}}); |
|
54 |
|
|
55 |
$imap_client->clean_up_imported_emails_from_folder($folder); |
|
56 |
|
|
57 |
return "Cleaned imported emails"; |
|
58 |
} |
|
59 |
|
|
60 |
sub run { |
|
61 |
my ($self, $job_obj) = @_; |
|
62 |
$self->{job_obj} = $job_obj; |
|
63 |
|
|
64 |
my @results; |
|
65 |
push @results, $self->delete_email_imports(); |
|
66 |
push @results, $self->sync_email_folder(); |
|
67 |
if ($self->{job_obj}->data_as_hash->{clean_up_imported_emails}) { |
|
68 |
push @results, $self->clean_up_imported_emails(); |
|
69 |
} |
|
70 |
|
|
71 |
return join(". ", grep { $_ ne ''} @results); |
|
72 |
} |
|
73 |
|
|
74 |
1; |
|
75 |
|
|
76 |
__END__ |
|
77 |
|
|
78 |
=encoding utf8 |
|
79 |
|
|
80 |
=head1 NAME |
|
81 |
|
|
82 |
SL::BackgroundJob::ImportPurchaseInvoiceEmails - Background job for syncing |
|
83 |
emails from a folder for purchase invoices . |
|
84 |
|
|
85 |
=head1 SYNOPSIS |
|
86 |
|
|
87 |
This background job is used to sync emails from a folder with purchase invoices. |
|
88 |
It can be used to sync emails from a folder on a regular basis for multiple |
|
89 |
folders . The folder to sync is specified in the data field 'folder' of the |
|
90 |
background job, by default the folder 'base_folder' from |
|
91 |
[purchase_invoice_emails_imap] in kivitendo.conf is used. Sub folders are |
|
92 |
separated by a forward slash, e.g. 'INBOX/Archive'. Subfolders are not synced. |
|
93 |
It can also remove emails from the folder which have been imported into kivitendo |
|
94 |
by setting the data field 'clean_up_imported_emails' to a true value. |
|
95 |
|
|
96 |
=head1 BUGS |
|
97 |
|
|
98 |
Nothing here yet. |
|
99 |
|
|
100 |
=head1 AUTHOR |
|
101 |
|
|
102 |
Tamino Steinert E<lt>tamino.steinert@tamino.stE<gt> |
|
103 |
|
|
104 |
=cut |
SL/IMAPClient.pm | ||
---|---|---|
65 | 65 |
} |
66 | 66 |
|
67 | 67 |
sub update_emails_from_folder { |
68 |
my ($self, $folder_path) = @_; |
|
68 |
my ($self, $folder_path, $params) = @_;
|
|
69 | 69 |
$folder_path ||= $self->{base_folder}; |
70 | 70 |
|
71 | 71 |
my $folder_string = $self->get_folder_string_from_path($folder_path); |
72 | 72 |
my $email_import = |
73 |
_update_emails_from_folder_strings($self, $folder_path, [$folder_string]); |
|
73 |
_update_emails_from_folder_strings($self, $folder_path, [$folder_string], $params);
|
|
74 | 74 |
|
75 | 75 |
return $email_import; |
76 | 76 |
} |
77 | 77 |
|
78 | 78 |
sub update_emails_from_subfolders { |
79 |
my ($self, $base_folder_path) = @_; |
|
79 |
my ($self, $base_folder_path, $params) = @_;
|
|
80 | 80 |
$base_folder_path ||= $self->{base_folder}; |
81 | 81 |
my $base_folder_string = $self->get_folder_string_from_path($base_folder_path); |
82 | 82 |
|
... | ... | |
85 | 85 |
@subfolder_strings = grep { $_ ne $base_folder_string } @subfolder_strings; |
86 | 86 |
|
87 | 87 |
my $email_import = |
88 |
_update_emails_from_folder_strings($self, $base_folder_path, \@subfolder_strings); |
|
88 |
_update_emails_from_folder_strings($self, $base_folder_path, \@subfolder_strings, $params);
|
|
89 | 89 |
|
90 | 90 |
return $email_import; |
91 | 91 |
} |
92 | 92 |
|
93 | 93 |
sub _update_emails_from_folder_strings { |
94 |
my ($self, $base_folder_path, $folder_strings) = @_; |
|
94 |
my ($self, $base_folder_path, $folder_strings, $params) = @_;
|
|
95 | 95 |
|
96 | 96 |
my $dbh = SL::DB->client->dbh; |
97 | 97 |
|
... | ... | |
131 | 131 |
my $new_email_string = $self->{imap_client}->message_string($new_uid); |
132 | 132 |
my $email = Email::MIME->new($new_email_string); |
133 | 133 |
my $email_journal = $self->_create_email_journal( |
134 |
$email, $email_import, $new_uid, $folder_string, $folder_uidvalidity |
|
134 |
$email, $email_import, $new_uid, $folder_string, $folder_uidvalidity, $params->{email_journal}
|
|
135 | 135 |
); |
136 | 136 |
$email_journal->save(); |
137 | 137 |
} |
... | ... | |
152 | 152 |
} |
153 | 153 |
|
154 | 154 |
sub _create_email_journal { |
155 |
my ($self, $email, $email_import, $uid, $folder_string, $folder_uidvalidity) = @_; |
|
155 |
my ($self, $email, $email_import, $uid, $folder_string, $folder_uidvalidity, $params) = @_;
|
|
156 | 156 |
|
157 | 157 |
my @email_parts = $email->parts; # get parts or self |
158 | 158 |
my $text_part = $email_parts[0]; |
... | ... | |
197 | 197 |
body => $body, |
198 | 198 |
headers => $header_string, |
199 | 199 |
attachments => \@attachments, |
200 |
%$params, |
|
200 | 201 |
); |
201 | 202 |
|
202 | 203 |
return $email_journal; |
config/kivitendo.conf.default | ||
---|---|---|
190 | 190 |
# If SSL is used, default port is 993 |
191 | 191 |
ssl = 1 |
192 | 192 |
|
193 |
# Import emails for purchase invoices |
|
194 |
[purchase_invoice_emails_imap] |
|
195 |
enabled = 0 |
|
196 |
hostname = localhost |
|
197 |
username = |
|
198 |
password = |
|
199 |
# This folder can be managed with kivitendo through the background |
|
200 |
# ImportPurchaseInvoiceEmails. Create no subfolder in the base folder by hand. |
|
201 |
# Use / for subfolders. |
|
202 |
base_folder = INBOX |
|
203 |
# Port only needs to be changed if it is not the default port. |
|
204 |
# port = 993 |
|
205 |
# If SSL is to be used, then set port to 993 or leave empty |
|
206 |
ssl = 1 |
|
207 |
|
|
193 | 208 |
[applications] |
194 | 209 |
# Location of OpenOffice.org/LibreOffice writer |
195 | 210 |
openofficeorg_writer = lowriter |
Auch abrufbar als: Unified diff
BJ: ImportPurchaseInvoiceEmails hinzugefügt