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