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 ) ),
79 ( C4::Context->only_my_library() ? ( homebranch => C4::Context::mybranch() ) : () ),
82 if ( C4::Context->preference("item-level_itypes") ) {
86 itype => { -not_in => \@hold_not_allowed_itypes },
93 'biblioitem.itemtype' => { -not_in => \@hold_not_allowed_itypes },
102 =head3 filter_by_visible_in_opac
104 my $filered_items = $items->filter_by_visible_in_opac(
106 [ patron => $patron ]
110 Returns a new resultset, containing those items that are not expected to be hidden in OPAC
111 for the passed I<Koha::Patron> object that is passed.
113 The I<OpacHiddenItems>, I<hidelostitems> and I<OpacHiddenItemsExceptions> system preferences
118 sub filter_by_visible_in_opac {
119 my ($self, $params) = @_;
121 my $patron = $params->{patron};
125 # Filter out OpacHiddenItems unless disabled by OpacHiddenItemsExceptions
126 unless ( $patron and $patron->category->override_hidden_items ) {
127 my $rules = C4::Context->yaml_preference('OpacHiddenItems') // {};
130 foreach my $field ( keys %$rules ) {
131 $rules_params->{'me.'.$field} =
132 [ { '-not_in' => $rules->{$field} }, undef ];
135 $result = $result->search( $rules_params );
138 if (C4::Context->preference('hidelostitems')) {
139 $result = $result->filter_out_lost;
145 =head3 filter_out_lost
147 my $filered_items = $items->filter_out_lost;
149 Returns a new resultset, containing those items that are not marked as lost.
153 sub filter_out_lost {
156 my $params = { itemlost => 0 };
158 return $self->search( $params );
162 =head3 move_to_biblio
164 $items->move_to_biblio($to_biblio);
166 Move items to a given biblio.
171 my ( $self, $to_biblio ) = @_;
173 my $biblionumbers = { $to_biblio->biblionumber => 1 };
174 while ( my $item = $self->next() ) {
175 $biblionumbers->{ $item->biblionumber } = 1;
176 $item->move_to_biblio( $to_biblio, { skip_record_index => 1 } );
178 my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
179 for my $biblionumber ( keys %{$biblionumbers} ) {
180 $indexer->index_records( $biblionumber, "specialUpdate", "biblioserver" );
186 Koha::Items->search->batch_update
189 itemnotes => $new_item_notes,
193 itemnotes_nonpublic => {
199 exclude_from_local_holds_priority => 1|0,
201 # increment something here
206 Batch update the items.
208 Returns ( $report, $self )
210 * modified_itemnumbers - list of the modified itemnumbers
211 * modified_fields - number of fields modified
219 Allows to set a new value for given fields.
220 The key can be one of the item's column name, or one subfieldcode of a MARC subfields not linked with a Koha field
224 Allows to modify existing subfield's values using a regular expression
226 =item exclude_from_local_holds_priority
228 Set the passed boolean value to items.exclude_from_local_holds_priority
230 =item mark_items_returned
232 Move issues on these items to the old issues table, do not mark items found, or
233 adjust damaged/withdrawn statuses, or fines, or locations.
237 Callback function to call after an item has been modified
244 my ( $self, $params ) = @_;
246 my $regex_mod = $params->{regex_mod} || {};
247 my $new_values = $params->{new_values} || {};
248 my $exclude_from_local_holds_priority = $params->{exclude_from_local_holds_priority};
249 my $mark_items_returned = $params->{mark_items_returned};
250 my $callback = $params->{callback};
252 my (@modified_itemnumbers, $modified_fields);
254 my $schema = Koha::Database->new->schema;
255 while ( my $item = $self->next ) {
257 try {$schema->txn_do(sub {
258 my $modified_holds_priority = 0;
259 my $item_returned = 0;
260 if ( defined $exclude_from_local_holds_priority ) {
261 if(!defined $item->exclude_from_local_holds_priority || $item->exclude_from_local_holds_priority != $exclude_from_local_holds_priority) {
262 $item->exclude_from_local_holds_priority($exclude_from_local_holds_priority)->store;
263 $modified_holds_priority = 1;
268 my $new_values = {%$new_values}; # Don't modify the original
270 my $old_values = $item->unblessed;
271 if ( $item->more_subfields_xml ) {
274 %{$item->additional_attributes->to_hashref},
278 for my $attr ( keys %$regex_mod ) {
279 my $old_value = $old_values->{$attr};
281 next unless $old_value;
283 my $value = apply_regex(
285 %{ $regex_mod->{$attr} },
290 $new_values->{$attr} = $value;
293 for my $attribute ( keys %$new_values ) {
294 next if $attribute eq 'more_subfields_xml'; # Already counted before
296 my $old = $old_values->{$attribute};
297 my $new = $new_values->{$attribute};
299 if ( defined $old xor defined $new )
300 || ( defined $old && defined $new && $new ne $old );
303 { # Dealing with more_subfields_xml
305 my $frameworkcode = $item->biblio->frameworkcode;
306 my $tagslib = C4::Biblio::GetMarcStructure( 1, $frameworkcode, { unsafe => 1 });
307 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
309 my @more_subfield_tags = map {
313 && !$_->{kohafield} # Get subfields that are not mapped
317 } values %{ $tagslib->{$itemtag} };
319 my $more_subfields_xml = Koha::Item::Attributes->new(
322 exists $new_values->{$_} ? ( $_ => $new_values->{$_} )
323 : exists $old_values->{$_}
324 ? ( $_ => $old_values->{$_} )
326 } @more_subfield_tags
328 )->to_marcxml($frameworkcode);
330 $new_values->{more_subfields_xml} = $more_subfields_xml;
332 delete $new_values->{$_} for @more_subfield_tags; # Clean the hash
337 my $itemlost_pre = $item->itemlost;
338 $item->set($new_values)->store({skip_record_index => 1});
340 C4::Circulation::LostItem(
341 $item->itemnumber, 'batchmod', undef,
342 { skip_record_index => 1 }
344 and not $itemlost_pre;
346 if ( $mark_items_returned ){
347 my $issue = $item->checkout;
350 C4::Circulation::MarkIssueReturned(
351 $issue->borrowernumber,
354 $issue->patron->privacy,
356 skip_record_index => 1,
357 skip_holds_queue => 1,
363 push @modified_itemnumbers, $item->itemnumber if $modified || $modified_holds_priority || $item_returned;
364 $modified_fields += $modified + $modified_holds_priority + $item_returned;
375 if (@modified_itemnumbers) {
376 my @biblionumbers = uniq(
377 Koha::Items->search( { itemnumber => \@modified_itemnumbers } )
378 ->get_column('biblionumber'));
380 if ( @biblionumbers ) {
381 my $indexer = Koha::SearchEngine::Indexer->new(
382 { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
384 $indexer->index_records( \@biblionumbers, 'specialUpdate',
385 "biblioserver", undef );
389 return ( { modified_itemnumbers => \@modified_itemnumbers, modified_fields => $modified_fields }, $self );
393 # FIXME Should be moved outside of Koha::Items
394 # FIXME This is nearly identical to Koha::SimpleMARC::_modify_values
396 my $search = $params->{search};
397 my $replace = $params->{replace};
398 my $modifiers = $params->{modifiers} || q{};
399 my $value = $params->{value};
401 $replace =~ s/"/\\"/g; # Protection from embedded code
402 $replace = '"' . $replace . '"'; # Put in a string for /ee
403 my @available_modifiers = qw( i g );
404 my $retained_modifiers = q||;
405 for my $modifier ( split //, $modifiers ) {
406 $retained_modifiers .= $modifier
407 if grep { /$modifier/ } @available_modifiers;
409 if ( $retained_modifiers =~ m/^(ig|gi)$/ ) {
410 $value =~ s/$search/$replace/igee;
412 elsif ( $retained_modifiers eq 'i' ) {
413 $value =~ s/$search/$replace/iee;
415 elsif ( $retained_modifiers eq 'g' ) {
416 $value =~ s/$search/$replace/gee;
419 $value =~ s/$search/$replace/ee;
425 =head3 search_ordered
427 $items->search_ordered;
429 Search and sort items in a specific order, depending if serials are present or not
434 my ($self, $params, $attributes) = @_;
436 $self = $self->search($params, $attributes);
438 my @biblionumbers = uniq $self->get_column('biblionumber');
440 if ( scalar ( @biblionumbers ) == 1
441 && Koha::Biblios->find( $biblionumbers[0] )->serial )
443 return $self->search(
446 order_by => [ 'serialid.publisheddate', 'me.enumchron' ],
447 join => { serialitem => 'serialid' }
451 return $self->search(
455 'homebranch.branchname',
457 \"LPAD( me.copynumber, 8, '0' )",
458 {-desc => 'me.dateaccessioned'}
460 join => ['homebranch']
466 =head2 Internal methods
486 Kyle M Hall <kyle@bywatersolutions.com>
487 Tomas Cohen Arazi <tomascohen@theke.io>
488 Martin Renvoize <martin.renvoize@ptfs-europe.com>