Bug 20473: Whitespace
[koha.git] / Koha / ERM / Agreements.pm
1 package Koha::ERM::Agreements;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19 use DateTime;
20
21 use Koha::Database;
22 use Koha::DateUtils qw( dt_from_string );
23
24 use Koha::ERM::Agreement;
25 use Koha::ERM::Agreement::Periods;
26
27 use base qw(Koha::Objects);
28
29 =head1 NAME
30
31 Koha::ERM::Agreements- Koha ErmAgreement Object set class
32
33 =head1 API
34
35 =head2 Class Methods
36
37 =cut
38
39 =head3 filter_by_expired
40
41 =cut
42
43 sub filter_by_expired {
44     my ($self, $max_expiration_date) = @_;
45
46     $max_expiration_date =
47       $max_expiration_date
48       ? dt_from_string( $max_expiration_date, 'iso' )
49       : dt_from_string;
50
51     my $periods = Koha::ERM::Agreement::Periods->search(
52         { agreement_id => [ $self->get_column('agreement_id') ] },
53         {
54             select => [
55                 'agreement_id',
56                 \do {"CASE WHEN MAX(me.ended_on IS NULL) = 0 THEN max(me.ended_on) END"}
57             ],
58             as       => [ 'agreement_id', 'max_ended_on' ],
59             group_by => ['agreement_id'],
60         }
61     );
62
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;
71     }
72
73     return $self->search( { agreement_id => \@expired_agreement_ids } );
74 }
75
76 =head3 type
77
78 =cut
79
80 sub _type {
81     return 'ErmAgreement';
82 }
83
84 =head3 object_class
85
86 =cut
87
88 sub object_class {
89     return 'Koha::ERM::Agreement';
90 }
91
92 1;