Revision 7d9823cd
Von Tamino Steinert vor 10 Monaten hinzugefügt
SL/Helper/EmailProcessing.pm | ||
---|---|---|
8 | 8 |
use XML::LibXML; |
9 | 9 |
|
10 | 10 |
use SL::ZUGFeRD; |
11 |
use SL::Locale::String qw(t8); |
|
11 | 12 |
|
12 | 13 |
use SL::DB::PurchaseInvoice; |
13 | 14 |
|
14 | 15 |
sub process_attachments { |
15 | 16 |
my ($self, $function_name, $email_journal, %params) = @_; |
16 | 17 |
|
17 |
unless ($self->can("process_attachments_$function_name")) { |
|
18 |
my $full_function_name = "process_attachments_$function_name"; |
|
19 |
unless ($self->can($full_function_name)) { |
|
18 | 20 |
croak "Function not implemented for: $function_name"; |
19 | 21 |
} |
20 |
$function_name = "process_attachments_$function_name"; |
|
21 | 22 |
|
22 |
my $processed_count = 0; |
|
23 |
my @processed_files; |
|
24 |
my @errors; |
|
23 | 25 |
foreach my $attachment (@{$email_journal->attachments_sorted}) { |
24 |
my $processed = $self->$function_name($email_journal, $attachment, %params); |
|
25 |
$processed_count += $processed; |
|
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 |
} |
|
26 | 33 |
} |
27 |
return $processed_count; |
|
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; |
|
28 | 51 |
} |
29 | 52 |
|
30 | 53 |
sub can_function { |
... | ... | |
36 | 59 |
my ($self, $email_journal, $attachment, %params) = @_; |
37 | 60 |
|
38 | 61 |
my $content = $attachment->content; # scalar ref |
39 |
my $name = $attachment->name; |
|
40 | 62 |
|
41 |
return 0 unless $content =~ m/^%PDF|<\?xml/;
|
|
63 |
return t8("Not a PDF or XML file") unless $content =~ m/^%PDF|<\?xml/;
|
|
42 | 64 |
|
43 | 65 |
my %res; |
44 | 66 |
if ( $content =~ m/^%PDF/ ) { |
... | ... | |
48 | 70 |
} |
49 | 71 |
|
50 | 72 |
unless ($res{'result'} == SL::ZUGFeRD::RES_OK()) { |
51 |
my $error = $res{'message'}; |
|
52 |
$email_journal->extended_status( |
|
53 |
join "\n", $email_journal->extended_status, |
|
54 |
"Error processing ZUGFeRD attachment $name: $error" |
|
55 |
)->save; |
|
56 |
return 0; |
|
73 |
# my $error = $res{'message'}; # technical error |
|
74 |
my $error = t8('No vaild Factur-X/ZUGFeRD file'); |
|
75 |
return $error; |
|
57 | 76 |
} |
58 | 77 |
|
59 | 78 |
my $purchase_invoice; |
... | ... | |
62 | 81 |
1; |
63 | 82 |
} or do { |
64 | 83 |
my $error = $@; |
65 |
$email_journal->update_attributes( |
|
66 |
extended_status => |
|
67 |
join "\n", $email_journal->extended_status, |
|
68 |
"Error processing ZUGFeRD attachment $name: $error" |
|
69 |
); |
|
70 |
return 0; |
|
84 |
return $error; |
|
71 | 85 |
}; |
72 | 86 |
|
73 | 87 |
$self->_add_attachment_to_record($email_journal, $attachment, $purchase_invoice); |
74 | 88 |
|
75 |
return 1;
|
|
89 |
return 0;
|
|
76 | 90 |
} |
77 | 91 |
|
78 | 92 |
sub _add_attachment_to_record { |
Auch abrufbar als: Unified diff
BJ:ImportRecordEmails: Status vom auto. ZUGFeRD-Import in erw. Status