View | Details | Raw Unified | Return to bug 11983
Collapse All | Expand All

(-)a/Koha/Patrons.pm (-1 / +258 lines)
Lines 62-67 sub search_limited { Link Here
62
    return $self->search( $params, $attributes );
62
    return $self->search( $params, $attributes );
63
}
63
}
64
64
65
=head3 filter_by_guarantors
66
67
    Koha::Patrons->filter_by_guarantors(1);
68
69
Returns patrons filtered by whether they are guarantors or not.
70
71
=head4 options
72
73
=over 4
74
75
=item undef - do not apply filter
76
77
=item 0 - limit to NOT guarantors 
78
79
=item 1 - limit to ONLY guarantors
80
81
=back
82
83
=cut
84
85
sub filter_by_guarantors {
86
    my ( $self, $option ) = @_;
87
88
    return $self unless defined($option);
89
90
    my $guarantorList = Koha::Patrons->search(
91
        { guarantorid => [ { '!=' => 0 }, { '!=' => undef } ] },
92
        { select      => ['borrowernumber'] } )->_resultset->as_query;
93
94
    # Patrons who are guarantors
95
    return $self->search(
96
        { 'me.borrowernumber' => { '-in' => $guarantorList } } )
97
      if $option;
98
99
    # Patrons who are not guarantors
100
    return $self->search(
101
        { 'me.borrowernumber' => { '-not_in' => $guarantorList } } );
102
}
103
104
=head3 filter_by_last_issued
105
106
    Koha::Patrons->filter_by_last_issued( { after => DateTime, before => DateTime );
107
108
Returns patrons filtered by whether their last issue falls betwen the passed limits.
109
110
=head4 arguments hashref
111
112
=over 4
113
114
=item before (optional) - filter out patrons whose last_issue was since DateTime
115
116
=item after (optional) - filter out patrons whose last_issue has been since DateTime
117
118
=back
119
120
=cut
121
122
sub filter_by_last_issued {
123
    my ( $self, $options ) = @_;
124
125
    return $self
126
      unless ( defined($options)
127
        && ( $options->{before} || $options->{after} ) );
128
129
    my $where = {};
130
    my $attrs = {
131
        join      => 'old_issues',
132
        '+select' => { max => 'old_issues.timestamp', '-as' => 'last_issued' },
133
        '+as'     => 'last_issued'
134
    };
135
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
136
137
    push @{ $attrs->{'having'}->{'-and'} },
138
      { 'last_issued' => { '<' => $dtf->format_datetime( $options->{before} ) }
139
      }
140
      if $options->{before};
141
142
    push @{ $attrs->{'having'}->{'-and'} },
143
      { 'last_issued' => { '>' => $dtf->format_datetime( $options->{after} ) } }
144
      if $options->{after};
145
146
    return $self->search( $where, $attrs );
147
}
148
149
=head3 filter_by_has_issues
150
151
    Koha::Patrons->filter_by_has_issues(1);
152
153
Returns patrons filtered by whether they have current issues or not.
154
155
=head4 options
156
157
=over 4
158
159
=item undef - do not apply filter
160
161
=item 0 - limit to patrons WITHOUT current issues 
162
163
=item 1 - limit to patrons WITH current issues
164
165
=back
166
167
=cut
168
169
sub filter_by_has_issues {
170
    my ( $self, $option ) = @_;
171
172
    return $self
173
      unless defined($option);
174
175
    my $where = {};
176
    my $attrs = {
177
        join      => 'issues',
178
        '+select' => { max => 'issues.timestamp', '-as' => 'current_issue' },
179
        '+as'     => 'current_issue'
180
    };
181
182
    # Patrons who have current issues
183
    if ($option) {
184
        $attrs->{'having'} = { 'currentissue' => { '!=' => undef } };
185
    }
186
187
    # Patrons who do not have current issues
188
    else {
189
        $attrs->{'having'} = { 'currentissue' => undef };
190
191
    }
192
193
    return $self->search($where,$attrs);
194
}
195
196
197
=head3 filter_by_expired_date
198
199
    Koha::Patrons->filter_by_expired_date( { after => DateTime, before => DateTime );
200
201
Returns patrons filtered by whether their accounts expired between between the passed limits.
202
203
=head4 arguments hashref
204
205
=over 4
206
207
=item before (optional) - filter out patrons whose expired_date was since DateTime
208
209
=item after (optional) - filter out patrons whose expired_date has been since DateTime
210
211
=back
212
213
=cut
214
215
sub filter_by_expired_date {
216
    my ( $self, $options ) = @_;
217
218
    return $self
219
      unless ( defined($options)
220
        && ( $options->{before} || $options->{after} ) );
221
222
    my $where = {};
223
    my $attrs = {};
224
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
225
226
    push @{ $where->{'dateexpiry'} },
227
      { '<' => $dtf->format_datetime( $options->{before} ) }
228
      if $options->{before};
229
230
    push @{ $where->{'dateexpiry'} },
231
      { '>' => $dtf->format_datetime( $options->{after} ) }
232
      if $options->{after};
233
234
    return $self->search( $where, $attrs );
235
}
236
237
238
=head3 filter_by_amount_owed
239
240
    Koha::Patrons->filter_by_amount_owed( { less_than => '2.00', more_than => '0.50' } );
241
242
Returns patrons filtered by how much money they owe, between passed limits.
243
244
=head4 arguments hashref
245
246
=over 4
247
248
=item less_than (optional) - filter out patrons who owe less than Amount
249
250
=item more_than (optional) - filter out patrons who owe more than Amount
251
252
=back
253
254
=cut
255
256
sub filter_by_amount_owed {
257
    my ( $self, $options ) = @_;
258
259
    return $self
260
      unless ( defined($options)
261
        && ( $options->{less_than} || $options->{more_than} ) );
262
263
    my $where = {};
264
    my $attrs = {
265
        join => 'accountlines',
266
        '+select' =>
267
          { sum => 'accountlines.amountoutstanding', '-as' => 'outstanding' },
268
        '+as' => 'outstanding'
269
    };
270
271
    push @{ $attrs->{'having'}->{'-and'} },
272
      { 'outstanding' => { '<' => $options->{less_than} } }
273
      if defined( $options->{less_than} );
274
275
    push @{ $attrs->{'having'}->{'-and'} },
276
      { 'outstanding' => { '>' => $options->{more_than} } }
277
      if defined( $options->{more_than} );
278
279
    return $self->search( $where, $attrs );
280
}
281
282
283
=head3 filter_by_lists
284
285
    Koha::Patrons->filter_by_lists( {  on => [1], not_on => [2] } );
286
287
Returns patrons filtered by which lists they appear on.
288
289
=head4 arguments hashref
290
291
=over 4
292
293
=item on (optional) - filter out patrons who appear on List(s)
294
295
=item not_on (optional) - filter out patrons who do not appear on List(s)
296
297
=back
298
299
=cut
300
301
sub filter_by_lists {
302
    my ( $self, $options ) = @_;
303
304
    return $self
305
      unless ( defined($options)
306
        && ( $options->{on} || $options->{not_on} ) );
307
308
    my $where = {};
309
    my $attrs = { join => 'patron_list_patrons' };
310
311
    push @{ $attrs->{'where'}->{'-and'} },
312
      { 'patron_list_patrons.patron_list_id' => { '-in' => $options->{on} } }
313
      if defined( $options->{on} );
314
315
    push @{ $attrs->{'where'}->{'-and'} },
316
      { 'patron_list_patrons.patron_list_id' =>
317
          { '-not_in' => $options->{not_on} } }
318
      if defined( $options->{not_on} );
319
320
    return $self->search( $where, $attrs );
321
}
322
65
=head3 search_housebound_choosers
323
=head3 search_housebound_choosers
66
324
67
Returns all Patrons which are Housebound choosers.
325
Returns all Patrons which are Housebound choosers.
68
- 

Return to bug 11983