From 82a5b673e18ea423260d3699eed982980c5081aa 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 Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer --- cataloguing/addbiblio.pl | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/cataloguing/addbiblio.pl b/cataloguing/addbiblio.pl index 6230c922f6..9d5a216549 100755 --- a/cataloguing/addbiblio.pl +++ b/cataloguing/addbiblio.pl @@ -20,9 +20,10 @@ # along with Koha; if not, see . use Modern::Perl; - use CGI; use POSIX; +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( @@ -48,6 +49,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; @@ -779,15 +781,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.39.5