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

(-)a/Koha/Objects.pm (-6 / +4 lines)
Lines 235-241 sub update { Link Here
235
my $filtered_objects = $objects->filter_by_last_update({
235
my $filtered_objects = $objects->filter_by_last_update({
236
    from => $date1, to => $date2,
236
    from => $date1, to => $date2,
237
    days|older_than => $days, min_days => $days, younger_than => $days,
237
    days|older_than => $days, min_days => $days, younger_than => $days,
238
    datetime => 1,
239
});
238
});
240
239
241
You should pass at least one of the parameters: from, to, days|older_than,
240
You should pass at least one of the parameters: from, to, days|older_than,
Lines 243-249 min_days or younger_than. Make sure that they do not conflict with each other Link Here
243
to get meaningful results.
242
to get meaningful results.
244
Note: from, to and min_days are inclusive! And by nature days|older_than
243
Note: from, to and min_days are inclusive! And by nature days|older_than
245
and younger_than are exclusive.
244
and younger_than are exclusive.
246
The optional parameter datetime allows datetime comparison.
247
245
248
The from and to parameters can be DateTime objects or date strings.
246
The from and to parameters can be DateTime objects or date strings.
249
247
Lines 257-276 sub filter_by_last_update { Link Here
257
        unless grep { exists $params->{$_} } qw/days from to older_than younger_than min_days/;
255
        unless grep { exists $params->{$_} } qw/days from to older_than younger_than min_days/;
258
256
259
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
257
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
260
    my $method =  $params->{datetime} ? 'format_datetime' : 'format_date';
261
    foreach my $p ( qw/days older_than younger_than min_days/  ) {
258
    foreach my $p ( qw/days older_than younger_than min_days/  ) {
262
        next if !exists $params->{$p};
259
        next if !exists $params->{$p};
263
        my $dt = Koha::DateUtils::dt_from_string();
260
        my $dt = Koha::DateUtils::dt_from_string();
264
        my $operator = { days => '<', older_than => '<', min_days => '<=' }->{$p} // '>';
261
        my $operator = { days => '<', older_than => '<', min_days => '<=' }->{$p} // '>';
265
        $conditions->{$operator} = $dtf->$method( $dt->subtract( days => $params->{$p} ) );
262
        $dt->subtract( days => $params->{$p} )->truncate( to => 'day' );
263
        $conditions->{$operator} = $dtf->format_datetime( $dt );
266
    }
264
    }
267
    if ( exists $params->{from} ) {
265
    if ( exists $params->{from} ) {
268
        my $from = ref($params->{from}) ? $params->{from} : dt_from_string($params->{from});
266
        my $from = ref($params->{from}) ? $params->{from} : dt_from_string($params->{from});
269
        $conditions->{'>='} = $dtf->$method( $from );
267
        $conditions->{'>='} = $dtf->format_datetime( $from );
270
    }
268
    }
271
    if ( exists $params->{to} ) {
269
    if ( exists $params->{to} ) {
272
        my $to = ref($params->{to}) ? $params->{to} : dt_from_string($params->{to});
270
        my $to = ref($params->{to}) ? $params->{to} : dt_from_string($params->{to});
273
        $conditions->{'<='} = $dtf->$method( $to );
271
        $conditions->{'<='} = $dtf->format_datetime( $to );
274
    }
272
    }
275
273
276
    return $self->search(
274
    return $self->search(
(-)a/misc/cronjobs/cleanup_database.pl (+6 lines)
Lines 673-678 if ($pTransfers) { Link Here
673
673
674
if (defined $pPseudoTransactions or $pPseudoTransactionsFrom or $pPseudoTransactionsTo ) {
674
if (defined $pPseudoTransactions or $pPseudoTransactionsFrom or $pPseudoTransactionsTo ) {
675
    print "Purging pseudonymized transactions\n" if $verbose;
675
    print "Purging pseudonymized transactions\n" if $verbose;
676
    if ($pPseudoTransactionsTo) {
677
        $pPseudoTransactionsTo = dt_from_string($pPseudoTransactionsTo);
678
        if ( $pPseudoTransactionsTo->hour == 0 && $pPseudoTransactionsTo->minute == 0 ) {
679
            $pPseudoTransactionsTo->set( hour => 23, minute => 59, second => 59 );
680
        }
681
    }
676
    my $anonymized_transactions = Koha::PseudonymizedTransactions->filter_by_last_update(
682
    my $anonymized_transactions = Koha::PseudonymizedTransactions->filter_by_last_update(
677
        {
683
        {
678
            timestamp_column_name => 'datetime',
684
            timestamp_column_name => 'datetime',
(-)a/t/db_dependent/Koha/Objects.t (-16 / +31 lines)
Lines 1188-1209 subtest "filter_by_last_update" => sub { Link Here
1188
        );
1188
        );
1189
    };
1189
    };
1190
1190
1191
    subtest 'Parameters datetime, older_than, younger_than' => sub {
1191
    subtest 'Parameters older_than, younger_than' => sub {
1192
        my $now = dt_from_string();
1192
        my $now = dt_from_string();
1193
        my $rs = Koha::Patrons->search({ borrowernumber => { -in => \@borrowernumbers } } );
1193
        my $rs  = Koha::Patrons->search( { borrowernumber => { -in => \@borrowernumbers } } );
1194
        $rs->update({ updated_on => $now->clone->subtract( hours => 25 ) });
1194
        $rs->update( { updated_on => $now->clone->subtract( hours => 25 ) } );
1195
        is( $rs->filter_by_last_update({ timestamp_column_name => 'updated_on', from => $now })->count, 0, 'All updated yesterday' );
1195
        is(
1196
        is( $rs->filter_by_last_update({ timestamp_column_name => 'updated_on', from => $now->clone->subtract( days => 1 ) })->count,
1196
            $rs->filter_by_last_update( { timestamp_column_name => 'updated_on', from => $now } )->count, 0,
1197
            6, 'Yesterday, truncated from is inclusive' );
1197
            'All updated yesterday'
1198
        is( $rs->filter_by_last_update({ timestamp_column_name => 'updated_on', from => $now->clone->subtract( days => 1 ),
1198
        );
1199
            datetime => 1 })->count, 0, 'Yesterday, not truncated, one hour too late' );
1199
        is(
1200
        is( $rs->filter_by_last_update({ timestamp_column_name => 'updated_on', from => $now->clone->subtract( hours => 25 ),
1200
            $rs->filter_by_last_update(
1201
            datetime => 1 })->count, 6, 'Yesterday - 1h, not truncated, within time frame' );
1201
                {
1202
1202
                    timestamp_column_name => 'updated_on',
1203
        is( $rs->filter_by_last_update({ timestamp_column_name => 'updated_on', younger_than => 2, older_than => 1 })->count,
1203
                    from                  => $now->clone->subtract( days => 1 )->truncate( to => 'day' )
1204
            0, 'when using dates, we will find nothing' );
1204
                }
1205
        is( $rs->filter_by_last_update({ timestamp_column_name => 'updated_on', younger_than => 2, older_than => 1, datetime => 1 })->count,
1205
            )->count,
1206
            6, 'when using datetime, we will find them all' );
1206
            6,
1207
            'Yesterday, truncated from is inclusive'
1208
        );
1209
        is(
1210
            $rs->filter_by_last_update(
1211
                { timestamp_column_name => 'updated_on', from => $now->clone->subtract( days => 1 ), }
1212
            )->count,
1213
            0,
1214
            'Yesterday, not truncated, one hour too late'
1215
        );
1216
        is(
1217
            $rs->filter_by_last_update(
1218
                { timestamp_column_name => 'updated_on', from => $now->clone->subtract( hours => 25 ), }
1219
            )->count,
1220
            6,
1221
            'Yesterday - 1h, not truncated, within time frame'
1222
        );
1207
    };
1223
    };
1208
1224
1209
    $schema->storage->txn_rollback;
1225
    $schema->storage->txn_rollback;
1210
- 

Return to bug 33837