7e055c6fd8148b1dcc8584325ef434d8102c55d4
[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 = CGI->new;
45
46 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user({
47     template_name   => 'members/discharge.tt',
48     query           => $input,
49     type            => 'intranet',
50     flagsrequired   => { 'borrowers' => 'edit_borrowers' },
51 });
52
53 my $borrowernumber = $input->param('borrowernumber');
54
55 unless ( C4::Context->preference('useDischarge') ) {
56    print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber&nopermission=1");
57    exit;
58 }
59
60 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
61 my $patron = Koha::Patrons->find( $borrowernumber );
62 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
63
64 my $can_be_discharged = Koha::Patron::Discharge::can_be_discharged({
65     borrowernumber => $borrowernumber
66 });
67
68 # Generating discharge if needed
69 if ( $input->param('discharge') and $can_be_discharged ) {
70     my $is_discharged = Koha::Patron::Discharge::is_discharged({
71         borrowernumber => $borrowernumber,
72     });
73     unless ($is_discharged) {
74         Koha::Patron::Discharge::discharge({
75             borrowernumber => $borrowernumber
76         });
77     }
78     eval {
79         my $pdf_path = Koha::Patron::Discharge::generate_as_pdf(
80             { borrowernumber => $borrowernumber, branchcode => $patron->branchcode } );
81
82         binmode(STDOUT);
83         print $input->header(
84             -type       => 'application/pdf',
85             -charset    => 'utf-8',
86             -attachment => "discharge_$borrowernumber.pdf",
87         );
88         open my $fh, '<', $pdf_path;
89         my @lines = <$fh>;
90         close $fh;
91         print @lines;
92     };
93     if ( $@ ) {
94         carp $@;
95         $template->param( messages => [ {type => 'error', code => 'unable_to_generate_pdf'} ] );
96     } else {
97         # no error, pdf is sent, so stop sending data to browser
98         exit;
99     }
100 }
101
102 # Already generated discharges
103 my @validated_discharges = Koha::Patron::Discharge::get_validated({
104     borrowernumber => $borrowernumber,
105 });
106
107 $template->param(
108     patron => $patron,
109     can_be_discharged => $can_be_discharged,
110     validated_discharges => \@validated_discharges,
111 );
112
113 $template->param( dischargeview => 1, );
114
115 output_html_with_http_headers $input, $cookie, $template->output;