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