Bug 6906: Follow coding guidelines in terminology.
[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 Koha::Database;
27 use Koha::Issues;
28 use Koha::OldIssues;
29 use Koha::Patron::Categories;
30 use Koha::Patron::Images;
31 use Koha::Patrons;
32
33 use base qw(Koha::Object);
34
35 =head1 NAME
36
37 Koha::Patron - Koha Patron Object class
38
39 =head1 API
40
41 =head2 Class Methods
42
43 =cut
44
45 =head3 guarantor
46
47 Returns a Koha::Patron object for this patron's guarantor
48
49 =cut
50
51 sub guarantor {
52     my ( $self ) = @_;
53
54     return unless $self->guarantorid();
55
56     return Koha::Patrons->find( $self->guarantorid() );
57 }
58
59 sub image {
60     my ( $self ) = @_;
61
62     return Koha::Patron::Images->find( $self->borrowernumber )
63 }
64
65 =head3 guarantees
66
67 Returns the guarantees (list of Koha::Patron) of this patron
68
69 =cut
70
71 sub guarantees {
72     my ( $self ) = @_;
73
74     return Koha::Patrons->search( { guarantorid => $self->borrowernumber } );
75 }
76
77 =head3 siblings
78
79 Returns the siblings of this patron.
80
81 =cut
82
83 sub siblings {
84     my ( $self ) = @_;
85
86     my $guarantor = $self->guarantor;
87
88     return unless $guarantor;
89
90     return Koha::Patrons->search(
91         {
92             guarantorid => {
93                 '!=' => undef,
94                 '=' => $guarantor->id,
95             },
96             borrowernumber => {
97                 '!=' => $self->borrowernumber,
98             }
99         }
100     );
101 }
102
103 =head3 wants_check_for_previous_checkout
104
105     $wants_check = $patron->wants_check_for_previous_checkout;
106
107 Return 1 if Koha needs to perform PrevIssue checking, else 0.
108
109 =cut
110
111 sub wants_check_for_previous_checkout {
112     my ( $self ) = @_;
113     my $syspref = C4::Context->preference("checkPrevCheckout");
114
115     # Simple cases
116     ## Hard syspref trumps all
117     return 1 if ($syspref eq 'hardyes');
118     return 0 if ($syspref eq 'hardno');
119     ## Now, patron pref trumps all
120     return 1 if ($self->checkprevcheckout eq 'yes');
121     return 0 if ($self->checkprevcheckout eq 'no');
122
123     # More complex: patron inherits -> determine category preference
124     my $checkPrevCheckoutByCat = Koha::Patron::Categories
125         ->find($self->categorycode)->checkprevcheckout;
126     return 1 if ($checkPrevCheckoutByCat eq 'yes');
127     return 0 if ($checkPrevCheckoutByCat eq 'no');
128
129     # Finally: category preference is inherit, default to 0
130     if ($syspref eq 'softyes') {
131         return 1;
132     } else {
133         return 0;
134     }
135 }
136
137 =head3 do_check_for_previous_checkout
138
139     $do_check = $patron->do_check_for_previous_checkout($item);
140
141 Return 1 if the bib associated with $ITEM has previously been checked out to
142 $PATRON, 0 otherwise.
143
144 =cut
145
146 sub do_check_for_previous_checkout {
147     my ( $self, $item ) = @_;
148
149     # Find all items for bib and extract item numbers.
150     my @items = Koha::Items->search({biblionumber => $item->{biblionumber}});
151     my @item_nos;
152     foreach my $item (@items) {
153         push @item_nos, $item->itemnumber;
154     }
155
156     # Create (old)issues search criteria
157     my $criteria = {
158         borrowernumber => $self->borrowernumber,
159         itemnumber => \@item_nos,
160     };
161
162     # Check current issues table
163     my $issues = Koha::Issues->search($criteria);
164     return 1 if $issues->count; # 0 || N
165
166     # Check old issues table
167     my $old_issues = Koha::OldIssues->search($criteria);
168     return $old_issues->count;  # 0 || N
169 }
170
171 =head3 type
172
173 =cut
174
175 sub _type {
176     return 'Borrower';
177 }
178
179 =head1 AUTHOR
180
181 Kyle M Hall <kyle@bywatersolutions.com>
182 Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
183
184 =cut
185
186 1;