Bug 21772: (QA follow-up) Fix setting variables to PROCESS in holds
[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 $total = $patron->account->balance;
51 my $accts = Koha::Account::Lines->search(
52     { borrowernumber => $patron->borrowernumber, amountoutstanding => { '!=' => 0 } },
53     { order_by       => { -desc => 'accountlines_id' } }
54 );
55
56 our $totalprice = 0;
57
58 my $holds_rs = Koha::Holds->search(
59     { borrowernumber => $borrowernumber },
60 );
61
62 $template->param(
63     patron => $patron,
64
65     accounts => $accts,
66     totaldue => $total,
67
68     issues     => build_issue_data( $borrowernumber ),
69     totalprice => $totalprice,
70
71     reserves => build_reserve_data( $holds_rs ),
72 );
73
74 output_html_with_http_headers $input, $cookie, $template->output;
75
76 sub build_issue_data {
77     my ( $borrowernumber ) = @_;
78     my $patron = Koha::Patrons->find( $borrowernumber );
79     return unless $patron;
80
81     my $pending_checkouts = $patron->pending_checkouts->search( {},
82         { order_by => [ { -desc => 'date_due' }, { -asc => 'issue_id' } ] } );
83
84     my @checkouts;
85
86     while ( my $c = $pending_checkouts->next ) {
87         my $checkout = $c->unblessed_all_relateds;
88
89         $totalprice += $checkout->{replacementprice}
90             if $checkout->{replacementprice};
91
92         #find the charge for an item
93         my ( $charge, $itemtype ) =
94           GetIssuingCharges( $checkout->{itemnumber}, $borrowernumber );
95
96         $itemtype = Koha::ItemTypes->find( $itemtype );
97         $checkout->{itemtype_description} = $itemtype->description; #FIXME Should not it be translated_description
98
99         $checkout->{charge} = $charge;
100
101         $checkout->{overdue} = $c->is_overdue;
102
103         push @checkouts, $checkout;
104     }
105
106     return \@checkouts;
107
108 }
109
110 sub build_reserve_data {
111     my $reserves = shift;
112
113     my $return = [];
114
115     my $today = DateTime->now( time_zone => C4::Context->tz );
116     $today->truncate( to => 'day' );
117
118     while ( my $reserve = $reserves->next() ) {
119
120         my $row = {
121             title          => $reserve->biblio()->title(),
122             author         => $reserve->biblio()->author(),
123             reservedate    => $reserve->reservedate(),
124             expirationdate => $reserve->expirationdate(),
125             waiting_at     => $reserve->branch()->branchname(),
126         };
127
128         push( @{$return}, $row );
129     }
130
131     @{$return} = sort { $a->{reservedate} <=> $b->{reservedate} } @{$return};
132
133     return $return;
134 }