Koha/acqui/invoice-files.pl
Jacek Ablewicz 480847e81c Bug 3050 - Add an option to upload scanned invoices #1/3
(part #1: new module w/ UT + script + template)

New feature, adds an ability to attach arbitrary files to
acquisition records (currently: to the invoices - but it can
be extended to baskets, basketgroups, budgets etc.).

Note: this code is (heavily) based on "Bug 8130 - attach PDF
files to a patron record" by Kale M Hall, main difference being
that new table (misc_files) and new module (Koha/Misc/Files.pm)
are intended to be a little more generic solution - they allow to
store and manage files associated with great many kinds of records,
from arbitrary tables.

Test plan:
1) Apply patch[es]
2) Run installer/data/mysql/updatedatabase.pl
3) Enable system preference 'AcqEnableFiles' in acquisition
4) New option 'Manage invoice files' appears in the invoice
detail page
5) Upload/view/download/delete some files for some invoices
6) Try to delete invoice with files attached (files should
get deleted as well)
7) Try to merge 2+ invoices with files attached; after merge,
all files previously attached to individual invoices being
merged should be attached to resulting invoice (merge destination)
8) prove t/db_dependent/Koha_Misc_Files.t
9) Ensure there are no regressions of any kind in invoice detail
page (acqui/invoice.pl).

Signed-off-by: Paola Rossi <paola.rossi@cineca.it>
Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
2014-05-25 21:14:00 +00:00

121 lines
3.7 KiB
Perl
Executable file

#!/usr/bin/perl
# This file is part of Koha.
#
# Copyright 2014 Jacek Ablewicz
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
=head1 NAME
invoice-files.pl
=head1 DESCRIPTION
Manage files associated with invoice
=cut
use Modern::Perl;
use CGI;
use C4::Auth;
use C4::Output;
use C4::Acquisition;
use Koha::Misc::Files;
my $input = new CGI;
my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
{
template_name => 'acqui/invoice-files.tt',
query => $input,
type => 'intranet',
authnotrequired => 0,
flagsrequired => { 'acquisition' => '*' },
debug => 1,
}
);
my $invoiceid = $input->param('invoiceid') // '';
my $op = $input->param('op') // '';
my %errors;
my $mf = Koha::Misc::Files->new( tabletag => 'aqinvoices', recordid => $invoiceid );
defined($mf) || do { $op = 'none'; $errors{'invalid_parameter'} = 1; };
if ( $op eq 'download' ) {
my $file_id = $input->param('file_id');
my $file = $mf->GetFile( id => $file_id );
my $fname = $file->{'file_name'};
my $ftype = $file->{'file_type'};
if ($input->param('view') && ($ftype =~ m|^image/|i || $fname =~ /\.pdf/i)) {
$fname =~ /\.pdf/i && do { $ftype='application/pdf'; };
print $input->header(
-type => $ftype,
-charset => 'utf-8'
);
} else {
print $input->header(
-type => $file->{'file_type'},
-charset => 'utf-8',
-attachment => $file->{'file_name'}
);
}
print $file->{'file_content'};
}
else {
my $details = GetInvoiceDetails($invoiceid);
$template->param(
invoiceid => $details->{'invoiceid'},
invoicenumber => $details->{'invoicenumber'},
suppliername => $details->{'suppliername'},
booksellerid => $details->{'booksellerid'},
datereceived => $details->{'datereceived'},
);
if ( $op eq 'upload' ) {
my $uploaded_file = $input->upload('uploadfile');
if ($uploaded_file) {
my $filename = $input->param('uploadfile');
my $mimetype = $input->uploadInfo($filename)->{'Content-Type'};
$errors{'empty_upload'} = 1 if ( -z $uploaded_file );
unless (%errors) {
my $file_content = do { local $/; <$uploaded_file>; };
if ($mimetype =~ /^application\/(force-download|unknown)$/i && $filename =~ /\.pdf$/i) {
$mimetype = 'application/pdf';
}
$mf->AddFile(
name => $filename,
type => $mimetype,
content => $file_content,
description => $input->param('description')
);
}
}
else {
$errors{'no_file'} = 1;
}
} elsif ( $op eq 'delete' ) {
$mf->DelFile( id => $input->param('file_id') );
}
$template->param(
files => (defined($mf)? $mf->GetFilesInfo(): undef),
errors => \%errors
);
output_html_with_http_headers $input, $cookie, $template->output;
}