Bug 29234: Further clean Z3950 Tests
[srvgit] / Koha / Old / Checkouts.pm
1 package Koha::Old::Checkouts;
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
20 use Koha::Database;
21 use Koha::DateUtils qw( dt_from_string );
22 use Koha::Old::Checkout;
23
24 use base qw(Koha::Objects);
25
26 =head1 NAME
27
28 Koha::Old::Checkouts - Koha Old Checkouts object set class
29
30 This object represents a set of completed checkouts
31
32 =head1 API
33
34 =head2 Class methods
35
36 =head3 filter_by_anonymizable
37
38     my $checkouts = $patron->old_checkouts;
39     my $anonymizable_checkouts = $checkouts->filter_by_anonymizable;
40
41 This method filters the resultset, so it only contains checkouts that can be
42 anonymized given the patron privacy settings.
43
44 =cut
45
46 sub filter_by_anonymizable {
47     my ( $self, $params ) = @_;
48
49     my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef;
50
51     return $self->search(
52         {
53             'me.borrowernumber' => { 'not' => undef },
54             'patron.privacy'    => { '<>'  => 0 },
55             (
56                 $anonymous_patron
57                 ? ( 'me.borrowernumber' => { '!=' => $anonymous_patron } )
58                 : ()
59             ),
60         },
61         {
62             join => ['patron'],
63         }
64     );
65 }
66
67 =head3 filter_by_todays_checkins
68
69 =cut
70
71 sub filter_by_todays_checkins {
72     my ( $self ) = @_;
73
74     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
75     my $today = dt_from_string;
76     my $today_start = $today->clone->set( hour =>  0, minute =>  0, second =>  0 );
77     my $today_end   = $today->clone->set( hour => 23, minute => 59, second => 59 );
78     $today_start = $dtf->format_datetime( $today_start );
79     $today_end   = $dtf->format_datetime( $today_end );
80     return $self->search({
81         returndate => {
82             '>=' => $today_start,
83             '<=' => $today_end,
84         },
85     });
86 }
87
88 =head3 anonymize
89
90     $patron->old_checkouts->anonymize();
91
92 Anonymize the given I<Koha::Old::Checkouts> resultset.
93
94 =cut
95
96 sub anonymize {
97     my ( $self, $params ) = @_;
98
99     my $anonymous_id = C4::Context->preference('AnonymousPatron');
100
101     Koha::Exceptions::SysPref::NotSet->throw( syspref => 'AnonymousPatron' )
102         unless $anonymous_id;
103
104     return $self->update( { borrowernumber => $anonymous_id }, { no_triggers => 1 } );
105 }
106
107 =head2 Internal methods
108
109 =head3 _type
110
111 =cut
112
113 sub _type {
114     return 'OldIssue';
115 }
116
117 =head3 object_class
118
119 =cut
120
121 sub object_class {
122     return 'Koha::Old::Checkout';
123 }
124
125 1;