Lines 136-141
sub guarantor {
Link Here
|
136 |
return Koha::Patrons->find( $self->guarantorid() ); |
136 |
return Koha::Patrons->find( $self->guarantorid() ); |
137 |
} |
137 |
} |
138 |
|
138 |
|
|
|
139 |
=head3 filter |
140 |
|
141 |
my $patrons = Koha::Patrons->filter({}); |
142 |
|
143 |
This method returns a Koha::Patrons resultset that matches the passed filters; The primary objective of this method is |
144 |
to simplify the use of commonly required filters which would normally require complex joins. |
145 |
|
146 |
Available filters (all are optional and may be used in combination): |
147 |
|
148 |
=over 4 |
149 |
|
150 |
=item not_guarantors |
151 |
|
152 |
Boolean stating whether to filter out guarantors |
153 |
|
154 |
=item issued_before |
155 |
|
156 |
DateTime denoting last time the patrons were lent to |
157 |
|
158 |
=item has_issues |
159 |
|
160 |
Boolean stating whether to filter out patrons with current issues |
161 |
|
162 |
=item expired_before |
163 |
|
164 |
DateTime denoting beggining of period before which the users have expired |
165 |
|
166 |
=item amount_outstanding |
167 |
|
168 |
Number (decimal) denoting the total amount outstanding a use may have before being filtered out |
169 |
|
170 |
=item last_seen |
171 |
|
172 |
DateTime denoting the date since which the patron has not been seen |
173 |
|
174 |
=item branch |
175 |
|
176 |
String OR Arrayref of strings matching patron branchcodes to filter by |
177 |
|
178 |
=item categories |
179 |
|
180 |
String OR Arrayref of strings matching patron categories to filter by |
181 |
|
182 |
=item in_list |
183 |
|
184 |
String OR Arrayref of strings matching patron list ids to filter by |
185 |
|
186 |
=back |
187 |
|
188 |
=cut |
189 |
|
190 |
sub filter { |
191 |
my ( $class, $filters ) = @_; |
192 |
|
193 |
my $where = {}; |
194 |
my $attr = {}; |
195 |
my $dtf = Koha::Database->new->schema->storage->datetime_parser; |
196 |
|
197 |
# Track joins to prevent unrequired double joins |
198 |
my $joined_issues = 0; |
199 |
|
200 |
# Filter out all guarantors |
201 |
if ( $filters->{not_guarantors} ) { |
202 |
my $guarantorList = Koha::Patrons->search( |
203 |
{ guarantorid => [ { '!=' => 0 }, { '!=' => undef } ] }, |
204 |
{ select => ['borrowernumber'] } )->_resultset->as_query; |
205 |
|
206 |
push @{ $where->{'-and'} }, |
207 |
{ 'me.borrowernumber' => { '-not_in' => $guarantorList } }; |
208 |
} |
209 |
|
210 |
# Limit to patrons not issued to for at least X days |
211 |
if ( $filters->{issued_before} ) { |
212 |
push @{ $attr->{'join'} }, ( 'issues', 'old_issues' ); |
213 |
push @{ $attr->{'+select'} }, |
214 |
{ max => 'old_issues.timestamp', '-as' => 'lastissue' }; |
215 |
push @{ $attr->{'+as'} }, 'lastissue'; |
216 |
push @{ $attr->{'having'}->{'-and'} }, |
217 |
{ 'lastissue' => |
218 |
{ '<' => $dtf->format_datetime( $filters->{issued_before} ) } }; |
219 |
$joined_issues++; |
220 |
} |
221 |
|
222 |
# Limit to patrons without any current issues |
223 |
if ( defined( $filters->{has_issues} ) && !$filters->{has_issues} ) { |
224 |
push @{ $attr->{'join'} }, ('issues') unless $joined_issues; |
225 |
push @{ $attr->{'+select'} }, |
226 |
{ max => 'issues.timestamp', '-as' => 'currentissue' }; |
227 |
push @{ $attr->{'+as'} }, 'currentissue'; |
228 |
push @{ $attr->{'having'}->{'-and'} }, { 'currentissue' => undef }; |
229 |
} |
230 |
|
231 |
# Limit to patrons expired more than X days |
232 |
if ( $filters->{expired_before} ) { |
233 |
push @{ $where->{'-and'} }, |
234 |
{ 'dateexpiry' => |
235 |
{ '<' => $dtf->format_datetime( $filters->{expired_before} ) } }; |
236 |
} |
237 |
|
238 |
# Limit to patrons not owing more than X in fines |
239 |
if ( defined( $filters->{amount_outstanding} ) ) { |
240 |
push @{ $attr->{'join'} }, 'accountlines'; |
241 |
push @{ $attr->{'+select'} }, |
242 |
{ sum => 'accountlines.amountoutstanding', '-as' => 'outstanding' }; |
243 |
push @{ $attr->{'+as'} }, 'outstanding'; |
244 |
push @{ $attr->{'having'}->{'-and'} }, |
245 |
{ outstanding => { '<=' => $filters->{amount_outstanding} } }; |
246 |
} |
247 |
|
248 |
# Limit to patrons not seen for at least X days |
249 |
if ( $filters->{last_seen} ) { |
250 |
push @{ $where->{'-and'} }, |
251 |
{ lastseen => |
252 |
{ '<' => $dtf->format_datetime( $filters->{last_seen} ) } }; |
253 |
} |
254 |
|
255 |
# Limit to patrons enrolled at branch X |
256 |
if ( $filters->{branch} ) { |
257 |
push @{ $where->{'-and'} }, { branchcode => $filters->{branch} }; |
258 |
} |
259 |
|
260 |
# Limit to patrons belonging to categories specified |
261 |
if ( @{ $filters->{categories} } ) { |
262 |
push @{ $where->{'-and'} }, { categorycode => $filters->{categories} }; |
263 |
} |
264 |
|
265 |
# Limit to patrons on patron list X |
266 |
if ( $filters->{in_list} ) { |
267 |
push @{ $attr->{'join'} }, 'patron_list_patrons'; |
268 |
push @{ $where->{'-and'} }, |
269 |
{ 'patron_list_patrons.patron_list_id' => $filters->{in_list} }; |
270 |
} |
271 |
|
272 |
# Group by borrowernumber |
273 |
$attr->{group_by} = 'me.borrowernumber'; |
274 |
|
275 |
return $class->search( $where, $attr ); |
276 |
} |
277 |
|
139 |
=head3 search_patrons_to_anonymise |
278 |
=head3 search_patrons_to_anonymise |
140 |
|
279 |
|
141 |
my $patrons = Koha::Patrons->search_patrons_to_anonymise( { before => $older_than_date, [ library => $library ] } ); |
280 |
my $patrons = Koha::Patrons->search_patrons_to_anonymise( { before => $older_than_date, [ library => $library ] } ); |
142 |
- |
|
|