+=head3 store
+
+ $item->store;
+
+$params can take an optional 'skip_record_index' parameter.
+If set, the reindexation process will not happen (index_records 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 = shift;
+ my $params = @_ ? shift : {};
+
+ my $log_action = $params->{log_action} // 1;
+
+ # We do not want to oblige callers to pass this value
+ # Dev conveniences vs performance?
+ unless ( $self->biblioitemnumber ) {
+ $self->biblioitemnumber( $self->biblio->biblioitem->biblioitemnumber );
+ }
+
+ # See related changes from C4::Items::AddItem
+ unless ( $self->itype ) {
+ $self->itype($self->biblio->biblioitem->itemtype);
+ }
+
+ my $today = dt_from_string;
+ my $action = 'create';
+
+ unless ( $self->in_storage ) { #AddItem
+
+ unless ( $self->permanent_location ) {
+ $self->permanent_location($self->location);
+ }
+
+ my $default_location = C4::Context->preference('NewItemsDefaultLocation');
+ unless ( $self->location || !$default_location ) {
+ $self->permanent_location( $self->location || $default_location )
+ unless $self->permanent_location;
+ $self->location($default_location);
+ }
+
+ unless ( $self->replacementpricedate ) {
+ $self->replacementpricedate($today);
+ }
+ unless ( $self->datelastseen ) {
+ $self->datelastseen($today);
+ }
+
+ unless ( $self->dateaccessioned ) {
+ $self->dateaccessioned($today);
+ }
+
+ if ( $self->itemcallnumber
+ or $self->cn_source )
+ {
+ my $cn_sort = GetClassSort( $self->cn_source, $self->itemcallnumber, "" );
+ $self->cn_sort($cn_sort);
+ }
+
+ } else { # ModItem
+
+ $action = 'modify';
+
+ my %updated_columns = $self->_result->get_dirty_columns;
+ return $self->SUPER::store unless %updated_columns;
+
+ # Retrieve the item for comparison if we need to
+ my $pre_mod_item = (
+ exists $updated_columns{itemlost}
+ or exists $updated_columns{withdrawn}
+ or exists $updated_columns{damaged}
+ ) ? $self->get_from_storage : undef;
+
+ # Update *_on fields if needed
+ # FIXME: Why not for AddItem as well?
+ my @fields = qw( itemlost withdrawn damaged );
+ for my $field (@fields) {
+
+ # If the field is defined but empty or 0, we are
+ # removing/unsetting and thus need to clear out
+ # the 'on' field
+ if ( exists $updated_columns{$field}
+ && defined( $self->$field )
+ && !$self->$field )
+ {
+ my $field_on = "${field}_on";
+ $self->$field_on(undef);
+ }
+ # If the field has changed otherwise, we much update
+ # the 'on' field
+ elsif (exists $updated_columns{$field}
+ && $updated_columns{$field}
+ && !$pre_mod_item->$field )
+ {
+ my $field_on = "${field}_on";
+ $self->$field_on(
+ DateTime::Format::MySQL->format_datetime(
+ dt_from_string()
+ )
+ );
+ }
+ }
+
+ 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'
+ and not exists $updated_columns{permanent_location} )
+ {
+ $self->permanent_location( $self->location );
+ }
+
+ # If item was lost and has now been found,
+ # reverse any list item charges if necessary.
+ if ( exists $updated_columns{itemlost}
+ and $updated_columns{itemlost} <= 0
+ and $pre_mod_item->itemlost > 0 )
+ {
+ $self->_set_found_trigger($pre_mod_item);
+ }
+
+ }
+
+ unless ( $self->dateaccessioned ) {
+ $self->dateaccessioned($today);
+ }
+
+ my $result = $self->SUPER::store;
+ if ( $log_action && C4::Context->preference("CataloguingLog") ) {
+ $action eq 'create'
+ ? logaction( "CATALOGUING", "ADD", $self->itemnumber, "item" )
+ : logaction( "CATALOGUING", "MODIFY", $self->itemnumber, "item " . Dumper( $self->unblessed ) );
+ }
+ my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
+ $indexer->index_records( $self->biblionumber, "specialUpdate", "biblioserver" )
+ unless $params->{skip_record_index};
+ $self->get_from_storage->_after_item_action_hooks({ action => $action });
+
+ return $result;
+}
+
+=head3 delete
+
+=cut
+
+sub delete {
+ my $self = shift;
+ my $params = @_ ? shift : {};
+
+ # FIXME check the item has no current issues
+ # i.e. raise the appropriate exception
+
+ my $result = $self->SUPER::delete;
+
+ my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
+ $indexer->index_records( $self->biblionumber, "specialUpdate", "biblioserver" )
+ unless $params->{skip_record_index};
+
+ $self->_after_item_action_hooks({ action => 'delete' });
+
+ logaction( "CATALOGUING", "DELETE", $self->itemnumber, "item" )
+ if C4::Context->preference("CataloguingLog");
+
+ return $result;
+}
+
+=head3 safe_delete
+
+=cut
+
+sub safe_delete {
+ 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($params);
+}
+
+=head3 safe_to_delete
+
+returns 1 if the item is safe to delete,
+
+"book_on_loan" if the item is checked out,
+
+"not_same_branch" if the item is blocked by independent branches,
+
+"book_reserved" if the there are holds aganst the item, or
+
+"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 {
+ my ($self) = @_;
+
+ return "book_on_loan" if $self->checkout;
+
+ return "not_same_branch"
+ if defined C4::Context->userenv
+ and !C4::Context->IsSuperLibrarian()
+ and C4::Context->preference("IndependentBranches")
+ and ( C4::Context->userenv->{branch} ne $self->homebranch );
+
+ # check it doesn't have a waiting reserve
+ return "book_reserved"
+ if $self->holds->search( { found => [ 'W', 'T' ] } )->count;
+
+ 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;
+}
+
+=head3 move_to_deleted
+
+my $is_moved = $item->move_to_deleted;
+
+Move an item to the deleteditems table.
+This can be done before deleting an item, to make sure the data are not completely deleted.
+
+=cut
+
+sub move_to_deleted {
+ my ($self) = @_;
+ my $item_infos = $self->unblessed;
+ delete $item_infos->{timestamp}; #This ensures the timestamp date in deleteditems will be set to the current timestamp
+ return Koha::Database->new->schema->resultset('Deleteditem')->create($item_infos);
+}
+
+