From e37d0a9f8829db29883aab20d53a2c458d64ca88 Mon Sep 17 00:00:00 2001 From: Paul Derscheid Date: Mon, 2 Sep 2024 12:42:11 +0200 Subject: [PATCH] Bug 37803: Add patron notification when a new booking has been created successfully MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To test: 1) Apply the patch 2) Include the sample notice, e.g. run 'reset_all' on ktd 3) Create a booking 4) Look into the borrower notifications 5) Confirm the notice has been generated 6) Run t/db_dependent/Koha/Booking.t Signed-off-by: LEBSimonsen Signed-off-by: Martin Renvoize Sponsored-by: Büchereizentrale Schleswig-Holstein --- Koha/Booking.pm | 66 ++++++++++++------- .../mysql/en/mandatory/sample_notices.yml | 24 +++++++ t/db_dependent/Koha/Booking.t | 55 +++++++++++++++- 3 files changed, 120 insertions(+), 25 deletions(-) diff --git a/Koha/Booking.pm b/Koha/Booking.pm index c849c07e9e..879ae3b6f6 100644 --- a/Koha/Booking.pm +++ b/Koha/Booking.pm @@ -153,35 +153,55 @@ sub 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) ) - { + + if ( $self = $self->SUPER::store ) { my $patron = $self->patron; my $pickup_library = $self->pickup_library; my $branch = C4::Context->userenv->{'branch'}; - my $letter = C4::Letters::GetPreparedLetter( - module => 'bookings', - letter_code => 'BOOKING_MODIFICATION', - message_transport_type => 'email', - branchcode => $branch, - lang => $patron->lang, - objects => { - old_booking => $old_booking, - booking => $self - }, - ); + if ( $is_modification + and any { $old_booking->$_ ne $self->$_ } qw(pickup_library_id start_date end_date) ) + { + my $letter = C4::Letters::GetPreparedLetter( + module => 'bookings', + letter_code => 'BOOKING_MODIFICATION', + message_transport_type => 'email', + branchcode => $branch, + lang => $patron->lang, + objects => { + old_booking => $old_booking, + booking => $self + }, + ); - if ($letter) { - C4::Letters::EnqueueLetter( - { - letter => $letter, - borrowernumber => $patron->borrowernumber, - message_transport_type => 'email', - } + if ($letter) { + C4::Letters::EnqueueLetter( + { + letter => $letter, + borrowernumber => $patron->borrowernumber, + message_transport_type => 'email', + } + ); + } + } elsif ( !$is_modification ) { + my $letter = C4::Letters::GetPreparedLetter( + module => 'bookings', + letter_code => 'BOOKING_CONFIRMATION', + message_transport_type => 'email', + branchcode => $branch, + 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 7d4363884f..52ec98776d 100644 --- a/installer/data/mysql/en/mandatory/sample_notices.yml +++ b/installer/data/mysql/en/mandatory/sample_notices.yml @@ -90,6 +90,30 @@ tables: - "
" - "[% booking.pickup_library.branchname %]" + - module: bookings + code: BOOKING_CONFIRMATION + branchcode: "" + name: "Booking confirmation notice" + is_html: 1 + title: "A library booking was confirmed" + 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 confirmed.
" + - "
" + - "The 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 cfb9764a6e..80b7983cf1 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 => 14; + plan tests => 15; $schema->storage->txn_begin; my $patron = $builder->build_object( { class => "Koha::Patrons" } ); @@ -323,7 +323,7 @@ subtest 'store() tests' => sub { # ✓ Any (1) |--| }; - subtest 'notice trigger' => sub { + subtest 'modification notice trigger' => sub { plan tests => 3; my $original_notices_count = Koha::Notice::Messages->search( @@ -405,6 +405,57 @@ subtest 'store() tests' => sub { ); }; + subtest 'confirmation notice trigger' => sub { + plan tests => 2; + + my $original_notices_count = Koha::Notice::Messages->search( + { + letter_code => 'BOOKING_CONFIRMATION', + borrowernumber => $patron->borrowernumber, + } + )->count; + + # Reuse previous booking to produce a clash + eval { $booking = Koha::Booking->new( $booking->unblessed )->store }; + + my $post_notices_count = Koha::Notice::Messages->search( + { + letter_code => 'BOOKING_CONFIRMATION', + borrowernumber => $patron->borrowernumber, + } + )->count; + is( + $post_notices_count, + $original_notices_count, + 'Koha::Booking->store should not have enqueued a BOOKING_CONFIRMATION email if booking creation fails' + ); + + $start_1 = dt_from_string->add( months => 1 )->truncate( to => 'day' ); + $end_1 = $start_1->clone()->add( days => 1 ); + + $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; + + $post_notices_count = Koha::Notice::Messages->search( + { + letter_code => 'BOOKING_CONFIRMATION', + borrowernumber => $patron->borrowernumber, + } + )->count; + is( + $post_notices_count, + $original_notices_count + 1, + 'Koha::Booking->store should have enqueued a BOOKING_CONFIRMATION email for a new booking' + ); + }; + $schema->storage->txn_rollback; }; -- 2.47.0