@@ -, +, @@ $ kshell k$ prove t/db_dependent/Koha/Acquisition/Order.t --- Koha/Acquisition/Order.pm | 57 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) --- a/Koha/Acquisition/Order.pm +++ a/Koha/Acquisition/Order.pm @@ -18,6 +18,9 @@ package Koha::Acquisition::Order; use Modern::Perl; use Carp qw( croak ); +use Try::Tiny; + +use C4::Biblio qw(DelBiblio); use Koha::Acquisition::Baskets; use Koha::Acquisition::Funds; @@ -25,6 +28,7 @@ use Koha::Acquisition::Invoices; use Koha::Acquisition::Order::Claims; use Koha::Database; use Koha::DateUtils qw( dt_from_string output_pref ); +use Koha::Exceptions::Object; use Koha::Biblios; use Koha::Holds; use Koha::Items; @@ -97,6 +101,59 @@ sub store { return $self; } +=head3 cancel + + $order->cancel( + { [ reason => $reason, + delete_biblio => $delete_biblio ] + } + ); + +=cut + +sub cancel { + my ($self, $params) = @_; + + my $delete_biblio = $params->{delete_biblio}; + my $reason = $params->{reason}; + + try { + $self->_result->result_source->schema->txn_do( + sub { + # Delete the related items + $self->items->safe_delete; + + if ( $delete_biblio ) { + my $biblio = $self->biblio; + if ( $biblio and $biblio->items->count == 0 ) { + my $delcheck = DelBiblio( $biblio->id ); + if ( $delcheck ) { + Koha::Exceptions::Object::CannotBeDeleted->throw( + object => $biblio, + reason => $delcheck + ); + } + } + } + + # Update order status + $self->set( + { + cancellationreason => $reason, + datecancellationprinted => \'NOW()', + orderstatus => 'cancelled', + } + )->store; + } + ); + } + catch { + $_->rethrow; + }; + + return $self; +} + =head3 add_item $order->add_item( $itemnumber ); --