Revision a566082c
Von Tamino Steinert vor mehr als 1 Jahr hinzugefügt
SL/BackgroundJob/ImportPurchaseInvoiceEmails.pm | ||
---|---|---|
package SL::BackgroundJob::ImportPurchaseInvoiceEmails;
|
||
|
||
use strict;
|
||
use warnings;
|
||
|
||
use parent qw(SL::BackgroundJob::Base);
|
||
|
||
use SL::IMAPClient;
|
||
use SL::DB::Manager::EmailImport;
|
||
|
||
sub sync_email_folder {
|
||
my ($self) = @_;
|
||
my $folder = $self->{job_obj}->data_as_hash->{folder};
|
||
|
||
my $imap_client = SL::IMAPClient->new(%{$::lx_office_conf{purchase_invoice_emails_imap}});
|
||
|
||
my $email_import = $imap_client->update_emails_from_folder(
|
||
$folder,
|
||
{
|
||
email_journal => {
|
||
extended_status => 'purchase_invoice_import',
|
||
},
|
||
}
|
||
);
|
||
return unless $email_import;
|
||
|
||
return "Created email import: " . $email_import->id;
|
||
}
|
||
|
||
sub delete_email_imports {
|
||
my ($self) = @_;
|
||
my $job_obj = $self->{job_obj};
|
||
|
||
my $email_import_ids_to_delete =
|
||
$job_obj->data_as_hash->{email_import_ids_to_delete} || [];
|
||
|
||
my @deleted_email_imports_ids;
|
||
foreach my $email_import_id (@$email_import_ids_to_delete) {
|
||
my $email_import = SL::DB::Manager::EmailImport->find_by(id => $email_import_id);
|
||
next unless $email_import;
|
||
$email_import->delete(cascade => 1);
|
||
push @deleted_email_imports_ids, $email_import_id;
|
||
}
|
||
return unless @deleted_email_imports_ids;
|
||
|
||
return "Deleted email import(s): " . join(', ', @deleted_email_imports_ids);
|
||
}
|
||
|
||
sub clean_up_imported_emails {
|
||
my ($self) = @_;
|
||
my $folder = $self->{job_obj}->data_as_hash->{folder};
|
||
|
||
my $imap_client = SL::IMAPClient->new(%{$::lx_office_conf{purchase_invoice_emails_imap}});
|
||
|
||
$imap_client->clean_up_imported_emails_from_folder($folder);
|
||
|
||
return "Cleaned imported emails";
|
||
}
|
||
|
||
sub run {
|
||
my ($self, $job_obj) = @_;
|
||
$self->{job_obj} = $job_obj;
|
||
|
||
my @results;
|
||
push @results, $self->delete_email_imports();
|
||
push @results, $self->sync_email_folder();
|
||
if ($self->{job_obj}->data_as_hash->{clean_up_imported_emails}) {
|
||
push @results, $self->clean_up_imported_emails();
|
||
}
|
||
|
||
return join(". ", grep { $_ ne ''} @results);
|
||
}
|
||
|
||
1;
|
||
|
||
__END__
|
||
|
||
=encoding utf8
|
||
|
||
=head1 NAME
|
||
|
||
SL::BackgroundJob::ImportPurchaseInvoiceEmails - Background job for syncing
|
||
emails from a folder for purchase invoices .
|
||
|
||
=head1 SYNOPSIS
|
||
|
||
This background job is used to sync emails from a folder with purchase invoices.
|
||
It can be used to sync emails from a folder on a regular basis for multiple
|
||
folders . The folder to sync is specified in the data field 'folder' of the
|
||
background job, by default the folder 'base_folder' from
|
||
[purchase_invoice_emails_imap] in kivitendo.conf is used. Sub folders are
|
||
separated by a forward slash, e.g. 'INBOX/Archive'. Subfolders are not synced.
|
||
It can also remove emails from the folder which have been imported into kivitendo
|
||
by setting the data field 'clean_up_imported_emails' to a true value.
|
||
|
||
=head1 BUGS
|
||
|
||
Nothing here yet.
|
||
|
||
=head1 AUTHOR
|
||
|
||
Tamino Steinert E<lt>tamino.steinert@tamino.stE<gt>
|
||
|
||
=cut
|
SL/IMAPClient.pm | ||
---|---|---|
}
|
||
|
||
sub update_emails_from_folder {
|
||
my ($self, $folder_path) = @_;
|
||
my ($self, $folder_path, $params) = @_;
|
||
$folder_path ||= $self->{base_folder};
|
||
|
||
my $folder_string = $self->get_folder_string_from_path($folder_path);
|
||
my $email_import =
|
||
_update_emails_from_folder_strings($self, $folder_path, [$folder_string]);
|
||
_update_emails_from_folder_strings($self, $folder_path, [$folder_string], $params);
|
||
|
||
return $email_import;
|
||
}
|
||
|
||
sub update_emails_from_subfolders {
|
||
my ($self, $base_folder_path) = @_;
|
||
my ($self, $base_folder_path, $params) = @_;
|
||
$base_folder_path ||= $self->{base_folder};
|
||
my $base_folder_string = $self->get_folder_string_from_path($base_folder_path);
|
||
|
||
... | ... | |
@subfolder_strings = grep { $_ ne $base_folder_string } @subfolder_strings;
|
||
|
||
my $email_import =
|
||
_update_emails_from_folder_strings($self, $base_folder_path, \@subfolder_strings);
|
||
_update_emails_from_folder_strings($self, $base_folder_path, \@subfolder_strings, $params);
|
||
|
||
return $email_import;
|
||
}
|
||
|
||
sub _update_emails_from_folder_strings {
|
||
my ($self, $base_folder_path, $folder_strings) = @_;
|
||
my ($self, $base_folder_path, $folder_strings, $params) = @_;
|
||
|
||
my $dbh = SL::DB->client->dbh;
|
||
|
||
... | ... | |
my $new_email_string = $self->{imap_client}->message_string($new_uid);
|
||
my $email = Email::MIME->new($new_email_string);
|
||
my $email_journal = $self->_create_email_journal(
|
||
$email, $email_import, $new_uid, $folder_string, $folder_uidvalidity
|
||
$email, $email_import, $new_uid, $folder_string, $folder_uidvalidity, $params->{email_journal}
|
||
);
|
||
$email_journal->save();
|
||
}
|
||
... | ... | |
}
|
||
|
||
sub _create_email_journal {
|
||
my ($self, $email, $email_import, $uid, $folder_string, $folder_uidvalidity) = @_;
|
||
my ($self, $email, $email_import, $uid, $folder_string, $folder_uidvalidity, $params) = @_;
|
||
|
||
my @email_parts = $email->parts; # get parts or self
|
||
my $text_part = $email_parts[0];
|
||
... | ... | |
body => $body,
|
||
headers => $header_string,
|
||
attachments => \@attachments,
|
||
%$params,
|
||
);
|
||
|
||
return $email_journal;
|
config/kivitendo.conf.default | ||
---|---|---|
# If SSL is used, default port is 993
|
||
ssl = 1
|
||
|
||
# Import emails for purchase invoices
|
||
[purchase_invoice_emails_imap]
|
||
enabled = 0
|
||
hostname = localhost
|
||
username =
|
||
password =
|
||
# This folder can be managed with kivitendo through the background
|
||
# ImportPurchaseInvoiceEmails. Create no subfolder in the base folder by hand.
|
||
# Use / for subfolders.
|
||
base_folder = INBOX
|
||
# Port only needs to be changed if it is not the default port.
|
||
# port = 993
|
||
# If SSL is to be used, then set port to 993 or leave empty
|
||
ssl = 1
|
||
|
||
[applications]
|
||
# Location of OpenOffice.org/LibreOffice writer
|
||
openofficeorg_writer = lowriter
|
Auch abrufbar als: Unified diff
BJ: ImportPurchaseInvoiceEmails hinzugefügt