my $filtered_objects = $objects->filter_by_last_update
-days exclusive
+days exclusive by default (will be inclusive if days_inclusive is passed and set)
from inclusive
to inclusive
sub filter_by_last_update {
my ( $self, $params ) = @_;
my $timestamp_column_name = $params->{timestamp_column_name} || 'timestamp';
+ my $days_inclusive = $params->{days_inclusive} || 0;
my $conditions;
Koha::Exceptions::MissingParameter->throw(
"Missing mandatory parameter: days or from or to")
my $dtf = Koha::Database->new->schema->storage->datetime_parser;
if ( exists $params->{days} ) {
- $conditions->{'<'} = $dtf->format_date( dt_from_string->subtract( days => $params->{days} ) );
+ my $dt = Koha::DateUtils::dt_from_string();
+ my $operator = $days_inclusive ? '<=' : '<';
+ $conditions->{$operator} = $dtf->format_date( $dt->subtract( days => $params->{days} ) );
}
if ( exists $params->{from} ) {
my $from = ref($params->{from}) ? $params->{from} : dt_from_string($params->{from});
sub filter_by_dateexpiry {
my ( $class, $params ) = @_;
- my $days = $params->{days} || 0;
- my $parser = Koha::Database->new->schema->storage->datetime_parser;
- my $dt = dt_from_string()->subtract( days => $days );
- return $class->search({
- dateexpiry => { '<=' => $parser->format_datetime($dt) },
- });
+
+ return $class->filter_by_last_update(
+ {
+ timestamp_column_name => 'dateexpiry',
+ days => $params->{days} || 0,
+ days_inclusive => 1,
+ }
+ );
}
=head3 search_unsubscribed
is( $count, 3, '3 patrons have been updated before the last 2 days (exclusive)' );
$count = $patrons->filter_by_last_update(
+ { timestamp_column_name => 'updated_on', days => 2, days_inclusive => 1 } )->count;
+ is( $count, 4, '4 patrons have been updated before the last 2 days (inclusive)' );
+
+ $count = $patrons->filter_by_last_update(
{ timestamp_column_name => 'updated_on', days => 1 } )->count;
is( $count, 4, '4 patrons have been updated before yesterday (exclusive)' );
$count = $patrons->filter_by_last_update(
+ { timestamp_column_name => 'updated_on', days => 1, days_inclusive => 1 } )->count;
+ is( $count, 5, '5 patrons have been updated before yesterday (inclusive)' );
+
+ $count = $patrons->filter_by_last_update(
{ timestamp_column_name => 'updated_on', days => 0 } )->count;
is( $count, 5, '5 patrons have been updated before today (exclusive)' );
$count = $patrons->filter_by_last_update(
+ { timestamp_column_name => 'updated_on', days => 0, days_inclusive => 1 } )->count;
+ is( $count, 6, '6 patrons have been updated before today (inclusive)' );
+
+ $count = $patrons->filter_by_last_update(
{ timestamp_column_name => 'updated_on', from => $now } )->count;
is( $count, 1, '1 patron has been updated "from today" (inclusive)' );