Bug 16224: Fix t/db_dependent/Reports_Guided.t
[koha.git] / members / discharge.pl
blob466c119773a09976bb7f448d7e6b8d63ef5f400b
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;
44 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user({
45 template_name => 'members/discharge.tt',
46 query => $input,
47 type => 'intranet',
48 authnotrequired => 0,
49 flagsrequired => { 'borrowers' => '*' },
50 });
52 my $borrowernumber;
53 my $data;
54 if ( $input->param('borrowernumber') ) {
55 $borrowernumber = $input->param('borrowernumber');
57 # Getting member data
58 $data = GetMember( borrowernumber => $borrowernumber );
60 my $can_be_discharged = Koha::Borrower::Discharge::can_be_discharged({
61 borrowernumber => $borrowernumber
62 });
64 # Getting reserves
65 my @reserves = GetReservesFromBorrowernumber($borrowernumber);
66 my $has_reserves = scalar(@reserves);
68 # Generating discharge if needed
69 if ( $input->param('discharge') and $can_be_discharged ) {
70 my $is_discharged = Koha::Borrower::Discharge::is_discharged({
71 borrowernumber => $borrowernumber,
72 });
73 unless ($is_discharged) {
74 Koha::Borrower::Discharge::discharge({
75 borrowernumber => $borrowernumber
76 });
78 eval {
79 my $pdf_path = Koha::Borrower::Discharge::generate_as_pdf(
80 { borrowernumber => $borrowernumber, branchcode => $data->{'branchcode'} } );
82 binmode(STDOUT);
83 print $input->header(
84 -type => 'application/pdf',
85 -charset => 'utf-8',
86 -attachment => "discharge_$borrowernumber.pdf",
88 open my $fh, '<', $pdf_path;
89 my @lines = <$fh>;
90 close $fh;
91 print @lines;
92 exit;
94 if ( $@ ) {
95 carp $@;
96 $template->param( messages => [ {type => 'error', code => 'unable_to_generate_pdf'} ] );
100 # Already generated discharges
101 my $validated_discharges = Koha::Borrower::Discharge::get_validated({
102 borrowernumber => $borrowernumber,
105 my ($picture, $dberror) = GetPatronImage($borrowernumber);
106 $template->param( picture => 1 ) if $picture;
108 $template->param(
109 borrowernumber => $borrowernumber,
110 biblionumber => $data->{'biblionumber'},
111 title => $data->{'title'},
112 initials => $data->{'initials'},
113 surname => $data->{'surname'},
114 borrowernumber => $borrowernumber,
115 firstname => $data->{'firstname'},
116 cardnumber => $data->{'cardnumber'},
117 categorycode => $data->{'categorycode'},
118 category_type => $data->{'category_type'},
119 categoryname => $data->{'description'},
120 address => $data->{'address'},
121 streetnumber => $data->{streetnumber},
122 streettype => $data->{streettype},
123 address2 => $data->{'address2'},
124 city => $data->{'city'},
125 zipcode => $data->{'zipcode'},
126 country => $data->{'country'},
127 phone => $data->{'phone'},
128 email => $data->{'email'},
129 branchcode => $data->{'branchcode'},
130 has_reserves => $has_reserves,
131 can_be_discharged => $can_be_discharged,
132 validated_discharges => $validated_discharges,
136 $template->param( dischargeview => 1, );
138 output_html_with_http_headers $input, $cookie, $template->output;