Bug 6104: Modify item looses branch
[koha.git] / cataloguing / additem.pl
index b79e7f5..e2dfc02 100755 (executable)
@@ -2,6 +2,7 @@
 
 
 # Copyright 2000-2002 Katipo Communications
+# Copyright 2004-2010 BibLibre
 #
 # This file is part of Koha.
 #
@@ -149,7 +150,7 @@ sub generate_subfield_form {
                 foreach my $thisbranch (@$branches) {
                     push @authorised_values, $thisbranch->{value};
                     $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
-                    $value = $thisbranch->{value} if $thisbranch->{selected};
+                    $value = $thisbranch->{value} if $thisbranch->{selected} && !$value;
                 }
             }
             elsif ( $subfieldlib->{authorised_value} eq "itemtypes" ) {
@@ -435,14 +436,28 @@ if ($op eq "additem") {
 } elsif ($op eq "delallitems") {
 #-------------------------------------------------------------------------------
     my @biblioitems = &GetBiblioItemByBiblioNumber($biblionumber);
-    foreach my $biblioitem (@biblioitems){
-        my $items = &GetItemsByBiblioitemnumber($biblioitem->{biblioitemnumber});
-
-        foreach my $item (@$items){
-            # FIXME although it won't delete items that have loans
-            # or waiting holds on them, should explicitly tell operator
-            # about items that are not deleted
-            &DelItemCheck($dbh,$biblionumber,$item->{itemnumber});
+    my $errortest=0;
+    my $itemfail;
+    foreach my $biblioitem (@biblioitems) {
+        my $items = &GetItemsByBiblioitemnumber( $biblioitem->{biblioitemnumber} );
+
+        foreach my $item (@$items) {
+            $error =&DelItemCheck( $dbh, $biblionumber, $item->{itemnumber} );
+            $itemfail =$item;
+        if($error == 1){
+            next
+            }
+        else {
+            push @errors,$error;
+            $errortest++
+            }
+        }
+        if($errortest > 0){
+            $nextop="additem";
+        } 
+        else {
+            print $input->redirect("/cgi-bin/koha/catalogue/moredetail.pl?biblionumber=$biblionumber");
+            exit;
         }
        }
 #-------------------------------------------------------------------------------
@@ -555,7 +570,7 @@ my $onlymine = C4::Context->preference('IndependantBranches') &&
                C4::Context->userenv                           && 
                C4::Context->userenv->{flags}!=1               && 
                C4::Context->userenv->{branch};
-my $branches = GetBranchesLoop(undef,$onlymine);  # build once ahead of time, instead of multiple times later.
+my $branches = GetBranchesLoop(C4::Context->userenv->{branch},$onlymine);  # build once ahead of time, instead of multiple times later.
 
 # We generate form, from actuel record
 my @fields;
@@ -588,13 +603,17 @@ foreach my $tag ( keys %{$tagslib}){
         next if ($tagslib->{$tag}->{$subtag}->{'tab'} ne "10");
         next if any { /^$tag$subtag$/ }  @fields;
 
-        my $value = "";
-        my $subfield_data = generate_subfield_form($tag, $subtag, $value, $tagslib, $tagslib->{$tag}->{$subtag}, $branches, $today_iso, $biblionumber, $temp, \@loop_data, $i);
-
-        push (@loop_data, $subfield_data);
-        $i++;
-    }
+        my @values = (undef);
+        @values = $itemrecord->field($tag)->subfield($subtag) if ($itemrecord && defined($itemrecord->field($tag)->subfield($subtag)));
+        for my $value (@values){
+            my $subfield_data = generate_subfield_form($tag, $subtag, $value, $tagslib, $tagslib->{$tag}->{$subtag}, $branches, $today_iso, $biblionumber, $temp, \@loop_data, $i); 
+            push (@loop_data, $subfield_data);
+            $i++;
+        } 
   }
+}
+@loop_data = sort {$a->{subfield} cmp $b->{subfield} } @loop_data;
+
 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
 $template->param( title => $record->title() ) if ($record ne "-1");
 $template->param(