Fix use of non-existent variable $borrower
[koha.git] / members / discharge.pl
blob44f141d819ecf1a8f7e93c983fb41a5d8d5eb6e5
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::Borrower::Discharge;
41 use Koha::DateUtils;
43 my $input = new CGI;
45 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user({
46 template_name => 'members/discharge.tt',
47 query => $input,
48 type => 'intranet',
49 authnotrequired => 0,
50 flagsrequired => { 'borrowers' => '*' },
51 });
53 my $borrowernumber = $input->param('borrowernumber');
55 unless ( C4::Context->preference('useDischarge') ) {
56 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber&nopermission=1");
57 exit;
60 my $data;
61 if ( $input->param('borrowernumber') ) {
62 $borrowernumber = $input->param('borrowernumber');
64 # Getting member data
65 $data = GetMember( borrowernumber => $borrowernumber );
67 my $can_be_discharged = Koha::Borrower::Discharge::can_be_discharged({
68 borrowernumber => $borrowernumber
69 });
71 # Getting reserves
72 my @reserves = GetReservesFromBorrowernumber($borrowernumber);
73 my $has_reserves = scalar(@reserves);
75 # Generating discharge if needed
76 if ( $input->param('discharge') and $can_be_discharged ) {
77 my $is_discharged = Koha::Borrower::Discharge::is_discharged({
78 borrowernumber => $borrowernumber,
79 });
80 unless ($is_discharged) {
81 Koha::Borrower::Discharge::discharge({
82 borrowernumber => $borrowernumber
83 });
85 eval {
86 my $pdf_path = Koha::Borrower::Discharge::generate_as_pdf(
87 { borrowernumber => $borrowernumber, branchcode => $data->{'branchcode'} } );
89 binmode(STDOUT);
90 print $input->header(
91 -type => 'application/pdf',
92 -charset => 'utf-8',
93 -attachment => "discharge_$borrowernumber.pdf",
95 open my $fh, '<', $pdf_path;
96 my @lines = <$fh>;
97 close $fh;
98 print @lines;
99 exit;
101 if ( $@ ) {
102 carp $@;
103 $template->param( messages => [ {type => 'error', code => 'unable_to_generate_pdf'} ] );
107 # Already generated discharges
108 my $validated_discharges = Koha::Borrower::Discharge::get_validated({
109 borrowernumber => $borrowernumber,
112 my ($picture, $dberror) = GetPatronImage($borrowernumber);
113 $template->param( picture => 1 ) if $picture;
115 $template->param(
116 borrowernumber => $borrowernumber,
117 biblionumber => $data->{'biblionumber'},
118 title => $data->{'title'},
119 initials => $data->{'initials'},
120 surname => $data->{'surname'},
121 borrowernumber => $borrowernumber,
122 firstname => $data->{'firstname'},
123 cardnumber => $data->{'cardnumber'},
124 categorycode => $data->{'categorycode'},
125 category_type => $data->{'category_type'},
126 categoryname => $data->{'description'},
127 address => $data->{'address'},
128 streetnumber => $data->{streetnumber},
129 streettype => $data->{streettype},
130 address2 => $data->{'address2'},
131 city => $data->{'city'},
132 zipcode => $data->{'zipcode'},
133 country => $data->{'country'},
134 phone => $data->{'phone'},
135 email => $data->{'email'},
136 branchcode => $data->{'branchcode'},
137 has_reserves => $has_reserves,
138 can_be_discharged => $can_be_discharged,
139 validated_discharges => $validated_discharges,
143 $template->param( dischargeview => 1, );
145 output_html_with_http_headers $input, $cookie, $template->output;