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