From f264cfa0515a07fda12ad1e408e9c1152c719c98 Mon Sep 17 00:00:00 2001 From: Alex Buckley Date: Mon, 6 Mar 2023 11:29:35 +0000 Subject: [PATCH] Bug 32560: (follow-up) Replace raw SQL queries with Koha::Patrons search and update Also: - Add use Koha::Script -cron; - Add license - Add optional verbose flag - Add cronjob logging Test plan: 1. Set 'CronjobLog' syspref = 'Log' 2. Edit a borrower to have an expiration date in the past - also make sure they have 'Lost card' set to 'No' 3. Run the set_expired_lost.pl script: sudo koha-shell cd misc/cronjobs ./set_expired_lost.pl 4. Observe no output is displayed but viewing the borrower account from step #1 they now have a 'Lost card' value of 'Yes'. 5. Change the borrower account 'Lost card' back to 'No' 6. Run the set_expired_lost.pl script again, this time with the verbose flag: sudo koha-shell cd misc/cronjobs ./set_expired_lost.pl -v 7. Observe output is displayed to the commandline showing the number of borrowers that have been changed. Check the borrower account again and notice the 'Lost card' has been set back to 'Yes'. 8. Repeat step #6 and observe no output is printed to the terminal - this is because all expired borrowers have already been marked as lost. 9. Check there are log entries for set_expired_lost.pl cronjob in the action_logs table: sudo koha-mysql select * from action_logs where script='set_expired_lost.pl'; Sponsored-by: Dalton McCaughey Library, Australia Signed-off-by: Sam Lau --- misc/cronjobs/set_expired_lost.pl | 56 ++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/misc/cronjobs/set_expired_lost.pl b/misc/cronjobs/set_expired_lost.pl index 02c1a5efd6..ce5b0d0eec 100755 --- a/misc/cronjobs/set_expired_lost.pl +++ b/misc/cronjobs/set_expired_lost.pl @@ -1,24 +1,50 @@ #!/usr/bin/perl -use strict; -use C4::Context; +# This file is part of Koha. +# +# Copyright (C) 2023 Catalyst IT +# +# 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 . +#----------------------------------- -my $dbh = C4::Context->dbh(); -my $DEBUG = 1; +=head1 NAME +set_expired_lost.pl cron script to mark expired borrowers as having list their card + (borrowers.lost = 1) -my $sth = $dbh->prepare("SELECT COUNT(*) AS amount FROM borrowers WHERE dateexpiry < NOW() AND lost <> 1"); -$sth->execute; -my $row = $sth->fetchrow_hashref(); +=cut + +use Modern::Perl; +use Koha::Script -cron; +use Koha::Patrons; +use C4::Log qw( cronlogaction ); +use Getopt::Long qw( GetOptions ); + +my $verbose; +GetOptions( 'v|verbose' => \$verbose ); + +my $command_line_options = join(" ",@ARGV); +cronlogaction({ info => $command_line_options }); + +my $patrons = Koha::Patrons->search({ dateexpiry => { '<' => \'NOW()' }, lost => { '<>' => 1 } }); # Output for email only if borrowers need changing -if ($row->{'amount'}) { - $DEBUG && print $row->{'amount'} . " borrower(s) changed"; +if ($patrons->count) { + print $patrons->count . " borrower(s) changed\n" if $verbose; } -$sth->finish(); - -$sth = $dbh->prepare("UPDATE borrowers SET lost = 1 WHERE dateexpiry < NOW() AND lost <> 1"); -$sth->execute; +foreach my $patron ($patrons) { + $patron->update({ lost => 1 }); +} -$sth->finish; -$dbh->disconnect; +cronlogaction({ action => 'End', info => "COMPLETED" }); -- 2.30.2