Bug 18624: Run time errors when searching authorities with Elastic search 5.3
[koha.git] / members / discharge.pl
blobb24c41b1db82a7a3d5758bb155c9d8c662004a78
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Copyright 2013 BibLibre
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.
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.
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>.
20 =head1 NAME
22 discharges.pl
24 =head1 DESCRIPTION
26 Allows librarian to edit and/or manage borrowers' discharges
28 =cut
30 use Modern::Perl;
31 use Carp;
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;
42 use Koha::DateUtils;
44 my $input = new CGI;
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 });
54 my $borrowernumber = $input->param('borrowernumber');
56 unless ( C4::Context->preference('useDischarge') ) {
57 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber&nopermission=1");
58 exit;
61 if ( $input->param('borrowernumber') ) {
62 $borrowernumber = $input->param('borrowernumber');
64 # Getting member data
65 my $patron = Koha::Patrons->find( $borrowernumber );
67 my $can_be_discharged = Koha::Patron::Discharge::can_be_discharged({
68 borrowernumber => $borrowernumber
69 });
71 my $holds = $patron->holds;
72 my $has_reserves = $holds->count;
74 # Generating discharge if needed
75 if ( $input->param('discharge') and $can_be_discharged ) {
76 my $is_discharged = Koha::Patron::Discharge::is_discharged({
77 borrowernumber => $borrowernumber,
78 });
79 unless ($is_discharged) {
80 Koha::Patron::Discharge::discharge({
81 borrowernumber => $borrowernumber
82 });
84 eval {
85 my $pdf_path = Koha::Patron::Discharge::generate_as_pdf(
86 { borrowernumber => $borrowernumber, branchcode => $patron->branchcode } );
88 binmode(STDOUT);
89 print $input->header(
90 -type => 'application/pdf',
91 -charset => 'utf-8',
92 -attachment => "discharge_$borrowernumber.pdf",
94 open my $fh, '<', $pdf_path;
95 my @lines = <$fh>;
96 close $fh;
97 print @lines;
98 exit;
100 if ( $@ ) {
101 carp $@;
102 $template->param( messages => [ {type => 'error', code => 'unable_to_generate_pdf'} ] );
106 # Already generated discharges
107 my $validated_discharges = Koha::Patron::Discharge::get_validated({
108 borrowernumber => $borrowernumber,
111 $template->param( picture => 1 ) if $patron->image;
113 $template->param(
114 # FIXME The patron object should be passed to the template
115 borrowernumber => $borrowernumber,
116 title => $patron->title,
117 initials => $patron->initials,
118 surname => $patron->surname,
119 borrowernumber => $borrowernumber,
120 firstname => $patron->firstname,
121 cardnumber => $patron->cardnumber,
122 categorycode => $patron->categorycode,
123 category_type => $patron->category->category_type,
124 categoryname => $patron->category->description,
125 address => $patron->address,
126 streetnumber => $patron->streetnumber,
127 streettype => $patron->streettype,
128 address2 => $patron->address2,
129 city => $patron->city,
130 zipcode => $patron->zipcode,
131 country => $patron->country,
132 phone => $patron->phone,
133 email => $patron->email,
134 branchcode => $patron->branchcode,
135 has_reserves => $has_reserves,
136 can_be_discharged => $can_be_discharged,
137 validated_discharges => $validated_discharges,
141 $template->param( dischargeview => 1, );
143 output_html_with_http_headers $input, $cookie, $template->output;