From 11cf28158ed4ad2268fa038010253d373c614c98 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Mon, 19 Feb 2024 11:43:32 +0000 Subject: [PATCH] Bug 27893: Cancel acq orders in addbiblio.pl When the user confirms removal although we have linked orders, we should cancel them. Test plan: Add order line to a basket. Goto newly added biblio record, remove items, remove biblio. Notice the warning about attached order lines. Confirm. Check the basket again after deletion took place (cancelled line). Signed-off-by: Marcel de Rooy Signed-off-by: David Nind --- cataloguing/addbiblio.pl | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/cataloguing/addbiblio.pl b/cataloguing/addbiblio.pl index a340c6ebbf..407ef39585 100755 --- a/cataloguing/addbiblio.pl +++ b/cataloguing/addbiblio.pl @@ -20,8 +20,9 @@ # along with Koha; if not, see . use Modern::Perl; - use CGI; +use Try::Tiny qw(catch try); + use C4::Output qw( output_html_with_http_headers ); use C4::Auth qw( get_template_and_user haspermission ); use C4::Biblio qw( @@ -47,6 +48,7 @@ use C4::Charset qw( SetMarcUnicodeFlag ); use Koha::BiblioFrameworks; use Koha::DateUtils qw( dt_from_string ); +use Koha::Acquisition::Orders; use Koha::Biblios; use Koha::ItemTypes; use Koha::Libraries; @@ -765,15 +767,29 @@ if ( $op eq "cud-addbiblio" ) { ); } } -elsif ( $op eq "cud-delete" ) { - my $error = &DelBiblio($biblionumber); +elsif ( $op eq "cud-delete" ) { + + # Cancel attached order lines first before deleting biblio ! + my $error; + try { + my @result = Koha::Acquisition::Orders->search( { biblionumber => $biblionumber } )->cancel; + my $warns = @{ $result[1] }; + if ( $result[0] && $warns ) { # warnings about order lines not removed + warn sprintf( "%d order lines were deleted, but %d lines gave a warning\n", $result[0], $warns ); + } + $error = &DelBiblio($biblionumber); + } catch { + $error = ref($_) ? 'Exception raised - ' . $_->error : $_; + }; + if ($error) { + #FIXME This should be handled in template alert warn "ERROR when DELETING BIBLIO $biblionumber : $error"; print "Content-Type: text/html\n\n

ERROR when DELETING BIBLIO $biblionumber : $error

"; - exit; + exit; } - + print $input->redirect('/cgi-bin/koha/catalogue/search.pl' . ($searchid ? "?searchid=$searchid" : "")); exit; -- 2.30.2