Bug 20640: (follow-up) Degrade gracefully on error
[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 Koha::Patrons;
30 use Koha::Patron::Discharge;
31 use Koha::DateUtils;
32
33 my $input = new CGI;
34
35 unless ( C4::Context->preference('useDischarge') ) {
36     print $input->redirect("/cgi-bin/koha/errors/404.pl");
37     exit;
38 }
39
40 my $op = $input->param("op") // '';
41
42 # Getting the template and auth
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
44     template_name => "opac-discharge.tt",
45     query         => $input,
46     type          => "opac",
47     debug         => 1,
48 });
49
50 my $can_be_discharged = Koha::Patron::Discharge::can_be_discharged({ borrowernumber => $loggedinuser });
51 if ($can_be_discharged == 0) {
52     $template->param( has_checkouts => 1 );
53 }
54
55 my $pending = Koha::Patron::Discharge::count({
56     borrowernumber => $loggedinuser,
57     pending        => 1,
58 });
59 my $available = Koha::Patron::Discharge::is_discharged({borrowernumber => $loggedinuser});
60
61 if ( $op eq 'request' ) {
62     if ($pending || $available) {
63         # Request already done
64         print $input->redirect("/cgi-bin/koha/opac-discharge.pl");
65         exit;
66     }
67     my $success = Koha::Patron::Discharge::request({
68         borrowernumber => $loggedinuser,
69     });
70     if ($success) {
71         $template->param( success => 1 );
72     }
73     else {
74         $template->param( has_issues => 1 );
75     }
76 }
77 elsif ( $op eq 'get' ) {
78     unless ($available) {
79         # No valid discharge to get
80         print $input->redirect("/cgi-bin/koha/opac-discharge.pl");
81         exit;
82     }
83     eval {
84
85         # Getting member data
86         my $patron = Koha::Patrons->find( $loggedinuser );
87         my $pdf_path = Koha::Patron::Discharge::generate_as_pdf({
88             borrowernumber => $loggedinuser,
89             branchcode => $patron->branchcode,
90         });
91
92         binmode(STDOUT);
93         print $input->header(
94             -type       => 'application/pdf',
95             -charset    => 'utf-8',
96             -attachment => "discharge_$loggedinuser.pdf",
97         );
98         open my $fh, '<', $pdf_path;
99         my @lines = <$fh>;
100         close $fh;
101         print @lines;
102         exit;
103     };
104     if ( $@ ) {
105         carp $@;
106         $template->param( messages => [ {type => 'error', code => 'unable_to_generate_pdf'} ] );
107     }
108 }
109 else {
110     $template->param(
111         available => $available,
112         pending   => $pending,
113     );
114 }
115
116 $template->param( dischargeview => 1 );
117
118 output_html_with_http_headers $input, $cookie, $template->output, undef, { force_no_caching => 1 };