Projekt

Allgemein

Profil

Herunterladen (2,53 KB) Statistiken
| Zweig: | Markierung: | Revision:
7e62d6a2 Tamino Steinert
package SL::Helper::EmailProcessing;

use strict;
use warnings;

use Carp;

use XML::LibXML;

use SL::ZUGFeRD;
b8dfb10a Tamino Steinert
use SL::Locale::String qw(t8);
7e62d6a2 Tamino Steinert
use SL::DB::PurchaseInvoice;

sub process_attachments {
my ($self, $function_name, $email_journal, %params) = @_;

b8dfb10a Tamino Steinert
my $full_function_name = "process_attachments_$function_name";
unless ($self->can($full_function_name)) {
7e62d6a2 Tamino Steinert
croak "Function not implemented for: $function_name";
}

b8dfb10a Tamino Steinert
my @processed_files;
my @errors;
7e62d6a2 Tamino Steinert
foreach my $attachment (@{$email_journal->attachments_sorted}) {
b8dfb10a Tamino Steinert
my $attachment_name = $attachment->name;
my $error = $self->$full_function_name($email_journal, $attachment, %params);
if ($error) {
push @errors, "$attachment_name: $error.";
} else {
push @processed_files, $attachment_name;
}
7e62d6a2 Tamino Steinert
}
b8dfb10a Tamino Steinert
my $extended_status = t8("Processed attachments with function '#1':", $function_name);
if (scalar @processed_files) {
$extended_status .= "\n" . t8("Processed successfully: ")
. join(', ', @processed_files);
}
if (scalar @errors) {
$extended_status .= "\n" . t8("Errors while processing: ")
. "\n" . join("\n", @errors);
}
unless (scalar @processed_files || scalar @errors) {
$extended_status .= "\n" . t8("No attachments.");
}
$email_journal->extended_status(
join "\n", $email_journal->extended_status, $extended_status
);
$email_journal->save;
return scalar @processed_files;
7e62d6a2 Tamino Steinert
}

9ea083c2 Tamino Steinert
sub can_function {
my ($self, $function_name) = @_;
$self->can("process_attachments_$function_name")
}

7e62d6a2 Tamino Steinert
sub process_attachments_zugferd {
my ($self, $email_journal, $attachment, %params) = @_;

my $content = $attachment->content; # scalar ref

b8dfb10a Tamino Steinert
return t8("Not a PDF or XML file") unless $content =~ m/^%PDF|<\?xml/;
7e62d6a2 Tamino Steinert
b0d95d74 Tamino Steinert
my %res;
if ( $content =~ m/^%PDF/ ) {
%res = %{SL::ZUGFeRD->extract_from_pdf($content)};
} else {
%res = %{SL::ZUGFeRD->extract_from_xml($content)};
}
7e62d6a2 Tamino Steinert
b0d95d74 Tamino Steinert
unless ($res{'result'} == SL::ZUGFeRD::RES_OK()) {
b8dfb10a Tamino Steinert
# my $error = $res{'message'}; # technical error
my $error = t8('No vaild Factur-X/ZUGFeRD file');
return $error;
b0d95d74 Tamino Steinert
}
7e62d6a2 Tamino Steinert
b0d95d74 Tamino Steinert
my $purchase_invoice;
eval {
ba8675f5 Tamino Steinert
$purchase_invoice = SL::DB::PurchaseInvoice->create_from_zugferd_data($res{invoice_xml})->save();
b0d95d74 Tamino Steinert
1;
} or do {
my $error = $@;
b8dfb10a Tamino Steinert
return $error;
b0d95d74 Tamino Steinert
};
7e62d6a2 Tamino Steinert
$self->_add_attachment_to_record($email_journal, $attachment, $purchase_invoice);

b8dfb10a Tamino Steinert
return 0;
7e62d6a2 Tamino Steinert
}

sub _add_attachment_to_record {
my ($self, $email_journal, $attachment, $record) = @_;

f642a664 Tamino Steinert
$attachment->add_file_to_record($record);
7e62d6a2 Tamino Steinert
f642a664 Tamino Steinert
$email_journal->link_to_record($record);
7e62d6a2 Tamino Steinert
}

1;