Bug 12497: Fix search history non-accessible when OPAC was private
[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::Circulation qw( GetIssuingCharges );
26 use C4::Reserves;
27 use C4::Items;
28 use Koha::Holds;
29 use Koha::ItemTypes;
30 use Koha::Patrons;
31
32 my $input          = CGI->new;
33 my $borrowernumber = $input->param('borrowernumber');
34
35 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36     {
37         template_name   => "members/moremember-print.tt",
38         query           => $input,
39         type            => "intranet",
40         authnotrequired => 0,
41         flagsrequired   => { circulate => "circulate_remaining_permissions" },
42         debug           => 1,
43     }
44 );
45
46 my $patron = Koha::Patrons->find( $borrowernumber );
47 unless ( $patron ) {
48     print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
49     exit;
50 }
51 my $category = $patron->category;
52 my $data = $patron->unblessed;
53 $data->{description} = $category->description;
54 $data->{category_type} = $category->category_type;
55
56 my ( $total, $accts, $numaccts ) = GetMemberAccountRecords($borrowernumber);
57 foreach my $accountline (@$accts) {
58     if (   $accountline->{accounttype} ne 'F'
59         && $accountline->{accounttype} ne 'FU' )
60     {
61         $accountline->{printtitle} = 1;
62     }
63 }
64
65 our $totalprice = 0;
66
67 my $holds_rs = Koha::Holds->search(
68     { borrowernumber => $borrowernumber },
69 );
70
71 $template->param(
72     %$data,
73
74     borrowernumber => $borrowernumber,
75
76     accounts => $accts,
77     totaldue => $total,
78
79     issues     => build_issue_data( $borrowernumber ),
80     totalprice => $totalprice,
81
82     reserves => build_reserve_data( $holds_rs ),
83 );
84
85 output_html_with_http_headers $input, $cookie, $template->output;
86
87 sub build_issue_data {
88     my ( $borrowernumber ) = @_;
89     my $issues = GetPendingIssues( $borrowernumber );
90
91     my $return = [];
92
93     my $today = DateTime->now( time_zone => C4::Context->tz );
94     $today->truncate( to => 'day' );
95
96     foreach my $issue ( @{$issues} ) {
97
98         my %row = %{$issue};
99         $totalprice += $issue->{replacementprice}
100             if ( $issue->{replacementprice} );
101
102         #find the charge for an item
103         my ( $charge, $itemtype ) =
104           GetIssuingCharges( $issue->{itemnumber}, $borrowernumber );
105
106         $itemtype = Koha::ItemTypes->find( $itemtype );
107         $row{'itemtype_description'} = $itemtype->description; #FIXME Should not it be translated_description
108
109         $row{'charge'} = sprintf( "%.2f", $charge );
110
111         $row{date_due} = $row{date_due_sql};
112
113         push( @{$return}, \%row );
114     }
115
116     @{$return} = sort { $a->{date_due} eq $b->{date_due} } @{$return};
117
118     return $return;
119
120 }
121
122 sub build_reserve_data {
123     my $reserves = shift;
124
125     my $return = [];
126
127     my $today = DateTime->now( time_zone => C4::Context->tz );
128     $today->truncate( to => 'day' );
129
130     while ( my $reserve = $reserves->next() ) {
131
132         my $row = {
133             title          => $reserve->biblio()->title(),
134             author         => $reserve->biblio()->author(),
135             reservedate    => $reserve->reservedate(),
136             expirationdate => $reserve->expirationdate(),
137             waiting_at     => $reserve->branch()->branchname(),
138         };
139
140         push( @{$return}, $row );
141     }
142
143     @{$return} = sort { $a->{reservedate} <=> $b->{reservedate} } @{$return};
144
145     return $return;
146 }