Bug 30914: (24153 follow-up) Fix cleanup_database.pl --transfers --old-reserves
[koha.git] / Koha / Suggestions.pm
1 package Koha::Suggestions;
2
3 # Copyright ByWater Solutions 2015
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Carp;
23
24 use Koha::Database;
25 use Koha::DateUtils qw(dt_from_string);
26 use Koha::Suggestion;
27
28 use base qw(Koha::Objects);
29
30 =head1 NAME
31
32 Koha::Suggestions - Koha Suggestion object set class
33
34 =head1 API
35
36 =head2 Class methods
37
38 =head3 search_limited
39
40     my $suggestions = Koha::Suggestions->search_limited( $params, $attributes );
41
42 Returns all the suggestions the logged in user is allowed to see.
43
44 =cut
45
46 sub search_limited {
47     my ( $self, $params, $attributes ) = @_;
48
49     my $resultset = $self;
50
51     # filter on user branch
52     if (   C4::Context->preference('IndependentBranches')
53         && !C4::Context->IsSuperLibrarian() )
54     {
55         # If IndependentBranches is set and the logged in user is not superlibrarian
56         # Then we want to filter by the user's library (i.e. cannot see suggestions
57         # from other libraries)
58         my $userenv = C4::Context->userenv;
59
60         $resultset = $self->search({ branchcode => $userenv->{branch} })
61             if $userenv && $userenv->{branch};
62     }
63
64     return $resultset->search( $params, $attributes);
65 }
66
67 =head3 filter_by_pending
68
69     my $open = $suggestions->filter_by_pending;
70
71 Filters the resultset on those that are considered pending (i.e. STATUS = ASKED).
72
73 =cut
74
75 sub filter_by_pending {
76     my ($self) = @_;
77
78     return $self->search( { STATUS => 'ASKED' } );
79 }
80
81 =head3 filter_by_suggested_days_range
82
83     my $suggestions = $suggestions->filter_by_suggested_days_range( $days );
84
85 Filters the resultset on those placed within some I<$days> range.
86
87 =cut
88
89 sub filter_by_suggested_days_range {
90     my ( $self, $days ) = @_;
91
92     my $dtf = Koha::Database->new->schema->storage->datetime_parser;
93
94     return $self->search(
95         { suggesteddate => { '>=' => $dtf->format_date( dt_from_string->subtract( days => $days ) ) } } );
96 }
97
98 =head2 Internal methods
99
100 =head3 _type
101
102 =cut
103
104 sub _type {
105     return 'Suggestion';
106 }
107
108 =head3 object_class
109
110 =cut
111
112 sub object_class {
113     return 'Koha::Suggestion';
114 }
115
116 =head1 AUTHOR
117
118 Kyle M Hall <kyle@bywatersolutions.com>
119
120 =cut
121
122 1;