Bug 16554: rewrite mandatory and sample data - pl-PL
[koha.git] / Koha / Patron.pm
blobb5449a30a3f11ccbd573752596ee253151704a01
1 package Koha::Patron;
3 # Copyright ByWater Solutions 2014
4 # Copyright PTFS Europe 2016
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 C4::Context;
26 use C4::Log;
27 use Koha::Database;
28 use Koha::DateUtils;
29 use Koha::Issues;
30 use Koha::OldIssues;
31 use Koha::Patron::Categories;
32 use Koha::Patron::Images;
33 use Koha::Patrons;
35 use base qw(Koha::Object);
37 =head1 NAME
39 Koha::Patron - Koha Patron Object class
41 =head1 API
43 =head2 Class Methods
45 =cut
47 =head3 guarantor
49 Returns a Koha::Patron object for this patron's guarantor
51 =cut
53 sub guarantor {
54 my ( $self ) = @_;
56 return unless $self->guarantorid();
58 return Koha::Patrons->find( $self->guarantorid() );
61 sub image {
62 my ( $self ) = @_;
64 return Koha::Patron::Images->find( $self->borrowernumber )
67 =head3 guarantees
69 Returns the guarantees (list of Koha::Patron) of this patron
71 =cut
73 sub guarantees {
74 my ( $self ) = @_;
76 return Koha::Patrons->search( { guarantorid => $self->borrowernumber } );
79 =head3 siblings
81 Returns the siblings of this patron.
83 =cut
85 sub siblings {
86 my ( $self ) = @_;
88 my $guarantor = $self->guarantor;
90 return unless $guarantor;
92 return Koha::Patrons->search(
94 guarantorid => {
95 '!=' => undef,
96 '=' => $guarantor->id,
98 borrowernumber => {
99 '!=' => $self->borrowernumber,
105 =head3 wants_check_for_previous_checkout
107 $wants_check = $patron->wants_check_for_previous_checkout;
109 Return 1 if Koha needs to perform PrevIssue checking, else 0.
111 =cut
113 sub wants_check_for_previous_checkout {
114 my ( $self ) = @_;
115 my $syspref = C4::Context->preference("checkPrevCheckout");
117 # Simple cases
118 ## Hard syspref trumps all
119 return 1 if ($syspref eq 'hardyes');
120 return 0 if ($syspref eq 'hardno');
121 ## Now, patron pref trumps all
122 return 1 if ($self->checkprevcheckout eq 'yes');
123 return 0 if ($self->checkprevcheckout eq 'no');
125 # More complex: patron inherits -> determine category preference
126 my $checkPrevCheckoutByCat = Koha::Patron::Categories
127 ->find($self->categorycode)->checkprevcheckout;
128 return 1 if ($checkPrevCheckoutByCat eq 'yes');
129 return 0 if ($checkPrevCheckoutByCat eq 'no');
131 # Finally: category preference is inherit, default to 0
132 if ($syspref eq 'softyes') {
133 return 1;
134 } else {
135 return 0;
139 =head3 do_check_for_previous_checkout
141 $do_check = $patron->do_check_for_previous_checkout($item);
143 Return 1 if the bib associated with $ITEM has previously been checked out to
144 $PATRON, 0 otherwise.
146 =cut
148 sub do_check_for_previous_checkout {
149 my ( $self, $item ) = @_;
151 # Find all items for bib and extract item numbers.
152 my @items = Koha::Items->search({biblionumber => $item->{biblionumber}});
153 my @item_nos;
154 foreach my $item (@items) {
155 push @item_nos, $item->itemnumber;
158 # Create (old)issues search criteria
159 my $criteria = {
160 borrowernumber => $self->borrowernumber,
161 itemnumber => \@item_nos,
164 # Check current issues table
165 my $issues = Koha::Issues->search($criteria);
166 return 1 if $issues->count; # 0 || N
168 # Check old issues table
169 my $old_issues = Koha::OldIssues->search($criteria);
170 return $old_issues->count; # 0 || N
173 =head2 is_debarred
175 my $debarment_expiration = $patron->is_debarred;
177 Returns the date a patron debarment will expire, or undef if the patron is not
178 debarred
180 =cut
182 sub is_debarred {
183 my ($self) = @_;
185 return unless $self->debarred;
186 return $self->debarred
187 if $self->debarred =~ '^9999'
188 or dt_from_string( $self->debarred ) > dt_from_string;
189 return;
192 =head2 update_password
194 my $updated = $patron->update_password( $userid, $password );
196 Update the userid and the password of a patron.
197 If the userid already exists, returns and let DBIx::Class warns
198 This will add an entry to action_logs if BorrowersLog is set.
200 =cut
202 sub update_password {
203 my ( $self, $userid, $password ) = @_;
204 eval { $self->userid($userid)->store; };
205 return if $@; # Make sure the userid is not already in used by another patron
206 $self->password($password)->store;
207 logaction( "MEMBERS", "CHANGE PASS", $self->borrowernumber, "" ) if C4::Context->preference("BorrowersLog");
208 return 1;
211 =head3 renew_account
213 my $new_expiry_date = $patron->renew_account
215 Extending the subscription to the expiry date.
217 =cut
219 sub renew_account {
220 my ($self) = @_;
222 my $date =
223 C4::Context->preference('BorrowerRenewalPeriodBase') eq 'dateexpiry'
224 ? dt_from_string( $self->dateexpiry )
225 : dt_from_string;
226 my $patron_category = Koha::Patron::Categories->find( $self->categorycode ); # FIXME Should be $self->category
227 my $expiry_date = $patron_category->get_expiry_date($date);
229 $self->dateexpiry($expiry_date)->store;
231 C4::Members::AddEnrolmentFeeIfNeeded( $self->categorycode, $self->borrowernumber );
233 logaction( "MEMBERS", "RENEW", $self->borrowernumber, "Membership renewed" ) if C4::Context->preference("BorrowersLog");
234 return dt_from_string( $expiry_date )->truncate( to => 'day' );
237 =head2 has_overdues
239 my $has_overdues = $patron->has_overdues;
241 Returns the number of patron's overdues
243 =cut
245 sub has_overdues {
246 my ($self) = @_;
247 my $dtf = Koha::Database->new->schema->storage->datetime_parser;
248 return $self->_result->issues->search({ date_due => { '<' => $dtf->format_datetime( dt_from_string() ) } })->count;
251 =head2 track_login
253 $patron->track_login;
254 $patron->track_login({ force => 1 });
256 Tracks a (successful) login attempt.
257 The preference TrackLastPatronActivity must be enabled. Or you
258 should pass the force parameter.
260 =cut
262 sub track_login {
263 my ( $self, $params ) = @_;
264 return if
265 !$params->{force} &&
266 !C4::Context->preference('TrackLastPatronActivity');
267 $self->lastseen( dt_from_string() )->store;
270 =head3 type
272 =cut
274 sub _type {
275 return 'Borrower';
278 =head1 AUTHOR
280 Kyle M Hall <kyle@bywatersolutions.com>
281 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
283 =cut