Bug 16911: Koha::Patrons - Move ExtendMemberSubscriptionTo to ->extend_subscription
[koha.git] / Koha / Patron.pm
1 package Koha::Patron;
2
3 # Copyright ByWater Solutions 2014
4 # Copyright PTFS Europe 2016
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 C4::Context;
26 use C4::Members;
27 use C4::Log;
28 use Koha::Database;
29 use Koha::DateUtils;
30 use Koha::Issues;
31 use Koha::OldIssues;
32 use Koha::Patron::Categories;
33 use Koha::Patron::Images;
34 use Koha::Patrons;
35
36 use base qw(Koha::Object);
37
38 =head1 NAME
39
40 Koha::Patron - Koha Patron Object class
41
42 =head1 API
43
44 =head2 Class Methods
45
46 =cut
47
48 =head3 guarantor
49
50 Returns a Koha::Patron object for this patron's guarantor
51
52 =cut
53
54 sub guarantor {
55     my ( $self ) = @_;
56
57     return unless $self->guarantorid();
58
59     return Koha::Patrons->find( $self->guarantorid() );
60 }
61
62 sub image {
63     my ( $self ) = @_;
64
65     return Koha::Patron::Images->find( $self->borrowernumber )
66 }
67
68 =head3 guarantees
69
70 Returns the guarantees (list of Koha::Patron) of this patron
71
72 =cut
73
74 sub guarantees {
75     my ( $self ) = @_;
76
77     return Koha::Patrons->search( { guarantorid => $self->borrowernumber } );
78 }
79
80 =head3 siblings
81
82 Returns the siblings of this patron.
83
84 =cut
85
86 sub siblings {
87     my ( $self ) = @_;
88
89     my $guarantor = $self->guarantor;
90
91     return unless $guarantor;
92
93     return Koha::Patrons->search(
94         {
95             guarantorid => {
96                 '!=' => undef,
97                 '=' => $guarantor->id,
98             },
99             borrowernumber => {
100                 '!=' => $self->borrowernumber,
101             }
102         }
103     );
104 }
105
106 =head3 wants_check_for_previous_checkout
107
108     $wants_check = $patron->wants_check_for_previous_checkout;
109
110 Return 1 if Koha needs to perform PrevIssue checking, else 0.
111
112 =cut
113
114 sub wants_check_for_previous_checkout {
115     my ( $self ) = @_;
116     my $syspref = C4::Context->preference("checkPrevCheckout");
117
118     # Simple cases
119     ## Hard syspref trumps all
120     return 1 if ($syspref eq 'hardyes');
121     return 0 if ($syspref eq 'hardno');
122     ## Now, patron pref trumps all
123     return 1 if ($self->checkprevcheckout eq 'yes');
124     return 0 if ($self->checkprevcheckout eq 'no');
125
126     # More complex: patron inherits -> determine category preference
127     my $checkPrevCheckoutByCat = Koha::Patron::Categories
128         ->find($self->categorycode)->checkprevcheckout;
129     return 1 if ($checkPrevCheckoutByCat eq 'yes');
130     return 0 if ($checkPrevCheckoutByCat eq 'no');
131
132     # Finally: category preference is inherit, default to 0
133     if ($syspref eq 'softyes') {
134         return 1;
135     } else {
136         return 0;
137     }
138 }
139
140 =head3 do_check_for_previous_checkout
141
142     $do_check = $patron->do_check_for_previous_checkout($item);
143
144 Return 1 if the bib associated with $ITEM has previously been checked out to
145 $PATRON, 0 otherwise.
146
147 =cut
148
149 sub do_check_for_previous_checkout {
150     my ( $self, $item ) = @_;
151
152     # Find all items for bib and extract item numbers.
153     my @items = Koha::Items->search({biblionumber => $item->{biblionumber}});
154     my @item_nos;
155     foreach my $item (@items) {
156         push @item_nos, $item->itemnumber;
157     }
158
159     # Create (old)issues search criteria
160     my $criteria = {
161         borrowernumber => $self->borrowernumber,
162         itemnumber => \@item_nos,
163     };
164
165     # Check current issues table
166     my $issues = Koha::Issues->search($criteria);
167     return 1 if $issues->count; # 0 || N
168
169     # Check old issues table
170     my $old_issues = Koha::OldIssues->search($criteria);
171     return $old_issues->count;  # 0 || N
172 }
173
174 =head2 is_debarred
175
176 my $debarment_expiration = $patron->is_debarred;
177
178 Returns the date a patron debarment will expire, or undef if the patron is not
179 debarred
180
181 =cut
182
183 sub is_debarred {
184     my ($self) = @_;
185
186     return unless $self->debarred;
187     return $self->debarred
188       if $self->debarred =~ '^9999'
189       or dt_from_string( $self->debarred ) > dt_from_string;
190     return;
191 }
192
193 =head2 update_password
194
195 my $updated = $patron->update_password( $userid, $password );
196
197 Update the userid and the password of a patron.
198 If the userid already exists, returns and let DBIx::Class warns
199 This will add an entry to action_logs if BorrowersLog is set.
200
201 =cut
202
203 sub update_password {
204     my ( $self, $userid, $password ) = @_;
205     eval { $self->userid($userid)->store; };
206     return if $@; # Make sure the userid is not already in used by another patron
207     $self->password($password)->store;
208     logaction( "MEMBERS", "CHANGE PASS", $self->borrowernumber, "" ) if C4::Context->preference("BorrowersLog");
209     return 1;
210 }
211
212 =head3 extend_subscription
213
214 my $new_expiry_date = $patron->extend_subscription
215
216 Extending the subscription to the expiry date.
217
218 =cut
219
220 sub extend_subscription {
221     my ($self) = @_;
222
223     my $date =
224       C4::Context->preference('BorrowerRenewalPeriodBase') eq 'dateexpiry'
225       ? dt_from_string( $self->dateexpiry )
226       : dt_from_string;
227     my $patron_category = Koha::Patron::Categories->find( $self->categorycode );    # FIXME Should be $self->category
228     my $expiry_date     = $patron_category->get_expiry_date($date);
229
230     $self->dateexpiry($expiry_date)->store;
231
232     C4::Members::AddEnrolmentFeeIfNeeded( $self->categorycode, $self->borrowernumber );
233
234     logaction( "MEMBERS", "RENEW", $self->borrowernumber, "Membership renewed" ) if C4::Context->preference("BorrowersLog");
235     return dt_from_string( $expiry_date )->truncate( to => 'day' );
236 }
237
238 =head3 type
239
240 =cut
241
242 sub _type {
243     return 'Borrower';
244 }
245
246 =head1 AUTHOR
247
248 Kyle M Hall <kyle@bywatersolutions.com>
249 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
250
251 =cut
252
253 1;