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>.
26 Allows librarian to edit and/or manage borrowers' discharges
34 use C4::Auth qw( get_template_and_user );
35 use C4::Output qw( output_and_exit_if_error output_and_exit output_html_with_http_headers );
39 use Koha::Patron::Discharge;
45 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user({
46 template_name => 'members/discharge.tt',
49 flagsrequired => { 'borrowers' => 'edit_borrowers' },
52 my $borrowernumber = $input->param('borrowernumber');
54 unless ( C4::Context->preference('useDischarge') ) {
55 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber&nopermission=1");
59 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
60 my $patron = Koha::Patrons->find( $borrowernumber );
61 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
63 my $can_be_discharged = Koha::Patron::Discharge::can_be_discharged({
64 borrowernumber => $borrowernumber
67 # Generating discharge if needed
68 if ( $input->param('discharge') and $can_be_discharged ) {
69 my $is_discharged = Koha::Patron::Discharge::is_discharged({
70 borrowernumber => $borrowernumber,
72 unless ($is_discharged) {
73 Koha::Patron::Discharge::discharge({
74 borrowernumber => $borrowernumber
78 my $pdf_path = Koha::Patron::Discharge::generate_as_pdf(
79 { borrowernumber => $borrowernumber, branchcode => $patron->branchcode } );
83 -type => 'application/pdf',
85 -attachment => "discharge_$borrowernumber.pdf",
87 open my $fh, '<', $pdf_path;
94 $template->param( messages => [ {type => 'error', code => 'unable_to_generate_pdf'} ] );
96 # no error, pdf is sent, so stop sending data to browser
101 # Already generated discharges
102 my @validated_discharges = Koha::Patron::Discharge::get_validated({
103 borrowernumber => $borrowernumber,
108 can_be_discharged => $can_be_discharged,
109 validated_discharges => \@validated_discharges,
112 $template->param( dischargeview => 1, );
114 output_html_with_http_headers $input, $cookie, $template->output;