Bug 12497: Fix search history non-accessible when OPAC was private
[koha.git] / members / 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 =head1 NAME
21
22 discharges.pl
23
24 =head1 DESCRIPTION
25
26 Allows librarian to edit and/or manage borrowers' discharges
27
28 =cut
29
30 use Modern::Perl;
31 use Carp;
32
33 use CGI qw( -utf8 );
34 use C4::Auth;
35 use C4::Output;
36 use C4::Members;
37 use C4::Reserves;
38 use C4::Letters;
39 use Koha::Patron::Discharge;
40 use Koha::Patrons;
41
42 use Koha::DateUtils;
43
44 my $input = new CGI;
45
46 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user({
47     template_name   => 'members/discharge.tt',
48     query           => $input,
49     type            => 'intranet',
50     authnotrequired => 0,
51     flagsrequired   => { 'borrowers' => '*' },
52 });
53
54 my $borrowernumber = $input->param('borrowernumber');
55
56 unless ( C4::Context->preference('useDischarge') ) {
57    print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber&nopermission=1");
58    exit;
59 }
60
61 $borrowernumber = $input->param('borrowernumber');
62
63 my $patron = Koha::Patrons->find( $borrowernumber );
64 unless ( $patron ) {
65     print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
66     exit;
67 }
68
69 my $can_be_discharged = Koha::Patron::Discharge::can_be_discharged({
70     borrowernumber => $borrowernumber
71 });
72
73 my $holds = $patron->holds;
74 my $has_reserves = $holds->count;
75
76 # Generating discharge if needed
77 if ( $input->param('discharge') and $can_be_discharged ) {
78     my $is_discharged = Koha::Patron::Discharge::is_discharged({
79         borrowernumber => $borrowernumber,
80     });
81     unless ($is_discharged) {
82         Koha::Patron::Discharge::discharge({
83             borrowernumber => $borrowernumber
84         });
85     }
86     eval {
87         my $pdf_path = Koha::Patron::Discharge::generate_as_pdf(
88             { borrowernumber => $borrowernumber, branchcode => $patron->branchcode } );
89
90         binmode(STDOUT);
91         print $input->header(
92             -type       => 'application/pdf',
93             -charset    => 'utf-8',
94             -attachment => "discharge_$borrowernumber.pdf",
95         );
96         open my $fh, '<', $pdf_path;
97         my @lines = <$fh>;
98         close $fh;
99         print @lines;
100         exit;
101     };
102     if ( $@ ) {
103         carp $@;
104         $template->param( messages => [ {type => 'error', code => 'unable_to_generate_pdf'} ] );
105     }
106 }
107
108 # Already generated discharges
109 my $validated_discharges = Koha::Patron::Discharge::get_validated({
110     borrowernumber => $borrowernumber,
111 });
112
113 $template->param( picture => 1 ) if $patron->image;
114
115 $template->param(
116     # FIXME The patron object should be passed to the template
117     borrowernumber    => $borrowernumber,
118     title             => $patron->title,
119     initials          => $patron->initials,
120     surname           => $patron->surname,
121     borrowernumber    => $borrowernumber,
122     firstname         => $patron->firstname,
123     cardnumber        => $patron->cardnumber,
124     categorycode      => $patron->categorycode,
125     category_type     => $patron->category->category_type,
126     categoryname      => $patron->category->description,
127     address           => $patron->address,
128     streetnumber      => $patron->streetnumber,
129     streettype        => $patron->streettype,
130     address2          => $patron->address2,
131     city              => $patron->city,
132     zipcode           => $patron->zipcode,
133     country           => $patron->country,
134     phone             => $patron->phone,
135     email             => $patron->email,
136     branchcode        => $patron->branchcode,
137     has_reserves      => $has_reserves,
138     can_be_discharged => $can_be_discharged,
139     validated_discharges => $validated_discharges,
140 );
141
142 $template->param( dischargeview => 1, );
143
144 output_html_with_http_headers $input, $cookie, $template->output;