From feefbfcc0ee1141717bb1ddcea542fbc99f19353 Mon Sep 17 00:00:00 2001
From: Thibaud Guillot <thibaud.guillot@biblibre.com>
Date: Wed, 5 Jun 2024 11:24:24 +0200
Subject: [PATCH] Bug 37034: Add script to alerts patron when booking ended a
day before
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Test plan:
1) Write a notice template to use datas from this script.
For example :
"
Hello <<borrowers.firstname>>, <<borrowers.surname>>
Some bookings ended yesterday:
[% FOREACH booking IN bookings %]
Booking number [% booking.booking_id %] linked to [% booking.biblio.title %] which was to be
taken from the library [% booking.pickup_library.branchname %], ended [% booking.end_date %].
[% END %]
"
2) This template can have custom lettercode but don't forget to use it
when you launch it. By default (if no lettercode is send to script,
'BOOKINGS_OUTDATED' code will be used)
3) Launch 'perl misc/cronjobs/bookings/bookings_outdated.pl' with args
or not
4) Go on message_queue and you can normally see your message.
Sponsored by: Association de Gestion des Å’uvres Sociales d'Inria (AGOS)
Signed-off-by: David Nind <david@davidnind.com>
---
misc/cronjobs/bookings/bookings_outdated.pl | 186 ++++++++++++++++++++
1 file changed, 186 insertions(+)
create mode 100755 misc/cronjobs/bookings/bookings_outdated.pl
diff --git a/misc/cronjobs/bookings/bookings_outdated.pl b/misc/cronjobs/bookings/bookings_outdated.pl
new file mode 100755
index 0000000000..03f9c90e67
--- /dev/null
+++ b/misc/cronjobs/bookings/bookings_outdated.pl
@@ -0,0 +1,186 @@
+#!/usr/bin/perl
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Getopt::Long qw( GetOptions );
+use Pod::Usage qw( pod2usage );
+use DateTime;
+use DateTime::Duration;
+
+use C4::Context;
+use C4::Letters;
+use C4::Log qw( cronlogaction );
+use Koha::DateUtils qw( dt_from_string );
+use Koha::Notice::Templates;
+use Koha::Patrons;
+use Koha::Bookings;
+use Koha::Script -cron;
+
+=head1 NAME
+
+bookings_overdated.pl - prepare messages to be sent to patrons with bookings ended a day before and not been converted into a loan.
+
+=head1 SYNOPSIS
+
+bookings_overdated.pl
+ -lettercode <notice to send>
+ [ -c ][ -v ]
+
+ Options:
+ -help brief help message
+ -v verbose
+ -lettercode <lettercode> predefined notice to use, default is BOOKING_OUTDATED
+
+=head1 OPTIONS
+
+=over 8
+
+=item B<-help>
+
+Print a brief help message and exits.
+
+=item B<-v>
+
+Verbose. Without this flag set, only fatal errors are reported.
+
+=item B<-lettercode>
+
+Optional parameter, choose a notice to use. Default is 'BOOKING_OUTDATED'.
+
+=back
+
+=head1 DESCRIPTION
+
+This script is designed to alert patrons with bookings
+ended a day before and not been converted into a loan.
+
+=head2 Configuration
+
+This script sends alerts to patrons using a notice
+defined in the Tools > Notices and slips module within Koha. The lettercode
+is passed into this script and, along with other options, determine the content
+of the notices sent to patrons.
+
+
+=head1 USAGE EXAMPLES
+
+C<bookings_overdated.pl -help> Show help for this script
+C<bookings_overdated.pl> Launch without verbose
+C<bookings_overdated.pl -lettercode=FOO -v> Launch with custom notice template code and verbose
+
+=cut
+
+# These variables are set by command line options.
+# They are initially set to default values.
+my $dbh = C4::Context->dbh();
+my $help = 0;
+my $verbose = 0;
+my $lettercode;
+
+my $command_line_options = join( " ", @ARGV );
+
+GetOptions(
+ 'help|?' => \$help,
+ 'v' => \$verbose,
+ 'lettercode=s' => \$lettercode,
+);
+pod2usage(1) if $help;
+
+$lettercode ||= 'BOOKINGS_OUTDATED';
+
+cronlogaction( { info => $command_line_options } );
+
+my $outdated_date_start = dt_from_string()->clone->subtract( days => '1' );
+$outdated_date_start = $outdated_date_start->set(
+ hour => 00,
+ minute => 00,
+ second => 00
+);
+my $outdated_date_end = dt_from_string()->clone->subtract( days => '1' );
+$outdated_date_end = $outdated_date_end->set(
+ hour => 23,
+ minute => 59,
+ second => 59
+);
+
+my $template_exists = Koha::Notice::Templates->find_effective_template(
+ {
+ module => 'bookings',
+ code => $lettercode,
+ lang => 'default',
+ }
+);
+unless ($template_exists) {
+ $verbose and print qq|Message '$lettercode' content not found\n|;
+ next;
+}
+
+my $dtf = Koha::Database->new->schema->storage->datetime_parser;
+my $booking_outdated_start = $dtf->format_datetime($outdated_date_start);
+my $booking_outdated_end = $dtf->format_datetime($outdated_date_end);
+
+my $bookings = Koha::Bookings->search(
+ { end_date => [ -and => { '>=' => $booking_outdated_start }, { '<=' => $booking_outdated_end } ] },
+ { prefetch => 'patron' }
+);
+
+$verbose and warn $bookings->count . " bookings outdated between $booking_outdated_start and $booking_outdated_end \n";
+
+my %done;
+
+my %patrons;
+while ( my $booking = $bookings->next ) {
+ my $booking = Koha::Bookings->find( $booking->booking_id );
+ push @{ $patrons{ $booking->patron_id }->{bookings} }, $booking;
+}
+
+foreach my $borrowernumber ( keys %patrons ) {
+
+ my $patron = Koha::Patrons->find( { borrowernumber => $borrowernumber } );
+ $verbose
+ and print " borrower "
+ . $patron->surname . ", "
+ . $patron->firstname . " has "
+ . scalar @{ $patrons{$borrowernumber}->{bookings} }
+ . " booking(s) outdated.\n";
+
+ my $letter = C4::Letters::GetPreparedLetter(
+ module => 'bookings',
+ letter_code => $lettercode,
+ borrowernumber => $borrowernumber,
+ tables => {
+ borrowers => $borrowernumber,
+ },
+ objects => { bookings => $patrons{$borrowernumber}->{bookings} },
+ );
+
+ if ($letter) {
+ C4::Letters::EnqueueLetter(
+ {
+ letter => $letter,
+ borrowernumber => $patron->borrowernumber,
+ message_transport_type => 'email',
+ }
+ );
+ }
+
+ # Mark this borrower as completed
+ $done{ $patron->borrowernumber } = 1;
+}
+
+cronlogaction( { action => 'End', info => "COMPLETED" } );
--
2.39.2