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