From b371df5428c7f2c8ac716471d9933db811dccbc1 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 Content-Type: text/plain; charset=utf-8 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 --- cataloguing/addbiblio.pl | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/cataloguing/addbiblio.pl b/cataloguing/addbiblio.pl index e84c542092..60c8c45e06 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; @@ -762,14 +764,27 @@ if ( $op eq "addbiblio" ) { } } elsif ( $op eq "delete" ) { - - my $error = &DelBiblio($biblionumber); + + # 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