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