Bug 18403: Add sub output_and_exit_if_error - unknown_patron & cannot_see_patron_infos
[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 $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
47 my $patron         = Koha::Patrons->find( $borrowernumber );
48 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
49
50 my $category = $patron->category;
51 my $data = $patron->unblessed;
52 $data->{description} = $category->description;
53 $data->{category_type} = $category->category_type;
54
55 my ( $total, $accts, $numaccts ) = GetMemberAccountRecords($borrowernumber);
56 foreach my $accountline (@$accts) {
57     if (   $accountline->{accounttype} ne 'F'
58         && $accountline->{accounttype} ne 'FU' )
59     {
60         $accountline->{printtitle} = 1;
61     }
62 }
63
64 our $totalprice = 0;
65
66 my $holds_rs = Koha::Holds->search(
67     { borrowernumber => $borrowernumber },
68 );
69
70 $template->param(
71     %$data,
72
73     borrowernumber => $borrowernumber,
74
75     accounts => $accts,
76     totaldue => $total,
77
78     issues     => build_issue_data( $borrowernumber ),
79     totalprice => $totalprice,
80
81     reserves => build_reserve_data( $holds_rs ),
82 );
83
84 output_html_with_http_headers $input, $cookie, $template->output;
85
86 sub build_issue_data {
87     my ( $borrowernumber ) = @_;
88     my $issues = GetPendingIssues( $borrowernumber );
89
90     my $return = [];
91
92     my $today = DateTime->now( time_zone => C4::Context->tz );
93     $today->truncate( to => 'day' );
94
95     foreach my $issue ( @{$issues} ) {
96
97         my %row = %{$issue};
98         $totalprice += $issue->{replacementprice}
99             if ( $issue->{replacementprice} );
100
101         #find the charge for an item
102         my ( $charge, $itemtype ) =
103           GetIssuingCharges( $issue->{itemnumber}, $borrowernumber );
104
105         $itemtype = Koha::ItemTypes->find( $itemtype );
106         $row{'itemtype_description'} = $itemtype->description; #FIXME Should not it be translated_description
107
108         $row{'charge'} = sprintf( "%.2f", $charge );
109
110         $row{date_due} = $row{date_due_sql};
111
112         push( @{$return}, \%row );
113     }
114
115     @{$return} = sort { $a->{date_due} eq $b->{date_due} } @{$return};
116
117     return $return;
118
119 }
120
121 sub build_reserve_data {
122     my $reserves = shift;
123
124     my $return = [];
125
126     my $today = DateTime->now( time_zone => C4::Context->tz );
127     $today->truncate( to => 'day' );
128
129     while ( my $reserve = $reserves->next() ) {
130
131         my $row = {
132             title          => $reserve->biblio()->title(),
133             author         => $reserve->biblio()->author(),
134             reservedate    => $reserve->reservedate(),
135             expirationdate => $reserve->expirationdate(),
136             waiting_at     => $reserve->branch()->branchname(),
137         };
138
139         push( @{$return}, $row );
140     }
141
142     @{$return} = sort { $a->{reservedate} <=> $b->{reservedate} } @{$return};
143
144     return $return;
145 }