Bug 17492: (RM follow-up) Use more specific method name
[koha.git] / members / moremember.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
5 # Copyright 2014 ByWater Solutions
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22
23 =head1 moremember.pl
24
25  script to do a borrower enquiry/bring up patron details etc
26  Displays all the details about a patron
27
28 =cut
29
30 use Modern::Perl;
31 use CGI qw ( -utf8 );
32 use C4::Context;
33 use C4::Auth;
34 use C4::Output;
35 use C4::Members::AttributeTypes;
36 use C4::Form::MessagingPreferences;
37 use List::MoreUtils qw/uniq/;
38 use C4::Members::Attributes qw(GetBorrowerAttributes);
39 use Koha::Patron::Debarments qw(GetDebarments);
40 use Koha::Patron::Messages;
41 use Koha::DateUtils;
42 use Koha::CsvProfiles;
43 use Koha::Patrons;
44 use Koha::Token;
45 use Koha::Checkouts;
46
47 use vars qw($debug);
48
49 BEGIN {
50     $debug = $ENV{DEBUG} || 0;
51 }
52
53 my $input = CGI->new;
54 $debug or $debug = $input->param('debug') || 0;
55
56
57 my $print = $input->param('print');
58
59 my $template_name;
60
61 if (defined $print and $print eq "brief") {
62         $template_name = "members/moremember-brief.tt";
63 } else {
64         $template_name = "members/moremember.tt";
65 }
66
67 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
68     {
69         template_name   => $template_name,
70         query           => $input,
71         type            => "intranet",
72         authnotrequired => 0,
73         flagsrequired   => { borrowers => 'edit_borrowers' },
74         debug           => 1,
75     }
76 );
77 my $borrowernumber = $input->param('borrowernumber');
78 my $error = $input->param('error');
79 $template->param( error => $error ) if ( $error );
80
81 my $patron         = Koha::Patrons->find( $borrowernumber );
82 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
83 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
84
85 my $category_type = $patron->category->category_type;
86
87 for (qw(gonenoaddress lost borrowernotes is_debarred)) {
88     $patron->$_ and $template->param(flagged => 1) and last;
89 }
90
91 if ( $patron->is_debarred ) {
92     $template->param(
93         debarments => scalar GetDebarments({ borrowernumber => $borrowernumber }),
94     );
95     if ( $patron->debarred ne "9999-12-31" ) {
96         $template->param( 'userdebarreddate' => $patron->debarred );
97     }
98 }
99 $template->param( flagged => 1 ) if $patron->account_locked;
100
101 my @relatives;
102 if ( my $guarantor = $patron->guarantor ) {
103     $template->param( guarantor => $guarantor );
104     push @relatives, $guarantor->borrowernumber;
105     push @relatives, $_->borrowernumber for $patron->siblings;
106 } elsif ( $patron->contactname || $patron->contactfirstname ) {
107     $template->param(
108         guarantor => {
109             firstname => $patron->contactfirstname,
110             surname   => $patron->contactname,
111         }
112     );
113 } else {
114     my @guarantees = $patron->guarantees;
115     $template->param( guarantees => \@guarantees );
116     push @relatives, $_->borrowernumber for @guarantees;
117 }
118
119 my $relatives_issues_count =
120     Koha::Checkouts->count({ borrowernumber => \@relatives });
121
122 # Calculate and display patron's age
123 if ( !$patron->is_valid_age ) {
124     $template->param( age_limitations => 1 );
125     $template->param( age_low => $patron->category->dateofbirthrequired );
126     $template->param( age_high => $patron->category->upperagelimit );
127 }
128 $template->param( age => $patron->get_age );
129
130 # Generate CSRF token for upload and delete image buttons
131 $template->param(
132     csrf_token => Koha::Token->new->generate_csrf({ session_id => $input->cookie('CGISESSID'),}),
133 );
134
135 if (C4::Context->preference('ExtendedPatronAttributes')) {
136     my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
137     my @classes = uniq( map {$_->{class}} @$attributes );
138     @classes = sort @classes;
139
140     my @attributes_loop;
141     for my $class (@classes) {
142         my @items;
143         for my $attr (@$attributes) {
144             push @items, $attr if $attr->{class} eq $class
145         }
146         my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
147         my $lib = $av->count ? $av->next->lib : $class;
148
149         push @attributes_loop, {
150             class => $class,
151             items => \@items,
152             lib   => $lib,
153         };
154     }
155
156     $template->param(
157         attributes_loop => \@attributes_loop
158     );
159
160     my @types = C4::Members::AttributeTypes::GetAttributeTypes();
161     if (scalar(@types) == 0) {
162         $template->param(no_patron_attribute_types => 1);
163     }
164 }
165
166 if (C4::Context->preference('EnhancedMessagingPreferences')) {
167     C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
168     $template->param(messaging_form_inactive => 1);
169 }
170
171 if ( C4::Context->preference("ExportCircHistory") ) {
172     $template->param(csv_profiles => [ Koha::CsvProfiles->search({ type => 'marc' }) ]);
173 }
174
175 my $patron_messages = Koha::Patron::Messages->search(
176     {
177         'me.borrowernumber' => $patron->borrowernumber,
178     },
179     {
180         join => 'manager',
181         '+select' => ['manager.surname', 'manager.firstname' ],
182         '+as' => ['manager_surname', 'manager_firstname'],
183     }
184 );
185
186 if( $patron_messages->count > 0 ){
187     $template->param( patron_messages => $patron_messages );
188 }
189
190 # Display the language description instead of the code
191 # Note that this is certainly wrong
192 my ( $subtag, $region ) = split '-', $patron->lang;
193 my $translated_language = C4::Languages::language_get_description( $subtag, $subtag, 'language' );
194
195 # if the expiry date is before today ie they have expired
196 if ( $patron->is_expired || $patron->is_going_to_expire ) {
197     $template->param(
198         flagged => 1
199     );
200 }
201
202 $template->param(
203     patron          => $patron,
204     issuecount      => $patron->checkouts->count,
205     holds_count     => $patron->holds->count,
206     fines           => $patron->account->balance,
207     translated_language => $translated_language,
208     detailview      => 1,
209     was_renewed     => scalar $input->param('was_renewed') ? 1 : 0,
210     $category_type  => 1, # [% IF ( I ) %] = institutional/organisation
211     housebound_role => scalar $patron->housebound_role,
212     relatives_issues_count => $relatives_issues_count,
213     relatives_borrowernumbers => \@relatives,
214 );
215
216 output_html_with_http_headers $input, $cookie, $template->output;