ab052ca2b324622467b87bbcf64204db73315c8e
[srvgit] / 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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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::DateUtils;
29 use Koha::Holds;
30 use Koha::ItemTypes;
31 use Koha::Patrons;
32
33 my $input          = CGI->new;
34 my $borrowernumber = $input->param('borrowernumber');
35
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
37     {
38         template_name   => "members/moremember-print.tt",
39         query           => $input,
40         type            => "intranet",
41         flagsrequired   => { circulate => "circulate_remaining_permissions" },
42     }
43 );
44
45 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
46 my $patron         = Koha::Patrons->find( $borrowernumber );
47 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
48
49 my $total = $patron->account->balance;
50 my $accts = Koha::Account::Lines->search(
51     { borrowernumber => $patron->borrowernumber, amountoutstanding => { '!=' => 0 } },
52     { order_by       => { -desc => 'accountlines_id' } }
53 );
54
55 our $totalprice = 0;
56
57 my $holds_rs = Koha::Holds->search(
58     { borrowernumber => $borrowernumber },
59 );
60
61 $template->param(
62     patron => $patron,
63
64     accounts => $accts,
65     totaldue => $total,
66
67     issues     => build_issue_data( $borrowernumber ),
68     totalprice => $totalprice,
69
70     reserves => build_reserve_data( $holds_rs ),
71 );
72
73 output_html_with_http_headers $input, $cookie, $template->output;
74
75 sub build_issue_data {
76     my ( $borrowernumber ) = @_;
77     my $patron = Koha::Patrons->find( $borrowernumber );
78     return unless $patron;
79
80     my $pending_checkouts = $patron->pending_checkouts->search( {},
81         { order_by => [ { -desc => 'date_due' }, { -asc => 'issue_id' } ] } );
82
83     my @checkouts;
84
85     while ( my $c = $pending_checkouts->next ) {
86         my $checkout = $c->unblessed_all_relateds;
87
88         $totalprice += $checkout->{replacementprice}
89             if $checkout->{replacementprice};
90
91         #find the charge for an item
92         my ( $charge, $itemtype ) =
93           GetIssuingCharges( $checkout->{itemnumber}, $borrowernumber );
94
95         $checkout->{charge} = $charge;
96
97         $checkout->{overdue} = $c->is_overdue;
98
99         push @checkouts, $checkout;
100     }
101
102     return \@checkouts;
103
104 }
105
106 sub build_reserve_data {
107     my $reserves = shift;
108
109     my $return = [];
110
111     my $today = dt_from_string();
112     $today->truncate( to => 'day' );
113
114     while ( my $reserve = $reserves->next() ) {
115
116         my $row = {
117             title          => $reserve->biblio()->title(),
118             author         => $reserve->biblio()->author(),
119             reservedate    => $reserve->reservedate(),
120             expirationdate => $reserve->expirationdate(),
121             waiting_at     => $reserve->branch()->branchname(),
122         };
123
124         push( @{$return}, $row );
125     }
126
127     @{$return} = sort { $a->{reservedate} <=> $b->{reservedate} } @{$return};
128
129     return $return;
130 }