From 870ef9e2d9046914e198d7c03fa64b4b89de3b41 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 30 Sep 2020 11:14:37 -0300 Subject: [PATCH] Bug 26577: Make basket.pl and cancelorder.pl use ->cancel 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 Signed-off-by: Kyle M Hall Signed-off-by: Jonathan Druart --- acqui/basket.pl | 67 ++++++++++++++++++++------------------------ acqui/cancelorder.pl | 18 +++++++----- 2 files changed, 41 insertions(+), 44 deletions(-) diff --git a/acqui/basket.pl b/acqui/basket.pl index a53160d70a..d0fa4f2eb7 100755 --- a/acqui/basket.pl +++ b/acqui/basket.pl @@ -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, diff --git a/acqui/cancelorder.pl b/acqui/cancelorder.pl index 84d03485b5..d5b57f81d6 100755 --- a/acqui/cancelorder.pl +++ b/acqui/cancelorder.pl @@ -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; -- 2.39.5