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

(-)a/Koha/Objects.pm (-16 / +10 lines)
Lines 233-249 sub update { Link Here
233
=head3 filter_by_last_update
233
=head3 filter_by_last_update
234
234
235
my $filtered_objects = $objects->filter_by_last_update({
235
my $filtered_objects = $objects->filter_by_last_update({
236
    days => $days, from => $date1, to => $date2, days_inclusive => 1,
236
    from => $date1, to => $date2,
237
    older_than => $days, younger_than => $days,
237
    days|older_than => $days, min_days => $days, younger_than => $days,
238
    datetime => 1,
238
    datetime => 1,
239
});
239
});
240
240
241
You should pass at least one of the parameters: days, from, to, older_than,
241
You should pass at least one of the parameters: from, to, days|older_than,
242
younger_than. Make sure that they do not conflict with each other to get
242
min_days or younger_than. Make sure that they do not conflict with each other
243
meaningful results.
243
to get meaningful results.
244
By default, from and to are inclusive and days is exclusive (unless you
244
Note: from, to and min_days are inclusive! And by nature days|older_than
245
passed the optional days_inclusive parameter).
245
and younger_than are exclusive.
246
By nature older_than and younger_than are exclusive. This cannot be changed.
247
The optional parameter datetime allows datetime comparison.
246
The optional parameter datetime allows datetime comparison.
248
247
249
The from and to parameters can be DateTime objects or date strings.
248
The from and to parameters can be DateTime objects or date strings.
Lines 253-273 The from and to parameters can be DateTime objects or date strings. Link Here
253
sub filter_by_last_update {
252
sub filter_by_last_update {
254
    my ( $self, $params ) = @_;
253
    my ( $self, $params ) = @_;
255
    my $timestamp_column_name = $params->{timestamp_column_name} || 'timestamp';
254
    my $timestamp_column_name = $params->{timestamp_column_name} || 'timestamp';
256
    my $days_inclusive = $params->{days_inclusive} || 0;
257
    my $conditions;
255
    my $conditions;
258
    Koha::Exceptions::MissingParameter->throw("Please pass: days|from|to|older_than|younger_than")
256
    Koha::Exceptions::MissingParameter->throw("Please pass: days|from|to|older_than|younger_than")
259
        unless grep { exists $params->{$_} } qw/days from to older_than younger_than/;
257
        unless grep { exists $params->{$_} } qw/days from to older_than younger_than min_days/;
260
258
261
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
259
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
262
    my $method =  $params->{datetime} ? 'format_datetime' : 'format_date';
260
    my $method =  $params->{datetime} ? 'format_datetime' : 'format_date';
263
    foreach my $p ( qw/days older_than younger_than/  ) {
261
    foreach my $p ( qw/days older_than younger_than min_days/  ) {
264
        next if !exists $params->{$p};
262
        next if !exists $params->{$p};
265
        my $dt = Koha::DateUtils::dt_from_string();
263
        my $dt = Koha::DateUtils::dt_from_string();
266
        my $operator = $p eq 'days' && $days_inclusive
264
        my $operator = { days => '<', older_than => '<', min_days => '<=' }->{$p} // '>';
267
            ? '<='
268
            : $p eq 'younger_than'
269
            ? '>'
270
            : '<';
271
        $conditions->{$operator} = $dtf->$method( $dt->subtract( days => $params->{$p} ) );
265
        $conditions->{$operator} = $dtf->$method( $dt->subtract( days => $params->{$p} ) );
272
    }
266
    }
273
    if ( exists $params->{from} ) {
267
    if ( exists $params->{from} ) {
(-)a/Koha/Patrons.pm (-2 / +1 lines)
Lines 205-212 sub filter_by_expiration_date { Link Here
205
    return $class->filter_by_last_update(
205
    return $class->filter_by_last_update(
206
        {
206
        {
207
            timestamp_column_name => 'dateexpiry',
207
            timestamp_column_name => 'dateexpiry',
208
            days                  => $params->{days} || 0,
208
            min_days              => $params->{days} || 0,
209
            days_inclusive        => 1,
210
        }
209
        }
211
    );
210
    );
212
}
211
}
(-)a/t/db_dependent/Koha/Objects.t (-3 / +3 lines)
Lines 1126-1132 subtest "filter_by_last_update" => sub { Link Here
1126
    is( $count, 3, '3 patrons have been updated before the last 2 days (exclusive)' );
1126
    is( $count, 3, '3 patrons have been updated before the last 2 days (exclusive)' );
1127
1127
1128
    $count = $patrons->filter_by_last_update(
1128
    $count = $patrons->filter_by_last_update(
1129
        { timestamp_column_name => 'updated_on', days => 2, days_inclusive => 1 } )->count;
1129
        { timestamp_column_name => 'updated_on', min_days => 2 } )->count;
1130
    is( $count, 4, '4 patrons have been updated before the last 2 days (inclusive)' );
1130
    is( $count, 4, '4 patrons have been updated before the last 2 days (inclusive)' );
1131
1131
1132
    $count = $patrons->filter_by_last_update(
1132
    $count = $patrons->filter_by_last_update(
Lines 1134-1140 subtest "filter_by_last_update" => sub { Link Here
1134
    is( $count, 4, '4 patrons have been updated before yesterday (exclusive)' );
1134
    is( $count, 4, '4 patrons have been updated before yesterday (exclusive)' );
1135
1135
1136
    $count = $patrons->filter_by_last_update(
1136
    $count = $patrons->filter_by_last_update(
1137
        { timestamp_column_name => 'updated_on', days => 1, days_inclusive => 1 } )->count;
1137
        { timestamp_column_name => 'updated_on', min_days => 1 } )->count;
1138
    is( $count, 5, '5 patrons have been updated before yesterday (inclusive)' );
1138
    is( $count, 5, '5 patrons have been updated before yesterday (inclusive)' );
1139
1139
1140
    $count = $patrons->filter_by_last_update(
1140
    $count = $patrons->filter_by_last_update(
Lines 1142-1148 subtest "filter_by_last_update" => sub { Link Here
1142
    is( $count, 5, '5 patrons have been updated before today (exclusive)' );
1142
    is( $count, 5, '5 patrons have been updated before today (exclusive)' );
1143
1143
1144
    $count = $patrons->filter_by_last_update(
1144
    $count = $patrons->filter_by_last_update(
1145
        { timestamp_column_name => 'updated_on', days => 0, days_inclusive => 1 } )->count;
1145
        { timestamp_column_name => 'updated_on', min_days => 0 } )->count;
1146
    is( $count, 6, '6 patrons have been updated before today (inclusive)' );
1146
    is( $count, 6, '6 patrons have been updated before today (inclusive)' );
1147
1147
1148
    $count = $patrons->filter_by_last_update(
1148
    $count = $patrons->filter_by_last_update(
(-)a/t/db_dependent/Koha/Old/Checkouts.t (-2 / +1 lines)
Lines 94-100 subtest 'anonymize() tests' => sub { Link Here
94
94
95
    # filter them so only the older two are part of the resultset
95
    # filter them so only the older two are part of the resultset
96
    my $checkouts = $patron->old_checkouts->filter_by_last_update(
96
    my $checkouts = $patron->old_checkouts->filter_by_last_update(
97
        { days => 1, days_inclusive => 1 } );
97
        { min_days => 1 } );
98
98
99
    t::lib::Mocks::mock_preference( 'AnonymousPatron', undef );
99
    t::lib::Mocks::mock_preference( 'AnonymousPatron', undef );
100
    throws_ok
100
    throws_ok
101
- 

Return to bug 33837