diff --git a/Koha/Booking.pm b/Koha/Booking.pm index e5297d8d17..c16ae91c4f 100644 --- a/Koha/Booking.pm +++ b/Koha/Booking.pm @@ -27,6 +27,8 @@ use Koha::Libraries; use C4::Letters; +use List::Util qw(any); + use base qw(Koha::Object); =head1 NAME @@ -149,7 +151,35 @@ sub store { $self->_assign_item_for_booking; } - $self = $self->SUPER::store; + my $is_modification = $self->in_storage; + my $old_booking = $self->get_from_storage; + if ( $self = + $self->SUPER::store + and $is_modification + and any { $old_booking->$_ ne $self->$_ } qw(pickup_library_id start_date end_date) ) + { + my $patron = $self->patron; + my $pickup_library = $self->pickup_library; + + my $letter = C4::Letters::GetPreparedLetter( + module => 'bookings', + letter_code => 'BOOKING_MODIFICATION', + message_transport_type => 'email', + branchcode => $pickup_library->branchcode, + lang => $patron->lang, + objects => { booking => $self }, + ); + + if ($letter) { + C4::Letters::EnqueueLetter( + { + letter => $letter, + borrowernumber => $patron->borrowernumber, + message_transport_type => 'email', + } + ); + } + } } ); diff --git a/installer/data/mysql/en/mandatory/sample_notices.yml b/installer/data/mysql/en/mandatory/sample_notices.yml index 8950c41e51..c7aa453037 100644 --- a/installer/data/mysql/en/mandatory/sample_notices.yml +++ b/installer/data/mysql/en/mandatory/sample_notices.yml @@ -66,6 +66,30 @@ tables: - "
" - "[% booking.pickup_library.branchname %]" + - module: bookings + code: BOOKING_MODIFICATION + branchcode: "" + name: "Booking modification notice" + is_html: 1 + title: "A library booking was modified" + message_transport_type: email + lang: default + content: + - "[%- PROCESS 'html_helpers.inc' -%]" + - "[%- USE KohaDates -%]" + - "Dear [%- INCLUDE 'patron-title.inc' patron => booking.patron -%],
" + - "
" + - "Your booking of [%- INCLUDE 'biblio-title.inc' biblio=booking.biblio link = 0 -%] has been modified.
" + - "
" + - "The new details are:
" + - "
" + - "Dates: [% booking.start_date | $KohaDates %] to [% booking.end_date | $KohaDates %]
" + - "
" + - "Pickup at: [% booking.pickup_library.branchname %]
" + - "Kind regards
" + - "
" + - "[% booking.pickup_library.branchname %]" + - module: catalogue code: TICKET_ACKNOWLEDGE branchcode: "" diff --git a/t/db_dependent/Koha/Booking.t b/t/db_dependent/Koha/Booking.t index a0805a6a14..9eef67069d 100755 --- a/t/db_dependent/Koha/Booking.t +++ b/t/db_dependent/Koha/Booking.t @@ -123,7 +123,7 @@ subtest 'Relation accessor tests' => sub { }; subtest 'store() tests' => sub { - plan tests => 13; + plan tests => 14; $schema->storage->txn_begin; my $patron = $builder->build_object( { class => "Koha::Patrons" } ); @@ -322,6 +322,88 @@ subtest 'store() tests' => sub { # ✓ Any (1) |--| }; + subtest 'notice trigger' => sub { + plan tests => 3; + + my $original_notices_count = Koha::Notice::Messages->search( + { + letter_code => 'BOOKING_MODIFICATION', + borrowernumber => $patron->borrowernumber, + } + )->count; + + $start_1 = dt_from_string->add( months => 1 )->truncate( to => 'day' ); + $end_1 = $start_1->clone()->add( days => 1 ); + + # Use datetime formatting so that we don't get DateTime objects + $booking = Koha::Booking->new( + { + patron_id => $patron->borrowernumber, + biblio_id => $biblio->biblionumber, + pickup_library_id => $item_2->homebranch, + start_date => $start_1->datetime(q{ }), + end_date => $end_1->datetime(q{ }), + } + )->store; + + my $post_notices_count = Koha::Notice::Messages->search( + { + letter_code => 'BOOKING_MODIFICATION', + borrowernumber => $patron->borrowernumber, + } + )->count; + is( + $post_notices_count, + $original_notices_count, + 'Koha::Booking->store should not have enqueued a BOOKING_MODIFICATION email for a new booking' + ); + + my $item_4 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber, bookable => 1 } ); + + $booking->update( + { + item_id => $item_4->itemnumber, + } + ); + + $post_notices_count = Koha::Notice::Messages->search( + { + letter_code => 'BOOKING_MODIFICATION', + borrowernumber => $patron->borrowernumber, + } + )->count; + is( + $post_notices_count, + $original_notices_count, + 'Koha::Booking->store should not have enqueued a BOOKING_MODIFICATION email for a booking with modified item_id' + ); + + $booking->update( + { + start_date => $start_1->clone()->add( days => 1 )->datetime(q{ }), + } + ); + + # start_date, end_date and pickup_library_id should behave identical + $post_notices_count = Koha::Notice::Messages->search( + { + letter_code => 'BOOKING_MODIFICATION', + borrowernumber => $patron->borrowernumber, + } + )->count; + is( + $post_notices_count, + $original_notices_count + 1, + 'Koha::Booking->store should have enqueued a BOOKING_MODIFICATION email for a booking with modified start_date' + ); + + $booking->update( + { + end_date => $end_1->clone()->add( days => 1 )->datetime(q{ }), + } + ); + }; + $schema->storage->txn_rollback; };