Bug 3142: Add note about imprecision of filter_by_for_hold
[koha.git] / Koha / Items.pm
1 package Koha::Items;
2
3 # Copyright ByWater Solutions 2014
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
23 use Koha::Database;
24
25 use Koha::Item;
26 use Koha::CirculationRules;
27
28 use base qw(Koha::Objects);
29
30 use Koha::SearchEngine::Indexer;
31
32 =head1 NAME
33
34 Koha::Items - Koha Item object set class
35
36 =head1 API
37
38 =head2 Class methods
39
40 =cut
41
42 =head3 filter_by_for_hold
43
44     my $filtered_items = $items->filter_by_for_hold;
45
46 Return the items of the set that are *potentially* holdable
47
48 Caller has the responsability to call C4::Reserves::CanItemBeReserved before
49 placing a hold on one of those items.
50
51 =cut
52
53 sub filter_by_for_hold {
54     my ($self) = @_;
55
56     my @hold_not_allowed_itypes = Koha::CirculationRules->search(
57         {
58             rule_name    => 'holdallowed',
59             branchcode   => undef,
60             categorycode => undef,
61             rule_value   => 'not_allowed',
62         }
63     )->get_column('itemtype');
64
65     return $self->search(
66         {
67             itemlost   => 0,
68             withdrawn  => 0,
69             notforloan => { '<=' => 0 }
70             ,    # items with negative or zero notforloan value are holdable
71             ( ! C4::Context->preference('AllowHoldsOnDamagedItems' ) ? ( damaged => 0 ) : () ),
72             itype        => { -not_in => \@hold_not_allowed_itypes },
73         }
74     );
75 }
76
77 =head3 filter_by_visible_in_opac
78
79     my $filered_items = $items->filter_by_visible_in_opac(
80         {
81             [ patron => $patron ]
82         }
83     );
84
85 Returns a new resultset, containing those items that are not expected to be hidden in OPAC
86 for the passed I<Koha::Patron> object that is passed.
87
88 The I<OpacHiddenItems>, I<hidelostitems> and I<OpacHiddenItemsExceptions> system preferences
89 are honoured.
90
91 =cut
92
93 sub filter_by_visible_in_opac {
94     my ($self, $params) = @_;
95
96     my $patron = $params->{patron};
97
98     my $result = $self;
99
100     # Filter out OpacHiddenItems unless disabled by OpacHiddenItemsExceptions
101     unless ( $patron and $patron->category->override_hidden_items ) {
102         my $rules = C4::Context->yaml_preference('OpacHiddenItems') // {};
103
104         my $rules_params;
105         foreach my $field ( keys %$rules ) {
106             $rules_params->{$field} =
107               [ { '-not_in' => $rules->{$field} }, undef ];
108         }
109
110         $result = $result->search( $rules_params );
111     }
112
113     if (C4::Context->preference('hidelostitems')) {
114         $result = $result->filter_out_lost;
115     }
116
117     return $result;
118 }
119
120 =head3 filter_out_lost
121
122     my $filered_items = $items->filter_out_lost;
123
124 Returns a new resultset, containing those items that are not marked as lost.
125
126 =cut
127
128 sub filter_out_lost {
129     my ($self) = @_;
130
131     my $params = { itemlost => 0 };
132
133     return $self->search( $params );
134 }
135
136 =head3 move_to_biblio
137
138  $items->move_to_biblio($to_biblio);
139
140 Move items to a given biblio.
141
142 =cut
143
144 sub move_to_biblio {
145     my ( $self, $to_biblio ) = @_;
146
147     my $biblionumbers = { $to_biblio->biblionumber => 1 };
148     while ( my $item = $self->next() ) {
149         $biblionumbers->{ $item->biblionumber } = 1;
150         $item->move_to_biblio( $to_biblio, { skip_record_index => 1 } );
151     }
152     my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
153     for my $biblionumber ( keys %{$biblionumbers} ) {
154         $indexer->index_records( $biblionumber, "specialUpdate", "biblioserver" );
155     }
156 }
157
158
159 =head2 Internal methods
160
161 =head3 _type
162
163 =cut
164
165 sub _type {
166     return 'Item';
167 }
168
169 =head3 object_class
170
171 =cut
172
173 sub object_class {
174     return 'Koha::Item';
175 }
176
177 =head1 AUTHOR
178
179 Kyle M Hall <kyle@bywatersolutions.com>
180 Tomas Cohen Arazi <tomascohen@theke.io>
181 Martin Renvoize <martin.renvoize@ptfs-europe.com>
182
183 =cut
184
185 1;