Created
July 3, 2018 11:04
-
-
Save mrenvoize/3285acf8c87df08b8bd9dae34fa6607b to your computer and use it in GitHub Desktop.
Chainable filters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=head3 filter_by_last_issued | |
Koha::Patrons->filter_by_last_issued( { after => DateTime, before => DateTime } ); | |
Returns patrons filtered by whether their last issue falls betwen the passed limits. | |
=head4 arguments hashref | |
=over 4 | |
=item before (optional) - filter out patrons whose last_issue was since DateTime | |
=item after (optional) - filter out patrons whose last_issue has been since DateTime | |
=back | |
=cut | |
sub filter_by_last_issued { | |
my ( $self, $options ) = @_; | |
return $self | |
unless ( defined($options) | |
&& ( $options->{before} || $options->{after} ) ); | |
my $where = {}; | |
my $attrs = { | |
join => 'old_issues', | |
'+select' => { max => 'old_issues.timestamp', -as => 'last_issued' }, | |
'+as' => 'last_issued' | |
}; | |
my $dtf = Koha::Database->new->schema->storage->datetime_parser; | |
push @{ $attrs->{'having'}->{'-and'} }, | |
{ 'last_issued' => { '<' => $dtf->format_datetime( $options->{before} ) } | |
} | |
if $options->{before}; | |
push @{ $attrs->{'having'}->{'-and'} }, | |
{ 'last_issued' => { '>' => $dtf->format_datetime( $options->{after} ) } } | |
if $options->{after}; | |
return $self->search( $where, $attrs ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My test script results in the below structure for attrs and where,
but the final query fails with: