1 package SL::DB::EmailJournal;
6 use List::Util qw(first);
11 use SL::DB::MetaSetup::EmailJournal;
12 use SL::DB::Manager::EmailJournal;
13 use SL::DB::Helper::AttrSorted;
14 use SL::DB::Helper::LinkedRecords;
16 __PACKAGE__->meta->add_relationship(
18 type => 'one to many',
19 class => 'SL::DB::EmailJournalAttachment',
20 column_map => { id => 'email_journal_id' },
24 __PACKAGE__->meta->initialize;
26 __PACKAGE__->attr_sorted('attachments');
29 my ($self, $other) = @_;
31 return -1 if $self->sent_on && !$other->sent_on;
32 return 1 if !$self->sent_on && $other->sent_on;
35 $result = $other->sent_on <=> $self->sent_on;
36 return $result || ($self->id <=> $other->id);
39 sub link_to_record_with_attachment {
40 my ($self, $record, $attachment_or_id) = @_;
42 if ($attachment_or_id ne '') {
43 my $attachment = ref $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);
51 $self->link_to_record($record);
57 return !!scalar @{$self->linked_records};
60 sub process_attachments_as_purchase_invoices {
63 my $attachments = $self->attachments_sorted;
64 foreach my $attachment (@$attachments) {
65 my $ap_invoice = $attachment->create_ap_invoice();
66 next unless $ap_invoice;
68 # link to email journal
69 $self->link_to_record($ap_invoice);
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,
77 my $webdav_file = SL::Webdav::File->new(
79 filename => $attachment->name,
82 $webdav_file->store(data => \$attachment->content);
85 die 'Storing the ZUGFeRD file to the WebDAV folder failed: ' . $@;
88 # copy file to doc storage
89 if ($::instance_conf->get_doc_storage) {
92 object_id => $ap_invoice->id,
93 object_type => 'purchase_invoice',
94 mime_type => 'application/pdf',
96 file_type => 'document',
97 file_name => $attachment->name,
98 file_contents => $attachment->content,
102 die 'Storing the ZUGFeRD file in the storage backend failed: ' . $@;
107 my $new_ext_status = join('_', $self->extended_status, 'processed');
108 $self->update({ extended_status => $new_ext_status});
121 SL::DB::EmailJournal - RDBO model for email journal
125 This is a standard Rose::DB::Object based model and can be used as one.
131 =item C<compare_to $self, $other>
133 Compares C<$self> with C<$other> and returns the newer entry.