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

(-)a/Koha/Objects.pm (-13 / +15 lines)
Lines 25-31 use Class::Inspector; Link Here
25
25
26
use Koha::Database;
26
use Koha::Database;
27
use Koha::Exceptions::Object;
27
use Koha::Exceptions::Object;
28
use Koha::DateUtils qw( dt_from_string );
29
28
30
=head1 NAME
29
=head1 NAME
31
30
Lines 232-241 sub update { Link Here
232
231
233
=head3 filter_by_last_update
232
=head3 filter_by_last_update
234
233
235
my $filtered_objects = $objects->filter_by_last_update({
234
    my $filtered_objects = $objects->filter_by_last_update({
236
    from => $date1, to => $date2,
235
        from => $from_datetime, to => $to_datetime,
237
    days|older_than => $days, min_days => $days, younger_than => $days,
236
        days|older_than => $days, min_days => $days, younger_than => $days,
238
});
237
    });
239
238
240
You should pass at least one of the parameters: from, to, days|older_than,
239
You should pass at least one of the parameters: from, to, days|older_than,
241
min_days or younger_than. Make sure that they do not conflict with each other
240
min_days or younger_than. Make sure that they do not conflict with each other
Lines 243-249 to get meaningful results. Link Here
243
Note: from, to and min_days are inclusive! And by nature days|older_than
242
Note: from, to and min_days are inclusive! And by nature days|older_than
244
and younger_than are exclusive.
243
and younger_than are exclusive.
245
244
246
The from and to parameters can be DateTime objects or date strings.
245
The from and to parameters must be DateTime objects.
247
246
248
=cut
247
=cut
249
248
Lines 254-274 sub filter_by_last_update { Link Here
254
    Koha::Exceptions::MissingParameter->throw("Please pass: days|from|to|older_than|younger_than")
253
    Koha::Exceptions::MissingParameter->throw("Please pass: days|from|to|older_than|younger_than")
255
        unless grep { exists $params->{$_} } qw/days from to older_than younger_than min_days/;
254
        unless grep { exists $params->{$_} } qw/days from to older_than younger_than min_days/;
256
255
256
    foreach my $key (qw(from to)) {
257
        if (exists $params->{$key} and ref $params->{$key} ne 'DateTime') {
258
            Koha::Exceptions::WrongParameter->throw("'$key' parameter must be a DateTime object");
259
        }
260
    }
261
257
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
262
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
258
    foreach my $p ( qw/days older_than younger_than min_days/  ) {
263
    foreach my $p ( qw/days older_than younger_than min_days/  ) {
259
        next if !exists $params->{$p};
264
        next if !exists $params->{$p};
260
        my $dt = Koha::DateUtils::dt_from_string();
265
        my $days = $params->{$p};
261
        my $operator = { days => '<', older_than => '<', min_days => '<=' }->{$p} // '>';
266
        my $operator = { days => '<', older_than => '<', min_days => '<=' }->{$p} // '>';
262
        $dt->subtract( days => $params->{$p} )->truncate( to => 'day' );
267
        $conditions->{$operator} = \['DATE_SUB(CURDATE(), INTERVAL ? DAY)', $days];
263
        $conditions->{$operator} = $dtf->format_datetime( $dt );
264
    }
268
    }
265
    if ( exists $params->{from} ) {
269
    if ( exists $params->{from} ) {
266
        my $from = ref($params->{from}) ? $params->{from} : dt_from_string($params->{from});
270
        $conditions->{'>='} = $dtf->format_datetime( $params->{from} );
267
        $conditions->{'>='} = $dtf->format_datetime( $from );
268
    }
271
    }
269
    if ( exists $params->{to} ) {
272
    if ( exists $params->{to} ) {
270
        my $to = ref($params->{to}) ? $params->{to} : dt_from_string($params->{to});
273
        $conditions->{'<='} = $dtf->format_datetime( $params->{to} );
271
        $conditions->{'<='} = $dtf->format_datetime( $to );
272
    }
274
    }
273
275
274
    return $self->search(
276
    return $self->search(
(-)a/misc/cronjobs/cleanup_database.pl (+3 lines)
Lines 701-706 if ( defined $pPseudoTransactions or $pPseudoTransactionsFrom or $pPseudoTransac Link Here
701
            $pPseudoTransactionsTo->set( hour => 23, minute => 59, second => 59 );
701
            $pPseudoTransactionsTo->set( hour => 23, minute => 59, second => 59 );
702
        }
702
        }
703
    }
703
    }
704
    if ($pPseudoTransactionsFrom) {
705
        $pPseudoTransactionsFrom = dt_from_string($pPseudoTransactionsFrom);
706
    }
704
    my $anonymized_transactions = Koha::PseudonymizedTransactions->filter_by_last_update(
707
    my $anonymized_transactions = Koha::PseudonymizedTransactions->filter_by_last_update(
705
        {
708
        {
706
            timestamp_column_name => 'datetime',
709
            timestamp_column_name => 'datetime',
(-)a/t/db_dependent/Koha/Objects.t (-20 / +5 lines)
Lines 1162-1192 subtest "filter_by_last_update" => sub { Link Here
1162
    )->count;
1162
    )->count;
1163
    is( $count, 3, '3 patrons have been updated between D-4 and D-2' );
1163
    is( $count, 3, '3 patrons have been updated between D-4 and D-2' );
1164
1164
1165
    t::lib::Mocks::mock_preference( 'dateformat', 'metric' );
1165
    throws_ok {
1166
    try {
1167
        $count = $patrons->filter_by_last_update(
1166
        $count = $patrons->filter_by_last_update(
1168
            { timestamp_column_name => 'updated_on', from => '1970-12-31' } )
1167
            { timestamp_column_name => 'updated_on', from => '1970-12-31' } )
1169
          ->count;
1168
          ->count;
1170
    }
1169
    } 'Koha::Exceptions::WrongParameter', 'from parameter must be a DateTime object';
1171
    catch {
1170
    throws_ok {
1172
        ok(
1173
            $_->isa(
1174
                'No exception raised, from and to parameters can take an iso formatted date'
1175
            )
1176
        );
1177
    };
1178
    try {
1179
        $count = $patrons->filter_by_last_update(
1171
        $count = $patrons->filter_by_last_update(
1180
            { timestamp_column_name => 'updated_on', from => '31/12/1970' } )
1172
            { timestamp_column_name => 'updated_on', to => '1970-12-31' } )
1181
          ->count;
1173
          ->count;
1182
    }
1174
    } 'Koha::Exceptions::WrongParameter', 'to parameter must be a DateTime object';
1183
    catch {
1184
        ok(
1185
            $_->isa(
1186
                'No exception raised, from and to parameters can take an metric formatted date (depending on dateformat syspref)'
1187
            )
1188
        );
1189
    };
1190
1175
1191
    subtest 'Parameters older_than, younger_than' => sub {
1176
    subtest 'Parameters older_than, younger_than' => sub {
1192
        my $now = dt_from_string();
1177
        my $now = dt_from_string();
(-)a/tools/cleanborrowers.pl (-2 / +3 lines)
Lines 37-42 use CGI qw ( -utf8 ); Link Here
37
use C4::Auth qw( get_template_and_user );
37
use C4::Auth qw( get_template_and_user );
38
use C4::Output qw( output_html_with_http_headers );
38
use C4::Output qw( output_html_with_http_headers );
39
use C4::Members qw( GetBorrowersToExpunge );
39
use C4::Members qw( GetBorrowersToExpunge );
40
41
use Koha::DateUtils qw( dt_from_string );
40
use Koha::Old::Checkouts;
42
use Koha::Old::Checkouts;
41
use Koha::Patron::Categories;
43
use Koha::Patron::Categories;
42
use Koha::Patrons;
44
use Koha::Patrons;
Lines 149-155 elsif ( $op eq 'cud-delete' && $step == 3 ) { Link Here
149
        my $rows = Koha::Old::Checkouts
151
        my $rows = Koha::Old::Checkouts
150
                     ->filter_by_anonymizable
152
                     ->filter_by_anonymizable
151
                     ->filter_by_last_update({
153
                     ->filter_by_last_update({
152
                         to => $last_issue_date, timestamp_column_name => 'returndate' })
154
                         to => dt_from_string($last_issue_date), timestamp_column_name => 'returndate' })
153
                     ->anonymize;
155
                     ->anonymize;
154
156
155
        $template->param(
157
        $template->param(
156
- 

Return to bug 36526