Bug 36482: Add embed tests
[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::Categories;
41 use Koha::Patron::Messages;
42 use Koha::CsvProfiles;
43 use Koha::Holds;
44 use Koha::Patrons;
45 use Koha::Patron::Files;
46 use Koha::Token;
47 use Koha::Checkouts;
48
49 my $input = CGI->new;
50
51 my $print = $input->param('print');
52
53 my $template_name;
54
55 if (defined $print and $print eq "brief") {
56         $template_name = "members/moremember-brief.tt";
57 } else {
58         $template_name = "members/moremember.tt";
59 }
60
61 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
62     {
63         template_name   => $template_name,
64         query           => $input,
65         type            => "intranet",
66         flagsrequired   => { borrowers => ['edit_borrowers', 'list_borrowers'] },
67     }
68 );
69 my $borrowernumber = $input->param('borrowernumber');
70 my $error = $input->param('error');
71 $template->param( error => $error ) if ( $error );
72
73 my $patron         = Koha::Patrons->find( $borrowernumber );
74 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
75 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
76
77 my $category_type = $patron->category->category_type;
78
79 if ( $patron->borrowernumber eq C4::Context->preference("AnonymousPatron") ) {
80     $template->param( is_anonymous => 1 );
81 }
82
83 for (qw(gonenoaddress lost borrowernotes is_debarred)) {
84     $patron->$_ and $template->param(flagged => 1) and last;
85 }
86
87 $template->param(
88     restriction_types => scalar Koha::Patron::Restriction::Types->search()
89 );
90
91 if ( $patron->is_debarred ) {
92     $template->param(
93         'userdebarred'    => $patron->debarred,
94         'debarredcomment' => $patron->debarredcomment,
95         'debarredsince'   => $patron->restrictions->search()->single->created,
96     );
97
98     if ( $patron->debarred ne "9999-12-31" ) {
99         $template->param( 'userdebarreddate' => $patron->debarred );
100     }
101 }
102
103 $template->param( flagged => 1 ) if $patron->account_locked;
104
105 my @relatives;
106 my $guarantor_relationships = $patron->guarantor_relationships;
107 my @guarantees              = $patron->guarantee_relationships->guarantees->as_list;
108 my @guarantors              = $guarantor_relationships->guarantors->as_list;
109 if (@guarantors) {
110     push( @relatives, $_->id ) for @guarantors;
111     push( @relatives, $_->id ) for $patron->siblings->as_list;
112 }
113 else {
114     push( @relatives, $_->id ) for @guarantees;
115 }
116 $template->param(
117     guarantor_relationships => $guarantor_relationships,
118     guarantees              => \@guarantees,
119 );
120
121 my $relatives_issues_count =
122     Koha::Checkouts->count({ borrowernumber => \@relatives });
123
124 if ( @guarantees ) {
125     my $total_amount = $patron->relationships_debt({ include_guarantors => 0, only_this_guarantor => 1, include_this_patron => 1 });
126     $template->param( guarantees_fines => $total_amount );
127 }
128
129 # Calculate and display patron's age
130 if ( !$patron->is_valid_age ) {
131     $template->param( age_limitations => 1 );
132     $template->param( age_low => $patron->category->dateofbirthrequired );
133     $template->param( age_high => $patron->category->upperagelimit );
134 }
135
136 # Generate CSRF token for upload and delete image buttons
137 $template->param(
138     csrf_token => Koha::Token->new->generate_csrf({ session_id => $input->cookie('CGISESSID'),}),
139 );
140
141 unless ( Koha::Patron::Categories->search_with_library_limits( { 'me.categorycode' => $patron->categorycode } )->count )
142 {
143     $template->param( limited_category => 1 );
144 }
145
146 if (C4::Context->preference('ExtendedPatronAttributes')) {
147     my @attributes = $patron->extended_attributes->as_list; # FIXME Must be improved!
148     my @classes = uniq( map {$_->type->class} @attributes );
149     @classes = sort @classes;
150
151     my @attributes_loop;
152     for my $class (@classes) {
153         my @items;
154         for my $attr (@attributes) {
155             push @items, $attr if $attr->type->class eq $class
156         }
157         my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
158         my $lib = $av->count ? $av->next->lib : $class;
159
160         push @attributes_loop, {
161             class => $class,
162             items => \@items,
163             lib   => $lib,
164         };
165     }
166
167     $template->param(
168         attributes_loop => \@attributes_loop
169     );
170
171     my $library_id = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
172     my $nb_of_attribute_types = Koha::Patron::Attribute::Types->search_with_library_limits({}, {}, $library_id)->count;
173     if ( $nb_of_attribute_types == 0 ) {
174         $template->param(no_patron_attribute_types => 1);
175     }
176 }
177
178 if (C4::Context->preference('EnhancedMessagingPreferences')) {
179     C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
180     $template->param(messaging_form_inactive => 1);
181 }
182
183 if ( C4::Context->preference("ExportCircHistory") ) {
184     $template->param(csv_profiles => Koha::CsvProfiles->search({ type => 'marc' }));
185 }
186
187 my $patron_messages = Koha::Patron::Messages->search(
188     {
189         'me.borrowernumber' => $patron->borrowernumber,
190     },
191     {
192         join => 'manager',
193         '+select' => ['manager.surname', 'manager.firstname' ],
194         '+as' => ['manager_surname', 'manager_firstname'],
195     }
196 );
197
198 if( $patron_messages->count > 0 ){
199     $template->param( patron_messages => $patron_messages );
200 }
201
202 # Display the language description instead of the code
203 # Note that this is certainly wrong
204 my ( $subtag, $region ) = split '-', $patron->lang;
205 my $translated_language = C4::Languages::language_get_description( $subtag, $subtag, 'language' );
206
207 # if the expiry date is before today ie they have expired
208 if ( $patron->is_expired || $patron->is_going_to_expire ) {
209     $template->param(
210         flagged => 1
211     );
212 }
213
214 my $holds = Koha::Holds->search( { borrowernumber => $borrowernumber } ); # FIXME must be Koha::Patron->holds
215 my $waiting_holds = $holds->waiting;
216 $template->param(
217     holds_count  => $holds->count(),
218     WaitingHolds => $waiting_holds,
219 );
220
221 if ( C4::Context->preference('UseRecalls') ) {
222     my $waiting_recalls = $patron->recalls->search({ status => 'waiting' });
223     $template->param( waiting_recalls => $waiting_recalls );
224 }
225
226 my $no_issues_charge_guarantees = C4::Context->preference("NoIssuesChargeGuarantees");
227 $no_issues_charge_guarantees = undef unless looks_like_number( $no_issues_charge_guarantees );
228 if ( defined $no_issues_charge_guarantees ) {
229     my $guarantees_non_issues_charges = 0;
230     my $guarantees = $patron->guarantee_relationships->guarantees;
231     while ( my $g = $guarantees->next ) {
232         $guarantees_non_issues_charges += $g->account->non_issues_charges;
233     }
234     if ( $guarantees_non_issues_charges > $no_issues_charge_guarantees ) {
235         $template->param(
236             charges_guarantees    => 1,
237             chargesamount_guarantees => $guarantees_non_issues_charges,
238         );
239     }
240 }
241
242 if ( $patron->has_overdues ) {
243     $template->param( odues => 1 );
244 }
245 my $issues = $patron->checkouts;
246
247 my $balance = 0;
248 $balance = $patron->account->balance;
249
250 my $account = $patron->account;
251 if( ( my $owing = $account->non_issues_charges ) > 0 ) {
252     my $noissuescharge = C4::Context->preference("noissuescharge") || 5; # FIXME If noissuescharge == 0 then 5, why??
253     $template->param(
254         charges => 1,
255         chargesamount => $owing,
256     )
257 } elsif ( $balance < 0 ) {
258     $template->param(
259         credits => 1,
260         creditsamount => -$balance,
261     );
262 }
263
264 # Check the debt of this patrons guarantors *and* the guarantees of those guarantors
265 my $no_issues_charge_guarantors = C4::Context->preference("NoIssuesChargeGuarantorsWithGuarantees");
266 if ( $no_issues_charge_guarantors ) {
267     my $guarantors_non_issues_charges = $patron->relationships_debt({ include_guarantors => 1, only_this_guarantor => 0, include_this_patron => 1 });
268
269     if ( $guarantors_non_issues_charges > $no_issues_charge_guarantors ) {
270         $template->param(
271             noissues                      => 1,
272             charges_guarantors_guarantees => $guarantors_non_issues_charges
273         );
274     }
275 }
276
277 # if the expiry date is before today ie they have expired
278 if ( $patron->is_expired ) {
279     #borrowercard expired, no issues
280     $template->param(
281         expired => "1",
282     );
283 }
284 # check for NotifyBorrowerDeparture
285 elsif ( $patron->is_going_to_expire ) {
286     # borrower card soon to expire warn librarian
287     $template->param( "warndeparture" => $patron->dateexpiry ,
288                     );
289     if (C4::Context->preference('ReturnBeforeExpiry')){
290         $template->param("returnbeforeexpiry" => 1);
291     }
292 }
293
294
295 my $has_modifications = Koha::Patron::Modifications->search( { borrowernumber => $borrowernumber } )->count;
296 my $patron_lists_count = $patron->get_lists_with_patron->count();
297
298 $template->param(
299     patron          => $patron,
300     issuecount      => $patron->checkouts->count,
301     holds_count     => $patron->holds->count,
302     fines           => $patron->account->balance,
303     translated_language => $translated_language,
304     detailview      => 1,
305     was_renewed     => scalar $input->param('was_renewed') ? 1 : 0,
306     $category_type  => 1, # [% IF ( I ) %] = institutional/organisation
307     housebound_role => scalar $patron->housebound_role,
308     relatives_issues_count => $relatives_issues_count,
309     relatives_borrowernumbers => \@relatives,
310     logged_in_user => $logged_in_user,
311     files => Koha::Patron::Files->new( borrowernumber => $borrowernumber ) ->GetFilesInfo(),
312     has_modifications         => $has_modifications,
313     patron_lists_count => $patron_lists_count,
314 );
315
316 if ( C4::Context->preference('UseRecalls') ) {
317     $template->param(
318         recalls         => $patron->recalls({},{ order_by => { -asc => 'recalldate' } })->filter_by_current,
319         specific_patron => 1,
320     );
321 }
322
323 output_html_with_http_headers $input, $cookie, $template->output;