3 # Copyright 2014 ByWater Solutions
4 # Copyright 2016 Koha Development Team
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.
28 use Koha
::ArticleRequests
;
29 use Koha
::ArticleRequest
::Status
;
31 use Koha
::Exceptions
::Patron
;
33 use base
qw(Koha::Objects);
37 Koha::Patron - Koha Patron Object class
47 my $patrons = Koha::Patrons->search_limit( $params, $attributes );
49 Returns all the patrons the logged in user is allowed to see
54 my ( $self, $params, $attributes ) = @_;
56 my $userenv = C4
::Context
->userenv;
57 my @restricted_branchcodes;
58 if ( $userenv and $userenv->{number
} ) {
59 my $logged_in_user = Koha
::Patrons
->find( $userenv->{number
} );
60 @restricted_branchcodes = $logged_in_user->libraries_where_can_see_patrons;
62 $params->{'me.branchcode'} = { -in => \
@restricted_branchcodes } if @restricted_branchcodes;
63 return $self->search( $params, $attributes );
66 =head3 search_housebound_choosers
68 Returns all Patrons which are Housebound choosers.
72 sub search_housebound_choosers
{
74 my $cho = $self->_resultset
75 ->search_related('housebound_role', {
76 housebound_chooser
=> 1,
77 })->search_related('borrowernumber');
78 return Koha
::Patrons
->_new_from_dbic($cho);
81 =head3 search_housebound_deliverers
83 Returns all Patrons which are Housebound deliverers.
87 sub search_housebound_deliverers
{
89 my $del = $self->_resultset
90 ->search_related('housebound_role', {
91 housebound_deliverer
=> 1,
92 })->search_related('borrowernumber');
93 return Koha
::Patrons
->_new_from_dbic($del);
96 =head3 search_upcoming_membership_expires
98 my $patrons = Koha::Patrons->search_upcoming_membership_expires();
100 The 'before' and 'after' represent the number of days before/after the date
101 that is set by the preference MembershipExpiryDaysNotice.
102 If the pref is 14, before 2 and after 3 then you will get all expires
107 sub search_upcoming_membership_expires
{
108 my ( $self, $params ) = @_;
109 my $before = $params->{before
} || 0;
110 my $after = $params->{after
} || 0;
111 delete $params->{before
};
112 delete $params->{after
};
114 my $days = C4
::Context
->preference("MembershipExpiryDaysNotice") || 0;
115 my $date_before = dt_from_string
->add( days
=> $days - $before );
116 my $date_after = dt_from_string
->add( days
=> $days + $after );
117 my $dtf = Koha
::Database
->new->schema->storage->datetime_parser;
119 $params->{dateexpiry
} = {
120 ">=" => $dtf->format_date( $date_before ),
121 "<=" => $dtf->format_date( $date_after ),
123 return $self->SUPER::search
(
124 $params, { join => ['branchcode', 'categorycode'] }
130 Returns a Koha::Patron object for this borrower's guarantor
137 return Koha
::Patrons
->find( $self->guarantorid() );
140 =head3 search_patrons_to_anonymise
142 my $patrons = Koha::Patrons->search_patrons_to_anonymise( { before => $older_than_date, [ library => $library ] } );
144 This method returns all patrons who has an issue history older than a given date.
148 sub search_patrons_to_anonymise
{
149 my ( $class, $params ) = @_;
150 my $older_than_date = $params->{before
};
151 my $library = $params->{library
};
152 $older_than_date = $older_than_date ? dt_from_string
($older_than_date) : dt_from_string
;
154 ( C4
::Context
->preference('IndependentBranches') && C4
::Context
->userenv && !C4
::Context
->IsSuperLibrarian() && C4
::Context
->userenv->{branch
} )
155 ? C4
::Context
->userenv->{branch
}
158 my $dtf = Koha
::Database
->new->schema->storage->datetime_parser;
159 my $rs = $class->_resultset->search(
160 { returndate
=> { '<' => $dtf->format_datetime($older_than_date), },
161 'old_issues.borrowernumber' => { 'not' => undef },
162 privacy
=> { '<>' => 0 }, # Keep forever
163 ( $library ?
( 'old_issues.branchcode' => $library ) : () ),
165 { join => ["old_issues"],
169 return Koha
::Patrons
->_new_from_dbic($rs);
172 =head3 anonymise_issue_history
174 Koha::Patrons->search->anonymise_issue_history( { [ before => $older_than_date ] } );
176 Anonymise issue history (old_issues) for all patrons older than the given date (optional).
177 To make sure all the conditions are met, the caller has the responsibility to
178 call search_patrons_to_anonymise to filter the Koha::Patrons set
182 sub anonymise_issue_history
{
183 my ( $self, $params ) = @_;
185 my $older_than_date = $params->{before
};
187 $older_than_date = dt_from_string
$older_than_date if $older_than_date;
189 # The default of 0 does not work due to foreign key constraints
190 # The anonymisation should not fail quietly if AnonymousPatron is not a valid entry
191 # Set it to undef (NULL)
192 my $dtf = Koha
::Database
->new->schema->storage->datetime_parser;
194 while ( my $patron = $self->next ) {
195 my $old_issues_to_anonymise = $patron->old_checkouts->search(
200 { '<' => $dtf->format_datetime($older_than_date) } )
205 my $anonymous_patron = C4
::Context
->preference('AnonymousPatron') || undef;
206 $nb_rows += $old_issues_to_anonymise->update( { 'old_issues.borrowernumber' => $anonymous_patron } );
213 Koha::Patrons->search({ some filters here })->delete({ move => 1 });
215 Delete passed set of patron objects.
216 Wrapper for Koha::Patron->delete. (We do not want to bypass Koha::Patron
217 and let DBIx do the job without further housekeeping.)
218 Includes a move to deletedborrowers if move flag set.
220 Just like DBIx, the delete will only succeed when all entries could be
221 deleted. Returns true or throws an exception.
226 my ( $self, $params ) = @_;
228 $self->_resultset->result_source->schema->txn_do( sub {
229 my ( $set, $params ) = @_;
230 while( my $patron = $set->next ) {
231 $patron->move_to_deleted if $params->{move
};
232 $patron->delete == 1 || Koha
::Exceptions
::Patron
::FailedDelete
->throw;
236 return $patrons_deleted;
239 =head3 search_unsubscribed
241 Koha::Patrons->search_unsubscribed;
243 Returns a set of Koha patron objects for patrons that recently
244 unsubscribed and are not locked (candidates for locking).
245 Depends on UnsubscribeReflectionDelay.
249 sub search_unsubscribed
{
252 my $delay = C4
::Context
->preference('UnsubscribeReflectionDelay');
253 if( !defined($delay) || $delay eq q{} ) {
255 return $class->search({ borrowernumber
=> undef });
257 my $parser = Koha
::Database
->new->schema->storage->datetime_parser;
258 my $dt = dt_from_string
()->subtract( days
=> $delay );
259 my $str = $parser->format_datetime($dt);
260 my $fails = C4
::Context
->preference('FailedLoginAttempts') || 0;
261 my $cond = [ undef, 0, 1..$fails-1 ]; # NULL, 0, 1..fails-1 (if fails>0)
262 return $class->search(
264 'patron_consents.refused_on' => { '<=' => $str },
265 'login_attempts' => $cond,
267 { join => 'patron_consents' },
271 =head3 search_anonymize_candidates
273 Koha::Patrons->search_anonymize_candidates({ locked => 1 });
275 Returns a set of Koha patron objects for patrons whose account is expired
276 and locked (if parameter set). These are candidates for anonymizing.
277 Depends on PatronAnonymizeDelay.
281 sub search_anonymize_candidates
{
282 my ( $class, $params ) = @_;
284 my $delay = C4
::Context
->preference('PatronAnonymizeDelay');
285 if( !defined($delay) || $delay eq q{} ) {
287 return $class->search({ borrowernumber
=> undef });
290 my $parser = Koha
::Database
->new->schema->storage->datetime_parser;
291 my $dt = dt_from_string
()->subtract( days
=> $delay );
292 my $str = $parser->format_datetime($dt);
293 $cond->{dateexpiry
} = { '<=' => $str };
294 $cond->{flgAnonymized
} = [ undef, 0 ]; # not yet done
295 if( $params->{locked
} ) {
296 my $fails = C4
::Context
->preference('FailedLoginAttempts');
297 $cond->{login_attempts
} = [ -and => { '!=' => undef }, { -not_in
=> [0, 1..$fails-1 ] } ]; # -not_in does not like undef
299 return $class->search( $cond );
302 =head3 search_anonymized
304 Koha::Patrons->search_anonymized;
306 Returns a set of Koha patron objects for patron accounts that have been
307 anonymized before and could be removed.
308 Depends on PatronRemovalDelay.
312 sub search_anonymized
{
315 my $delay = C4
::Context
->preference('PatronRemovalDelay');
316 if( !defined($delay) || $delay eq q{} ) {
318 return $class->search({ borrowernumber
=> undef });
321 my $parser = Koha
::Database
->new->schema->storage->datetime_parser;
322 my $dt = dt_from_string
()->subtract( days
=> $delay );
323 my $str = $parser->format_datetime($dt);
324 $cond->{dateexpiry
} = { '<=' => $str };
325 $cond->{flgAnonymized
} = 1;
326 return $class->search( $cond );
331 Koha::Patrons->search({ some filters })->lock({ expire => 1, remove => 1 })
333 Lock the passed set of patron objects. Optionally expire and remove holds.
334 Wrapper around Koha::Patron->lock.
339 my ( $self, $params ) = @_;
340 while( my $patron = $self->next ) {
341 $patron->lock($params);
347 Koha::Patrons->search({ some filters })->anonymize;
349 Anonymize passed set of patron objects.
350 Wrapper around Koha::Patron->anonymize.
356 while( my $patron = $self->next ) {
370 return 'Koha::Patron';
375 Kyle M Hall <kyle@bywatersolutions.com>