From a7b676c63f4aa3fa284e647c121789b637c79f31 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Wed, 13 Oct 2021 14:02:47 +0100 Subject: [PATCH] Bug 24609: Add trigger to set items.onloan on due_date update This patch adds an overridden store method to Koha::Checkout to catch due_date changes and trigger an update of the items.onloan field. Signed-off-by: Tomas Cohen Arazi --- Koha/Checkout.pm | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Koha/Checkout.pm b/Koha/Checkout.pm index cc00194248..041cba9c65 100644 --- a/Koha/Checkout.pm +++ b/Koha/Checkout.pm @@ -42,6 +42,39 @@ Koha::Checkout - Koha Checkout object class =cut +=head3 store + + $item->store; + +$params can take an optional 'skip_record_index' parameter. +If set, the reindexation process will not happen (index_records not called) + +NOTE: This is a temporary fix to answer a performance issue when lot of checkout +dates change which would result in lots of items onloan dates changing. +The correct way to fix this is to make the ES reindexation process async. +You should not turn it on if you do not understand what it is doing exactly. + +=cut + +sub store { + my $self = shift; + my $params = @_ ? shift : {}; + + # Update + if ( $self->in_storage ) { + my %updated_columns = $self->_result->get_dirty_columns; + return $self->SUPER::store unless %updated_columns; + + # If checkout due_date was updated, trigger the item onloan date change + if ( exists $updated_columns{date_due} ) { + $self->item->onloan( $updated_columns{date_due} ) + ->store( { skip_record_index => $params->{skip_record_index} } ); + } + } + + return $self->SUPER::store; +} + =head3 is_overdue my $is_overdue = $checkout->is_overdue( [ $reference_dt ] ); -- 2.32.0