Bug 25765: (QA follow-up) Save a DB hit
[srvgit] / Koha / Item.pm
index cc4c772..9560a56 100644 (file)
@@ -58,10 +58,21 @@ Koha::Item - Koha Item object class
 
 =head3 store
 
+    $item->store;
+
+$params can take an optional 'skip_modzebra_update' parameter.
+If set, the reindexation process will not happen (ModZebra not called)
+
+NOTE: This is a temporary fix to answer a performance issue when lot of items
+are added (or modified) at the same time.
+The correct way to fix this is to make the ES reindexation process async.
+You should not turn it on if you do not understand what it is doing exactly.
+
 =cut
 
 sub store {
-    my ($self, $params) = @_;
+    my $self = shift;
+    my $params = @_ ? shift : {};
 
     my $log_action = $params->{log_action} // 1;
 
@@ -76,12 +87,6 @@ sub store {
         $self->itype($self->biblio->biblioitem->itemtype);
     }
 
-    my %updated_columns = $self->_result->get_dirty_columns;
-    if ( exists $updated_columns{itemcallnumber} ) {
-        my $cn_sort = GetClassSort($self->cn_source, $self->itemcallnumber, "");
-        $self->cn_sort($cn_sort);
-    }
-
     my $today = dt_from_string;
     unless ( $self->in_storage ) { #AddItem
         unless ( $self->permanent_location ) {
@@ -98,7 +103,15 @@ sub store {
             $self->dateaccessioned($today);
         }
 
-        C4::Biblio::ModZebra( $self->biblionumber, "specialUpdate", "biblioserver" );
+        if (   $self->itemcallnumber
+            or $self->cn_source )
+        {
+            my $cn_sort = GetClassSort( $self->cn_source, $self->itemcallnumber, "" );
+            $self->cn_sort($cn_sort);
+        }
+
+        C4::Biblio::ModZebra( $self->biblionumber, "specialUpdate", "biblioserver" )
+            unless $params->{skip_modzebra_update};
 
         logaction( "CATALOGUING", "ADD", $self->itemnumber, "item" )
           if $log_action && C4::Context->preference("CataloguingLog");
@@ -136,8 +149,17 @@ sub store {
             }
         }
 
-        %updated_columns = $self->_result->get_dirty_columns;
+        my %updated_columns = $self->_result->get_dirty_columns;
         return $self->SUPER::store unless %updated_columns;
+
+        if (   exists $updated_columns{itemcallnumber}
+            or exists $updated_columns{cn_source} )
+        {
+            my $cn_sort = GetClassSort( $self->cn_source, $self->itemcallnumber, "" );
+            $self->cn_sort($cn_sort);
+        }
+
+
         if (    exists $updated_columns{location}
             and $self->location ne 'CART'
             and $self->location ne 'PROC'
@@ -146,9 +168,8 @@ sub store {
             $self->permanent_location( $self->location );
         }
 
-        $self->timestamp(undef) if $self->timestamp; # Maybe move this to Koha::Object->store?
-
-        C4::Biblio::ModZebra( $self->biblionumber, "specialUpdate", "biblioserver" );
+        C4::Biblio::ModZebra( $self->biblionumber, "specialUpdate", "biblioserver" )
+            unless $params->{skip_modzebra_update};
 
         $self->_after_item_action_hooks({ action => 'modify' });
 
@@ -168,12 +189,14 @@ sub store {
 =cut
 
 sub delete {
-    my ( $self ) = @_;
+    my $self = shift;
+    my $params = @_ ? shift : {};
 
     # FIXME check the item has no current issues
     # i.e. raise the appropriate exception
 
-    C4::Biblio::ModZebra( $self->biblionumber, "specialUpdate", "biblioserver" );
+    C4::Biblio::ModZebra( $self->biblionumber, "specialUpdate", "biblioserver" )
+        unless $params->{skip_modzebra_update};
 
     $self->_after_item_action_hooks({ action => 'delete' });
 
@@ -188,14 +211,15 @@ sub delete {
 =cut
 
 sub safe_delete {
-    my ($self) = @_;
+    my $self = shift;
+    my $params = @_ ? shift : {};
 
     my $safe_to_delete = $self->safe_to_delete;
     return $safe_to_delete unless $safe_to_delete eq '1';
 
     $self->move_to_deleted;
 
-    return $self->delete;
+    return $self->delete($params);
 }
 
 =head3 safe_to_delete
@@ -210,6 +234,8 @@ returns 1 if the item is safe to delete,
 
 "linked_analytics" if the item has linked analytic records.
 
+"last_item_for_hold" if the item is the last one on a record on which a biblio-level hold is placed
+
 =cut
 
 sub safe_to_delete {
@@ -230,6 +256,14 @@ sub safe_to_delete {
     return "linked_analytics"
       if C4::Items::GetAnalyticsCount( $self->itemnumber ) > 0;
 
+    return "last_item_for_hold"
+      if $self->biblio->items->count == 1
+      && $self->biblio->holds->search(
+          {
+              itemnumber => undef,
+          }
+        )->count;
+
     return 1;
 }
 
@@ -498,7 +532,7 @@ sub can_be_transferred {
 
 =head3 pickup_locations
 
-@pickup_locations = $item->pickup_locations( {patron => $patron } )
+$pickup_locations = $item->pickup_locations( {patron => $patron } )
 
 Returns possible pickup locations for this item, according to patron's home library (if patron is defined and holds are allowed only from hold groups)
 and if item can be transferred to each pickup location.
@@ -517,8 +551,8 @@ sub pickup_locations {
 
     my @libs;
     if(defined $patron) {
-        return @libs if $branchitemrule->{holdallowed} == 3 && !$self->home_branch->validate_hold_sibling( {branchcode => $patron->branchcode} );
-        return @libs if $branchitemrule->{holdallowed} == 1 && $self->home_branch->branchcode ne $patron->branchcode;
+        return \@libs if $branchitemrule->{holdallowed} == 3 && !$self->home_branch->validate_hold_sibling( {branchcode => $patron->branchcode} );
+        return \@libs if $branchitemrule->{holdallowed} == 1 && $self->home_branch->branchcode ne $patron->branchcode;
     }
 
     if ($branchitemrule->{hold_fulfillment_policy} eq 'holdgroup') {
@@ -547,7 +581,7 @@ sub pickup_locations {
         }
     }
 
-    return wantarray ? @pickup_locations : \@pickup_locations;
+    return \@pickup_locations;
 }
 
 =head3 article_request_type
@@ -675,7 +709,8 @@ sub as_marc_field {
         next if !$tagfield; # TODO: Should we raise an exception instead?
                             # Feels like safe fallback is better
 
-        push @subfields, $tagsubfield => $self->$item_field;
+        push @subfields, $tagsubfield => $self->$item_field
+            if defined $self->$item_field and $item_field ne '';
     }
 
     my $unlinked_item_subfields = C4::Items::_parse_unlinked_item_subfields_from_xml($self->more_subfields_xml);
@@ -691,6 +726,42 @@ sub as_marc_field {
     return $field;
 }
 
+=head3 renewal_branchcode
+
+Returns the branchcode to be recorded in statistics renewal of the item
+
+=cut
+
+sub renewal_branchcode {
+
+    my ($self, $params ) = @_;
+
+    my $interface = C4::Context->interface;
+    my $branchcode;
+    if ( $interface eq 'opac' ){
+        my $renewal_branchcode = C4::Context->preference('OpacRenewalBranch');
+        if( !defined $renewal_branchcode || $renewal_branchcode eq 'opacrenew' ){
+            $branchcode = 'OPACRenew';
+        }
+        elsif ( $renewal_branchcode eq 'itemhomebranch' ) {
+            $branchcode = $self->homebranch;
+        }
+        elsif ( $renewal_branchcode eq 'patronhomebranch' ) {
+            $branchcode = $self->checkout->patron->branchcode;
+        }
+        elsif ( $renewal_branchcode eq 'checkoutbranch' ) {
+            $branchcode = $self->checkout->branchcode;
+        }
+        else {
+            $branchcode = "";
+        }
+    } else {
+        $branchcode = ( C4::Context->userenv && defined C4::Context->userenv->{branch} )
+            ? C4::Context->userenv->{branch} : $params->{branch};
+    }
+    return $branchcode;
+}
+
 =head3 to_api_mapping
 
 This method returns the mapping for representing a Koha::Item object
@@ -761,7 +832,7 @@ sub _after_item_action_hooks {
 
     my $action = $params->{action};
 
-    if ( C4::Context->preference('UseKohaPlugins') && C4::Context->config("enable_plugins") ) {
+    if ( C4::Context->config("enable_plugins") ) {
 
         my @plugins = Koha::Plugins->new->GetPlugins({
             method => 'after_item_action',