d4e43cfc83677c5dd40804787d1d48c3454c571d
[koha.git] / acqui / invoice-files.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2014 Jacek Ablewicz
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 =head1 NAME
21
22 invoice-files.pl
23
24 =head1 DESCRIPTION
25
26 Manage files associated with invoice
27
28 =cut
29
30 use Modern::Perl;
31
32 use CGI;
33 use C4::Auth qw( get_template_and_user );
34 use C4::Output qw( output_html_with_http_headers );
35 use C4::Acquisition qw( GetInvoice GetInvoiceDetails );
36 use Koha::Misc::Files;
37
38 my $input = CGI->new;
39 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
40     {
41         template_name   => 'acqui/invoice-files.tt',
42         query           => $input,
43         type            => 'intranet',
44         flagsrequired   => { 'acquisition' => '*' },
45     }
46 );
47
48 my $invoiceid = $input->param('invoiceid') // '';
49 my $op = $input->param('op') // '';
50 my %errors;
51
52 my $mf = Koha::Misc::Files->new( tabletag => 'aqinvoices', recordid => $invoiceid );
53 defined($mf) || do { $op = 'none'; $errors{'invalid_parameter'} = 1; };
54
55 if ( $op eq 'download' ) {
56     my $file_id = $input->param('file_id');
57     my $file = $mf->GetFile( id => $file_id );
58
59     my $fname = $file->{'file_name'};
60     my $ftype = $file->{'file_type'};
61     if ($input->param('view') && ($ftype =~ m|^image/|i || $fname =~ /\.pdf/i)) {
62         $fname =~ /\.pdf/i && do { $ftype='application/pdf'; };
63         print $input->header(
64             -type       => $ftype,
65             -charset    => 'utf-8'
66         );
67     } else {
68         print $input->header(
69             -type       => $file->{'file_type'},
70             -charset    => 'utf-8',
71             -attachment => $file->{'file_name'}
72         );
73     }
74     print $file->{'file_content'};
75 }
76 else {
77     my $details = GetInvoiceDetails($invoiceid);
78     $template->param(
79         invoiceid        => $details->{'invoiceid'},
80         invoicenumber    => $details->{'invoicenumber'},
81         suppliername     => $details->{'suppliername'},
82         booksellerid     => $details->{'booksellerid'},
83         datereceived     => $details->{'datereceived'},
84     );
85
86     if ( $op eq 'upload' ) {
87         my $uploaded_file = $input->upload('uploadfile');
88
89         if ($uploaded_file) {
90             my $filename = $input->param('uploadfile');
91             my $mimetype = $input->uploadInfo($filename)->{'Content-Type'};
92
93             $errors{'empty_upload'} = 1 if ( -z $uploaded_file );
94             unless (%errors) {
95                 my $file_content = do { local $/; <$uploaded_file>; };
96                 if ($mimetype =~ /^application\/(force-download|unknown)$/i && $filename =~ /\.pdf$/i) {
97                     $mimetype = 'application/pdf';
98                 }
99                 $mf->AddFile(
100                     name    => $filename,
101                     type    => $mimetype,
102                     content => $file_content,
103                     description => scalar $input->param('description')
104                 );
105             }
106         }
107         else {
108             $errors{'no_file'} = 1;
109         }
110     } elsif ( $op eq 'delete' ) {
111         $mf->DelFile( id => scalar $input->param('file_id') );
112     }
113
114     $template->param(
115         files => (defined($mf)? $mf->GetFilesInfo(): undef),
116         errors => \%errors
117     );
118     output_html_with_http_headers $input, $cookie, $template->output;
119 }