Bug 33837: Add datetime comparison to filter_by_last_update

Test plan:
Verify that filter_by_last_update still works as expected by
running cleanup_database.pl with --messages DAYS parameter.
Look at the oldest messages.message_date (or insert one).
Run with -days X where today - X is before that date.
Run with -days Y where today - Y is just after it.

Note: misc/cronjobs/cleanup_database.pl -confirm -messages -1 -v
will remove all messages including one you inserted just now.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Marcel de Rooy 2023-05-15 09:48:58 +00:00 committed by Tomas Cohen Arazi
parent 0b5bf20c99
commit 47288e2336
Signed by: tomascohen
GPG key ID: 0A272EA1B2F3C15F

View file

@ -232,11 +232,14 @@ sub update {
=head3 filter_by_last_update
my $filtered_objects = $objects->filter_by_last_update
my $filtered_objects = $objects->filter_by_last_update({
days => $x, from => $date1, to => $date2, days_inclusive => 1, datetime => 1,
});
days exclusive by default (will be inclusive if days_inclusive is passed and set)
from inclusive
to inclusive
Note: days are exclusive by default (will be inclusive if days_inclusive is passed and set).
The parameters from and to are inclusive. They can be DateTime objects or date strings.
You should pass at least one of the parameters days, from or to.
The optional parameter datetime allows datetime comparison.
=cut
@ -252,18 +255,19 @@ sub filter_by_last_update {
or exists $params->{to};
my $dtf = Koha::Database->new->schema->storage->datetime_parser;
my $method = $params->{datetime} ? 'format_datetime' : 'format_date';
if ( exists $params->{days} ) {
my $dt = Koha::DateUtils::dt_from_string();
my $operator = $days_inclusive ? '<=' : '<';
$conditions->{$operator} = $dtf->format_date( $dt->subtract( days => $params->{days} ) );
$conditions->{$operator} = $dtf->$method( $dt->subtract( days => $params->{days} ) );
}
if ( exists $params->{from} ) {
my $from = ref($params->{from}) ? $params->{from} : dt_from_string($params->{from});
$conditions->{'>='} = $dtf->format_date( $from );
$conditions->{'>='} = $dtf->$method( $from );
}
if ( exists $params->{to} ) {
my $to = ref($params->{to}) ? $params->{to} : dt_from_string($params->{to});
$conditions->{'<='} = $dtf->format_date( $to );
$conditions->{'<='} = $dtf->$method( $to );
}
return $self->search(