Bug 26577: Make basket.pl and cancelorder.pl use ->cancel
authorTomas Cohen Arazi <tomascohen@theke.io>
Wed, 30 Sep 2020 14:14:37 +0000 (11:14 -0300)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Thu, 15 Oct 2020 12:50:07 +0000 (14:50 +0200)
This patch makes the mentioned controller scripts use the new
$order->cancel method instead of DelOrder.

To test:
1. Apply this patches
2. Have a basket with some orders
3. Play with cancelling orders and removing baskets
=> SUCCESS: No behaviour change!
=> SUCCESS: No errors!
4. Sign off :-D

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
acqui/basket.pl
acqui/cancelorder.pl

index a53160d..d0fa4f2 100755 (executable)
@@ -128,45 +128,38 @@ if ( $op eq 'delete_confirm' ) {
     output_and_exit( $query, $cookie, $template, 'insufficient_permission' )
       unless $logged_in_patron->has_permission( { acquisition => 'delete_baskets' } );
 
-    my $basketno = $query->param('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 $biblio = Koha::Biblios->find( $biblionumber );
-            my $countbiblio = $biblio->active_orders->count;
-            my $ordernumber = $myorder->{'ordernumber'};
-            my $cnt_subscriptions = $biblio->subscriptions->count;
-            my $itemcount = $biblio->items->count;
-            my $error;
-            if ($countbiblio == 0 && $itemcount == 0 && not $cnt_subscriptions ) {
-                $error = DelBiblio($myorder->{biblionumber}) }
-            else {
-                push @cannotdelbiblios, {biblionumber=> ($myorder->{biblionumber}),
-                                         title=> $myorder->{'title'},
-                                         author=> $myorder->{'author'},
-                                         countbiblio=> $countbiblio,
-                                         itemcount=>$itemcount,
-                                         subscriptions => $cnt_subscriptions};
-            }
-            if ($error) {
-                push @cannotdelbiblios, {biblionumber=> ($myorder->{biblionumber}),
-                                         title=> $myorder->{'title'},
-                                         author=> $myorder->{'author'},
-                                         othererror=> $error};
-            }
+    my $basketno   = $query->param('basketno');
+    my $delbiblio  = $query->param('delbiblio');
+    my $basket_obj = Koha::Acquisition::Baskets->find($basketno);
+
+    my $orders = $basket_obj->orders;
+
+    my @cannotdelbiblios;
+
+    while ( my $order = $orders->next ) {
+        # cancel the order
+        $order->cancel({ delete_biblio => $delbiblio });
+        my @messages = @{ $order->messages };
+
+        if ( scalar @messages > 0 ) {
+
+            my $biblio = $order->biblio;
+
+            push @cannotdelbiblios, {
+                biblionumber  => $biblio->id,
+                title         => $biblio->title // '',
+                author        => $biblio->author // '',
+                countbiblio   => $biblio->active_orders->count,
+                itemcount     => $biblio->items->count,
+                subscriptions => $biblio->subscriptions->count,
+            };
         }
-        $template->param( cannotdelbiblios => \@cannotdelbiblios );
     }
- # delete the basket
-    DelBasket($basketno,);
+
+    $template->param( cannotdelbiblios => \@cannotdelbiblios );
+
+    # delete the basket
+    $basket_obj->delete;
     $template->param(
         delete_confirmed => 1,
         booksellername => $bookseller->name,
index 84d0348..d5b57f8 100755 (executable)
@@ -52,15 +52,19 @@ my $biblionumber = $input->param('biblionumber');
 my $basketno = $input->param('basketno');
 my $basket = Koha::Acquisition::Baskets->find({ basketno => $basketno }, { prefetch => 'booksellerid' });
 my $referrer = $input->param('referrer') || $input->referer;
-my $del_biblio = $input->param('del_biblio') ? 1 : 0;
+my $delete_biblio = $input->param('del_biblio') ? 1 : 0;
 
-if($action and $action eq "confirmcancel") {
+if( $action and $action eq "confirmcancel" ) {
     my $reason = $input->param('reason');
-    my $error = DelOrder($biblionumber, $ordernumber, $del_biblio, $reason);
+    my $order  = Koha::Acquisition::Orders->find($ordernumber);
+    $order->cancel({ reason => $reason, delete_biblio => $delete_biblio });
+    my @messages = @{ $order->messages };
 
-    if($error) {
-        $template->param(error_delitem => 1) if $error->{'delitem'};
-        $template->param(error_delbiblio => 1) if $error->{'delbiblio'};
+    if ( scalar @messages > 0 ) {
+        $template->param( error_delitem => 1 )
+            if $messages[0]->message eq 'error_delitem';
+        $template->param( error_delbiblio => 1 )
+            if $messages[0]->message eq 'error_delbiblio';
     } else {
         $template->param(success_cancelorder => 1);
     }
@@ -72,7 +76,7 @@ $template->param(
     biblionumber => $biblionumber,
     basket => $basket,
     referrer => $referrer,
-    del_biblio => $del_biblio,
+    del_biblio => $delete_biblio,
 );
 
 output_html_with_http_headers $input, $cookie, $template->output;