Bug 15128 - DBRev 15128
[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 unless ( C4::Context->preference('useDischarge') ) {
37     print $input->redirect("/cgi-bin/koha/errors/404.pl");
38     exit;
39 }
40
41 my $op = $input->param("op");
42
43 # Getting the template and auth
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
45     template_name => "opac-discharge.tt",
46     query         => $input,
47     type          => "opac",
48     debug         => 1,
49 });
50
51 if ( $op eq 'request' ) {
52     my $success = Koha::Patron::Discharge::request({
53         borrowernumber => $loggedinuser,
54     });
55
56     if ($success) {
57         $template->param( success => 1 );
58     }
59     else {
60         $template->param( has_issues => 1 );
61     }
62 }
63 elsif ( $op eq 'get' ) {
64     eval {
65
66         # Getting member data
67         my $data = GetMember( borrowernumber => $loggedinuser );
68         my $pdf_path = Koha::Patron::Discharge::generate_as_pdf({
69             borrowernumber => $loggedinuser,
70             branchcode => $data->{'branchcode'},
71         });
72
73         binmode(STDOUT);
74         print $input->header(
75             -type       => 'application/pdf',
76             -charset    => 'utf-8',
77             -attachment => "discharge_$loggedinuser.pdf",
78         );
79         open my $fh, '<', $pdf_path;
80         my @lines = <$fh>;
81         close $fh;
82         print @lines;
83         exit;
84     };
85     if ( $@ ) {
86         carp $@;
87         $template->param( messages => [ {type => 'error', code => 'unable_to_generate_pdf'} ] );
88     }
89 }
90 else {
91     my $pending = Koha::Patron::Discharge::count({
92         borrowernumber => $loggedinuser,
93         pending        => 1,
94     });
95     my $available = Koha::Patron::Discharge::count({
96         borrowernumber => $loggedinuser,
97         validated      => 1,
98     });
99     $template->param(
100         available => $available && Koha::Patron::Discharge::is_discharged({borrowernumber => $loggedinuser}),
101         pending   => $pending,
102     );
103 }
104
105 $template->param( dischargeview => 1 );
106
107 output_html_with_http_headers $input, $cookie, $template->output, undef, { force_no_caching => 1 };