Bug 8007: Compatibility with bug 11944
[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
22 use C4::Auth qw(:DEFAULT get_session);
23 use CGI qw( -utf8 );
24 use C4::Context;
25 use C4::Output;
26 use C4::Log;
27 use C4::Debug;
28 use C4::Branch;
29 use C4::Members;
30 use Koha::Borrower::Discharge;
31 use Koha::DateUtils;
32
33 my $input = new CGI;
34
35 my $op = $input->param("op");
36
37 # Getting the template and auth
38 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
39     template_name => "opac-discharge.tt",
40     query         => $input,
41     type          => "opac",
42     debug         => 1,
43 });
44
45 if ( $op eq 'request' ) {
46     my $success = Koha::Borrower::Discharge::request({
47         borrowernumber => $loggedinuser,
48     });
49
50     if ($success) {
51         $template->param( success => 1 );
52     }
53     else {
54         $template->param( has_issues => 1 );
55     }
56 }
57 elsif ( $op eq 'get' ) {
58     my $pdf_path = Koha::Borrower::Discharge::generate_as_pdf({
59         borrowernumber => $loggedinuser
60     });
61
62     binmode(STDOUT);
63     print $input->header(
64         -type       => 'application/pdf',
65         -charset    => 'utf-8',
66         -attachment => "discharge_$loggedinuser.pdf",
67     );
68     open my $fh, '<', $pdf_path;
69     my @lines = <$fh>;
70     close $fh;
71     print @lines;
72     exit;
73 }
74 else {
75     my $pending = Koha::Borrower::Discharge::count({
76         borrowernumber => $loggedinuser,
77         pending        => 1,
78     });
79     my $available = Koha::Borrower::Discharge::count({
80         borrowernumber => $loggedinuser,
81         validated      => 1,
82     });
83     $template->param(
84         available => $available,
85         pending   => $pending,
86     );
87 }
88
89 $template->param( dischargeview => 1 );
90
91 output_html_with_http_headers $input, $cookie, $template->output;