From 402f66d272f3794e8bfa0e1a32c102485468f3bd Mon Sep 17 00:00:00 2001 From: Brendan Lawlor Date: Wed, 13 Nov 2024 21:26:43 +0000 Subject: [PATCH] Bug 35654: Add option to delete_items.pl cronjob to delete empty bibs This patch adds a new option to the delete_items cronjob When --del_bibs is set, bib records will be deleted if the last item is deleted. The following plan assumes default ktd data. To test: 1. Apply patch 2. Check the holdings for bib #115 and see 3 items http://localhost:8081/cgi-bin/koha/catalogue/detail.pl?biblionumber=115 2. Run the cron with the following flags perl ./misc/cronjobs/delete_items.pl --where "biblionumber=115 and homebranch ='MPL'" --verbose --del_bibs --commit 3. Notice in the output the count of current items on the bib record: Where statement: where biblionumber=115 and homebranch='MPL' Deleting empty bib records! Deleting item '237' (of 3) Deleting item '238' (of 2) 4. Confirm the two Midway items were deleted from bib #115, and the bib still exists with one item 4. Run the cron again but change the the --where flag to delete all the items from bib #115 perl ./misc/cronjobs/delete_items.pl --where "biblionumber=115" --verbose --del_bibs --commit 5. Notice in the output the bib is deleted after the last item is deleted Where statement: where biblionumber=115 Deleting empty bib records! Deleting item '239' (of 1) Last item deleted - deleting bib '115' 8. Confirm the bib #115 was deleted 9. Add some more items and test with some other --where parameters 10. Test with and without --commit and --del_bibs, test the --help flag Sponsored-by: CLAMS Signed-off-by: Andrew Fuerste Henry --- misc/cronjobs/delete_items.pl | 39 ++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/misc/cronjobs/delete_items.pl b/misc/cronjobs/delete_items.pl index ac86b07648..ad3e6e834e 100755 --- a/misc/cronjobs/delete_items.pl +++ b/misc/cronjobs/delete_items.pl @@ -21,8 +21,9 @@ use Getopt::Long qw( GetOptions ); use Pod::Usage qw( pod2usage ); use Koha::Script -cron; - +use C4::Biblio qw( DelBiblio ); use C4::Context; +use Koha::Biblios; use Koha::Items; my $dbh = C4::Context->dbh(); @@ -44,6 +45,7 @@ my $OPTIONS = { , help => '' , manual => '' , version => '' + , del_bibs => '' } }; @@ -54,6 +56,7 @@ GetOptions( , 'h|help' => sub { $OPTIONS->{flags}->{help} = 1 } , 'm|manual' => sub { $OPTIONS->{flags}->{manual} = 1 } , 'c|commit' => sub { $OPTIONS->{flags}->{commit} = 1 } # aka DO-EET! + , 'del_bibs' => sub { $OPTIONS->{flags}->{del_bibs} = 1 } ); my @where = @{ $OPTIONS->{where} }; @@ -69,6 +72,9 @@ sub verbose { my $where_clause = ' where ' . join ( " and ", @where ); verbose "Where statement: $where_clause"; +if( $OPTIONS->{flags}->{del_bibs} ) { + verbose "Deleting empty bib records!" +} # FIXME Use Koha::Items instead $GLOBAL->{sth}->{target_items} = $dbh->prepare( $query->{target_items} . $where_clause ); @@ -76,18 +82,31 @@ $GLOBAL->{sth}->{target_items}->execute(); DELITEM: while ( my $item = $GLOBAL->{sth}->{target_items}->fetchrow_hashref() ) { - my $item_object = Koha::Items->find($item->{itemnumber}); + my $item_object = Koha::Items->find( $item->{itemnumber} ); + my $bibs_object = Koha::Biblios->find( $item_object->biblionumber ); + my $holdings_count = $bibs_object->items->count; + my $error; + my $safe_to_delete = $item_object->safe_to_delete; - if( $safe_to_delete ) { + if ($safe_to_delete) { $item_object->safe_delete if $OPTIONS->{flags}->{commit}; - verbose "Deleting '$item->{itemnumber}'"; + verbose "Deleting item '$item->{itemnumber}' (of $holdings_count)"; + + if ( $OPTIONS->{flags}->{del_bibs} && $holdings_count == 1 ) { # aka DO-EET for empty bibs! + $error = &DelBiblio( $item->{biblionumber} ); + if ($error) { + verbose "Could not delete bib '$item->{biblionumber}': $error"; + } else { + verbose "Last item deleted - deleting bib '$item->{biblionumber}'"; + } + } } else { verbose sprintf "Item '%s' (Barcode: '%s', Title: '%s') not deleted: %s", - $item->{itemnumber}, - $item_object->barcode, - $item_object->biblio->title, - @{$safe_to_delete->messages}[0]->message; + $item->{itemnumber}, + $item_object->barcode, + $item_object->biblio->title, + @{ $safe_to_delete->messages }[0]->message; } } @@ -130,6 +149,10 @@ clause querying the items table. These are joined by C. No items will be deleted unless the C<--commit> flag is present. +=item B<--del_bibs> + +Deletes the bibliographic record if the last item is deleted. + =back =cut -- 2.39.5