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.
32 use Koha
::Patron
::Categories
;
33 use Koha
::Patron
::HouseboundProfile
;
34 use Koha
::Patron
::HouseboundRole
;
35 use Koha
::Patron
::Images
;
37 use Koha
::Virtualshelves
;
39 use base
qw(Koha::Object);
43 Koha::Patron - Koha Patron Object class
55 Delete patron's holds, lists and finally the patron.
57 Lists owned by the borrower are deleted, but entries from the borrower to
66 $self->_result->result_source->schema->txn_do(
68 # Delete Patron's holds
69 # FIXME Should be $patron->get_holds
70 $_->delete for Koha
::Holds
->search( { borrowernumber
=> $self->borrowernumber } );
72 # Delete all lists and all shares of this borrower
73 # Consistent with the approach Koha uses on deleting individual lists
74 # Note that entries in virtualshelfcontents added by this borrower to
75 # lists of others will be handled by a table constraint: the borrower
76 # is set to NULL in those entries.
78 # We could handle the above deletes via a constraint too.
79 # But a new BZ report 11889 has been opened to discuss another approach.
80 # Instead of deleting we could also disown lists (based on a pref).
81 # In that way we could save shared and public lists.
82 # The current table constraints support that idea now.
83 # This pref should then govern the results of other routines/methods such as
84 # Koha::Virtualshelf->new->delete too.
85 # FIXME Could be $patron->get_lists
86 $_->delete for Koha
::Virtualshelves
->search( { owner
=> $self->borrowernumber } );
88 $deleted = $self->SUPER::delete;
90 logaction
( "MEMBERS", "DELETE", $self->borrowernumber, "" ) if C4
::Context
->preference("BorrowersLog");
99 my $patron_category = $patron->category
101 Return the patron category for this patron
107 return Koha
::Patron
::Category
->_new_from_dbic( $self->_result->categorycode );
112 Returns a Koha::Patron object for this patron's guarantor
119 return unless $self->guarantorid();
121 return Koha
::Patrons
->find( $self->guarantorid() );
127 return Koha
::Patron
::Images
->find( $self->borrowernumber )
132 Returns the guarantees (list of Koha::Patron) of this patron
139 return Koha
::Patrons
->search( { guarantorid
=> $self->borrowernumber } );
142 =head3 housebound_profile
144 Returns the HouseboundProfile associated with this patron.
148 sub housebound_profile
{
150 my $profile = $self->_result->housebound_profile;
151 return Koha
::Patron
::HouseboundProfile
->_new_from_dbic($profile)
156 =head3 housebound_role
158 Returns the HouseboundRole associated with this patron.
162 sub housebound_role
{
165 my $role = $self->_result->housebound_role;
166 return Koha
::Patron
::HouseboundRole
->_new_from_dbic($role) if ( $role );
172 Returns the siblings of this patron.
179 my $guarantor = $self->guarantor;
181 return unless $guarantor;
183 return Koha
::Patrons
->search(
187 '=' => $guarantor->id,
190 '!=' => $self->borrowernumber,
196 =head3 wants_check_for_previous_checkout
198 $wants_check = $patron->wants_check_for_previous_checkout;
200 Return 1 if Koha needs to perform PrevIssue checking, else 0.
204 sub wants_check_for_previous_checkout
{
206 my $syspref = C4
::Context
->preference("checkPrevCheckout");
209 ## Hard syspref trumps all
210 return 1 if ($syspref eq 'hardyes');
211 return 0 if ($syspref eq 'hardno');
212 ## Now, patron pref trumps all
213 return 1 if ($self->checkprevcheckout eq 'yes');
214 return 0 if ($self->checkprevcheckout eq 'no');
216 # More complex: patron inherits -> determine category preference
217 my $checkPrevCheckoutByCat = $self->category->checkprevcheckout;
218 return 1 if ($checkPrevCheckoutByCat eq 'yes');
219 return 0 if ($checkPrevCheckoutByCat eq 'no');
221 # Finally: category preference is inherit, default to 0
222 if ($syspref eq 'softyes') {
229 =head3 do_check_for_previous_checkout
231 $do_check = $patron->do_check_for_previous_checkout($item);
233 Return 1 if the bib associated with $ITEM has previously been checked out to
234 $PATRON, 0 otherwise.
238 sub do_check_for_previous_checkout
{
239 my ( $self, $item ) = @_;
241 # Find all items for bib and extract item numbers.
242 my @items = Koha
::Items
->search({biblionumber
=> $item->{biblionumber
}});
244 foreach my $item (@items) {
245 push @item_nos, $item->itemnumber;
248 # Create (old)issues search criteria
250 borrowernumber
=> $self->borrowernumber,
251 itemnumber
=> \
@item_nos,
254 # Check current issues table
255 my $issues = Koha
::Issues
->search($criteria);
256 return 1 if $issues->count; # 0 || N
258 # Check old issues table
259 my $old_issues = Koha
::OldIssues
->search($criteria);
260 return $old_issues->count; # 0 || N
265 my $debarment_expiration = $patron->is_debarred;
267 Returns the date a patron debarment will expire, or undef if the patron is not
275 return unless $self->debarred;
276 return $self->debarred
277 if $self->debarred =~ '^9999'
278 or dt_from_string
( $self->debarred ) > dt_from_string
;
284 my $is_expired = $patron->is_expired;
286 Returns 1 if the patron is expired or 0;
292 return 0 unless $self->dateexpiry;
293 return 0 if $self->dateexpiry eq '0000-00-00';
294 return 1 if dt_from_string
( $self->dateexpiry ) < dt_from_string
->truncate( to
=> 'day' );
298 =head2 is_going_to_expire
300 my $is_going_to_expire = $patron->is_going_to_expire;
302 Returns 1 if the patron is going to expired, depending on the NotifyBorrowerDeparture pref or 0
306 sub is_going_to_expire
{
309 my $delay = C4
::Context
->preference('NotifyBorrowerDeparture') || 0;
311 return 0 unless $delay;
312 return 0 unless $self->dateexpiry;
313 return 0 if $self->dateexpiry eq '0000-00-00';
314 return 1 if dt_from_string
( $self->dateexpiry )->subtract( days
=> $delay ) < dt_from_string
->truncate( to
=> 'day' );
318 =head2 update_password
320 my $updated = $patron->update_password( $userid, $password );
322 Update the userid and the password of a patron.
323 If the userid already exists, returns and let DBIx::Class warns
324 This will add an entry to action_logs if BorrowersLog is set.
328 sub update_password
{
329 my ( $self, $userid, $password ) = @_;
330 eval { $self->userid($userid)->store; };
331 return if $@
; # Make sure the userid is not already in used by another patron
332 $self->password($password)->store;
333 logaction
( "MEMBERS", "CHANGE PASS", $self->borrowernumber, "" ) if C4
::Context
->preference("BorrowersLog");
339 my $new_expiry_date = $patron->renew_account
341 Extending the subscription to the expiry date.
348 if ( C4
::Context
->preference('BorrowerRenewalPeriodBase') eq 'combination' ) {
349 $date = ( dt_from_string
gt dt_from_string
( $self->dateexpiry ) ) ? dt_from_string
: dt_from_string
( $self->dateexpiry );
352 C4
::Context
->preference('BorrowerRenewalPeriodBase') eq 'dateexpiry'
353 ? dt_from_string
( $self->dateexpiry )
356 my $expiry_date = $self->category->get_expiry_date($date);
358 $self->dateexpiry($expiry_date)->store;
360 $self->add_enrolment_fee_if_needed;
362 logaction
( "MEMBERS", "RENEW", $self->borrowernumber, "Membership renewed" ) if C4
::Context
->preference("BorrowersLog");
363 return dt_from_string
( $expiry_date )->truncate( to
=> 'day' );
368 my $has_overdues = $patron->has_overdues;
370 Returns the number of patron's overdues
376 my $dtf = Koha
::Database
->new->schema->storage->datetime_parser;
377 return $self->_result->issues->search({ date_due
=> { '<' => $dtf->format_datetime( dt_from_string
() ) } })->count;
382 $patron->track_login;
383 $patron->track_login({ force => 1 });
385 Tracks a (successful) login attempt.
386 The preference TrackLastPatronActivity must be enabled. Or you
387 should pass the force parameter.
392 my ( $self, $params ) = @_;
395 !C4
::Context
->preference('TrackLastPatronActivity');
396 $self->lastseen( dt_from_string
() )->store;
399 =head2 move_to_deleted
401 my $is_moved = $patron->move_to_deleted;
403 Move a patron to the deletedborrowers table.
404 This can be done before deleting a patron, to make sure the data are not completely deleted.
408 sub move_to_deleted
{
410 my $patron_infos = $self->unblessed;
411 return Koha
::Database
->new->schema->resultset('Deletedborrower')->create($patron_infos);
414 =head3 article_requests
416 my @requests = $borrower->article_requests();
417 my $requests = $borrower->article_requests();
419 Returns either a list of ArticleRequests objects,
420 or an ArtitleRequests object, depending on the
425 sub article_requests
{
428 $self->{_article_requests
} ||= Koha
::ArticleRequests
->search({ borrowernumber
=> $self->borrowernumber() });
430 return $self->{_article_requests
};
433 =head3 article_requests_current
435 my @requests = $patron->article_requests_current
437 Returns the article requests associated with this patron that are incomplete
441 sub article_requests_current
{
444 $self->{_article_requests_current
} ||= Koha
::ArticleRequests
->search(
446 borrowernumber
=> $self->id(),
448 { status
=> Koha
::ArticleRequest
::Status
::Pending
},
449 { status
=> Koha
::ArticleRequest
::Status
::Processing
}
454 return $self->{_article_requests_current
};
457 =head3 article_requests_finished
459 my @requests = $biblio->article_requests_finished
461 Returns the article requests associated with this patron that are completed
465 sub article_requests_finished
{
466 my ( $self, $borrower ) = @_;
468 $self->{_article_requests_finished
} ||= Koha
::ArticleRequests
->search(
470 borrowernumber
=> $self->id(),
472 { status
=> Koha
::ArticleRequest
::Status
::Completed
},
473 { status
=> Koha
::ArticleRequest
::Status
::Canceled
}
478 return $self->{_article_requests_finished
};
481 =head3 add_enrolment_fee_if_needed
483 my $enrolment_fee = $patron->add_enrolment_fee_if_needed;
485 Add enrolment fee for a patron if needed.
489 sub add_enrolment_fee_if_needed
{
491 my $enrolment_fee = $self->category->enrolmentfee;
492 if ( $enrolment_fee && $enrolment_fee > 0 ) {
493 # insert fee in patron debts
494 C4
::Accounts
::manualinvoice
( $self->borrowernumber, '', '', 'A', $enrolment_fee );
496 return $enrolment_fee || 0;
501 my $overdue_items = $patron->get_overdues
503 Return the overdued items
509 my $dtf = Koha
::Database
->new->schema->storage->datetime_parser;
510 my $issues = Koha
::Issues
->search(
512 'me.borrowernumber' => $self->borrowernumber,
513 'me.date_due' => { '<' => $dtf->format_datetime(dt_from_string
) },
516 prefetch
=> { item
=> { biblio
=> 'biblioitems' } },
532 Kyle M Hall <kyle@bywatersolutions.com>
533 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>