3 # Copyright ByWater Solutions 2014
5 # This file is part of Koha.
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.
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.
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>.
21 use Array::Utils qw( array_minus );
22 use List::MoreUtils qw( uniq );
26 use C4::Biblio qw( GetMarcStructure GetMarcFromKohaField );
30 use Koha::SearchEngine::Indexer;
32 use Koha::Item::Attributes;
34 use Koha::CirculationRules;
36 use base qw(Koha::Objects);
38 use Koha::SearchEngine::Indexer;
42 Koha::Items - Koha Item object set class
50 =head3 filter_by_for_hold
52 my $filtered_items = $items->filter_by_for_hold;
54 Return the items of the set that are *potentially* holdable.
56 Caller has the responsibility to call C4::Reserves::CanItemBeReserved before
57 placing a hold on one of those items.
61 sub filter_by_for_hold {
64 my @hold_not_allowed_itypes = Koha::CirculationRules->search(
66 rule_name => 'holdallowed',
68 categorycode => undef,
69 rule_value => 'not_allowed',
71 )->get_column('itemtype');
72 push @hold_not_allowed_itypes, Koha::ItemTypes->search({ notforloan => 1 })->get_column('itemtype');
77 notforloan => { '<=' => 0 }, # items with negative or zero notforloan value are holdable
78 ( C4::Context->preference('AllowHoldsOnDamagedItems')? (): ( damaged => 0 ) ),
81 if ( C4::Context->preference("item-level_itypes") ) {
85 itype => { -not_in => \@hold_not_allowed_itypes },
92 'biblioitem.itemtype' => { -not_in => \@hold_not_allowed_itypes },
101 =head3 filter_by_visible_in_opac
103 my $filered_items = $items->filter_by_visible_in_opac(
105 [ patron => $patron ]
109 Returns a new resultset, containing those items that are not expected to be hidden in OPAC
110 for the passed I<Koha::Patron> object that is passed.
112 The I<OpacHiddenItems>, I<hidelostitems> and I<OpacHiddenItemsExceptions> system preferences
117 sub filter_by_visible_in_opac {
118 my ($self, $params) = @_;
120 my $patron = $params->{patron};
124 # Filter out OpacHiddenItems unless disabled by OpacHiddenItemsExceptions
125 unless ( $patron and $patron->category->override_hidden_items ) {
126 my $rules = C4::Context->yaml_preference('OpacHiddenItems') // {};
129 foreach my $field ( keys %$rules ) {
130 $rules_params->{'me.'.$field} =
131 [ { '-not_in' => $rules->{$field} }, undef ];
134 $result = $result->search( $rules_params );
137 if (C4::Context->preference('hidelostitems')) {
138 $result = $result->filter_out_lost;
144 =head3 filter_out_lost
146 my $filered_items = $items->filter_out_lost;
148 Returns a new resultset, containing those items that are not marked as lost.
152 sub filter_out_lost {
155 my $params = { itemlost => 0 };
157 return $self->search( $params );
161 =head3 move_to_biblio
163 $items->move_to_biblio($to_biblio);
165 Move items to a given biblio.
170 my ( $self, $to_biblio ) = @_;
172 my $biblionumbers = { $to_biblio->biblionumber => 1 };
173 while ( my $item = $self->next() ) {
174 $biblionumbers->{ $item->biblionumber } = 1;
175 $item->move_to_biblio( $to_biblio, { skip_record_index => 1 } );
177 my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
178 for my $biblionumber ( keys %{$biblionumbers} ) {
179 $indexer->index_records( $biblionumber, "specialUpdate", "biblioserver" );
185 Koha::Items->search->batch_update
188 itemnotes => $new_item_notes,
192 itemnotes_nonpublic => {
198 exclude_from_local_holds_priority => 1|0,
200 # increment something here
205 Batch update the items.
207 Returns ( $report, $self )
209 * modified_itemnumbers - list of the modified itemnumbers
210 * modified_fields - number of fields modified
218 Allows to set a new value for given fields.
219 The key can be one of the item's column name, or one subfieldcode of a MARC subfields not linked with a Koha field
223 Allows to modify existing subfield's values using a regular expression
225 =item exclude_from_local_holds_priority
227 Set the passed boolean value to items.exclude_from_local_holds_priority
231 Callback function to call after an item has been modified
238 my ( $self, $params ) = @_;
240 my $regex_mod = $params->{regex_mod} || {};
241 my $new_values = $params->{new_values} || {};
242 my $exclude_from_local_holds_priority = $params->{exclude_from_local_holds_priority};
243 my $callback = $params->{callback};
245 my (@modified_itemnumbers, $modified_fields);
247 my $schema = Koha::Database->new->schema;
248 while ( my $item = $self->next ) {
250 try {$schema->txn_do(sub {
251 my $modified_holds_priority = 0;
252 if ( defined $exclude_from_local_holds_priority ) {
253 if(!defined $item->exclude_from_local_holds_priority || $item->exclude_from_local_holds_priority != $exclude_from_local_holds_priority) {
254 $item->exclude_from_local_holds_priority($exclude_from_local_holds_priority)->store;
255 $modified_holds_priority = 1;
260 my $new_values = {%$new_values}; # Don't modify the original
262 my $old_values = $item->unblessed;
263 if ( $item->more_subfields_xml ) {
266 %{$item->additional_attributes->to_hashref},
270 for my $attr ( keys %$regex_mod ) {
271 my $old_value = $old_values->{$attr};
273 next unless $old_value;
275 my $value = apply_regex(
277 %{ $regex_mod->{$attr} },
282 $new_values->{$attr} = $value;
285 for my $attribute ( keys %$new_values ) {
286 next if $attribute eq 'more_subfields_xml'; # Already counted before
288 my $old = $old_values->{$attribute};
289 my $new = $new_values->{$attribute};
291 if ( defined $old xor defined $new )
292 || ( defined $old && defined $new && $new ne $old );
295 { # Dealing with more_subfields_xml
297 my $frameworkcode = $item->biblio->frameworkcode;
298 my $tagslib = C4::Biblio::GetMarcStructure( 1, $frameworkcode, { unsafe => 1 });
299 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
301 my @more_subfield_tags = map {
305 && !$_->{kohafield} # Get subfields that are not mapped
309 } values %{ $tagslib->{$itemtag} };
311 my $more_subfields_xml = Koha::Item::Attributes->new(
314 exists $new_values->{$_} ? ( $_ => $new_values->{$_} )
315 : exists $old_values->{$_}
316 ? ( $_ => $old_values->{$_} )
318 } @more_subfield_tags
320 )->to_marcxml($frameworkcode);
322 $new_values->{more_subfields_xml} = $more_subfields_xml;
324 delete $new_values->{$_} for @more_subfield_tags; # Clean the hash
329 my $itemlost_pre = $item->itemlost;
330 $item->set($new_values)->store({skip_record_index => 1});
332 C4::Circulation::LostItem(
333 $item->itemnumber, 'batchmod', undef,
334 { skip_record_index => 1 }
336 and not $itemlost_pre;
339 push @modified_itemnumbers, $item->itemnumber if $modified || $modified_holds_priority;
340 $modified_fields += $modified + $modified_holds_priority;
351 if (@modified_itemnumbers) {
352 my @biblionumbers = uniq(
353 Koha::Items->search( { itemnumber => \@modified_itemnumbers } )
354 ->get_column('biblionumber'));
356 if ( @biblionumbers ) {
357 my $indexer = Koha::SearchEngine::Indexer->new(
358 { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
360 $indexer->index_records( \@biblionumbers, 'specialUpdate',
361 "biblioserver", undef );
365 return ( { modified_itemnumbers => \@modified_itemnumbers, modified_fields => $modified_fields }, $self );
369 # FIXME Should be moved outside of Koha::Items
370 # FIXME This is nearly identical to Koha::SimpleMARC::_modify_values
372 my $search = $params->{search};
373 my $replace = $params->{replace};
374 my $modifiers = $params->{modifiers} || q{};
375 my $value = $params->{value};
377 $replace =~ s/"/\\"/g; # Protection from embedded code
378 $replace = '"' . $replace . '"'; # Put in a string for /ee
379 my @available_modifiers = qw( i g );
380 my $retained_modifiers = q||;
381 for my $modifier ( split //, $modifiers ) {
382 $retained_modifiers .= $modifier
383 if grep { /$modifier/ } @available_modifiers;
385 if ( $retained_modifiers =~ m/^(ig|gi)$/ ) {
386 $value =~ s/$search/$replace/igee;
388 elsif ( $retained_modifiers eq 'i' ) {
389 $value =~ s/$search/$replace/iee;
391 elsif ( $retained_modifiers eq 'g' ) {
392 $value =~ s/$search/$replace/gee;
395 $value =~ s/$search/$replace/ee;
401 =head3 search_ordered
403 $items->search_ordered;
405 Search and sort items in a specific order, depending if serials are present or not
410 my ($self, $params, $attributes) = @_;
412 $self = $self->search($params, $attributes);
414 my @biblionumbers = uniq $self->get_column('biblionumber');
416 if ( scalar ( @biblionumbers ) == 1
417 && Koha::Biblios->find( $biblionumbers[0] )->serial )
419 return $self->search(
422 order_by => [ 'serialid.publisheddate', 'me.enumchron' ],
423 join => { serialitem => 'serialid' }
427 return $self->search(
431 'homebranch.branchname',
433 \"LPAD( me.copynumber, 8, '0' )",
434 {-desc => 'me.dateaccessioned'}
436 join => ['homebranch']
442 =head2 Internal methods
462 Kyle M Hall <kyle@bywatersolutions.com>
463 Tomas Cohen Arazi <tomascohen@theke.io>
464 Martin Renvoize <martin.renvoize@ptfs-europe.com>