Bug 17927: (QA followup) Fix timestamp nullable in hold.json
[koha.git] / Koha / Patrons.pm
1 package Koha::Patrons;
2
3 # Copyright ByWater Solutions 2014
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
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.
19
20 use Modern::Perl;
21
22 use Carp;
23
24 use Koha::Database;
25 use Koha::DateUtils;
26
27 use Koha::ArticleRequests;
28 use Koha::ArticleRequest::Status;
29 use Koha::Patron;
30
31 use base qw(Koha::Objects);
32
33 =head1 NAME
34
35 Koha::Patron - Koha Patron Object class
36
37 =head1 API
38
39 =head2 Class Methods
40
41 =cut
42
43 =head3 search_housebound_choosers
44
45 Returns all Patrons which are Housebound choosers.
46
47 =cut
48
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);
56 }
57
58 =head3 search_housebound_deliverers
59
60 Returns all Patrons which are Housebound deliverers.
61
62 =cut
63
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);
71 }
72
73 =head3
74
75 my $patrons = Koha::Patrons->search_upcoming_membership_expires();
76
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.
81
82 =cut
83
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};
90
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;
95
96     $params->{dateexpiry} = {
97         ">=" => $dtf->format_date( $date_before ),
98         "<=" => $dtf->format_date( $date_after ),
99     };
100     return $self->SUPER::search(
101         $params, { join => ['branchcode', 'categorycode'] }
102     );
103 }
104
105 =head3 guarantor
106
107 Returns a Koha::Patron object for this borrower's guarantor
108
109 =cut
110
111 sub guarantor {
112     my ( $self ) = @_;
113
114     return Koha::Patrons->find( $self->guarantorid() );
115 }
116
117 =head3 article_requests
118
119 my @requests = $borrower->article_requests();
120 my $requests = $borrower->article_requests();
121
122 Returns either a list of ArticleRequests objects,
123 or an ArtitleRequests object, depending on the
124 calling context.
125
126 =cut
127
128 sub article_requests {
129     my ( $self ) = @_;
130
131     $self->{_article_requests} ||= Koha::ArticleRequests->search({ borrowernumber => $self->borrowernumber() });
132
133     return $self->{_article_requests};
134 }
135
136 =head3 article_requests_current
137
138 my @requests = $patron->article_requests_current
139
140 Returns the article requests associated with this patron that are incomplete
141
142 =cut
143
144 sub article_requests_current {
145     my ( $self ) = @_;
146
147     $self->{_article_requests_current} ||= Koha::ArticleRequests->search(
148         {
149             borrowernumber => $self->id(),
150             -or          => [
151                 { status => Koha::ArticleRequest::Status::Pending },
152                 { status => Koha::ArticleRequest::Status::Processing }
153             ]
154         }
155     );
156
157     return $self->{_article_requests_current};
158 }
159
160 =head3 article_requests_finished
161
162 my @requests = $biblio->article_requests_finished
163
164 Returns the article requests associated with this patron that are completed
165
166 =cut
167
168 sub article_requests_finished {
169     my ( $self, $borrower ) = @_;
170
171     $self->{_article_requests_finished} ||= Koha::ArticleRequests->search(
172         {
173             borrowernumber => $self->id(),
174             -or          => [
175                 { status => Koha::ArticleRequest::Status::Completed },
176                 { status => Koha::ArticleRequest::Status::Canceled }
177             ]
178         }
179     );
180
181     return $self->{_article_requests_finished};
182 }
183
184 =head3 type
185
186 =cut
187
188 sub _type {
189     return 'Borrower';
190 }
191
192 sub object_class {
193     return 'Koha::Patron';
194 }
195
196 =head1 AUTHOR
197
198 Kyle M Hall <kyle@bywatersolutions.com>
199
200 =cut
201
202 1;