Bug 32401: Remove x-koha-query support
[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 for (qw(gonenoaddress lost borrowernotes is_debarred)) {
79     $patron->$_ and $template->param(flagged => 1) and last;
80 }
81
82 $template->param(
83     restriction_types => scalar Koha::Patron::Restriction::Types->search()
84 );
85
86 if ( $patron->is_debarred ) {
87     $template->param(
88         'userdebarred'    => $patron->debarred,
89         'debarredcomment' => $patron->debarredcomment,
90     );
91
92     if ( $patron->debarred ne "9999-12-31" ) {
93         $template->param( 'userdebarreddate' => $patron->debarred );
94     }
95 }
96
97 $template->param( flagged => 1 ) if $patron->account_locked;
98
99 my @relatives;
100 my $guarantor_relationships = $patron->guarantor_relationships;
101 my @guarantees              = $patron->guarantee_relationships->guarantees->as_list;
102 my @guarantors              = $guarantor_relationships->guarantors->as_list;
103 if (@guarantors) {
104     push( @relatives, $_->id ) for @guarantors;
105     push( @relatives, $_->id ) for $patron->siblings->as_list;
106 }
107 else {
108     push( @relatives, $_->id ) for @guarantees;
109 }
110 $template->param(
111     guarantor_relationships => $guarantor_relationships,
112     guarantees              => \@guarantees,
113 );
114
115 my $relatives_issues_count =
116     Koha::Checkouts->count({ borrowernumber => \@relatives });
117
118 # Calculate and display patron's age
119 if ( !$patron->is_valid_age ) {
120     $template->param( age_limitations => 1 );
121     $template->param( age_low => $patron->category->dateofbirthrequired );
122     $template->param( age_high => $patron->category->upperagelimit );
123 }
124
125 # Generate CSRF token for upload and delete image buttons
126 $template->param(
127     csrf_token => Koha::Token->new->generate_csrf({ session_id => $input->cookie('CGISESSID'),}),
128 );
129
130 if (C4::Context->preference('ExtendedPatronAttributes')) {
131     my @attributes = $patron->extended_attributes->as_list; # FIXME Must be improved!
132     my @classes = uniq( map {$_->type->class} @attributes );
133     @classes = sort @classes;
134
135     my @attributes_loop;
136     for my $class (@classes) {
137         my @items;
138         for my $attr (@attributes) {
139             push @items, $attr if $attr->type->class eq $class
140         }
141         my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
142         my $lib = $av->count ? $av->next->lib : $class;
143
144         push @attributes_loop, {
145             class => $class,
146             items => \@items,
147             lib   => $lib,
148         };
149     }
150
151     $template->param(
152         attributes_loop => \@attributes_loop
153     );
154
155     my $library_id = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
156     my $nb_of_attribute_types = Koha::Patron::Attribute::Types->search_with_library_limits({}, {}, $library_id)->count;
157     if ( $nb_of_attribute_types == 0 ) {
158         $template->param(no_patron_attribute_types => 1);
159     }
160 }
161
162 if (C4::Context->preference('EnhancedMessagingPreferences')) {
163     C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
164     $template->param(messaging_form_inactive => 1);
165 }
166
167 if ( C4::Context->preference("ExportCircHistory") ) {
168     $template->param(csv_profiles => Koha::CsvProfiles->search({ type => 'marc' }));
169 }
170
171 my $patron_messages = Koha::Patron::Messages->search(
172     {
173         'me.borrowernumber' => $patron->borrowernumber,
174     },
175     {
176         join => 'manager',
177         '+select' => ['manager.surname', 'manager.firstname' ],
178         '+as' => ['manager_surname', 'manager_firstname'],
179     }
180 );
181
182 if( $patron_messages->count > 0 ){
183     $template->param( patron_messages => $patron_messages );
184 }
185
186 # Display the language description instead of the code
187 # Note that this is certainly wrong
188 my ( $subtag, $region ) = split '-', $patron->lang;
189 my $translated_language = C4::Languages::language_get_description( $subtag, $subtag, 'language' );
190
191 # if the expiry date is before today ie they have expired
192 if ( $patron->is_expired || $patron->is_going_to_expire ) {
193     $template->param(
194         flagged => 1
195     );
196 }
197
198 my $holds = Koha::Holds->search( { borrowernumber => $borrowernumber } ); # FIXME must be Koha::Patron->holds
199 my $waiting_holds = $holds->waiting;
200 $template->param(
201     holds_count  => $holds->count(),
202     WaitingHolds => $waiting_holds,
203 );
204
205 if ( C4::Context->preference('UseRecalls') ) {
206     my $waiting_recalls = $patron->recalls->search({ status => 'waiting' });
207     $template->param( waiting_recalls => $waiting_recalls );
208 }
209
210 my $no_issues_charge_guarantees = C4::Context->preference("NoIssuesChargeGuarantees");
211 $no_issues_charge_guarantees = undef unless looks_like_number( $no_issues_charge_guarantees );
212 if ( defined $no_issues_charge_guarantees ) {
213     my $guarantees_non_issues_charges = 0;
214     my $guarantees = $patron->guarantee_relationships->guarantees;
215     while ( my $g = $guarantees->next ) {
216         $guarantees_non_issues_charges += $g->account->non_issues_charges;
217     }
218     if ( $guarantees_non_issues_charges > $no_issues_charge_guarantees ) {
219         $template->param(
220             charges_guarantees    => 1,
221             chargesamount_guarantees => $guarantees_non_issues_charges,
222         );
223     }
224 }
225
226 if ( $patron->has_overdues ) {
227     $template->param( odues => 1 );
228 }
229 my $issues = $patron->checkouts;
230
231 my $balance = 0;
232 $balance = $patron->account->balance;
233
234 my $account = $patron->account;
235 if( ( my $owing = $account->non_issues_charges ) > 0 ) {
236     my $noissuescharge = C4::Context->preference("noissuescharge") || 5; # FIXME If noissuescharge == 0 then 5, why??
237     $template->param(
238         charges => 1,
239         chargesamount => $owing,
240     )
241 } elsif ( $balance < 0 ) {
242     $template->param(
243         credits => 1,
244         creditsamount => -$balance,
245     );
246 }
247
248 # if the expiry date is before today ie they have expired
249 if ( $patron->is_expired ) {
250     #borrowercard expired, no issues
251     $template->param(
252         expired => "1",
253     );
254 }
255 # check for NotifyBorrowerDeparture
256 elsif ( $patron->is_going_to_expire ) {
257     # borrower card soon to expire warn librarian
258     $template->param( "warndeparture" => $patron->dateexpiry ,
259                     );
260     if (C4::Context->preference('ReturnBeforeExpiry')){
261         $template->param("returnbeforeexpiry" => 1);
262     }
263 }
264
265
266 my $has_modifications = Koha::Patron::Modifications->search( { borrowernumber => $borrowernumber } )->count;
267
268 $template->param(
269     patron          => $patron,
270     issuecount      => $patron->checkouts->count,
271     holds_count     => $patron->holds->count,
272     fines           => $patron->account->balance,
273     translated_language => $translated_language,
274     detailview      => 1,
275     was_renewed     => scalar $input->param('was_renewed') ? 1 : 0,
276     $category_type  => 1, # [% IF ( I ) %] = institutional/organisation
277     housebound_role => scalar $patron->housebound_role,
278     relatives_issues_count => $relatives_issues_count,
279     relatives_borrowernumbers => \@relatives,
280     logged_in_user => $logged_in_user,
281     files => Koha::Patron::Files->new( borrowernumber => $borrowernumber ) ->GetFilesInfo(),
282     has_modifications         => $has_modifications,
283 );
284
285 if ( C4::Context->preference('UseRecalls') ) {
286     $template->param(
287         recalls         => $patron->recalls({},{ order_by => { -asc => 'recalldate' } })->filter_by_current,
288         specific_patron => 1,
289     );
290 }
291
292 output_html_with_http_headers $input, $cookie, $template->output;