3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 BibLibre
5 # Copyright 2014 ByWater Solutions
7 # This file is part of Koha.
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.
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.
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>.
25 script to do a borrower enquiry/bring up patron details etc
26 Displays all the details about a patron
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::Restriction::Types;
41 use Koha::Patron::Messages;
42 use Koha::CsvProfiles;
45 use Koha::Patron::Files;
51 my $print = $input->param('print');
55 if (defined $print and $print eq "brief") {
56 $template_name = "members/moremember-brief.tt";
58 $template_name = "members/moremember.tt";
61 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
63 template_name => $template_name,
66 flagsrequired => { borrowers => 'edit_borrowers' },
69 my $borrowernumber = $input->param('borrowernumber');
70 my $error = $input->param('error');
71 $template->param( error => $error ) if ( $error );
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 } );
77 my $category_type = $patron->category->category_type;
79 for (qw(gonenoaddress lost borrowernotes is_debarred)) {
80 $patron->$_ and $template->param(flagged => 1) and last;
84 restriction_types => scalar Koha::Patron::Restriction::Types->keyed_on_code()
87 if ( $patron->is_debarred ) {
89 'debarments' => scalar GetDebarments({ borrowernumber => $borrowernumber }),
90 'userdebarred' => $patron->debarred,
91 'debarredcomment' => $patron->debarredcomment,
94 if ( $patron->debarred ne "9999-12-31" ) {
95 $template->param( 'userdebarreddate' => $patron->debarred );
99 $template->param( flagged => 1 ) if $patron->account_locked;
102 my $guarantor_relationships = $patron->guarantor_relationships;
103 my @guarantees = $patron->guarantee_relationships->guarantees->as_list;
104 my @guarantors = $guarantor_relationships->guarantors->as_list;
106 push( @relatives, $_->id ) for @guarantors;
107 push( @relatives, $_->id ) for $patron->siblings->as_list;
110 push( @relatives, $_->id ) for @guarantees;
113 guarantor_relationships => $guarantor_relationships,
114 guarantees => \@guarantees,
117 my $relatives_issues_count =
118 Koha::Checkouts->count({ borrowernumber => \@relatives });
120 # Calculate and display patron's age
121 if ( !$patron->is_valid_age ) {
122 $template->param( age_limitations => 1 );
123 $template->param( age_low => $patron->category->dateofbirthrequired );
124 $template->param( age_high => $patron->category->upperagelimit );
127 # Generate CSRF token for upload and delete image buttons
129 csrf_token => Koha::Token->new->generate_csrf({ session_id => $input->cookie('CGISESSID'),}),
132 if (C4::Context->preference('ExtendedPatronAttributes')) {
133 my @attributes = $patron->extended_attributes->as_list; # FIXME Must be improved!
134 my @classes = uniq( map {$_->type->class} @attributes );
135 @classes = sort @classes;
138 for my $class (@classes) {
140 for my $attr (@attributes) {
141 push @items, $attr if $attr->type->class eq $class
143 my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
144 my $lib = $av->count ? $av->next->lib : $class;
146 push @attributes_loop, {
154 attributes_loop => \@attributes_loop
157 my $library_id = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
158 my $nb_of_attribute_types = Koha::Patron::Attribute::Types->search_with_library_limits({}, {}, $library_id)->count;
159 if ( $nb_of_attribute_types == 0 ) {
160 $template->param(no_patron_attribute_types => 1);
164 if (C4::Context->preference('EnhancedMessagingPreferences')) {
165 C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
166 $template->param(messaging_form_inactive => 1);
169 if ( C4::Context->preference("ExportCircHistory") ) {
170 $template->param(csv_profiles => Koha::CsvProfiles->search({ type => 'marc' }));
173 my $patron_messages = Koha::Patron::Messages->search(
175 'me.borrowernumber' => $patron->borrowernumber,
179 '+select' => ['manager.surname', 'manager.firstname' ],
180 '+as' => ['manager_surname', 'manager_firstname'],
184 if( $patron_messages->count > 0 ){
185 $template->param( patron_messages => $patron_messages );
188 # Display the language description instead of the code
189 # Note that this is certainly wrong
190 my ( $subtag, $region ) = split '-', $patron->lang;
191 my $translated_language = C4::Languages::language_get_description( $subtag, $subtag, 'language' );
193 # if the expiry date is before today ie they have expired
194 if ( $patron->is_expired || $patron->is_going_to_expire ) {
200 my $holds = Koha::Holds->search( { borrowernumber => $borrowernumber } ); # FIXME must be Koha::Patron->holds
201 my $waiting_holds = $holds->waiting;
203 holds_count => $holds->count(),
204 WaitingHolds => $waiting_holds,
207 if ( C4::Context->preference('UseRecalls') ) {
208 my $waiting_recalls = $patron->recalls->search({ status => 'waiting' });
209 $template->param( waiting_recalls => $waiting_recalls );
212 my $no_issues_charge_guarantees = C4::Context->preference("NoIssuesChargeGuarantees");
213 $no_issues_charge_guarantees = undef unless looks_like_number( $no_issues_charge_guarantees );
214 if ( defined $no_issues_charge_guarantees ) {
215 my $guarantees_non_issues_charges = 0;
216 my $guarantees = $patron->guarantee_relationships->guarantees;
217 while ( my $g = $guarantees->next ) {
218 $guarantees_non_issues_charges += $g->account->non_issues_charges;
220 if ( $guarantees_non_issues_charges > $no_issues_charge_guarantees ) {
222 charges_guarantees => 1,
223 chargesamount_guarantees => $guarantees_non_issues_charges,
228 if ( $patron->has_overdues ) {
229 $template->param( odues => 1 );
231 my $issues = $patron->checkouts;
234 $balance = $patron->account->balance;
236 my $account = $patron->account;
237 if( ( my $owing = $account->non_issues_charges ) > 0 ) {
238 my $noissuescharge = C4::Context->preference("noissuescharge") || 5; # FIXME If noissuescharge == 0 then 5, why??
241 chargesamount => $owing,
243 } elsif ( $balance < 0 ) {
246 creditsamount => -$balance,
250 # if the expiry date is before today ie they have expired
251 if ( $patron->is_expired ) {
252 #borrowercard expired, no issues
257 # check for NotifyBorrowerDeparture
258 elsif ( $patron->is_going_to_expire ) {
259 # borrower card soon to expire warn librarian
260 $template->param( "warndeparture" => $patron->dateexpiry ,
262 if (C4::Context->preference('ReturnBeforeExpiry')){
263 $template->param("returnbeforeexpiry" => 1);
268 my $has_modifications = Koha::Patron::Modifications->search( { borrowernumber => $borrowernumber } )->count;
272 issuecount => $patron->checkouts->count,
273 holds_count => $patron->holds->count,
274 fines => $patron->account->balance,
275 translated_language => $translated_language,
277 was_renewed => scalar $input->param('was_renewed') ? 1 : 0,
278 $category_type => 1, # [% IF ( I ) %] = institutional/organisation
279 housebound_role => scalar $patron->housebound_role,
280 relatives_issues_count => $relatives_issues_count,
281 relatives_borrowernumbers => \@relatives,
282 logged_in_user => $logged_in_user,
283 files => Koha::Patron::Files->new( borrowernumber => $borrowernumber ) ->GetFilesInfo(),
284 #debarments => scalar GetDebarments({ borrowernumber => $borrowernumber }),
285 has_modifications => $has_modifications,
288 if ( C4::Context->preference('UseRecalls') ) {
290 recalls => $patron->recalls({},{ order_by => { -asc => 'recalldate' } })->filter_by_current,
291 specific_patron => 1,
295 output_html_with_http_headers $input, $cookie, $template->output;