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

(-)a/Koha/Objects.pm (-12 / +21 lines)
Lines 233-246 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 => $x, from => $date1, to => $date2, days_inclusive => 1, datetime => 1,
236
    days => $days, from => $date1, to => $date2, days_inclusive => 1,
237
    older_than => $days, younger_than => $days,
238
    datetime => 1,
237
});
239
});
238
240
239
Note: days are exclusive by default (will be inclusive if days_inclusive is passed and set).
241
You should pass at least one of the parameters: days, from, to, older_than,
240
The parameters from and to are inclusive. They can be DateTime objects or date strings.
242
younger_than. Make sure that they do not conflict with each other to get
241
You should pass at least one of the parameters days, from or to.
243
meaningful results.
244
By default, from and to are inclusive and days is exclusive (unless you
245
passed the optional days_inclusive parameter).
246
By nature older_than and younger_than are exclusive. This cannot be changed.
242
The optional parameter datetime allows datetime comparison.
247
The optional parameter datetime allows datetime comparison.
243
248
249
The from and to parameters can be DateTime objects or date strings.
250
244
=cut
251
=cut
245
252
246
sub filter_by_last_update {
253
sub filter_by_last_update {
Lines 248-265 sub filter_by_last_update { Link Here
248
    my $timestamp_column_name = $params->{timestamp_column_name} || 'timestamp';
255
    my $timestamp_column_name = $params->{timestamp_column_name} || 'timestamp';
249
    my $days_inclusive = $params->{days_inclusive} || 0;
256
    my $days_inclusive = $params->{days_inclusive} || 0;
250
    my $conditions;
257
    my $conditions;
251
    Koha::Exceptions::MissingParameter->throw(
258
    Koha::Exceptions::MissingParameter->throw("Please pass: days|from|to|older_than|younger_than")
252
        "Missing mandatory parameter: days or from or to")
259
        unless grep { exists $params->{$_} } qw/days from to older_than younger_than/;
253
      unless exists $params->{days}
254
          or exists $params->{from}
255
          or exists $params->{to};
256
260
257
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
261
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
258
    my $method =  $params->{datetime} ? 'format_datetime' : 'format_date';
262
    my $method =  $params->{datetime} ? 'format_datetime' : 'format_date';
259
    if ( exists $params->{days} ) {
263
    foreach my $p ( qw/days older_than younger_than/  ) {
264
        next if !exists $params->{$p};
260
        my $dt = Koha::DateUtils::dt_from_string();
265
        my $dt = Koha::DateUtils::dt_from_string();
261
        my $operator = $days_inclusive ? '<=' : '<';
266
        my $operator = $p eq 'days' && $days_inclusive
262
        $conditions->{$operator} = $dtf->$method( $dt->subtract( days => $params->{days} ) );
267
            ? '<='
268
            : $p eq 'younger_than'
269
            ? '>'
270
            : '<';
271
        $conditions->{$operator} = $dtf->$method( $dt->subtract( days => $params->{$p} ) );
263
    }
272
    }
264
    if ( exists $params->{from} ) {
273
    if ( exists $params->{from} ) {
265
        my $from = ref($params->{from}) ? $params->{from} : dt_from_string($params->{from});
274
        my $from = ref($params->{from}) ? $params->{from} : dt_from_string($params->{from});
(-)a/t/db_dependent/Koha/Objects.t (-2 / +6 lines)
Lines 1188-1194 subtest "filter_by_last_update" => sub { Link Here
1188
        );
1188
        );
1189
    };
1189
    };
1190
1190
1191
    subtest 'Parameter datetime' => sub {
1191
    subtest 'Parameters datetime, 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 ) });
Lines 1199-1204 subtest "filter_by_last_update" => sub { Link Here
1199
            datetime => 1 })->count, 0, 'Yesterday, not truncated, one hour too late' );
1199
            datetime => 1 })->count, 0, 'Yesterday, not truncated, one hour too late' );
1200
        is( $rs->filter_by_last_update({ timestamp_column_name => 'updated_on', from => $now->clone->subtract( hours => 25 ),
1200
        is( $rs->filter_by_last_update({ timestamp_column_name => 'updated_on', from => $now->clone->subtract( hours => 25 ),
1201
            datetime => 1 })->count, 6, 'Yesterday - 1h, not truncated, within time frame' );
1201
            datetime => 1 })->count, 6, 'Yesterday - 1h, not truncated, within time frame' );
1202
1203
        is( $rs->filter_by_last_update({ timestamp_column_name => 'updated_on', younger_than => 2, older_than => 1 })->count,
1204
            0, 'when using dates, we will find nothing' );
1205
        is( $rs->filter_by_last_update({ timestamp_column_name => 'updated_on', younger_than => 2, older_than => 1, datetime => 1 })->count,
1206
            6, 'when using datetime, we will find them all' );
1202
    };
1207
    };
1203
1208
1204
    $schema->storage->txn_rollback;
1209
    $schema->storage->txn_rollback;
1205
- 

Return to bug 33837