Bug 6027 - Delete biblios if deleting all their items in batch deletion
authorKyle M Hall <kyle@bywatersolutions.com>
Fri, 17 Feb 2012 14:36:38 +0000 (09:36 -0500)
committerPaul Poulain <paul.poulain@biblibre.com>
Wed, 21 Mar 2012 13:31:38 +0000 (14:31 +0100)
Optionally delete bibliographic record when batch deleting items, if no items remain on the record.

Adds deleting of reserves to DelBiblio. Since subscriptions are deleted automatically,
it made sense for deletion of reserves to maintain the same behavior.

Signed-off-by: Liz Rea <wizzyrea@gmail.com>
I like the way this works, and it does. Passes tests.

Signed-off-by: Paul Poulain <paul.poulain@biblibre.com>
C4/Biblio.pm
C4/Items.pm
C4/Reserves.pm
C4/Search.pm
koha-tmpl/intranet-tmpl/prog/en/modules/tools/batchMod-del.tt
tools/batchMod.pl

index 432eb6b..39a2b7a 100644 (file)
@@ -430,6 +430,13 @@ sub DelBiblio {
         C4::Serials::DelSubscription( $subscription->{subscriptionid} );
     }
 
+    # We delete any existing holds
+    require C4::Reserves;
+    my ($count, $reserves) = C4::Reserves::GetReservesFromBiblionumber($biblionumber);
+    foreach my $res ( @$reserves ) {
+        C4::Reserves::CancelReserve( $res->{'biblionumber'}, $res->{'itemnumber'}, $res->{'borrowernumber'} );
+    }
+
     # Delete in Zebra. Be careful NOT to move this line after _koha_delete_biblio
     # for at least 2 reasons :
     # - we need to read the biblio if NoZebra is set (to remove it from the indexes
index 676c91b..2f32c98 100644 (file)
@@ -26,6 +26,7 @@ use C4::Context;
 use C4::Koha;
 use C4::Biblio;
 use C4::Dates qw/format_date format_date_in_iso/;
+use C4::Search qw/SimpleSearch/;
 use MARC::Record;
 use C4::ClassSource;
 use C4::Log;
index a63e30f..276658c 100644 (file)
@@ -98,7 +98,7 @@ BEGIN {
         &GetReservesToBranch
         &GetReserveCount
         &GetReserveFee
-               &GetReserveInfo
+        &GetReserveInfo
         &GetReserveStatus
         
         &GetOtherReserves
@@ -901,8 +901,8 @@ sub CancelExpiredReserves {
 Cancels a reserve.
 
 Use either C<$biblionumber> or C<$itemnumber> to specify the item to
-cancel, but not both: if both are given, C<&CancelReserve> does
-nothing.
+cancel, but not both: if both are given, C<&CancelReserve> uses
+C<$itemnumber>.
 
 C<$borrowernumber> is the borrower number of the patron on whose
 behalf the book was reserved.
index 7adb625..9bc0af2 100644 (file)
@@ -71,6 +71,7 @@ This module provides searching functions for Koha's bibliographic databases
   &AddSearchHistory
   &GetDistinctValues
   &enabled_staff_search_views
+  &SimpleSearch
 );
 
 # make all your functions, whether exported or not;
index af2a239..3f0f4b9 100644 (file)
@@ -133,6 +133,7 @@ for( x=0; x<allColumns.length; x++ ){
                    <div id="jobfailed"></div>
                </div>
                [% IF ( too_many_items ) %]<input type="submit" name="mainformsubmit" value="Delete ALL submitted items" onclick="return submitBackgroundJob(this.form);" />[% ELSE %]
+               <input type="checkbox" name="del_records" id="del_records" /> <label for="del_records">Delete records if no items remain.</label><p>
                <input type="submit" name="mainformsubmit" value="Delete selected items" onclick="return submitBackgroundJob(this.form);" />[% END %]
 
                <a href="/cgi-bin/koha/tools/batchMod.pl?del=1" class="cancel">Cancel</a>
@@ -146,7 +147,10 @@ for( x=0; x<allColumns.length; x++ ){
     [% END %]
 
 [% IF ( action ) %]
-       <div class="dialog message"><p>[% deleted_items %] item(s) deleted.</p></div>
+       <div class="dialog message">
+            <p>[% deleted_items %] item(s) deleted.</p>
+            [% IF delete_records %] <p>[% deleted_records %] record(s) deleted.</p> [% END %]
+        </div>
        [% IF ( not_deleted_items ) %]
        <div style="width:55%;margin:auto;">
            <p>[% not_deleted_items %] item(s) could not be deleted: [% FOREACH not_deleted_itemnumber IN not_deleted_itemnumbers %][% not_deleted_itemnumber.itemnumber %][% END %]</p>
index 440e516..9d59e3a 100755 (executable)
@@ -41,6 +41,7 @@ my $error        = $input->param('error');
 my @itemnumbers  = $input->param('itemnumber');
 my $op           = $input->param('op');
 my $del          = $input->param('del');
+my $del_records  = $input->param('del_records');
 my $completedJobID = $input->param('completedJobID');
 my $runinbackground = $input->param('runinbackground');
 
@@ -76,8 +77,9 @@ my $items_display_hashref;
 my $frameworkcode="";
 my $tagslib = &GetMarcStructure(1,$frameworkcode);
 
-my $deleted_items = 0;     # Numbers of deleted items
-my $not_deleted_items = 0; # Numbers of items that could not be deleted
+my $deleted_items = 0;     # Number of deleted items
+my $deleted_records = 0;   # Number of deleted records ( with no items attached )
+my $not_deleted_items = 0; # Number of items that could not be deleted
 my @not_deleted;           # List of the itemnumbers that could not be deleted
 
 my %cookies = parse CGI::Cookie($cookie);
@@ -156,7 +158,7 @@ if ($op eq "action") {
        foreach my $itemnumber(@itemnumbers){
 
                $job->progress($i) if $runinbackground;
-               my $itemdata=GetItem($itemnumber);
+               my $itemdata = GetItem($itemnumber);
                if ($input->param("del")){
                        my $return = DelItemCheck(C4::Context->dbh, $itemdata->{'biblionumber'}, $itemdata->{'itemnumber'});
                        if ($return == 1) {
@@ -171,15 +173,24 @@ if ($op eq "action") {
                                  $return => 1
                                };
                        }
+
+                       # If there are no items left, delete the biblio
+                       if ( $del_records ) {
+                            my $itemscount = GetItemsCount($itemdata->{'biblionumber'});
+                            if ( $itemscount == 0 ) {
+                               my $error = DelBiblio($itemdata->{'biblionumber'});
+                               $deleted_records++ unless ( $error );
+                            }
+                        }
                } else {
                    if ($values_to_modify || $values_to_blank) {
                        my $localmarcitem = Item2Marc($itemdata);
                        UpdateMarcWith( $marcitem, $localmarcitem );
                        eval{
-                if ( my $item = ModItemFromMarc( $localmarcitem, $itemdata->{biblionumber}, $itemnumber ) ) {
-                    LostItem($itemnumber, 'MARK RETURNED', 'CHARGE FEE') if $item->{itemlost};
-                }
-            };
+                            if ( my $item = ModItemFromMarc( $localmarcitem, $itemdata->{biblionumber}, $itemnumber ) ) {
+                                LostItem($itemnumber, 'MARK RETURNED', 'CHARGE FEE') if $item->{itemlost};
+                            }
+                        };
                    }
                }
                $i++;
@@ -433,6 +444,8 @@ if ($op eq "action") {
     $template->param(
        not_deleted_items => $not_deleted_items,
        deleted_items => $deleted_items,
+       delete_records => $del_records,
+       deleted_records => $deleted_records,
        not_deleted_loop => \@not_deleted 
     );
 }