]> wagnertech.de Git - mfinanz.git/blob - SL/DB/EmailJournalAttachment.pm
Merge branch 'master' of http://wagnertech.de/git/mfinanz
[mfinanz.git] / SL / DB / EmailJournalAttachment.pm
1 package SL::DB::EmailJournalAttachment;
2
3 use strict;
4
5 use SL::DB::MetaSetup::EmailJournalAttachment;
6 use SL::DB::Manager::EmailJournalAttachment;
7 use SL::DB::Helper::ActsAsList (group_by => [ qw(email_journal_id) ]);
8
9 use SL::Webdav;
10 use SL::File;
11
12 __PACKAGE__->meta->initialize;
13
14 sub add_file_to_record {
15   my ($self, $record) = @_;
16
17   # copy file to webdav folder
18   if ($::instance_conf->get_webdav_documents) {
19     my $record_type = $record->record_type;
20     # TODO: file and webdav use different types
21     $record_type = 'accounts_payable' if $record_type eq 'ap_transaction';
22     $record_type = 'invoice'          if $record_type eq 'ar_transaction';
23     $record_type = 'general_ledger'   if $record_type eq 'gl_transaction';
24     $record_type = 'invoice'          if $record_type eq 'invoice_storno';
25     $record_type = 'purchase_invoice' if $record_type eq 'purchase_credit_note';
26     my $webdav = SL::Webdav->new(
27       type     => $record_type,
28       number   => $record->record_number,
29     );
30     my $webdav_file = SL::Webdav::File->new(
31       webdav => $webdav,
32       filename => $self->name,
33     );
34     eval {
35       $webdav_file->store(data => \$self->content);
36       1;
37     } or do {
38       die 'Storing the attachment file to the WebDAV folder failed: ' . $@;
39     };
40   }
41   # copy file to doc storage
42   if ($::instance_conf->get_doc_storage) {
43     my $record_type = $record->record_type;
44     # TODO: file and webdav use different types
45     $record_type = 'purchase_invoice' if $record_type eq 'ap_transaction';
46     $record_type = 'purchase_invoice' if $record_type eq 'purchase_credit_note';
47     $record_type = 'invoice'          if $record_type eq 'ar_transaction';
48     $record_type = 'invoice'          if $record_type eq 'invoice_storno';
49     eval {
50       SL::File->save(
51         object_id     => $record->id,
52         object_type   => $record_type,
53         source        => 'uploaded',
54         file_type     => 'document',
55         file_name     => $self->name,
56         file_contents => $self->content,
57         mime_type     => $self->mime_type,
58       );
59       1;
60     } or do {
61       die 'Storing the attachment file to the file management failed: ' . $@;
62     };
63   }
64
65 }
66
67 1;