Bug 29138: (QA follow-up) Changes to UPDATE
[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 responsibility 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     push @hold_not_allowed_itypes, Koha::ItemTypes->search({ notforloan => 1 })->get_column('itemtype');
65
66     my $params = {
67         itemlost   => 0,
68         withdrawn  => 0,
69         notforloan => { '<=' => 0 },    # items with negative or zero notforloan value are holdable
70         ( C4::Context->preference('AllowHoldsOnDamagedItems')? (): ( damaged => 0 ) ),
71     };
72
73     if ( C4::Context->preference("item-level_itypes") ) {
74         return $self->search(
75             {
76                 %$params,
77                 itype        => { -not_in => \@hold_not_allowed_itypes },
78             }
79         );
80     } else {
81         return $self->search(
82             {
83                 %$params,
84                 'biblioitem.itemtype' => { -not_in => \@hold_not_allowed_itypes },
85             },
86             {
87                 join => 'biblioitem',
88             }
89         );
90     }
91 }
92
93 =head3 filter_by_visible_in_opac
94
95     my $filered_items = $items->filter_by_visible_in_opac(
96         {
97             [ patron => $patron ]
98         }
99     );
100
101 Returns a new resultset, containing those items that are not expected to be hidden in OPAC
102 for the passed I<Koha::Patron> object that is passed.
103
104 The I<OpacHiddenItems>, I<hidelostitems> and I<OpacHiddenItemsExceptions> system preferences
105 are honoured.
106
107 =cut
108
109 sub filter_by_visible_in_opac {
110     my ($self, $params) = @_;
111
112     my $patron = $params->{patron};
113
114     my $result = $self;
115
116     # Filter out OpacHiddenItems unless disabled by OpacHiddenItemsExceptions
117     unless ( $patron and $patron->category->override_hidden_items ) {
118         my $rules = C4::Context->yaml_preference('OpacHiddenItems') // {};
119
120         my $rules_params;
121         foreach my $field ( keys %$rules ) {
122             $rules_params->{$field} =
123               [ { '-not_in' => $rules->{$field} }, undef ];
124         }
125
126         $result = $result->search( $rules_params );
127     }
128
129     if (C4::Context->preference('hidelostitems')) {
130         $result = $result->filter_out_lost;
131     }
132
133     return $result;
134 }
135
136 =head3 filter_out_lost
137
138     my $filered_items = $items->filter_out_lost;
139
140 Returns a new resultset, containing those items that are not marked as lost.
141
142 =cut
143
144 sub filter_out_lost {
145     my ($self) = @_;
146
147     my $params = { itemlost => 0 };
148
149     return $self->search( $params );
150 }
151
152 =head3 move_to_biblio
153
154  $items->move_to_biblio($to_biblio);
155
156 Move items to a given biblio.
157
158 =cut
159
160 sub move_to_biblio {
161     my ( $self, $to_biblio ) = @_;
162
163     my $biblionumbers = { $to_biblio->biblionumber => 1 };
164     while ( my $item = $self->next() ) {
165         $biblionumbers->{ $item->biblionumber } = 1;
166         $item->move_to_biblio( $to_biblio, { skip_record_index => 1 } );
167     }
168     my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
169     for my $biblionumber ( keys %{$biblionumbers} ) {
170         $indexer->index_records( $biblionumber, "specialUpdate", "biblioserver" );
171     }
172 }
173
174
175 =head2 Internal methods
176
177 =head3 _type
178
179 =cut
180
181 sub _type {
182     return 'Item';
183 }
184
185 =head3 object_class
186
187 =cut
188
189 sub object_class {
190     return 'Koha::Item';
191 }
192
193 =head1 AUTHOR
194
195 Kyle M Hall <kyle@bywatersolutions.com>
196 Tomas Cohen Arazi <tomascohen@theke.io>
197 Martin Renvoize <martin.renvoize@ptfs-europe.com>
198
199 =cut
200
201 1;