Bug 16966: The method should return a Koha::Patrons object
[koha.git] / Koha / Patrons.pm
1 package Koha::Patrons;
2
3 # Copyright 2014 ByWater Solutions
4 # Copyright 2016 Koha Development Team
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
16 #
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.
20
21 use Modern::Perl;
22
23 use Carp;
24
25 use Koha::Database;
26 use Koha::DateUtils;
27
28 use Koha::ArticleRequests;
29 use Koha::ArticleRequest::Status;
30 use Koha::Patron;
31
32 use base qw(Koha::Objects);
33
34 =head1 NAME
35
36 Koha::Patron - Koha Patron Object class
37
38 =head1 API
39
40 =head2 Class Methods
41
42 =cut
43
44 =head3 search_housebound_choosers
45
46 Returns all Patrons which are Housebound choosers.
47
48 =cut
49
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);
57 }
58
59 =head3 search_housebound_deliverers
60
61 Returns all Patrons which are Housebound deliverers.
62
63 =cut
64
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);
72 }
73
74 =head3
75
76 my $patrons = Koha::Patrons->search_upcoming_membership_expires();
77
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.
82
83 =cut
84
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};
91
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;
96
97     $params->{dateexpiry} = {
98         ">=" => $dtf->format_date( $date_before ),
99         "<=" => $dtf->format_date( $date_after ),
100     };
101     return $self->SUPER::search(
102         $params, { join => ['branchcode', 'categorycode'] }
103     );
104 }
105
106 =head3 guarantor
107
108 Returns a Koha::Patron object for this borrower's guarantor
109
110 =cut
111
112 sub guarantor {
113     my ( $self ) = @_;
114
115     return Koha::Patrons->find( $self->guarantorid() );
116 }
117
118 =head3 article_requests
119
120 my @requests = $borrower->article_requests();
121 my $requests = $borrower->article_requests();
122
123 Returns either a list of ArticleRequests objects,
124 or an ArtitleRequests object, depending on the
125 calling context.
126
127 =cut
128
129 sub article_requests {
130     my ( $self ) = @_;
131
132     $self->{_article_requests} ||= Koha::ArticleRequests->search({ borrowernumber => $self->borrowernumber() });
133
134     return $self->{_article_requests};
135 }
136
137 =head3 article_requests_current
138
139 my @requests = $patron->article_requests_current
140
141 Returns the article requests associated with this patron that are incomplete
142
143 =cut
144
145 sub article_requests_current {
146     my ( $self ) = @_;
147
148     $self->{_article_requests_current} ||= Koha::ArticleRequests->search(
149         {
150             borrowernumber => $self->id(),
151             -or          => [
152                 { status => Koha::ArticleRequest::Status::Pending },
153                 { status => Koha::ArticleRequest::Status::Processing }
154             ]
155         }
156     );
157
158     return $self->{_article_requests_current};
159 }
160
161 =head3 article_requests_finished
162
163 my @requests = $biblio->article_requests_finished
164
165 Returns the article requests associated with this patron that are completed
166
167 =cut
168
169 sub article_requests_finished {
170     my ( $self, $borrower ) = @_;
171
172     $self->{_article_requests_finished} ||= Koha::ArticleRequests->search(
173         {
174             borrowernumber => $self->id(),
175             -or          => [
176                 { status => Koha::ArticleRequest::Status::Completed },
177                 { status => Koha::ArticleRequest::Status::Canceled }
178             ]
179         }
180     );
181
182     return $self->{_article_requests_finished};
183 }
184
185 =head3 search_patrons_to_anonymise
186
187     my $patrons = Koha::Patrons->search_patrons_to_anonymise( $date );
188
189 This method returns all patrons who has an issue history older than a given date.
190
191 =cut
192
193 sub search_patrons_to_anonymise {
194     my ( $class, $older_than_date, $library ) = @_;
195     $older_than_date = $older_than_date ? dt_from_string($older_than_date) : dt_from_string;
196     $library ||=
197       ( C4::Context->preference('IndependentBranches') && C4::Context->userenv && !C4::Context->IsSuperLibrarian() && C4::Context->userenv->{branch} )
198       ? C4::Context->userenv->{branch}
199       : undef;
200
201     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
202     my $rs = $class->_resultset->search(
203         {   returndate                  => { '<'   =>  $dtf->format_datetime($older_than_date), },
204             'old_issues.borrowernumber' => { 'not' => undef },
205             privacy                     => { '<>'  => 0 },                  # Keep forever
206             ( $library ? ( 'old_issues.branchcode' => $library ) : () ),
207         },
208         {   join     => ["old_issues"],
209             group_by => 'borrowernumber'
210         }
211     );
212     return Koha::Patrons->_new_from_dbic($rs);
213 }
214
215 =head3 anonymise_issue_history
216
217     Koha::Patrons->search->anonymise_issue_history( $older_than_date );
218
219 Anonymise issue history (old_issues) for all patrons older than the given date.
220 To make sure all the conditions are met, the caller has the responsability to
221 call search_patrons_to_anonymise to filter the Koha::Patrons set
222
223 =cut
224
225 sub anonymise_issue_history {
226     my ( $self, $older_than_date ) = @_;
227
228     return unless $older_than_date;
229     $older_than_date = dt_from_string $older_than_date;
230
231     # The default of 0 does not work due to foreign key constraints
232     # The anonymisation should not fail quietly if AnonymousPatron is not a valid entry
233     # Set it to undef (NULL)
234     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
235     my $old_issues_to_anonymise = $self->search_related( 'old_issues', { returndate => { '<' => $dtf->format_datetime($older_than_date) } } );
236     my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef;
237     $old_issues_to_anonymise->update( { 'old_issues.borrowernumber' => $anonymous_patron } );
238 }
239
240 =head3 type
241
242 =cut
243
244 sub _type {
245     return 'Borrower';
246 }
247
248 sub object_class {
249     return 'Koha::Patron';
250 }
251
252 =head1 AUTHOR
253
254 Kyle M Hall <kyle@bywatersolutions.com>
255
256 =cut
257
258 1;