Bug 35290: Sanitize field input on cataloguing/ysearch.pl
[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 qw( get_template_and_user );
34 use C4::Output qw( output_and_exit_if_error output_and_exit output_html_with_http_headers );
35 use C4::Form::MessagingPreferences;
36 use List::MoreUtils qw( uniq );
37 use Scalar::Util qw( looks_like_number );
38 use Koha::Patron::Attribute::Types;
39 use Koha::Patron::Restriction::Types;
40 use Koha::Patron::Messages;
41 use Koha::CsvProfiles;
42 use Koha::Holds;
43 use Koha::Patrons;
44 use Koha::Patron::Files;
45 use Koha::Token;
46 use Koha::Checkouts;
47
48 my $input = CGI->new;
49
50 my $print = $input->param('print');
51
52 my $template_name;
53
54 if (defined $print and $print eq "brief") {
55         $template_name = "members/moremember-brief.tt";
56 } else {
57         $template_name = "members/moremember.tt";
58 }
59
60 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
61     {
62         template_name   => $template_name,
63         query           => $input,
64         type            => "intranet",
65         flagsrequired   => { borrowers => 'edit_borrowers' },
66     }
67 );
68 my $borrowernumber = $input->param('borrowernumber');
69 my $error = $input->param('error');
70 $template->param( error => $error ) if ( $error );
71
72 my $patron         = Koha::Patrons->find( $borrowernumber );
73 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
74 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
75
76 my $category_type = $patron->category->category_type;
77
78 if ( $patron->borrowernumber eq C4::Context->preference("AnonymousPatron") ) {
79     $template->param( is_anonymous => 1 );
80 }
81
82 for (qw(gonenoaddress lost borrowernotes is_debarred)) {
83     $patron->$_ and $template->param(flagged => 1) and last;
84 }
85
86 $template->param(
87     restriction_types => scalar Koha::Patron::Restriction::Types->search()
88 );
89
90 if ( $patron->is_debarred ) {
91     $template->param(
92         'userdebarred'    => $patron->debarred,
93         'debarredcomment' => $patron->debarredcomment,
94     );
95
96     if ( $patron->debarred ne "9999-12-31" ) {
97         $template->param( 'userdebarreddate' => $patron->debarred );
98     }
99 }
100
101 $template->param( flagged => 1 ) if $patron->account_locked;
102
103 my @relatives;
104 my $guarantor_relationships = $patron->guarantor_relationships;
105 my @guarantees              = $patron->guarantee_relationships->guarantees->as_list;
106 my @guarantors              = $guarantor_relationships->guarantors->as_list;
107 if (@guarantors) {
108     push( @relatives, $_->id ) for @guarantors;
109     push( @relatives, $_->id ) for $patron->siblings->as_list;
110 }
111 else {
112     push( @relatives, $_->id ) for @guarantees;
113 }
114 $template->param(
115     guarantor_relationships => $guarantor_relationships,
116     guarantees              => \@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
129 # Generate CSRF token for upload and delete image buttons
130 $template->param(
131     csrf_token => Koha::Token->new->generate_csrf({ session_id => $input->cookie('CGISESSID'),}),
132 );
133
134 if (C4::Context->preference('ExtendedPatronAttributes')) {
135     my @attributes = $patron->extended_attributes->as_list; # FIXME Must be improved!
136     my @classes = uniq( map {$_->type->class} @attributes );
137     @classes = sort @classes;
138
139     my @attributes_loop;
140     for my $class (@classes) {
141         my @items;
142         for my $attr (@attributes) {
143             push @items, $attr if $attr->type->class eq $class
144         }
145         my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
146         my $lib = $av->count ? $av->next->lib : $class;
147
148         push @attributes_loop, {
149             class => $class,
150             items => \@items,
151             lib   => $lib,
152         };
153     }
154
155     $template->param(
156         attributes_loop => \@attributes_loop
157     );
158
159     my $library_id = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
160     my $nb_of_attribute_types = Koha::Patron::Attribute::Types->search_with_library_limits({}, {}, $library_id)->count;
161     if ( $nb_of_attribute_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 my $holds = Koha::Holds->search( { borrowernumber => $borrowernumber } ); # FIXME must be Koha::Patron->holds
203 my $waiting_holds = $holds->waiting;
204 $template->param(
205     holds_count  => $holds->count(),
206     WaitingHolds => $waiting_holds,
207 );
208
209 if ( C4::Context->preference('UseRecalls') ) {
210     my $waiting_recalls = $patron->recalls->search({ status => 'waiting' });
211     $template->param( waiting_recalls => $waiting_recalls );
212 }
213
214 my $no_issues_charge_guarantees = C4::Context->preference("NoIssuesChargeGuarantees");
215 $no_issues_charge_guarantees = undef unless looks_like_number( $no_issues_charge_guarantees );
216 if ( defined $no_issues_charge_guarantees ) {
217     my $guarantees_non_issues_charges = 0;
218     my $guarantees = $patron->guarantee_relationships->guarantees;
219     while ( my $g = $guarantees->next ) {
220         $guarantees_non_issues_charges += $g->account->non_issues_charges;
221     }
222     if ( $guarantees_non_issues_charges > $no_issues_charge_guarantees ) {
223         $template->param(
224             charges_guarantees    => 1,
225             chargesamount_guarantees => $guarantees_non_issues_charges,
226         );
227     }
228 }
229
230 if ( $patron->has_overdues ) {
231     $template->param( odues => 1 );
232 }
233 my $issues = $patron->checkouts;
234
235 my $balance = 0;
236 $balance = $patron->account->balance;
237
238 my $account = $patron->account;
239 if( ( my $owing = $account->non_issues_charges ) > 0 ) {
240     my $noissuescharge = C4::Context->preference("noissuescharge") || 5; # FIXME If noissuescharge == 0 then 5, why??
241     $template->param(
242         charges => 1,
243         chargesamount => $owing,
244     )
245 } elsif ( $balance < 0 ) {
246     $template->param(
247         credits => 1,
248         creditsamount => -$balance,
249     );
250 }
251
252 # if the expiry date is before today ie they have expired
253 if ( $patron->is_expired ) {
254     #borrowercard expired, no issues
255     $template->param(
256         expired => "1",
257     );
258 }
259 # check for NotifyBorrowerDeparture
260 elsif ( $patron->is_going_to_expire ) {
261     # borrower card soon to expire warn librarian
262     $template->param( "warndeparture" => $patron->dateexpiry ,
263                     );
264     if (C4::Context->preference('ReturnBeforeExpiry')){
265         $template->param("returnbeforeexpiry" => 1);
266     }
267 }
268
269
270 my $has_modifications = Koha::Patron::Modifications->search( { borrowernumber => $borrowernumber } )->count;
271
272 $template->param(
273     patron          => $patron,
274     issuecount      => $patron->checkouts->count,
275     holds_count     => $patron->holds->count,
276     fines           => $patron->account->balance,
277     translated_language => $translated_language,
278     detailview      => 1,
279     was_renewed     => scalar $input->param('was_renewed') ? 1 : 0,
280     $category_type  => 1, # [% IF ( I ) %] = institutional/organisation
281     housebound_role => scalar $patron->housebound_role,
282     relatives_issues_count => $relatives_issues_count,
283     relatives_borrowernumbers => \@relatives,
284     logged_in_user => $logged_in_user,
285     files => Koha::Patron::Files->new( borrowernumber => $borrowernumber ) ->GetFilesInfo(),
286     has_modifications         => $has_modifications,
287 );
288
289 if ( C4::Context->preference('UseRecalls') ) {
290     $template->param(
291         recalls         => $patron->recalls({},{ order_by => { -asc => 'recalldate' } })->filter_by_current,
292         specific_patron => 1,
293     );
294 }
295
296 output_html_with_http_headers $input, $cookie, $template->output;