Bug 16167: Remove Authorised value images prefs
[koha.git] / opac / opac-discharge.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2013 BibLibre
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 use Modern::Perl;
21 use Carp;
22
23 use C4::Auth qw(:DEFAULT get_session);
24 use CGI qw( -utf8 );
25 use C4::Context;
26 use C4::Output;
27 use C4::Log;
28 use C4::Debug;
29 use C4::Branch;
30 use C4::Members;
31 use Koha::Patron::Discharge;
32 use Koha::DateUtils;
33
34 my $input = new CGI;
35
36 my $op = $input->param("op");
37
38 # Getting the template and auth
39 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
40     template_name => "opac-discharge.tt",
41     query         => $input,
42     type          => "opac",
43     debug         => 1,
44 });
45
46 if ( $op eq 'request' ) {
47     my $success = Koha::Patron::Discharge::request({
48         borrowernumber => $loggedinuser,
49     });
50
51     if ($success) {
52         $template->param( success => 1 );
53     }
54     else {
55         $template->param( has_issues => 1 );
56     }
57 }
58 elsif ( $op eq 'get' ) {
59     eval {
60
61         # Getting member data
62         my $data = GetMember( borrowernumber => $loggedinuser );
63         my $pdf_path = Koha::Patron::Discharge::generate_as_pdf({
64             borrowernumber => $loggedinuser,
65             branchcode => $data->{'branchcode'},
66         });
67
68         binmode(STDOUT);
69         print $input->header(
70             -type       => 'application/pdf',
71             -charset    => 'utf-8',
72             -attachment => "discharge_$loggedinuser.pdf",
73         );
74         open my $fh, '<', $pdf_path;
75         my @lines = <$fh>;
76         close $fh;
77         print @lines;
78         exit;
79     };
80     if ( $@ ) {
81         carp $@;
82         $template->param( messages => [ {type => 'error', code => 'unable_to_generate_pdf'} ] );
83     }
84 }
85 else {
86     my $pending = Koha::Patron::Discharge::count({
87         borrowernumber => $loggedinuser,
88         pending        => 1,
89     });
90     my $available = Koha::Patron::Discharge::count({
91         borrowernumber => $loggedinuser,
92         validated      => 1,
93     });
94     $template->param(
95         available => $available && Koha::Patron::Discharge::is_discharged({borrowernumber => $loggedinuser}),
96         pending   => $pending,
97     );
98 }
99
100 $template->param( dischargeview => 1 );
101
102 output_html_with_http_headers $input, $cookie, $template->output, undef, { force_no_caching => 1 };