]> wagnertech.de Git - mfinanz.git/blob - SL/DB/EmailJournal.pm
restart apache2 in postinst
[mfinanz.git] / SL / DB / EmailJournal.pm
1 package SL::DB::EmailJournal;
2
3 use strict;
4
5 use Carp qw(croak);
6 use List::Util qw(first);
7
8 use SL::Webdav;
9 use SL::File;
10
11 use SL::DB::MetaSetup::EmailJournal;
12 use SL::DB::Manager::EmailJournal;
13 use SL::DB::Helper::AttrSorted;
14 use SL::DB::Helper::LinkedRecords;
15
16 __PACKAGE__->meta->add_relationship(
17   attachments  => {
18     type       => 'one to many',
19     class      => 'SL::DB::EmailJournalAttachment',
20     column_map => { id => 'email_journal_id' },
21   },
22 );
23
24 __PACKAGE__->meta->initialize;
25
26 __PACKAGE__->attr_sorted('attachments');
27
28 sub compare_to {
29   my ($self, $other) = @_;
30
31   return -1 if  $self->sent_on && !$other->sent_on;
32   return  1 if !$self->sent_on &&  $other->sent_on;
33
34   my $result = 0;
35   $result    = $other->sent_on <=> $self->sent_on;
36   return $result || ($self->id <=> $other->id);
37 }
38
39 sub link_to_record_with_attachment {
40   my ($self, $record, $attachment_or_id) = @_;
41
42   if ($attachment_or_id ne '') {
43     my $attachment = ref $attachment_or_id ?
44         $attachment_or_id
45       : first {$_->id == $attachment_or_id} @{$self->attachments_sorted};
46     croak "Email journal attachment does not belong to this email journal"
47       unless  $attachment && $attachment->email_journal_id == $self->id;
48     $attachment->add_file_to_record($record);
49   }
50
51   $self->link_to_record($record);
52 }
53
54 sub linked {
55   my ($self) = @_;
56
57   return !!scalar @{$self->linked_records};
58 }
59
60 sub process_attachments_as_purchase_invoices {
61   my ($self) = @_;
62
63   my $attachments = $self->attachments_sorted;
64   foreach my $attachment (@$attachments) {
65     my $ap_invoice = $attachment->create_ap_invoice();
66     next unless $ap_invoice;
67
68     # link to email journal
69     $self->link_to_record($ap_invoice);
70
71     # copy file to webdav folder
72     if ($::instance_conf->get_webdav_documents) {
73       my $webdav = SL::Webdav->new(
74         type     => 'accounts_payable',
75         number   => $ap_invoice->invnumber,
76       );
77       my $webdav_file = SL::Webdav::File->new(
78         webdav => $webdav,
79         filename => $attachment->name,
80       );
81       eval {
82         $webdav_file->store(data => \$attachment->content);
83         1;
84       } or do {
85         die 'Storing the ZUGFeRD file to the WebDAV folder failed: ' . $@;
86       };
87     }
88     # copy file to doc storage
89     if ($::instance_conf->get_doc_storage) {
90       eval {
91         SL::File->save(
92           object_id     => $ap_invoice->id,
93           object_type   => 'purchase_invoice',
94           mime_type     => 'application/pdf',
95           source        => 'uploaded',
96           file_type     => 'document',
97           file_name     => $attachment->name,
98           file_contents => $attachment->content,
99         );
100         1;
101       } or do {
102         die 'Storing the ZUGFeRD file in the storage backend failed: ' . $@;
103       };
104     }
105   }
106
107   my $new_ext_status = join('_', $self->extended_status, 'processed');
108   $self->update({ extended_status => $new_ext_status});
109 }
110
111 1;
112
113 __END__
114
115 =pod
116
117 =encoding utf8
118
119 =head1 NAME
120
121 SL::DB::EmailJournal - RDBO model for email journal
122
123 =head1 SYNOPSIS
124
125 This is a standard Rose::DB::Object based model and can be used as one.
126
127 =head1 METHODS
128
129 =over 4
130
131 =item C<compare_to $self, $other>
132
133 Compares C<$self> with C<$other> and returns the newer entry.
134
135 =back
136
137 =cut
138