1 package Koha::ERM::Agreements;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Koha::DateUtils qw( dt_from_string );
24 use Koha::ERM::Agreement;
25 use Koha::ERM::Agreement::Periods;
27 use base qw(Koha::Objects);
31 Koha::ERM::Agreements- Koha ErmAgreement Object set class
39 =head3 filter_by_expired
43 sub filter_by_expired {
44 my ($self, $max_expiration_date) = @_;
46 $max_expiration_date =
48 ? dt_from_string( $max_expiration_date, 'iso' )
51 my $periods = Koha::ERM::Agreement::Periods->search(
52 { agreement_id => [ $self->get_column('agreement_id') ] },
56 \do {"CASE WHEN MAX(me.ended_on IS NULL) = 0 THEN max(me.ended_on) END"}
58 as => [ 'agreement_id', 'max_ended_on' ],
59 group_by => ['agreement_id'],
63 my @expired_agreement_ids;
64 while ( my $p = $periods->next ) {
65 # FIXME Can this be moved in the HAVING clause of the previous query?
66 my $max_ended_on = $p->get_column('max_ended_on');
67 next unless $max_ended_on;
68 my $max_ended_on_dt = dt_from_string($max_ended_on);
69 next if DateTime->compare( $max_ended_on_dt, $max_expiration_date ) == 1;
70 push @expired_agreement_ids, $p->agreement_id;
73 return $self->search( { agreement_id => \@expired_agreement_ids } );
81 return 'ErmAgreement';
89 return 'Koha::ERM::Agreement';