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::Restriction::Types;
40 use Koha::Patron::Messages;
41 use Koha::CsvProfiles;
44 use Koha::Patron::Files;
50 my $print = $input->param('print');
54 if (defined $print and $print eq "brief") {
55 $template_name = "members/moremember-brief.tt";
57 $template_name = "members/moremember.tt";
60 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
62 template_name => $template_name,
65 flagsrequired => { borrowers => ['edit_borrowers', 'list_borrowers'] },
68 my $borrowernumber = $input->param('borrowernumber');
69 my $error = $input->param('error');
70 $template->param( error => $error ) if ( $error );
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 } );
76 my $category_type = $patron->category->category_type;
78 if ( $patron->borrowernumber eq C4::Context->preference("AnonymousPatron") ) {
79 $template->param( is_anonymous => 1 );
82 for (qw(gonenoaddress lost borrowernotes is_debarred)) {
83 $patron->$_ and $template->param(flagged => 1) and last;
87 restriction_types => scalar Koha::Patron::Restriction::Types->search()
90 if ( $patron->is_debarred ) {
92 'userdebarred' => $patron->debarred,
93 'debarredcomment' => $patron->debarredcomment,
94 'debarredsince' => $patron->restrictions->search()->single->created,
97 if ( $patron->debarred ne "9999-12-31" ) {
98 $template->param( 'userdebarreddate' => $patron->debarred );
102 $template->param( flagged => 1 ) if $patron->account_locked;
105 my $guarantor_relationships = $patron->guarantor_relationships;
106 my @guarantees = $patron->guarantee_relationships->guarantees->as_list;
107 my @guarantors = $guarantor_relationships->guarantors->as_list;
109 push( @relatives, $_->id ) for @guarantors;
110 push( @relatives, $_->id ) for $patron->siblings->as_list;
113 push( @relatives, $_->id ) for @guarantees;
116 guarantor_relationships => $guarantor_relationships,
117 guarantees => \@guarantees,
120 my $relatives_issues_count =
121 Koha::Checkouts->count({ borrowernumber => \@relatives });
124 my $total_amount = $patron->relationships_debt({ include_guarantors => 0, only_this_guarantor => 1, include_this_patron => 1 });
125 $template->param( guarantees_fines => $total_amount );
128 # Calculate and display patron's age
129 if ( !$patron->is_valid_age ) {
130 $template->param( age_limitations => 1 );
131 $template->param( age_low => $patron->category->dateofbirthrequired );
132 $template->param( age_high => $patron->category->upperagelimit );
135 if (C4::Context->preference('ExtendedPatronAttributes')) {
136 my @attributes = $patron->extended_attributes->as_list; # FIXME Must be improved!
137 my @classes = uniq( map {$_->type->class} @attributes );
138 @classes = sort @classes;
141 for my $class (@classes) {
143 for my $attr (@attributes) {
144 push @items, $attr if $attr->type->class eq $class
146 my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
147 my $lib = $av->count ? $av->next->lib : $class;
149 push @attributes_loop, {
157 attributes_loop => \@attributes_loop
160 my $library_id = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
161 my $nb_of_attribute_types = Koha::Patron::Attribute::Types->search_with_library_limits({}, {}, $library_id)->count;
162 if ( $nb_of_attribute_types == 0 ) {
163 $template->param(no_patron_attribute_types => 1);
167 if (C4::Context->preference('EnhancedMessagingPreferences')) {
168 C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
169 $template->param(messaging_form_inactive => 1);
172 if ( C4::Context->preference("ExportCircHistory") ) {
173 $template->param(csv_profiles => Koha::CsvProfiles->search({ type => 'marc' }));
176 my $patron_messages = Koha::Patron::Messages->search(
178 'me.borrowernumber' => $patron->borrowernumber,
182 '+select' => ['manager.surname', 'manager.firstname' ],
183 '+as' => ['manager_surname', 'manager_firstname'],
187 if( $patron_messages->count > 0 ){
188 $template->param( patron_messages => $patron_messages );
191 # Display the language description instead of the code
192 # Note that this is certainly wrong
193 my ( $subtag, $region ) = split '-', $patron->lang;
194 my $translated_language = C4::Languages::language_get_description( $subtag, $subtag, 'language' );
196 # if the expiry date is before today ie they have expired
197 if ( $patron->is_expired || $patron->is_going_to_expire ) {
203 my $holds = Koha::Holds->search( { borrowernumber => $borrowernumber } ); # FIXME must be Koha::Patron->holds
204 my $waiting_holds = $holds->waiting;
206 holds_count => $holds->count(),
207 WaitingHolds => $waiting_holds,
210 if ( C4::Context->preference('UseRecalls') ) {
211 my $waiting_recalls = $patron->recalls->search({ status => 'waiting' });
212 $template->param( waiting_recalls => $waiting_recalls );
215 my $no_issues_charge_guarantees = C4::Context->preference("NoIssuesChargeGuarantees");
216 $no_issues_charge_guarantees = undef unless looks_like_number( $no_issues_charge_guarantees );
217 if ( defined $no_issues_charge_guarantees ) {
218 my $guarantees_non_issues_charges = 0;
219 my $guarantees = $patron->guarantee_relationships->guarantees;
220 while ( my $g = $guarantees->next ) {
221 $guarantees_non_issues_charges += $g->account->non_issues_charges;
223 if ( $guarantees_non_issues_charges > $no_issues_charge_guarantees ) {
225 charges_guarantees => 1,
226 chargesamount_guarantees => $guarantees_non_issues_charges,
231 if ( $patron->has_overdues ) {
232 $template->param( odues => 1 );
234 my $issues = $patron->checkouts;
237 $balance = $patron->account->balance;
239 my $account = $patron->account;
240 if( ( my $owing = $account->non_issues_charges ) > 0 ) {
241 my $noissuescharge = C4::Context->preference("noissuescharge") || 5; # FIXME If noissuescharge == 0 then 5, why??
244 chargesamount => $owing,
246 } elsif ( $balance < 0 ) {
249 creditsamount => -$balance,
253 # Check the debt of this patrons guarantors *and* the guarantees of those guarantors
254 my $no_issues_charge_guarantors = C4::Context->preference("NoIssuesChargeGuarantorsWithGuarantees");
255 if ( $no_issues_charge_guarantors ) {
256 my $guarantors_non_issues_charges = $patron->relationships_debt({ include_guarantors => 1, only_this_guarantor => 0, include_this_patron => 1 });
258 if ( $guarantors_non_issues_charges > $no_issues_charge_guarantors ) {
261 charges_guarantors_guarantees => $guarantors_non_issues_charges
266 # if the expiry date is before today ie they have expired
267 if ( $patron->is_expired ) {
268 #borrowercard expired, no issues
273 # check for NotifyBorrowerDeparture
274 elsif ( $patron->is_going_to_expire ) {
275 # borrower card soon to expire warn librarian
276 $template->param( "warndeparture" => $patron->dateexpiry ,
278 if (C4::Context->preference('ReturnBeforeExpiry')){
279 $template->param("returnbeforeexpiry" => 1);
284 my $has_modifications = Koha::Patron::Modifications->search( { borrowernumber => $borrowernumber } )->count;
285 my $patron_lists_count = $patron->get_lists_with_patron->count();
289 issuecount => $patron->checkouts->count,
290 holds_count => $patron->holds->count,
291 fines => $patron->account->balance,
292 translated_language => $translated_language,
294 was_renewed => scalar $input->param('was_renewed') ? 1 : 0,
295 $category_type => 1, # [% IF ( I ) %] = institutional/organisation
296 housebound_role => scalar $patron->housebound_role,
297 relatives_issues_count => $relatives_issues_count,
298 relatives_borrowernumbers => \@relatives,
299 logged_in_user => $logged_in_user,
300 files => Koha::Patron::Files->new( borrowernumber => $borrowernumber ) ->GetFilesInfo(),
301 has_modifications => $has_modifications,
302 patron_lists_count => $patron_lists_count,
305 if ( C4::Context->preference('UseRecalls') ) {
307 recalls => $patron->recalls({},{ order_by => { -asc => 'recalldate' } })->filter_by_current,
308 specific_patron => 1,
312 output_html_with_http_headers $input, $cookie, $template->output;