Anpassung der CH-Erfolgsrechnung für nicht CH-Ausgabeformate
[kivitendo-erp.git] / SL / Helper / File.pm
1 package SL::Helper::File;
2
3 use strict;
4
5 use Exporter 'import';
6 our @EXPORT_OK = qw(store_pdf append_general_pdf_attachments);
7 our %EXPORT_TAGS = (all => \@EXPORT_OK,);
8 use SL::File;
9
10 sub store_pdf {
11   my ($self, $form) = @_;
12   return unless $::instance_conf->get_doc_storage;
13   my $type = $form->{type};
14   $type = $form->{formname} if $form->{formname} && !$form->{type};
15   my $id = $form->{id};
16   $id = $form->{attachment_id} if $form->{attachment_id} && !$form->{id};
17   return if !$id || !$type;
18   my $prefix = $form->get_number_prefix_for_type();
19   SL::File->save(
20     object_id   => $id,
21     object_type => $type,
22     mime_type   => 'application/pdf',
23     source      => 'created',
24     file_type   => 'document',
25     file_name   => $form->{attachment_filename},
26     file_path   => $form->{tmpfile},
27     file_number => $form->{"${prefix}number"},
28   );
29 }
30
31 # This method also needed by $form to append all general pdf attachments
32 #
33 sub append_general_pdf_attachments {
34   my ($self, %params) = @_;
35   return 0 unless $::instance_conf->get_doc_storage;
36   return 0 if !$params{filepath} || !$params{type};
37
38   my @files = SL::File->get_all(
39     object_id   => 0,
40     object_type => $params{type},
41     mime_type   => 'application/pdf'
42   );
43   return 0 if $#files < 0;
44
45   my @pdf_file_names = ($params{filepath});
46   foreach my $file (@files) {
47     my $path = $file->get_file;
48     push @pdf_file_names, $path if $path;
49   }
50
51   #TODO immer noch das alte Problem:
52   #je nachdem von woher der Aufruf kommt ist man in ./users oder .
53   my $savedir = POSIX::getcwd();
54   chdir("$self->{cwd}");
55   $self->merge_pdfs(
56     file_names => \@pdf_file_names,
57     out_path   => $params{filepath}
58   );
59   chdir("$savedir");
60
61   return 0;
62 }
63
64 1;
65
66 __END__
67
68 =encoding utf-8
69
70 =head1 NAME
71
72 SL::Helper::File - Helper for $::Form to store generated PDF-Documents
73
74
75 =head1 SYNOPSIS
76
77 # This Helper is used by SL::Form to store new generated PDF-Files and append general attachments to this documents.
78 #
79 # in SL::Form.pm:
80
81  $self->store_pdf($self);
82
83  $self->append_general_pdf_attachments(filepath => $pdf_filename, type => $form->{type}) if ( $ext_for_format eq 'pdf' );
84
85 #It is also used in MassPrint Helper
86
87
88 =head1 DESCRIPTION
89
90 The files with file_type "generated" are stored.
91
92 See also L<SL::File>.
93
94 =head1 METHODS
95
96
97 =head2 C<store_pdf>
98
99 Copy generated PDF-File to File destination.
100 This method is need from SL::Form after LaTeX-PDF Generation
101
102 =over 4
103
104 =item C<form.id>
105
106 ID of ERP-Document
107
108 =item C<form.type>
109
110 type of ERP-document
111
112 =item C<form.formname>
113
114 if no type is set this is used as type
115
116 =item C<form.attachment_id>
117
118 if no id is set this is used as id
119
120 =item C<form.tmpfile>
121
122 The path of the generated PDF-file
123
124 =item C<form.attachment_filename>
125
126 The generated filename which is used as new filename (without timestamp)
127
128 =back
129
130 =head2 C<append_general_pdf_attachments PARAMS>
131
132 This method also needed by SL::Form to append all general pdf attachments
133
134 needed C<PARAMS>:
135
136 =over 4
137
138 =item C<type>
139
140 type of ERP-document
141
142 =item C<outname>
143
144 Name of file to which the general attachments must be added
145
146 =back
147
148 =head1 AUTHOR
149
150 Martin Helmling E<lt>martin.helmling@opendynamic.deE<gt>
151
152
153 =cut
154