Bug 29915: Add selenium tests
[srvgit] / Koha / Biblio.pm
index 49707db..4022819 100644 (file)
@@ -35,10 +35,13 @@ use Koha::Acquisition::Orders;
 use Koha::ArticleRequests;
 use Koha::Biblio::Metadatas;
 use Koha::Biblioitems;
+use Koha::Checkouts;
 use Koha::CirculationRules;
 use Koha::Item::Transfer::Limits;
 use Koha::Items;
 use Koha::Libraries;
+use Koha::Old::Checkouts;
+use Koha::Recalls;
 use Koha::Suggestions;
 use Koha::Subscriptions;
 use Koha::SearchEngine;
@@ -356,6 +359,36 @@ sub article_requests {
     return Koha::ArticleRequests->_new_from_dbic( scalar $self->_result->article_requests );
 }
 
+=head3 current_checkouts
+
+    my $current_checkouts = $biblio->current_checkouts
+
+Returns the current checkouts associated with this biblio
+
+=cut
+
+sub current_checkouts {
+    my ($self) = @_;
+
+    return Koha::Checkouts->search( { "item.biblionumber" => $self->id },
+        { join => 'item' } );
+}
+
+=head3 old_checkouts
+
+    my $old_checkouts = $biblio->old_checkouts
+
+Returns the past checkouts associated with this biblio
+
+=cut
+
+sub old_checkouts {
+    my ( $self ) = @_;
+
+    return Koha::Old::Checkouts->search( { "item.biblionumber" => $self->id },
+        { join => 'item' } );
+}
+
 =head3 items
 
 my $items = $biblio->items();
@@ -495,13 +528,29 @@ sub get_marc_components {
 
     my $searchstr = $self->get_components_query;
 
+    my $components;
     if (defined($searchstr)) {
         my $searcher = Koha::SearchEngine::Search->new({index => $Koha::SearchEngine::BIBLIOS_INDEX});
-        my ( $errors, $results, $total_hits ) = $searcher->simple_search_compat( $searchstr, 0, $max_results );
-        $self->{_components} = $results if ( defined($results) && scalar(@$results) );
+        my ( $error, $results, $total_hits );
+        eval {
+            ( $error, $results, $total_hits ) = $searcher->simple_search_compat( $searchstr, 0, $max_results );
+        };
+        if( $error || $@ ) {
+            $error //= q{};
+            $error .= $@ if $@;
+            warn "Warning from simple_search_compat: '$error'";
+            $self->add_message(
+                {
+                    type    => 'error',
+                    message => 'component_search',
+                    payload => $error,
+                }
+            );
+        }
+        $components = $results if defined($results) && @$results;
     }
 
-    return $self->{_components} || [];
+    return $components // [];
 }
 
 =head2 get_components_query
@@ -918,6 +967,102 @@ sub get_marc_notes {
     return \@marcnotes;
 }
 
+=head3 get_marc_authors
+
+    my $authors = $biblio->get_marc_authors;
+
+Get all authors from the MARC record and returns them in an array.
+The authors are stored in different fields depending on MARC flavour
+
+=cut
+
+sub get_marc_authors {
+    my ( $self, $params ) = @_;
+
+    my ( $mintag, $maxtag, $fields_filter );
+    my $marcflavour = C4::Context->preference('marcflavour');
+
+    # tagslib useful only for UNIMARC author responsibilities
+    my $tagslib;
+    if ( $marcflavour eq "UNIMARC" ) {
+        $tagslib = C4::Biblio::GetMarcStructure( 1, $self->frameworkcode, { unsafe => 1 });
+        $mintag = "700";
+        $maxtag = "712";
+        $fields_filter = '7..';
+    } else { # marc21/normarc
+        $mintag = "700";
+        $maxtag = "720";
+        $fields_filter = '7..';
+    }
+
+    my @marcauthors;
+    my $AuthoritySeparator = C4::Context->preference('AuthoritySeparator');
+
+    foreach my $field ( $self->metadata->record->field($fields_filter) ) {
+        next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
+        my @subfields_loop;
+        my @link_loop;
+        my @subfields  = $field->subfields();
+        my $count_auth = 0;
+
+        # if there is an authority link, build the link with Koha-Auth-Number: subfield9
+        my $subfield9 = $field->subfield('9');
+        if ($subfield9) {
+            my $linkvalue = $subfield9;
+            $linkvalue =~ s/(\(|\))//g;
+            @link_loop = ( { 'limit' => 'an', 'link' => $linkvalue } );
+        }
+
+        # other subfields
+        my $unimarc3;
+        for my $authors_subfield (@subfields) {
+            next if ( $authors_subfield->[0] eq '9' );
+
+            # unimarc3 contains the $3 of the author for UNIMARC.
+            # For french academic libraries, it's the "ppn", and it's required for idref webservice
+            $unimarc3 = $authors_subfield->[1] if $marcflavour eq 'UNIMARC' and $authors_subfield->[0] =~ /3/;
+
+            # don't load unimarc subfields 3, 5
+            next if ( $marcflavour eq 'UNIMARC' and ( $authors_subfield->[0] =~ /3|5/ ) );
+
+            my $code = $authors_subfield->[0];
+            my $value        = $authors_subfield->[1];
+            my $linkvalue    = $value;
+            $linkvalue =~ s/(\(|\))//g;
+            # UNIMARC author responsibility
+            if ( $marcflavour eq 'UNIMARC' and $code eq '4' ) {
+                $value = C4::Biblio::GetAuthorisedValueDesc( $field->tag(), $code, $value, '', $tagslib );
+                $linkvalue = "($value)";
+            }
+            # if no authority link, build a search query
+            unless ($subfield9) {
+                push @link_loop, {
+                    limit    => 'au',
+                    'link'   => $linkvalue,
+                    operator => (scalar @link_loop) ? ' AND ' : undef
+                };
+            }
+            my @this_link_loop = @link_loop;
+            # do not display $0
+            unless ( $code eq '0') {
+                push @subfields_loop, {
+                    tag       => $field->tag(),
+                    code      => $code,
+                    value     => $value,
+                    link_loop => \@this_link_loop,
+                    separator => (scalar @subfields_loop) ? $AuthoritySeparator : ''
+                };
+            }
+        }
+        push @marcauthors, {
+            MARCAUTHOR_SUBFIELDS_LOOP => \@subfields_loop,
+            authoritylink => $subfield9,
+            unimarc3 => $unimarc3
+        };
+    }
+    return \@marcauthors;
+}
+
 =head3 to_api
 
     my $json = $biblio->to_api;
@@ -1018,6 +1163,115 @@ sub get_marc_host {
     }
 }
 
+=head3 recalls
+
+    my $recalls = $biblio->recalls;
+
+Return recalls linked to this biblio
+
+=cut
+
+sub recalls {
+    my ( $self ) = @_;
+    return Koha::Recalls->_new_from_dbic( scalar $self->_result->recalls );
+}
+
+=head3 can_be_recalled
+
+    my @items_for_recall = $biblio->can_be_recalled({ patron => $patron_object });
+
+Does biblio-level checks and returns the items attached to this biblio that are available for recall
+
+=cut
+
+sub can_be_recalled {
+    my ( $self, $params ) = @_;
+
+    return 0 if !( C4::Context->preference('UseRecalls') );
+
+    my $patron = $params->{patron};
+
+    my $branchcode = C4::Context->userenv->{'branch'};
+    if ( C4::Context->preference('CircControl') eq 'PatronLibrary' and $patron ) {
+        $branchcode = $patron->branchcode;
+    }
+
+    my @all_items = Koha::Items->search({ biblionumber => $self->biblionumber })->as_list;
+
+    # if there are no available items at all, no recall can be placed
+    return 0 if ( scalar @all_items == 0 );
+
+    my @itemtypes;
+    my @itemnumbers;
+    my @items;
+    my @all_itemnumbers;
+    foreach my $item ( @all_items ) {
+        push( @all_itemnumbers, $item->itemnumber );
+        if ( $item->can_be_recalled({ patron => $patron }) ) {
+            push( @itemtypes, $item->effective_itemtype );
+            push( @itemnumbers, $item->itemnumber );
+            push( @items, $item );
+        }
+    }
+
+    # if there are no recallable items, no recall can be placed
+    return 0 if ( scalar @items == 0 );
+
+    # Check the circulation rule for each relevant itemtype for this biblio
+    my ( @recalls_allowed, @recalls_per_record, @on_shelf_recalls );
+    foreach my $itemtype ( @itemtypes ) {
+        my $rule = Koha::CirculationRules->get_effective_rules({
+            branchcode => $branchcode,
+            categorycode => $patron ? $patron->categorycode : undef,
+            itemtype => $itemtype,
+            rules => [
+                'recalls_allowed',
+                'recalls_per_record',
+                'on_shelf_recalls',
+            ],
+        });
+        push( @recalls_allowed, $rule->{recalls_allowed} ) if $rule;
+        push( @recalls_per_record, $rule->{recalls_per_record} ) if $rule;
+        push( @on_shelf_recalls, $rule->{on_shelf_recalls} ) if $rule;
+    }
+    my $recalls_allowed = (sort {$b <=> $a} @recalls_allowed)[0]; # take highest
+    my $recalls_per_record = (sort {$b <=> $a} @recalls_per_record)[0]; # take highest
+    my %on_shelf_recalls_count = ();
+    foreach my $count ( @on_shelf_recalls ) {
+        $on_shelf_recalls_count{$count}++;
+    }
+    my $on_shelf_recalls = (sort {$on_shelf_recalls_count{$b} <=> $on_shelf_recalls_count{$a}} @on_shelf_recalls)[0]; # take most common
+
+    # check recalls allowed has been set and is not zero
+    return 0 if ( !defined($recalls_allowed) || $recalls_allowed == 0 );
+
+    if ( $patron ) {
+        # check borrower has not reached open recalls allowed limit
+        return 0 if ( $patron->recalls->filter_by_current->count >= $recalls_allowed );
+
+        # check borrower has not reached open recalls allowed per record limit
+        return 0 if ( $patron->recalls->filter_by_current->search({ biblionumber => $self->biblionumber })->count >= $recalls_per_record );
+
+        # check if any of the items under this biblio are already checked out by this borrower
+        return 0 if ( Koha::Checkouts->search({ itemnumber => [ @all_itemnumbers ], borrowernumber => $patron->borrowernumber })->count > 0 );
+    }
+
+    # check item availability
+    my $checked_out_count = 0;
+    foreach (@items) {
+        if ( Koha::Checkouts->search({ itemnumber => $_->itemnumber })->count > 0 ){ $checked_out_count++; }
+    }
+
+    # can't recall if on shelf recalls only allowed when all unavailable, but items are still available for checkout
+    return 0 if ( $on_shelf_recalls eq 'all' && $checked_out_count < scalar @items );
+
+    # can't recall if no items have been checked out
+    return 0 if ( $checked_out_count == 0 );
+
+    # can recall
+    return @items;
+}
+
 =head2 Internal methods
 
 =head3 type