From b844b19dfc9dacea4e6b27357844f731c6a389e3 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 6 Aug 2019 11:29:27 -0500 Subject: [PATCH] Bug 23463: Remove DelItem Signed-off-by: Tomas Cohen Arazi Signed-off-by: Nick Clemens Signed-off-by: Martin Renvoize --- C4/Items.pm | 81 ++-------------------------------- Koha/Item.pm | 37 ++++++++++++++++ t/db_dependent/Items.t | 2 +- t/db_dependent/Items/DelItem.t | 29 +++--------- 4 files changed, 48 insertions(+), 101 deletions(-) diff --git a/C4/Items.pm b/C4/Items.pm index 3bd391c551..2703bc10b0 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -32,7 +32,6 @@ BEGIN { Item2Marc ModDateLastSeen ModItemTransfer - DelItem CheckItemPreSave GetItemsForInventory GetItemsInfo @@ -425,40 +424,6 @@ sub ModDateLastSeen { $item->store({ log_action => 0 }); } -=head2 DelItem - - DelItem({ itemnumber => $itemnumber, [ biblionumber => $biblionumber ] } ); - -Exported function (core API) for deleting an item record in Koha. - -=cut - -sub DelItem { - my ( $params ) = @_; - - my $itemnumber = $params->{itemnumber}; - my $biblionumber = $params->{biblionumber}; - - unless ($biblionumber) { - my $item = Koha::Items->find( $itemnumber ); - $biblionumber = $item ? $item->biblio->biblionumber : undef; - } - - # If there is no biblionumber for the given itemnumber, there is nothing to delete - return 0 unless $biblionumber; - - # FIXME check the item has no current issues - my $deleted = _koha_delete_item( $itemnumber ); - - ModZebra( $biblionumber, "specialUpdate", "biblioserver" ); - - _after_item_action_hooks({ action => 'delete', item_id => $itemnumber }); - - #search item field code - logaction("CATALOGUING", "DELETE", $itemnumber, "item") if C4::Context->preference("CataloguingLog"); - return $deleted; -} - =head2 CheckItemPreSave my $item_ref = TransformMarcToKoha($marc, 'items'); @@ -1480,12 +1445,9 @@ sub DelItemCheck { my $status = ItemSafeToDelete( $biblionumber, $itemnumber ); if ( $status == 1 ) { - DelItem( - { - biblionumber => $biblionumber, - itemnumber => $itemnumber - } - ); + my $item = Koha::Items->find($itemnumber); + $item->move_to_deleted; + $item->delete; } return $status; } @@ -1518,43 +1480,6 @@ sub _mod_item_dates { # date formatting for date fields in item hash } } -=head2 _koha_delete_item - - _koha_delete_item( $itemnum ); - -Internal function to delete an item record from the koha tables - -=cut - -sub _koha_delete_item { - my ( $itemnum ) = @_; - - my $dbh = C4::Context->dbh; - # save the deleted item to deleteditems table - my $sth = $dbh->prepare("SELECT * FROM items WHERE itemnumber=?"); - $sth->execute($itemnum); - my $data = $sth->fetchrow_hashref(); - - # There is no item to delete - return 0 unless $data; - - my $query = "INSERT INTO deleteditems SET "; - my @bind = (); - foreach my $key ( keys %$data ) { - next if ( $key eq 'timestamp' ); # timestamp will be set by db - $query .= "$key = ?,"; - push( @bind, $data->{$key} ); - } - $query =~ s/\,$//; - $sth = $dbh->prepare($query); - $sth->execute(@bind); - - # delete from items table - $sth = $dbh->prepare("DELETE FROM items WHERE itemnumber=?"); - my $deleted = $sth->execute($itemnum); - return ( $deleted == 1 ) ? 1 : 0; -} - =head2 _marc_from_item_hash my $item_marc = _marc_from_item_hash($item, $frameworkcode[, $unlinked_item_subfields]); diff --git a/Koha/Item.pm b/Koha/Item.pm index db79cb6100..91119bd9b6 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -156,6 +156,43 @@ sub store { return $self->SUPER::store; } +=head3 delete + +=cut + +sub delete { + my ( $self ) = @_; + + # FIXME check the item has no current issues + # i.e. raise the appropriate exception + + C4::Biblio::ModZebra( $self->biblionumber, "specialUpdate", "biblioserver" ); + + $self->_after_item_action_hooks({ action => 'delete' }); + + logaction( "CATALOGUING", "DELETE", $self->itemnumber, "item" ) + if C4::Context->preference("CataloguingLog"); + + return $self->SUPER::delete; +} + +=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); +} + + =head3 effective_itemtype Returns the itemtype for the item based on whether item level itemtypes are set or not. diff --git a/t/db_dependent/Items.t b/t/db_dependent/Items.t index 2997e57379..c4bee0f700 100755 --- a/t/db_dependent/Items.t +++ b/t/db_dependent/Items.t @@ -90,7 +90,7 @@ subtest 'General Add, Get and Del tests' => sub { cmp_ok($moditem->barcode, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->barcode . '.'); # Delete item. - DelItem({ biblionumber => $biblio->biblionumber, itemnumber => $itemnumber }); + $moditem->delete; my $getdeleted = Koha::Items->find($itemnumber); is($getdeleted, undef, "Item deleted as expected."); diff --git a/t/db_dependent/Items/DelItem.t b/t/db_dependent/Items/DelItem.t index 5f2355f1e3..6b778ddaf8 100644 --- a/t/db_dependent/Items/DelItem.t +++ b/t/db_dependent/Items/DelItem.t @@ -1,5 +1,7 @@ use Modern::Perl; +# FIXME This file must be removed and the test moved to Koha::Item->delete + use MARC::Record; use C4::Items; use C4::Biblio; @@ -8,7 +10,7 @@ use Koha::Items; use t::lib::TestBuilder; -use Test::More tests => 6; +use Test::More tests => 2; my $schema = Koha::Database->schema; $schema->storage->txn_begin; @@ -20,31 +22,14 @@ my $library = $builder->build({ my $biblio = $builder->build_sample_biblio(); -my $itemnumber = $builder->build_sample_item( +my $item = $builder->build_sample_item( { biblionumber => $biblio->biblionumber, library => $library->{branchcode} } -)->itemnumber; +); -my $deleted = DelItem( { biblionumber => $biblio->biblionumber, itemnumber => $itemnumber } ); +my $deleted = $item->delete; is( $deleted, 1, "DelItem should return 1 if the item has been deleted" ); -my $deleted_item = Koha::Items->find($itemnumber); +my $deleted_item = Koha::Items->find($item->itemnumber); is( $deleted_item, undef, "DelItem with biblionumber parameter - the item should be deleted." ); - -$itemnumber = $builder->build_sample_item( - { - biblionumber => $biblio->biblionumber, - library => $library->{branchcode} - } -)->itemnumber; -$deleted = DelItem( { biblionumber => $biblio->biblionumber, itemnumber => $itemnumber } ); -is( $deleted, 1, "DelItem should return 1 if the item has been deleted" ); -$deleted_item = Koha::Items->find($itemnumber); -is( $deleted_item, undef, "DelItem without biblionumber parameter - the item should be deleted." ); - -$deleted = DelItem( { itemnumber => $itemnumber + 1} ); -is ( $deleted, 0, "DelItem should return 0 if no item has been deleted" ); - -$deleted = DelItem( { itemnumber => $itemnumber + 1, biblionumber => $biblio->biblionumber } ); -is ( $deleted, 0, "DelItem should return 0 if no item has been deleted" ); -- 2.39.5