Bug 8007: Compatibility with bug 11944
[koha-ffzg.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
32 use CGI qw( -utf8 );
33 use C4::Auth;
34 use C4::Output;
35 use C4::Members;
36 use C4::Reserves;
37 use C4::Letters;
38 use Koha::Borrower::Discharge;
39
40 use Koha::DateUtils;
41
42 my $input = new CGI;
43 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user({
44     template_name   => 'members/discharge.tt',
45     query           => $input,
46     type            => 'intranet',
47     authnotrequired => 0,
48     flagsrequired   => { 'borrowers' => '*' },
49 });
50
51 my $borrowernumber;
52 my $data;
53 if ( $input->param('borrowernumber') ) {
54     $borrowernumber = $input->param('borrowernumber');
55
56     # Getting member data
57     $data = GetMember( borrowernumber => $borrowernumber );
58
59     my $can_be_discharged = Koha::Borrower::Discharge::can_be_discharged({
60         borrowernumber => $borrowernumber
61     });
62
63     # Getting reserves
64     my @reserves    = GetReservesFromBorrowernumber($borrowernumber);
65     my $has_reserves = scalar(@reserves);
66
67     # Generating discharge if needed
68     if ( $input->param('discharge') and $can_be_discharged ) {
69         my $is_discharged = Koha::Borrower::Discharge::count({
70             borrowernumber => $borrowernumber,
71             validated      => 1,
72         });
73         unless ($is_discharged) {
74             Koha::Borrower::Discharge::discharge({
75                 borrowernumber => $borrowernumber
76             });
77         }
78         my $pdf_path = Koha::Borrower::Discharge::generate_as_pdf(
79             { borrowernumber => $borrowernumber, } );
80
81         binmode(STDOUT);
82         print $input->header(
83             -type       => 'application/pdf',
84             -charset    => 'utf-8',
85             -attachment => "discharge_$borrowernumber.pdf",
86         );
87         open my $fh, '<', $pdf_path;
88         my @lines = <$fh>;
89         close $fh;
90         print @lines;
91         exit;
92     }
93
94     $template->param(
95         borrowernumber    => $borrowernumber,
96         biblionumber      => $data->{'biblionumber'},
97         title             => $data->{'title'},
98         initials          => $data->{'initials'},
99         surname           => $data->{'surname'},
100         borrowernumber    => $borrowernumber,
101         firstname         => $data->{'firstname'},
102         cardnumber        => $data->{'cardnumber'},
103         categorycode      => $data->{'categorycode'},
104         category_type     => $data->{'category_type'},
105         categoryname      => $data->{'description'},
106         address           => $data->{'address'},
107         address2          => $data->{'address2'},
108         city              => $data->{'city'},
109         zipcode           => $data->{'zipcode'},
110         country           => $data->{'country'},
111         phone             => $data->{'phone'},
112         email             => $data->{'email'},
113         branchcode        => $data->{'branchcode'},
114         has_reserves      => $has_reserves,
115         can_be_discharged => $can_be_discharged,
116     );
117 }
118
119 $template->param( dischargeview => 1, );
120
121 output_html_with_http_headers $input, $cookie, $template->output;