Bug 19049: Fix regression on stage-marc-import with to_marc plugin
[koha.git] / Koha / Patrons.pm
blob263cec72a4524c6cf6dcd8175e1e89b43086d7c0
1 package Koha::Patrons;
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
11 # version.
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.
21 use Modern::Perl;
23 use Carp;
25 use Koha::Database;
26 use Koha::DateUtils;
28 use Koha::ArticleRequests;
29 use Koha::ArticleRequest::Status;
30 use Koha::Patron;
32 use base qw(Koha::Objects);
34 =head1 NAME
36 Koha::Patron - Koha Patron Object class
38 =head1 API
40 =head2 Class Methods
42 =cut
44 =head3 search_housebound_choosers
46 Returns all Patrons which are Housebound choosers.
48 =cut
50 sub search_housebound_choosers {
51 my ( $self ) = @_;
52 my $cho = $self->_resultset
53 ->search_related('housebound_role', {
54 housebound_chooser => 1,
55 })->search_related('borrowernumber');
56 return Koha::Patrons->_new_from_dbic($cho);
59 =head3 search_housebound_deliverers
61 Returns all Patrons which are Housebound deliverers.
63 =cut
65 sub search_housebound_deliverers {
66 my ( $self ) = @_;
67 my $del = $self->_resultset
68 ->search_related('housebound_role', {
69 housebound_deliverer => 1,
70 })->search_related('borrowernumber');
71 return Koha::Patrons->_new_from_dbic($del);
74 =head3 search_upcoming_membership_expires
76 my $patrons = Koha::Patrons->search_upcoming_membership_expires();
78 The 'before' and 'after' represent the number of days before/after the date
79 that is set by the preference MembershipExpiryDaysNotice.
80 If the pref is 14, before 2 and after 3 then you will get all expires
81 from 12 to 17 days.
83 =cut
85 sub search_upcoming_membership_expires {
86 my ( $self, $params ) = @_;
87 my $before = $params->{before} || 0;
88 my $after = $params->{after} || 0;
89 delete $params->{before};
90 delete $params->{after};
92 my $days = C4::Context->preference("MembershipExpiryDaysNotice") || 0;
93 my $date_before = dt_from_string->add( days => $days - $before );
94 my $date_after = dt_from_string->add( days => $days + $after );
95 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
97 $params->{dateexpiry} = {
98 ">=" => $dtf->format_date( $date_before ),
99 "<=" => $dtf->format_date( $date_after ),
101 return $self->SUPER::search(
102 $params, { join => ['branchcode', 'categorycode'] }
106 =head3 guarantor
108 Returns a Koha::Patron object for this borrower's guarantor
110 =cut
112 sub guarantor {
113 my ( $self ) = @_;
115 return Koha::Patrons->find( $self->guarantorid() );
118 =head3 search_patrons_to_anonymise
120 my $patrons = Koha::Patrons->search_patrons_to_anonymise( { before => $older_than_date, [ library => $library ] } );
122 This method returns all patrons who has an issue history older than a given date.
124 =cut
126 sub search_patrons_to_anonymise {
127 my ( $class, $params ) = @_;
128 my $older_than_date = $params->{before};
129 my $library = $params->{library};
130 $older_than_date = $older_than_date ? dt_from_string($older_than_date) : dt_from_string;
131 $library ||=
132 ( C4::Context->preference('IndependentBranches') && C4::Context->userenv && !C4::Context->IsSuperLibrarian() && C4::Context->userenv->{branch} )
133 ? C4::Context->userenv->{branch}
134 : undef;
136 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
137 my $rs = $class->_resultset->search(
138 { returndate => { '<' => $dtf->format_datetime($older_than_date), },
139 'old_issues.borrowernumber' => { 'not' => undef },
140 privacy => { '<>' => 0 }, # Keep forever
141 ( $library ? ( 'old_issues.branchcode' => $library ) : () ),
143 { join => ["old_issues"],
144 group_by => 'borrowernumber'
147 return Koha::Patrons->_new_from_dbic($rs);
150 =head3 anonymise_issue_history
152 Koha::Patrons->search->anonymise_issue_history( { [ before => $older_than_date ] } );
154 Anonymise issue history (old_issues) for all patrons older than the given date (optional).
155 To make sure all the conditions are met, the caller has the responsibility to
156 call search_patrons_to_anonymise to filter the Koha::Patrons set
158 =cut
160 sub anonymise_issue_history {
161 my ( $self, $params ) = @_;
163 my $older_than_date = $params->{before};
165 $older_than_date = dt_from_string $older_than_date if $older_than_date;
167 # The default of 0 does not work due to foreign key constraints
168 # The anonymisation should not fail quietly if AnonymousPatron is not a valid entry
169 # Set it to undef (NULL)
170 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
171 my $old_issues_to_anonymise = $self->search_related(
172 'old_issues',
175 $older_than_date
176 ? ( returndate =>
177 { '<' => $dtf->format_datetime($older_than_date) } )
178 : ()
182 my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef;
183 $old_issues_to_anonymise->update( { 'old_issues.borrowernumber' => $anonymous_patron } );
186 =head3 type
188 =cut
190 sub _type {
191 return 'Borrower';
194 sub object_class {
195 return 'Koha::Patron';
198 =head1 AUTHOR
200 Kyle M Hall <kyle@bywatersolutions.com>
202 =cut