Bug 18403: Refactor and add Koha::Patron->libraries_where_can_see_patrons
[koha.git] / Koha / Patrons.pm
1 package Koha::Patrons;
2
3 # Copyright 2014 ByWater Solutions
4 # Copyright 2016 Koha Development Team
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 Koha::Database;
26 use Koha::DateUtils;
27
28 use Koha::ArticleRequests;
29 use Koha::ArticleRequest::Status;
30 use Koha::Patron;
31
32 use base qw(Koha::Objects);
33
34 =head1 NAME
35
36 Koha::Patron - Koha Patron Object class
37
38 =head1 API
39
40 =head2 Class Methods
41
42 =cut
43
44 =head3 search_limited
45
46 my $patrons = Koha::Patrons->search_limit( $params, $attributes );
47
48 Returns all the patrons the logged in user is allowed to see
49
50 =cut
51
52 sub search_limited {
53     my ( $self, $params, $attributes ) = @_;
54
55     my $userenv = C4::Context->userenv;
56     my @restricted_branchcodes;
57     if ( $userenv ) {
58         my $logged_in_user = Koha::Patrons->find( $userenv->{number} );
59         @restricted_branchcodes = $logged_in_user->libraries_where_can_see_patrons;
60     }
61     $params->{'me.branchcode'} = { -in => \@restricted_branchcodes } if @restricted_branchcodes;
62     return $self->search( $params, $attributes );
63 }
64
65 =head3 search_housebound_choosers
66
67 Returns all Patrons which are Housebound choosers.
68
69 =cut
70
71 sub search_housebound_choosers {
72     my ( $self ) = @_;
73     my $cho = $self->_resultset
74         ->search_related('housebound_role', {
75             housebound_chooser => 1,
76         })->search_related('borrowernumber');
77     return Koha::Patrons->_new_from_dbic($cho);
78 }
79
80 =head3 search_housebound_deliverers
81
82 Returns all Patrons which are Housebound deliverers.
83
84 =cut
85
86 sub search_housebound_deliverers {
87     my ( $self ) = @_;
88     my $del = $self->_resultset
89         ->search_related('housebound_role', {
90             housebound_deliverer => 1,
91         })->search_related('borrowernumber');
92     return Koha::Patrons->_new_from_dbic($del);
93 }
94
95 =head3 search_upcoming_membership_expires
96
97 my $patrons = Koha::Patrons->search_upcoming_membership_expires();
98
99 The 'before' and 'after' represent the number of days before/after the date
100 that is set by the preference MembershipExpiryDaysNotice.
101 If the pref is 14, before 2 and after 3 then you will get all expires
102 from 12 to 17 days.
103
104 =cut
105
106 sub search_upcoming_membership_expires {
107     my ( $self, $params ) = @_;
108     my $before = $params->{before} || 0;
109     my $after  = $params->{after} || 0;
110     delete $params->{before};
111     delete $params->{after};
112
113     my $days = C4::Context->preference("MembershipExpiryDaysNotice") || 0;
114     my $date_before = dt_from_string->add( days => $days - $before );
115     my $date_after = dt_from_string->add( days => $days + $after );
116     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
117
118     $params->{dateexpiry} = {
119         ">=" => $dtf->format_date( $date_before ),
120         "<=" => $dtf->format_date( $date_after ),
121     };
122     return $self->SUPER::search(
123         $params, { join => ['branchcode', 'categorycode'] }
124     );
125 }
126
127 =head3 guarantor
128
129 Returns a Koha::Patron object for this borrower's guarantor
130
131 =cut
132
133 sub guarantor {
134     my ( $self ) = @_;
135
136     return Koha::Patrons->find( $self->guarantorid() );
137 }
138
139 =head3 search_patrons_to_anonymise
140
141     my $patrons = Koha::Patrons->search_patrons_to_anonymise( { before => $older_than_date, [ library => $library ] } );
142
143 This method returns all patrons who has an issue history older than a given date.
144
145 =cut
146
147 sub search_patrons_to_anonymise {
148     my ( $class, $params ) = @_;
149     my $older_than_date = $params->{before};
150     my $library         = $params->{library};
151     $older_than_date = $older_than_date ? dt_from_string($older_than_date) : dt_from_string;
152     $library ||=
153       ( C4::Context->preference('IndependentBranches') && C4::Context->userenv && !C4::Context->IsSuperLibrarian() && C4::Context->userenv->{branch} )
154       ? C4::Context->userenv->{branch}
155       : undef;
156
157     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
158     my $rs = $class->_resultset->search(
159         {   returndate                  => { '<'   =>  $dtf->format_datetime($older_than_date), },
160             'old_issues.borrowernumber' => { 'not' => undef },
161             privacy                     => { '<>'  => 0 },                  # Keep forever
162             ( $library ? ( 'old_issues.branchcode' => $library ) : () ),
163         },
164         {   join     => ["old_issues"],
165             group_by => 'borrowernumber'
166         }
167     );
168     return Koha::Patrons->_new_from_dbic($rs);
169 }
170
171 =head3 anonymise_issue_history
172
173     Koha::Patrons->search->anonymise_issue_history( { [ before => $older_than_date ] } );
174
175 Anonymise issue history (old_issues) for all patrons older than the given date (optional).
176 To make sure all the conditions are met, the caller has the responsibility to
177 call search_patrons_to_anonymise to filter the Koha::Patrons set
178
179 =cut
180
181 sub anonymise_issue_history {
182     my ( $self, $params ) = @_;
183
184     my $older_than_date = $params->{before};
185
186     $older_than_date = dt_from_string $older_than_date if $older_than_date;
187
188     # The default of 0 does not work due to foreign key constraints
189     # The anonymisation should not fail quietly if AnonymousPatron is not a valid entry
190     # Set it to undef (NULL)
191     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
192     my $nb_rows = 0;
193     while ( my $patron = $self->next ) {
194         my $old_issues_to_anonymise = $patron->old_checkouts->search(
195         {
196             (
197                 $older_than_date
198                 ? ( returndate =>
199                       { '<' => $dtf->format_datetime($older_than_date) } )
200                 : ()
201             )
202         }
203         );
204         my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef;
205         $nb_rows += $old_issues_to_anonymise->update( { 'old_issues.borrowernumber' => $anonymous_patron } );
206     }
207     return $nb_rows;
208 }
209
210 =head3 type
211
212 =cut
213
214 sub _type {
215     return 'Borrower';
216 }
217
218 sub object_class {
219     return 'Koha::Patron';
220 }
221
222 =head1 AUTHOR
223
224 Kyle M Hall <kyle@bywatersolutions.com>
225
226 =cut
227
228 1;