]> wagnertech.de Git - mfinanz.git/blob - SL/Helper/EmailProcessing.pm
Merge branch 'master' of http://wagnertech.de/git/mfinanz
[mfinanz.git] / SL / Helper / EmailProcessing.pm
1 package SL::Helper::EmailProcessing;
2
3 use strict;
4 use warnings;
5
6 use Carp;
7
8 use XML::LibXML;
9
10 use SL::ZUGFeRD;
11 use SL::Locale::String qw(t8);
12
13 use SL::DB::PurchaseInvoice;
14
15 sub process_attachments {
16   my ($self, $function_name, $email_journal, %params) = @_;
17
18   my $full_function_name = "process_attachments_$function_name";
19   unless ($self->can($full_function_name)) {
20     croak "Function not implemented for: $function_name";
21   }
22
23   my @processed_files;
24   my @errors;
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);
28     if ($error) {
29       push @errors, "$attachment_name: $error.";
30     } else {
31       push @processed_files, $attachment_name;
32     }
33   }
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);
38   }
39   if (scalar @errors) {
40     $extended_status .= "\n" . t8("Errors while processing: ")
41       . "\n" . join("\n", @errors);
42   }
43   unless (scalar @processed_files || scalar @errors) {
44     $extended_status .= "\n" . t8("No attachments.");
45   }
46   $email_journal->extended_status(
47     join "\n", $email_journal->extended_status, $extended_status
48   );
49   $email_journal->save;
50   return scalar @processed_files;
51 }
52
53 sub can_function {
54   my ($self, $function_name) = @_;
55   $self->can("process_attachments_$function_name")
56 }
57
58 sub process_attachments_zugferd {
59   my ($self, $email_journal, $attachment, %params) = @_;
60
61   my $content = $attachment->content; # scalar ref
62
63   return t8("Not a PDF or XML file") unless $content =~ m/^%PDF|<\?xml/;
64
65   my %res;
66   if ( $content =~ m/^%PDF/ ) {
67     %res = %{SL::ZUGFeRD->extract_from_pdf($content)};
68   } else {
69     %res = %{SL::ZUGFeRD->extract_from_xml($content)};
70   }
71
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');
75     return $error;
76   }
77
78   my $purchase_invoice;
79   eval {
80     $purchase_invoice = SL::DB::PurchaseInvoice->create_from_zugferd_data($res{invoice_xml})->save();
81     1;
82   } or do {
83     my $error = $@;
84     return $error;
85   };
86
87   $self->_add_attachment_to_record($email_journal, $attachment, $purchase_invoice);
88
89   return 0;
90 }
91
92 sub _add_attachment_to_record {
93   my ($self, $email_journal, $attachment, $record) = @_;
94
95   $attachment->add_file_to_record($record);
96
97   $email_journal->link_to_record($record);
98 }
99
100 1;
101
102
103 =encoding utf8
104
105 =head1 NAME
106
107 SL::Helper::EmailProcessing - Helper functions for processing email attachments
108
109 =head1 SYNOPSIS
110
111 This module provides helper functions for processing email attachments.
112
113 =head1 METHODS
114
115 =head2 process_attachments($function_name, $email_journal, %params)
116
117 Processes the attachments of an email journal. The function to be used for processing is determined by the first argument.
118
119 =head2 process_attachments_zugferd($function_name, $email_journal, %params)
120
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.
122
123 =head1 AUTHOR
124
125 Tamino Steinert E<lt>tamino.steinert@tamino.stE<gt>
126
127 =cut
128