Bug 17128: Make summary-print.pl plack safe
[koha.git] / members / summary-print.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19
20 use CGI;
21
22 use C4::Auth;
23 use C4::Output;
24 use C4::Members;
25 use C4::Koha qw( getitemtypeinfo );
26 use C4::Circulation qw( GetIssuingCharges );
27
28 my $input          = CGI->new;
29 my $borrowernumber = $input->param('borrowernumber');
30
31 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
32     {
33         template_name   => "members/moremember-print.tt",
34         query           => $input,
35         type            => "intranet",
36         authnotrequired => 0,
37         flagsrequired   => { circulate => "circulate_remaining_permissions" },
38         debug           => 1,
39     }
40 );
41
42 my $data = GetMember( 'borrowernumber' => $borrowernumber );
43
44 my ( $total, $accts, $numaccts ) = GetMemberAccountRecords($borrowernumber);
45 foreach my $accountline (@$accts) {
46     if (   $accountline->{accounttype} ne 'F'
47         && $accountline->{accounttype} ne 'FU' )
48     {
49         $accountline->{printtitle} = 1;
50     }
51 }
52
53 our $totalprice = 0;
54
55 $template->param(
56     %$data,
57
58     borrowernumber => $borrowernumber,
59
60     accounts => $accts,
61     totaldue => $total,
62
63     issues     => build_issue_data( $borrowernumber ),
64     totalprice => $totalprice,
65 );
66
67 output_html_with_http_headers $input, $cookie, $template->output;
68
69 sub build_issue_data {
70     my ( $borrowernumber ) = @_;
71     my $issues = GetPendingIssues( $borrowernumber );
72
73     my $return = [];
74
75     my $today = DateTime->now( time_zone => C4::Context->tz );
76     $today->truncate( to => 'day' );
77
78     foreach my $issue ( @{$issues} ) {
79
80         my %row = %{$issue};
81         $totalprice += $issue->{replacementprice}
82             if ( $issue->{replacementprice} );
83
84         #find the charge for an item
85         my ( $charge, $itemtype ) =
86           GetIssuingCharges( $issue->{itemnumber}, $borrowernumber );
87
88         my $itemtypeinfo = getitemtypeinfo($itemtype);
89         $row{'itemtype_description'} = $itemtypeinfo->{description};
90
91         $row{'charge'} = sprintf( "%.2f", $charge );
92
93         $row{date_due} = $row{date_due_sql};
94
95         push( @{$return}, \%row );
96     }
97
98     @{$return} = sort { $a->{date_due} eq $b->{date_due} } @{$return};
99
100     return $return;
101 }