3 # Copyright ByWater Solutions 2014
4 # Copyright PTFS Europe 2016
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 3 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 use Koha
::Old
::Checkouts
;
32 use Koha
::Patron
::Categories
;
33 use Koha
::Patron
::HouseboundProfile
;
34 use Koha
::Patron
::HouseboundRole
;
35 use Koha
::Patron
::Images
;
37 use Koha
::Virtualshelves
;
38 use Koha
::Club
::Enrollments
;
41 use base
qw(Koha::Object);
45 Koha::Patron - Koha Patron Object class
57 Delete patron's holds, lists and finally the patron.
59 Lists owned by the borrower are deleted, but entries from the borrower to
68 $self->_result->result_source->schema->txn_do(
70 # Delete Patron's holds
73 # Delete all lists and all shares of this borrower
74 # Consistent with the approach Koha uses on deleting individual lists
75 # Note that entries in virtualshelfcontents added by this borrower to
76 # lists of others will be handled by a table constraint: the borrower
77 # is set to NULL in those entries.
79 # We could handle the above deletes via a constraint too.
80 # But a new BZ report 11889 has been opened to discuss another approach.
81 # Instead of deleting we could also disown lists (based on a pref).
82 # In that way we could save shared and public lists.
83 # The current table constraints support that idea now.
84 # This pref should then govern the results of other routines/methods such as
85 # Koha::Virtualshelf->new->delete too.
86 # FIXME Could be $patron->get_lists
87 $_->delete for Koha
::Virtualshelves
->search( { owner
=> $self->borrowernumber } );
89 $deleted = $self->SUPER::delete;
91 logaction
( "MEMBERS", "DELETE", $self->borrowernumber, "" ) if C4
::Context
->preference("BorrowersLog");
100 my $patron_category = $patron->category
102 Return the patron category for this patron
108 return Koha
::Patron
::Category
->_new_from_dbic( $self->_result->categorycode );
113 Returns a Koha::Patron object for this patron's guarantor
120 return unless $self->guarantorid();
122 return Koha
::Patrons
->find( $self->guarantorid() );
128 return Koha
::Patron
::Images
->find( $self->borrowernumber );
133 return Koha
::Library
->_new_from_dbic($self->_result->branchcode);
138 Returns the guarantees (list of Koha::Patron) of this patron
145 return Koha
::Patrons
->search( { guarantorid
=> $self->borrowernumber } );
148 =head3 housebound_profile
150 Returns the HouseboundProfile associated with this patron.
154 sub housebound_profile
{
156 my $profile = $self->_result->housebound_profile;
157 return Koha
::Patron
::HouseboundProfile
->_new_from_dbic($profile)
162 =head3 housebound_role
164 Returns the HouseboundRole associated with this patron.
168 sub housebound_role
{
171 my $role = $self->_result->housebound_role;
172 return Koha
::Patron
::HouseboundRole
->_new_from_dbic($role) if ( $role );
178 Returns the siblings of this patron.
185 my $guarantor = $self->guarantor;
187 return unless $guarantor;
189 return Koha
::Patrons
->search(
193 '=' => $guarantor->id,
196 '!=' => $self->borrowernumber,
202 =head3 wants_check_for_previous_checkout
204 $wants_check = $patron->wants_check_for_previous_checkout;
206 Return 1 if Koha needs to perform PrevIssue checking, else 0.
210 sub wants_check_for_previous_checkout
{
212 my $syspref = C4
::Context
->preference("checkPrevCheckout");
215 ## Hard syspref trumps all
216 return 1 if ($syspref eq 'hardyes');
217 return 0 if ($syspref eq 'hardno');
218 ## Now, patron pref trumps all
219 return 1 if ($self->checkprevcheckout eq 'yes');
220 return 0 if ($self->checkprevcheckout eq 'no');
222 # More complex: patron inherits -> determine category preference
223 my $checkPrevCheckoutByCat = $self->category->checkprevcheckout;
224 return 1 if ($checkPrevCheckoutByCat eq 'yes');
225 return 0 if ($checkPrevCheckoutByCat eq 'no');
227 # Finally: category preference is inherit, default to 0
228 if ($syspref eq 'softyes') {
235 =head3 do_check_for_previous_checkout
237 $do_check = $patron->do_check_for_previous_checkout($item);
239 Return 1 if the bib associated with $ITEM has previously been checked out to
240 $PATRON, 0 otherwise.
244 sub do_check_for_previous_checkout
{
245 my ( $self, $item ) = @_;
247 # Find all items for bib and extract item numbers.
248 my @items = Koha
::Items
->search({biblionumber
=> $item->{biblionumber
}});
250 foreach my $item (@items) {
251 push @item_nos, $item->itemnumber;
254 # Create (old)issues search criteria
256 borrowernumber
=> $self->borrowernumber,
257 itemnumber
=> \
@item_nos,
260 # Check current issues table
261 my $issues = Koha
::Checkouts
->search($criteria);
262 return 1 if $issues->count; # 0 || N
264 # Check old issues table
265 my $old_issues = Koha
::Old
::Checkouts
->search($criteria);
266 return $old_issues->count; # 0 || N
271 my $debarment_expiration = $patron->is_debarred;
273 Returns the date a patron debarment will expire, or undef if the patron is not
281 return unless $self->debarred;
282 return $self->debarred
283 if $self->debarred =~ '^9999'
284 or dt_from_string
( $self->debarred ) > dt_from_string
;
290 my $is_expired = $patron->is_expired;
292 Returns 1 if the patron is expired or 0;
298 return 0 unless $self->dateexpiry;
299 return 0 if $self->dateexpiry eq '0000-00-00';
300 return 1 if dt_from_string
( $self->dateexpiry ) < dt_from_string
->truncate( to
=> 'day' );
304 =head3 is_going_to_expire
306 my $is_going_to_expire = $patron->is_going_to_expire;
308 Returns 1 if the patron is going to expired, depending on the NotifyBorrowerDeparture pref or 0
312 sub is_going_to_expire
{
315 my $delay = C4
::Context
->preference('NotifyBorrowerDeparture') || 0;
317 return 0 unless $delay;
318 return 0 unless $self->dateexpiry;
319 return 0 if $self->dateexpiry eq '0000-00-00';
320 return 1 if dt_from_string
( $self->dateexpiry )->subtract( days
=> $delay ) < dt_from_string
->truncate( to
=> 'day' );
324 =head3 update_password
326 my $updated = $patron->update_password( $userid, $password );
328 Update the userid and the password of a patron.
329 If the userid already exists, returns and let DBIx::Class warns
330 This will add an entry to action_logs if BorrowersLog is set.
334 sub update_password
{
335 my ( $self, $userid, $password ) = @_;
336 eval { $self->userid($userid)->store; };
337 return if $@
; # Make sure the userid is not already in used by another patron
340 password
=> $password,
344 logaction
( "MEMBERS", "CHANGE PASS", $self->borrowernumber, "" ) if C4
::Context
->preference("BorrowersLog");
350 my $new_expiry_date = $patron->renew_account
352 Extending the subscription to the expiry date.
359 if ( C4
::Context
->preference('BorrowerRenewalPeriodBase') eq 'combination' ) {
360 $date = ( dt_from_string
gt dt_from_string
( $self->dateexpiry ) ) ? dt_from_string
: dt_from_string
( $self->dateexpiry );
363 C4
::Context
->preference('BorrowerRenewalPeriodBase') eq 'dateexpiry'
364 ? dt_from_string
( $self->dateexpiry )
367 my $expiry_date = $self->category->get_expiry_date($date);
369 $self->dateexpiry($expiry_date);
370 $self->date_renewed( dt_from_string
() );
373 $self->add_enrolment_fee_if_needed;
375 logaction
( "MEMBERS", "RENEW", $self->borrowernumber, "Membership renewed" ) if C4
::Context
->preference("BorrowersLog");
376 return dt_from_string
( $expiry_date )->truncate( to
=> 'day' );
381 my $has_overdues = $patron->has_overdues;
383 Returns the number of patron's overdues
389 my $dtf = Koha
::Database
->new->schema->storage->datetime_parser;
390 return $self->_result->issues->search({ date_due
=> { '<' => $dtf->format_datetime( dt_from_string
() ) } })->count;
395 $patron->track_login;
396 $patron->track_login({ force => 1 });
398 Tracks a (successful) login attempt.
399 The preference TrackLastPatronActivity must be enabled. Or you
400 should pass the force parameter.
405 my ( $self, $params ) = @_;
408 !C4
::Context
->preference('TrackLastPatronActivity');
409 $self->lastseen( dt_from_string
() )->store;
412 =head3 move_to_deleted
414 my $is_moved = $patron->move_to_deleted;
416 Move a patron to the deletedborrowers table.
417 This can be done before deleting a patron, to make sure the data are not completely deleted.
421 sub move_to_deleted
{
423 my $patron_infos = $self->unblessed;
424 delete $patron_infos->{updated_on
}; #This ensures the updated_on date in deletedborrowers will be set to the current timestamp
425 return Koha
::Database
->new->schema->resultset('Deletedborrower')->create($patron_infos);
428 =head3 article_requests
430 my @requests = $borrower->article_requests();
431 my $requests = $borrower->article_requests();
433 Returns either a list of ArticleRequests objects,
434 or an ArtitleRequests object, depending on the
439 sub article_requests
{
442 $self->{_article_requests
} ||= Koha
::ArticleRequests
->search({ borrowernumber
=> $self->borrowernumber() });
444 return $self->{_article_requests
};
447 =head3 article_requests_current
449 my @requests = $patron->article_requests_current
451 Returns the article requests associated with this patron that are incomplete
455 sub article_requests_current
{
458 $self->{_article_requests_current
} ||= Koha
::ArticleRequests
->search(
460 borrowernumber
=> $self->id(),
462 { status
=> Koha
::ArticleRequest
::Status
::Pending
},
463 { status
=> Koha
::ArticleRequest
::Status
::Processing
}
468 return $self->{_article_requests_current
};
471 =head3 article_requests_finished
473 my @requests = $biblio->article_requests_finished
475 Returns the article requests associated with this patron that are completed
479 sub article_requests_finished
{
480 my ( $self, $borrower ) = @_;
482 $self->{_article_requests_finished
} ||= Koha
::ArticleRequests
->search(
484 borrowernumber
=> $self->id(),
486 { status
=> Koha
::ArticleRequest
::Status
::Completed
},
487 { status
=> Koha
::ArticleRequest
::Status
::Canceled
}
492 return $self->{_article_requests_finished
};
495 =head3 add_enrolment_fee_if_needed
497 my $enrolment_fee = $patron->add_enrolment_fee_if_needed;
499 Add enrolment fee for a patron if needed.
503 sub add_enrolment_fee_if_needed
{
505 my $enrolment_fee = $self->category->enrolmentfee;
506 if ( $enrolment_fee && $enrolment_fee > 0 ) {
507 # insert fee in patron debts
508 C4
::Accounts
::manualinvoice
( $self->borrowernumber, '', '', 'A', $enrolment_fee );
510 return $enrolment_fee || 0;
515 my $checkouts = $patron->checkouts
521 my $checkouts = $self->_result->issues;
522 return Koha
::Checkouts
->_new_from_dbic( $checkouts );
527 my $old_checkouts = $patron->old_checkouts
533 my $old_checkouts = $self->_result->old_issues;
534 return Koha
::Old
::Checkouts
->_new_from_dbic( $old_checkouts );
539 my $overdue_items = $patron->get_overdues
541 Return the overdued items
547 my $dtf = Koha
::Database
->new->schema->storage->datetime_parser;
548 return $self->checkouts->search(
550 'me.date_due' => { '<' => $dtf->format_datetime(dt_from_string
) },
553 prefetch
=> { item
=> { biblio
=> 'biblioitems' } },
560 my $age = $patron->get_age
562 Return the age of the patron
568 my $today_str = dt_from_string
->strftime("%Y-%m-%d");
569 return unless $self->dateofbirth;
570 my $dob_str = dt_from_string
( $self->dateofbirth )->strftime("%Y-%m-%d");
572 my ( $dob_y, $dob_m, $dob_d ) = split /-/, $dob_str;
573 my ( $today_y, $today_m, $today_d ) = split /-/, $today_str;
575 my $age = $today_y - $dob_y;
576 if ( $dob_m . $dob_d > $today_m . $today_d ) {
585 my $account = $patron->account
591 return Koha
::Account
->new( { patron_id
=> $self->borrowernumber } );
596 my $holds = $patron->holds
598 Return all the holds placed by this patron
604 my $holds_rs = $self->_result->reserves->search( {}, { order_by
=> 'reservedate' } );
605 return Koha
::Holds
->_new_from_dbic($holds_rs);
610 my $old_holds = $patron->old_holds
612 Return all the historical holds for this patron
618 my $old_holds_rs = $self->_result->old_reserves->search( {}, { order_by
=> 'reservedate' } );
619 return Koha
::Old
::Holds
->_new_from_dbic($old_holds_rs);
622 =head3 notice_email_address
624 my $email = $patron->notice_email_address;
626 Return the email address of patron used for notices.
627 Returns the empty string if no email address.
631 sub notice_email_address
{
634 my $which_address = C4
::Context
->preference("AutoEmailPrimaryAddress");
635 # if syspref is set to 'first valid' (value == OFF), look up email address
636 if ( $which_address eq 'OFF' ) {
637 return $self->first_valid_email_address;
640 return $self->$which_address || '';
643 =head3 first_valid_email_address
645 my $first_valid_email_address = $patron->first_valid_email_address
647 Return the first valid email address for a patron.
648 For now, the order is defined as email, emailpro, B_email.
649 Returns the empty string if the borrower has no email addresses.
653 sub first_valid_email_address
{
656 return $self->email() || $self->emailpro() || $self->B_email() || q{};
659 =head3 get_club_enrollments
663 sub get_club_enrollments
{
664 my ( $self, $return_scalar ) = @_;
666 my $e = Koha
::Club
::Enrollments
->search( { borrowernumber
=> $self->borrowernumber(), date_canceled
=> undef } );
668 return $e if $return_scalar;
670 return wantarray ?
$e->as_list : $e;
673 =head3 get_enrollable_clubs
677 sub get_enrollable_clubs
{
678 my ( $self, $is_enrollable_from_opac, $return_scalar ) = @_;
681 $params->{is_enrollable_from_opac
} = $is_enrollable_from_opac
682 if $is_enrollable_from_opac;
683 $params->{is_email_required
} = 0 unless $self->first_valid_email_address();
685 $params->{borrower
} = $self;
687 my $e = Koha
::Clubs
->get_enrollable($params);
689 return $e if $return_scalar;
691 return wantarray ?
$e->as_list : $e;
694 =head3 account_locked
696 my $is_locked = $patron->account_locked
698 Return true if the patron has reach the maximum number of login attempts (see pref FailedLoginAttempts).
699 Otherwise return false.
700 If the pref is not set (empty string, null or 0), the feature is considered as disabled.
706 my $FailedLoginAttempts = C4
::Context
->preference('FailedLoginAttempts');
707 return ( $FailedLoginAttempts
708 and $self->login_attempts
709 and $self->login_attempts >= $FailedLoginAttempts )?
1 : 0;
722 Kyle M Hall <kyle@bywatersolutions.com>
723 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>