1 package SL::Helper::EmailProcessing;
11 use SL::Locale::String qw(t8);
13 use SL::DB::PurchaseInvoice;
15 sub process_attachments {
16 my ($self, $function_name, $email_journal, %params) = @_;
18 my $full_function_name = "process_attachments_$function_name";
19 unless ($self->can($full_function_name)) {
20 croak "Function not implemented for: $function_name";
25 foreach my $attachment (@{$email_journal->attachments_sorted}) {
26 my $attachment_name = $attachment->name;
27 my $error = $self->$full_function_name($email_journal, $attachment, %params);
29 push @errors, "$attachment_name: $error.";
31 push @processed_files, $attachment_name;
34 my $extended_status = t8("Processed attachments with function '#1':", $function_name);
35 if (scalar @processed_files) {
36 $extended_status .= "\n" . t8("Processed successfully: ")
37 . join(', ', @processed_files);
40 $extended_status .= "\n" . t8("Errors while processing: ")
41 . "\n" . join("\n", @errors);
43 unless (scalar @processed_files || scalar @errors) {
44 $extended_status .= "\n" . t8("No attachments.");
46 $email_journal->extended_status(
47 join "\n", $email_journal->extended_status, $extended_status
50 return scalar @processed_files;
54 my ($self, $function_name) = @_;
55 $self->can("process_attachments_$function_name")
58 sub process_attachments_zugferd {
59 my ($self, $email_journal, $attachment, %params) = @_;
61 my $content = $attachment->content; # scalar ref
63 return t8("Not a PDF or XML file") unless $content =~ m/^%PDF|<\?xml/;
66 if ( $content =~ m/^%PDF/ ) {
67 %res = %{SL::ZUGFeRD->extract_from_pdf($content)};
69 %res = %{SL::ZUGFeRD->extract_from_xml($content)};
72 unless ($res{'result'} == SL::ZUGFeRD::RES_OK()) {
73 # my $error = $res{'message'}; # technical error
74 my $error = t8('No vaild Factur-X/ZUGFeRD file');
80 $purchase_invoice = SL::DB::PurchaseInvoice->create_from_zugferd_data($res{invoice_xml})->save();
87 $self->_add_attachment_to_record($email_journal, $attachment, $purchase_invoice);
92 sub _add_attachment_to_record {
93 my ($self, $email_journal, $attachment, $record) = @_;
95 $attachment->add_file_to_record($record);
97 $email_journal->link_to_record($record);
107 SL::Helper::EmailProcessing - Helper functions for processing email attachments
111 This module provides helper functions for processing email attachments.
115 =head2 process_attachments($function_name, $email_journal, %params)
117 Processes the attachments of an email journal. The function to be used for processing is determined by the first argument.
119 =head2 process_attachments_zugferd($function_name, $email_journal, %params)
121 Processes the attachments of an email journal. If it is a ZUGFeRD Invoiue it creates the PurchaseInvoice and links it to the email_journal.
125 Tamino Steinert E<lt>tamino.steinert@tamino.stE<gt>