Bug 11650: multiplicated authorities after link_bibs_to_authorities.pl
[koha_fer] / acqui / basket.pl
index 05f419e..4f2a37e 100755 (executable)
@@ -110,7 +110,43 @@ $template->param( skip_confirm_reopen => 1) if $confirm_pref eq '2';
 
 if ( $op eq 'delete_confirm' ) {
     my $basketno = $query->param('basketno');
-    DelBasket($basketno);
+    my $delbiblio = $query->param('delbiblio');
+    my @orders = GetOrders($basketno);
+#Delete all orders included in that basket, and all items received.
+    foreach my $myorder (@orders){
+        DelOrder($myorder->{biblionumber},$myorder->{ordernumber});
+    }
+# if $delbiblio = 1, delete the records if possible
+    if ((defined $delbiblio)and ($delbiblio ==1)){
+        my @cannotdelbiblios ;
+        foreach my $myorder (@orders){
+            my $biblionumber = $myorder->{'biblionumber'};
+            my $countbiblio = CountBiblioInOrders($biblionumber);
+            my $ordernumber = $myorder->{'ordernumber'};
+            my $subscriptions = scalar GetSubscriptionsId ($biblionumber);
+            my $itemcount = GetItemsCount($biblionumber);
+            my $error;
+            if ($countbiblio == 0 && $itemcount == 0 && $subscriptions == 0) {
+                $error = DelBiblio($myorder->{biblionumber}) }
+            else {
+                push @cannotdelbiblios, {biblionumber=> ($myorder->{biblionumber}),
+                                         title=> $myorder->{'title'},
+                                         author=> $myorder->{'author'},
+                                         countbiblio=> $countbiblio,
+                                         itemcount=>$itemcount,
+                                         subscriptions=>$subscriptions};
+            }
+            if ($error) {
+                push @cannotdelbiblios, {biblionumber=> ($myorder->{biblionumber}),
+                                         title=> $myorder->{'title'},
+                                         author=> $myorder->{'author'},
+                                         othererror=> $error};
+            }
+        }
+        $template->param( cannotdelbiblios => \@cannotdelbiblios );
+    }
+ # delete the basket
+    DelBasket($basketno,);
     $template->param( delete_confirmed => 1 );
 } elsif ( !$bookseller ) {
     $template->param( NO_BOOKSELLER => 1 );
@@ -118,7 +154,7 @@ if ( $op eq 'delete_confirm' ) {
     $template->param( delete_confirm => 1 );
     if ( C4::Context->preference("IndependentBranches") ) {
         my $userenv = C4::Context->userenv;
-        unless ( $userenv->{flags} == 1 ) {
+        unless ( C4::Context->IsSuperLibrarian() ) {
             my $validtest = ( $basket->{creationdate} eq '' )
               || ( $userenv->{branch} eq $basket->{branch} )
               || ( $userenv->{branch} eq '' )
@@ -153,9 +189,6 @@ if ( $op eq 'delete_confirm' ) {
         address3             => $bookseller->{'address3'},
         address4             => $bookseller->{'address4'},
       );
-} elsif ($op eq 'attachbasket' && $template->{'VARS'}->{'CAN_user_acquisition_group_manage'} == 1) {
-      print $query->redirect('/cgi-bin/koha/acqui/basketgroup.pl?basketno=' . $basket->{'basketno'} . '&op=attachbasket&booksellerid=' . $booksellerid);
-    # check if we have to "close" a basket before building page
 } elsif ($op eq 'export') {
     print $query->header(
         -type       => 'text/csv',
@@ -217,10 +250,11 @@ if ( $op eq 'delete_confirm' ) {
     print $query->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=$basketno");
     exit;
 } else {
+    my @branches_loop;
     # get librarian branch...
     if ( C4::Context->preference("IndependentBranches") ) {
         my $userenv = C4::Context->userenv;
-        unless ( $userenv->{flags} == 1 ) {
+        unless ( C4::Context->IsSuperLibrarian() ) {
             my $validtest = ( $basket->{creationdate} eq '' )
               || ( $userenv->{branch} eq $basket->{branch} )
               || ( $userenv->{branch} eq '' )
@@ -230,16 +264,32 @@ if ( $op eq 'delete_confirm' ) {
                 exit 1;
             }
         }
-    }
-    # get branches
-    my $branches = C4::Branch::GetBranches;
-    my @branches_loop;
-    foreach my $branch (sort keys %$branches) {
-        push @branches_loop, {
-            branchcode => $branch,
-            branchname => $branches->{$branch}->{branchname},
-            selected => (defined $basket->{branch} and $branch eq $basket->{branch}) ? 1 : 0
-        };
+        if (!defined $basket->{branch} or $basket->{branch} eq $userenv->{branch}) {
+            push @branches_loop, {
+                branchcode => $userenv->{branch},
+                branchname => $userenv->{branchname},
+                selected => 1,
+            };
+        }
+    } else {
+        # get branches
+        my $branches = C4::Branch::GetBranches;
+        my @branchcodes = sort {
+            $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname}
+        } keys %$branches;
+        foreach my $branch (@branchcodes) {
+            my $selected = 0;
+            if (defined $basket->{branch}) {
+                $selected = 1 if $branch eq $basket->{branch};
+            } else {
+                $selected = 1 if $branch eq C4::Context->userenv->{branch};
+            }
+            push @branches_loop, {
+                branchcode => $branch,
+                branchname => $branches->{$branch}->{branchname},
+                selected => $selected
+            };
+        }
     }
 
 #if the basket is closed,and the user has the permission to edit basketgroups, display a list of basketgroups
@@ -253,13 +303,6 @@ if ( $op eq 'delete_confirm' ) {
                 $basketgroup = $bg;
             }
         }
-        my %emptygroup = ( id   =>   undef,
-                           name =>   "No group");
-        if ( ! $basket->{basketgroupid} ) {
-            $emptygroup{default} = 1;
-            $emptygroup{nogroup} = 1;
-        }
-        unshift( @$basketgroups, \%emptygroup );
     }
 
     # if the basket is closed, calculate estimated delivery date
@@ -351,6 +394,7 @@ if ( $op eq 'delete_confirm' ) {
     $template->param(
         basketno             => $basketno,
         basketname           => $basket->{'basketname'},
+        basketbranchname     => C4::Branch::GetBranchName($basket->{branch}),
         basketnote           => $basket->{note},
         basketbooksellernote => $basket->{booksellernote},
         basketcontractno     => $basket->{contractnumber},