Bug 30477: Add new UNIMARC installer translation files
[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
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 qw( get_template_and_user );
23 use C4::Output qw( output_and_exit_if_error output_and_exit output_html_with_http_headers );
24 use C4::Members;
25 use C4::Circulation qw( GetIssuingCharges );
26 use C4::Reserves;
27 use Koha::DateUtils qw( dt_from_string );
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         flagsrequired   => { circulate => "circulate_remaining_permissions" },
41     }
42 );
43
44 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
45 my $patron         = Koha::Patrons->find( $borrowernumber );
46 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
47
48 my $total = $patron->account->balance;
49 my $accts = Koha::Account::Lines->search(
50     { borrowernumber => $patron->borrowernumber, amountoutstanding => { '!=' => 0 } },
51     { order_by       => { -desc => 'accountlines_id' } }
52 );
53
54 our $totalprice = 0;
55
56 my $holds_rs = Koha::Holds->search(
57     { borrowernumber => $borrowernumber },
58 );
59
60 $template->param(
61     patron => $patron,
62
63     accounts => $accts,
64     totaldue => $total,
65
66     issues     => build_issue_data( $borrowernumber ),
67     totalprice => $totalprice,
68
69     reserves => build_reserve_data( $holds_rs ),
70 );
71
72 output_html_with_http_headers $input, $cookie, $template->output;
73
74 sub build_issue_data {
75     my ( $borrowernumber ) = @_;
76     my $patron = Koha::Patrons->find( $borrowernumber );
77     return unless $patron;
78
79     my $pending_checkouts = $patron->pending_checkouts->search( {},
80         { order_by => [ { -desc => 'date_due' }, { -asc => 'issue_id' } ] } );
81
82     my @checkouts;
83
84     while ( my $c = $pending_checkouts->next ) {
85         my $checkout = $c->unblessed_all_relateds;
86
87         $totalprice += $checkout->{replacementprice}
88             if $checkout->{replacementprice};
89
90         #find the charge for an item
91         my ( $charge, $itemtype ) =
92           GetIssuingCharges( $checkout->{itemnumber}, $borrowernumber );
93
94         $checkout->{charge} = $charge;
95
96         $checkout->{overdue} = $c->is_overdue;
97
98         push @checkouts, $checkout;
99     }
100
101     return \@checkouts;
102
103 }
104
105 sub build_reserve_data {
106     my $reserves = shift;
107
108     my $return = [];
109
110     my $today = dt_from_string();
111     $today->truncate( to => 'day' );
112
113     while ( my $reserve = $reserves->next() ) {
114
115         my $row = {
116             title          => $reserve->biblio()->title(),
117             author         => $reserve->biblio()->author(),
118             reservedate    => $reserve->reservedate(),
119             expirationdate => $reserve->expirationdate(),
120             waiting_at     => $reserve->branch()->branchname(),
121         };
122
123         push( @{$return}, $row );
124     }
125
126     @{$return} = sort { $a->{reservedate} <=> $b->{reservedate} } @{$return};
127
128     return $return;
129 }