Bug 7958 - Batch Item Delete Fails
[koha_gimpoz] / C4 / Items.pm
index 365c1b3..52423a2 100644 (file)
@@ -26,11 +26,11 @@ 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;
 use List::MoreUtils qw/any/;
+use C4::Search;
 use Data::Dumper; # used as part of logging item record changes, not just for
                   # debugging; so please don't remove this
 
@@ -67,6 +67,7 @@ BEGIN {
         GetItemsInfo
        GetItemsLocationInfo
        GetHostItemsInfo
+        GetItemnumbersForBiblio
         get_itemnumbers_of
        get_hostitemnumbers_of
         GetItemnumberFromBarcode
@@ -308,7 +309,7 @@ Sequence number of original item tag in the MARC record.
 Item barcode, provide to assist in the construction of
 useful error messages.
 
-=item error_condition
+=item error_code
 
 Code representing the error condition.  Can be 'duplicate_barcode',
 'invalid_homebranch', or 'invalid_holdingbranch'.
@@ -328,6 +329,9 @@ sub AddItemBatchFromMarc {
     my @errors = ();
     my $dbh = C4::Context->dbh;
 
+    # We modify the record, so lets work on a clone so we don't change the
+    # original.
+    $record = $record->clone();
     # loop through the item tags and start creating items
     my @bad_item_fields = ();
     my ($itemtag, $itemsubfield) = &GetMarcFromKohaField("items.itemnumber",'');
@@ -1493,6 +1497,26 @@ sub  GetLastAcquisitions {
        return @results;
 }
 
+=head2 GetItemnumbersForBiblio
+
+  my $itemnumbers = GetItemnumbersForBiblio($biblionumber);
+
+Given a single biblionumber, return an arrayref of all the corresponding itemnumbers
+
+=cut
+
+sub GetItemnumbersForBiblio {
+    my $biblionumber = shift;
+    my @items;
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber = ?");
+    $sth->execute($biblionumber);
+    while (my $result = $sth->fetchrow_hashref) {
+        push @items, $result->{'itemnumber'};
+    }
+    return \@items;
+}
+
 =head2 get_itemnumbers_of
 
   my @itemnumbers_of = get_itemnumbers_of(@biblionumbers);
@@ -2576,6 +2600,7 @@ sub PrepareItemrecordDisplay {
                 $subfield_data{subfield}      = $subfield;
                 $subfield_data{countsubfield} = $cntsubf++;
                 $subfield_data{kohafield}     = $tagslib->{$tag}->{$subfield}->{'kohafield'};
+                $subfield_data{id}            = "tag_".$tag."_subfield_".$subfield."_".int(rand(1000000));
 
                 #        $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
                 $subfield_data{marc_lib}   = $tagslib->{$tag}->{$subfield}->{lib};
@@ -2714,8 +2739,6 @@ sub PrepareItemrecordDisplay {
                             my $extended_param = plugin_parameters( $dbh, $temp, $tagslib, $subfield_data{id}, undef );
                             my ( $function_name, $javascript ) = plugin_javascript( $dbh, $temp, $tagslib, $subfield_data{id}, undef );
                             $subfield_data{random}     = int(rand(1000000));    # why do we need 2 different randoms?
-                            my $index_subfield = int(rand(1000000));
-                            $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
                             $subfield_data{marc_value} = qq[<input tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255"
                                 onfocus="Focus$function_name($subfield_data{random}, '$subfield_data{id}');"
                                  onblur=" Blur$function_name($subfield_data{random}, '$subfield_data{id}');" />