Bug 9224: Make acqui/finishreceive.pl Plack-compatible
[koha_fer] / acqui / basket.pl
index 681cd60..6833eb6 100755 (executable)
@@ -28,6 +28,7 @@ use C4::Output;
 use CGI;
 use C4::Acquisition;
 use C4::Budgets;
+use C4::Branch;
 use C4::Bookseller qw( GetBookSellerFromId);
 use C4::Debug;
 use C4::Biblio;
@@ -80,13 +81,25 @@ my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
 );
 
 my $basket = GetBasket($basketno);
+$booksellerid = $basket->{booksellerid} unless $booksellerid;
+my ($bookseller) = GetBookSellerFromId($booksellerid);
+
+unless (CanUserManageBasket($loggedinuser, $basket, $userflags)) {
+    $template->param(
+        cannot_manage_basket => 1,
+        basketno => $basketno,
+        basketname => $basket->{basketname},
+        booksellerid => $booksellerid,
+        name => $bookseller->{name}
+    );
+    output_html_with_http_headers $query, $cookie, $template->output;
+    exit;
+}
 
 # FIXME : what about the "discount" percentage?
 # FIXME : the query->param('booksellerid') below is probably useless. The bookseller is always known from the basket
 # if no booksellerid in parameter, get it from basket
 # warn "=>".$basket->{booksellerid};
-$booksellerid = $basket->{booksellerid} unless $booksellerid;
-my ($bookseller) = GetBookSellerFromId($booksellerid);
 my $op = $query->param('op');
 if (!defined $op) {
     $op = q{};
@@ -97,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 );
@@ -177,20 +226,34 @@ if ( $op eq 'delete_confirm' ) {
         }
         exit;
     } else {
-    $template->param(confirm_close => "1",
-            booksellerid    => $booksellerid,
-            basketno        => $basket->{'basketno'},
-                basketname      => $basket->{'basketname'},
-            basketgroupname => $basket->{'basketname'});
-        
+    $template->param(
+        confirm_close   => "1",
+        booksellerid    => $booksellerid,
+        basketno        => $basket->{'basketno'},
+        basketname      => $basket->{'basketname'},
+        basketgroupname => $basket->{'basketname'},
+    );
     }
 } elsif ($op eq 'reopen') {
-    my $basket;
-    $basket->{basketno} = $query->param('basketno');
-    $basket->{closedate} = undef;
-    ModBasket($basket);
+    ReopenBasket($query->param('basketno'));
     print $query->redirect('/cgi-bin/koha/acqui/basket.pl?basketno='.$basket->{'basketno'})
+} elsif ( $op eq 'mod_users' ) {
+    my $basketusers_ids = $query->param('basketusers_ids');
+    my @basketusers = split( /:/, $basketusers_ids );
+    ModBasketUsers($basketno, @basketusers);
+    print $query->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=$basketno");
+    exit;
+} elsif ( $op eq 'mod_branch' ) {
+    my $branch = $query->param('branch');
+    $branch = undef if(defined $branch and $branch eq '');
+    ModBasket({
+        basketno => $basket->{basketno},
+        branch   => $branch
+    });
+    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;
@@ -204,7 +267,34 @@ if ( $op eq 'delete_confirm' ) {
                 exit 1;
             }
         }
+        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
     my ($basketgroup, $basketgroups);
     my $staffuser = GetMember(borrowernumber => $loggedinuser);
@@ -241,6 +331,13 @@ if ( $op eq 'delete_confirm' ) {
       "loggedinuser: $loggedinuser; creationdate: %s; authorisedby: %s",
       $basket->{creationdate}, $basket->{authorisedby};
 
+    my @basketusers_ids = GetBasketUsers($basketno);
+    my @basketusers;
+    foreach my $basketuser_id (@basketusers_ids) {
+        my $basketuser = GetMember(borrowernumber => $basketuser_id);
+        push @basketusers, $basketuser if $basketuser;
+    }
+
     #to get active currency
     my $cur = GetCurrency();
 
@@ -307,13 +404,17 @@ 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},
         basketcontractname   => $contract->{contractname},
+        branches_loop        => \@branches_loop,
         creationdate         => $basket->{creationdate},
         authorisedby         => $basket->{authorisedby},
         authorisedbyname     => $basket->{authorisedbyname},
+        basketusers_ids      => join(':', @basketusers_ids),
+        basketusers          => \@basketusers,
         closedate            => $basket->{closedate},
         estimateddeliverydate=> $estimateddeliverydate,
         deliveryplace        => C4::Branch::GetBranchName( $basket->{deliveryplace} ),