Bug 18089 - Unit test
[koha.git] / Koha / Patrons.pm
blobd079df0414407e8bb478f0ba8c28bd72f0010a4e
1 package Koha::Patrons;
3 # Copyright ByWater Solutions 2014
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Modern::Perl;
22 use Carp;
24 use Koha::Database;
25 use Koha::DateUtils;
27 use Koha::ArticleRequests;
28 use Koha::ArticleRequest::Status;
29 use Koha::Patron;
31 use base qw(Koha::Objects);
33 =head1 NAME
35 Koha::Patron - Koha Patron Object class
37 =head1 API
39 =head2 Class Methods
41 =cut
43 =head3 search_housebound_choosers
45 Returns all Patrons which are Housebound choosers.
47 =cut
49 sub search_housebound_choosers {
50 my ( $self ) = @_;
51 my $cho = $self->_resultset
52 ->search_related('housebound_role', {
53 housebound_chooser => 1,
54 })->search_related('borrowernumber');
55 return Koha::Patrons->_new_from_dbic($cho);
58 =head3 search_housebound_deliverers
60 Returns all Patrons which are Housebound deliverers.
62 =cut
64 sub search_housebound_deliverers {
65 my ( $self ) = @_;
66 my $del = $self->_resultset
67 ->search_related('housebound_role', {
68 housebound_deliverer => 1,
69 })->search_related('borrowernumber');
70 return Koha::Patrons->_new_from_dbic($del);
73 =head3
75 my $patrons = Koha::Patrons->search_upcoming_membership_expires();
77 The 'before' and 'after' represent the number of days before/after the date
78 that is set by the preference MembershipExpiryDaysNotice.
79 If the pref is 14, before 2 and after 3 then you will get all expires
80 from 12 to 17 days.
82 =cut
84 sub search_upcoming_membership_expires {
85 my ( $self, $params ) = @_;
86 my $before = $params->{before} || 0;
87 my $after = $params->{after} || 0;
88 delete $params->{before};
89 delete $params->{after};
91 my $days = C4::Context->preference("MembershipExpiryDaysNotice") || 0;
92 my $date_before = dt_from_string->add( days => $days - $before );
93 my $date_after = dt_from_string->add( days => $days + $after );
94 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
96 $params->{dateexpiry} = {
97 ">=" => $dtf->format_date( $date_before ),
98 "<=" => $dtf->format_date( $date_after ),
100 return $self->SUPER::search(
101 $params, { join => ['branchcode', 'categorycode'] }
105 =head3 guarantor
107 Returns a Koha::Patron object for this borrower's guarantor
109 =cut
111 sub guarantor {
112 my ( $self ) = @_;
114 return Koha::Patrons->find( $self->guarantorid() );
117 =head3 article_requests
119 my @requests = $borrower->article_requests();
120 my $requests = $borrower->article_requests();
122 Returns either a list of ArticleRequests objects,
123 or an ArtitleRequests object, depending on the
124 calling context.
126 =cut
128 sub article_requests {
129 my ( $self ) = @_;
131 $self->{_article_requests} ||= Koha::ArticleRequests->search({ borrowernumber => $self->borrowernumber() });
133 return $self->{_article_requests};
136 =head3 article_requests_current
138 my @requests = $patron->article_requests_current
140 Returns the article requests associated with this patron that are incomplete
142 =cut
144 sub article_requests_current {
145 my ( $self ) = @_;
147 $self->{_article_requests_current} ||= Koha::ArticleRequests->search(
149 borrowernumber => $self->id(),
150 -or => [
151 { status => Koha::ArticleRequest::Status::Pending },
152 { status => Koha::ArticleRequest::Status::Processing }
157 return $self->{_article_requests_current};
160 =head3 article_requests_finished
162 my @requests = $biblio->article_requests_finished
164 Returns the article requests associated with this patron that are completed
166 =cut
168 sub article_requests_finished {
169 my ( $self, $borrower ) = @_;
171 $self->{_article_requests_finished} ||= Koha::ArticleRequests->search(
173 borrowernumber => $self->id(),
174 -or => [
175 { status => Koha::ArticleRequest::Status::Completed },
176 { status => Koha::ArticleRequest::Status::Canceled }
181 return $self->{_article_requests_finished};
184 =head3 type
186 =cut
188 sub _type {
189 return 'Borrower';
192 sub object_class {
193 return 'Koha::Patron';
196 =head1 AUTHOR
198 Kyle M Hall <kyle@bywatersolutions.com>
200 =cut