Bug 22690: (QA follow-up) Move adopt_items_from_biblios to Koha::Items
[koha-ffzg.git] / Koha / Items.pm
index 40f6295..834ac18 100644 (file)
@@ -19,7 +19,6 @@ package Koha::Items;
 
 use Modern::Perl;
 
-use Carp;
 
 use Koha::Database;
 
@@ -27,6 +26,8 @@ use Koha::Item;
 
 use base qw(Koha::Objects);
 
+use Koha::SearchEngine::Indexer;
+
 =head1 NAME
 
 Koha::Items - Koha Item object set class
@@ -37,55 +38,98 @@ Koha::Items - Koha Item object set class
 
 =cut
 
-=head3 filter_by_for_loan
+=head3 filter_by_for_hold
 
-$items->filter_by_not_for_loan;
+    my $filtered_items = $items->filter_by_for_hold;
 
-Return the items of the set that are not for loan
+Return the items of the set that are holdable
 
 =cut
 
-sub filter_by_for_loan {
+sub filter_by_for_hold {
     my ($self) = @_;
-    return $self->search( { notforloan => [ 0, undef ] } );
+    return $self->search( { notforloan => { '<=' => 0 } } ); # items with negative or zero notforloan value are holdable
 }
 
 =head3 filter_by_visible_in_opac
 
-    my $filered_items = $items->filter_by_visible_in_opac;
+    my $filered_items = $items->filter_by_visible_in_opac(
+        {
+            [ patron => $patron ]
+        }
+    );
+
+Returns a new resultset, containing those items that are not expected to be hidden in OPAC
+for the passed I<Koha::Patron> object that is passed.
 
-Returns a new resultset, containing those items that are not expected to be hidden in OPAC.
-The I<OpacHiddenItems> and I<hidelostitems> system preferences are honoured.
+The I<OpacHiddenItems>, I<hidelostitems> and I<OpacHiddenItemsExceptions> system preferences
+are honoured.
 
 =cut
 
 sub filter_by_visible_in_opac {
     my ($self, $params) = @_;
 
-    my $rules = C4::Context->yaml_preference('OpacHiddenItems') // {};
+    my $patron = $params->{patron};
 
-    my $rules_params;
-    foreach my $field (keys %$rules){
-        $rules_params->{$field}->{'-not_in'} = $rules->{$field};
-    }
+    my $result = $self;
+
+    # Filter out OpacHiddenItems unless disabled by OpacHiddenItemsExceptions
+    unless ( $patron and $patron->category->override_hidden_items ) {
+        my $rules = C4::Context->yaml_preference('OpacHiddenItems') // {};
 
-    my $itemlost_params;
-    $itemlost_params = { itemlost => 0 }
-        if C4::Context->preference('hidelostitems');
+        my $rules_params;
+        foreach my $field ( keys %$rules ) {
+            $rules_params->{$field} =
+              [ { '-not_in' => $rules->{$field} }, undef ];
+        }
 
-    my $search_params;
-    if ( $rules_params and $itemlost_params ) {
-        $search_params = {
-            '-and' => [ $rules_params, $itemlost_params ]
-        };
+        $result = $result->search( $rules_params );
     }
-    else {
-        $search_params = $rules_params // $itemlost_params;
+
+    if (C4::Context->preference('hidelostitems')) {
+        $result = $result->filter_out_lost;
     }
 
-    return $self->search( $search_params );
+    return $result;
 }
 
+=head3 filter_out_lost
+
+    my $filered_items = $items->filter_out_lost;
+
+Returns a new resultset, containing those items that are not marked as lost.
+
+=cut
+
+sub filter_out_lost {
+    my ($self) = @_;
+
+    my $params = { itemlost => 0 };
+
+    return $self->search( $params );
+}
+
+=head3 move_to_biblio
+
+ $items->move_to_biblio($to_biblio);
+
+Move items to a given biblio.
+
+=cut
+
+sub move_to_biblio {
+    my ( $self, $to_biblio ) = @_;
+
+    while (my $item = $self->next()) {
+        $item->move_to_biblio($to_biblio, { skip_record_index => 1 });
+    }
+    my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
+    $indexer->index_records( $self->biblionumber, "specialUpdate", "biblioserver" );
+    $indexer->index_records( $from_biblio->biblionumber, "specialUpdate", "biblioserver" );
+}
+
+
 =head2 Internal methods
 
 =head3 _type
@@ -107,6 +151,8 @@ sub object_class {
 =head1 AUTHOR
 
 Kyle M Hall <kyle@bywatersolutions.com>
+Tomas Cohen Arazi <tomascohen@theke.io>
+Martin Renvoize <martin.renvoize@ptfs-europe.com>
 
 =cut