From c8608a0db3c6f90dec3cfd01c6860e1e8693708a Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 23 Sep 2020 11:36:21 -0300 Subject: [PATCH] Bug 26515: Add Koha::Acquisition::Order->cancel This patch introduces an OO replacement for DelOrder. It does the same thing, but raises exceptions instead of returning error values. It is designed so the caller catches the exceptions and (1) presents the situation to the end user or (2) does the right thing depending on the exception. To test: 1. Apply this patches 2. Run: $ kshell k$ prove t/db_dependent/Koha/Acquisition/Order.t => SUCCESS: Tests pass! 3. Read the tests carefully to understand how they cover all use cases. 4- Sign off :-D Sponsored-by: ByWater Solutions Signed-off-by: Tomas Cohen Arazi --- Koha/Acquisition/Order.pm | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Koha/Acquisition/Order.pm b/Koha/Acquisition/Order.pm index 2747a78fc5c..d3861db5370 100644 --- a/Koha/Acquisition/Order.pm +++ b/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,45 @@ sub store { return $self; } +=head3 cancel + + $order->cancel({ [ reason => $reason ] }); + +This method marks an order as cancelled. As the order is cancelled, the (eventual) items +attached to it are removed if possible. All happens inside a transaction which is rolled back +if either fails. + +=cut + +sub cancel { + my ($self, $params) = @_; + + my $reason = $params->{reason}; + + try { + $self->_result->result_source->schema->txn_do( + sub { + # Delete the related items + $self->items->safe_delete; + + # Update order status + $self->set( + { + cancellationreason => $reason, + datecancellationprinted => \'NOW()', + orderstatus => 'cancelled', + } + )->store; + } + ); + } + catch { + $_->rethrow; + }; + + return $self; +} + =head3 add_item $order->add_item( $itemnumber ); -- 2.28.0