Commit a79ea45d authored by Benjamin Rokseth's avatar Benjamin Rokseth
Browse files

cronjobs: split overdue_messages and debarring of patrons

parent de6db3c5
#!/usr/bin/perl -w
#
# cronjob: debar_patrons.pl
# This job handles debarments according to overduerules
# Mainly a replica of the logic in overdue_messages.pl
use Modern::Perl;
use strict;
use warnings;
BEGIN {
# find Koha's Perl modules
use FindBin;
eval { require "$FindBin::Bin/usr/share/koha/bin/kohalib.pl" };
}
use C4::Context;
use C4::Overdues;
use Carp;
use Koha::Calendar;
use Koha::DateUtils;
use Koha::Purresaker;
use Koha::Patron::Debarments qw(AddUniqueDebarment);
use C4::Log qw( cronlogaction );
use utf8;
binmode( STDOUT, ":utf8" );
use Getopt::Long;
my $help;
my ($verbose, $test_mode) = (0,0);
GetOptions(
'h|help' => \$help,
'v|verbose' => \$verbose,
't|test' => \$test_mode,
);
my $usage = << 'ENDUSAGE';
This script handles message shipment to patrons with overdues.
This script has the following parameters :
-h --help: this message
-v --verbose: more output
-t --test: test mode, don't debar
ENDUSAGE
if ($help) {
print $usage;
exit;
}
cronlogaction();
# SYSPREFS
my $circControl = C4::Context->preference('CircControl');
my $from_address = C4::Context->preference('KohaAdminEmailAddress');
my $today = DateTime->now( time_zone => C4::Context->tz() );
my ($totalOverdues,$triggeredOverdues,$patronsDebarred) = (0,0,0);
my %branch_holiday;
my $patrons = Koha::Purresaker->GetPatronsWithOverdues();
my $overdueRules = Koha::Purresaker->GetOverdueRules();
# Loop each patron with at least one overdue
PATRON: while ( my $patron = $patrons->fetchrow_hashref() ) {
# get applying circ rules for patron or item
# Is this relevant any longer?
my $circ_rules_branchcode =
( $circControl eq 'ItemHomeLibrary' ) ? $patron->{itemhomebranch}
: ( $circControl eq 'PatronLibrary' ) ? $patron->{borrbranch}
: $patron->{branchcode};
# Add holidays to hashmap if not already added
if (!$branch_holiday{$circ_rules_branchcode}) {
my $cal = Koha::Calendar->new( branchcode => $circ_rules_branchcode );
$branch_holiday{$circ_rules_branchcode} = $cal->is_holiday($today);
}
# overduerules hashmap has key "borrbranch|borrcat" if not, default to "|borrcat"
my $rule = $overdueRules->{"$patron->{borrbranch}|$patron->{borrcat}"} ? $overdueRules->{"$patron->{borrbranch}|$patron->{borrcat}"} : $overdueRules->{"|$patron->{borrcat}"};
$rule or next; # No overduerule = no message
use Data::Dumper;
# We loop through overduerules backwards, oldest overdues define what message is sent
# 1 - If we have any overdue passed letter3 give this notice, otherwise check letter2, etc..
# 2 - If days_overdue equals delay, we define a message for this patron to be sent
PERIOD: foreach my $i ( reverse 1 .. 3 ) {
my $delay = $rule->{"letter$i"}->{delay};
my $letter = $rule->{"letter$i"}->{letter};
if ($rule->{"letter$i"}->{delay}) {
my $overdues = Koha::Purresaker->GetPatronOverdues($patron->{borrowernumber}, $rule->{"letter$i"}->{delay});
$overdues or next PERIOD;
my $ct = scalar @{$overdues};
$totalOverdues += $ct;
# Check if we have an overdue that matches this delay trigger, otherwise move to next
my ($trigger) = grep { $_->{days_overdue} == $rule->{"letter$i"}->{delay} } @{$overdues};
if ($trigger) {
++$triggeredOverdues;
if ($rule->{"letter$i"}->{debar}) {
$verbose and print "debarring patron " .$patron->{borrowernumber}. "\tcategory: ".$patron->{borrcat}."\trule: " .$rule->{"letter$i"}->{delay}. "\tdays_overdue: " . $trigger->{days_overdue}. "\n";
$test_mode or Koha::Patron::Debarments::AddUniqueDebarment({
borrowernumber => $patron->{borrowernumber},
type => 'OVERDUES',
comment => "OVERDUES_PROCESS " . _today(),
});
++$patronsDebarred;
next PATRON;
}
}
}
}
}
sub _today {
return DateTime->now()->strftime('%d.%m.%Y');
}
my $patronsWithOverdues = $patrons->rows;
$test_mode and print "TEST MODE\n";
print <<"EOM";
Debarment Cronjob -- $today
Total overdues: $totalOverdues
Patrons with overdues: $patronsWithOverdues
Triggered overdues: $triggeredOverdues
Patrons debarred: $patronsDebarred
EOM
......@@ -68,6 +68,7 @@ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
##############
0 5 * * * root koha-foreach --enabled /cronjobs/purresaker.pl
15 5 * * * root koha-foreach --enabled /cronjobs/overdue_messages.pl
30 5 * * * root koha-foreach --enabled /cronjobs/debar_patrons.pl
##############
# Process notice with transport type 'print' to print service
......
......@@ -3,7 +3,6 @@
# cronjob: overdue_messages.pl
# This job handles cronjob for overduerules
# Sends notices for patrons with overdues and/or fines
# It also adds debarments
use Modern::Perl;
use strict;
......@@ -24,7 +23,6 @@ use C4::Letters;
use Koha::Calendar;
use Koha::DateUtils;
use Koha::Purresaker;
use Koha::Patron::Debarments qw(AddUniqueDebarment);
use C4::Log qw( cronlogaction );
use Template;
......@@ -49,7 +47,7 @@ This script handles message shipment to patrons with overdues.
This script has the following parameters :
-h --help: this message
-v --verbose: more output
-t --test: test mode, don't debar or send messages
-t --test: test mode, don't send messages
ENDUSAGE
......@@ -247,7 +245,7 @@ Du har lån som skulle vært levert for 5 dager siden <<issues.date_due>>
[% o.title %], [% o.author %] [% o.barcode %]
[%- END %]
Du kan prøve å forlenge lånene dine på Mine sider https://sok.deichman.no/profile
Du kan prøve å forlenge lånene dine på Mine sider https://sok.deichman.no/profile
Lån som allerede er forlenget 3 ganger eller reservert av andre, kan ikke forlenges.
Hilsen
......@@ -262,7 +260,7 @@ my $from_address = C4::Context->preference('KohaAdminEmailAddress');
my $today = DateTime->now( time_zone => C4::Context->tz() );
my ($totalOverdues,$triggeredOverdues,$smsMessagesSent,$emailMessagesSent,$printMessagesSent, $patronsDebarred) = (0,0,0,0,0,0);
my ($totalOverdues,$triggeredOverdues,$smsMessagesSent,$emailMessagesSent,$printMessagesSent) = (0,0,0,0,0);
my %branch_holiday;
my $patrons = Koha::Purresaker->GetPatronsWithOverdues();
......@@ -327,7 +325,6 @@ PATRON: while ( my $patron = $patrons->fetchrow_hashref() ) {
my $template = $templates->{$letter}; # TODO: swap with above when ready
$template or warn "Letter $letter not found";
$template and sendPatronMessage($template, $transport, $patron, $overdues);
$rule->{"letter$i"}->{debar} and debarPatron($patron);
next PATRON;
}
}
......@@ -359,17 +356,6 @@ sub generate_letter_content {
return $processed_content;
}
sub debarPatron {
my $patron = shift;
$test_mode or Koha::Patron::Debarments::AddUniqueDebarment({
borrowernumber => $patron->{borrowernumber},
type => 'OVERDUES',
comment => "OVERDUES_PROCESS " . _today(),
});
$verbose and warn "debarring patron $patron->{borrowernumber}\n";
++$patronsDebarred;
}
sub _today {
return DateTime->now()->strftime('%d.%m.%Y');
}
......@@ -384,6 +370,5 @@ Triggered overdues: $triggeredOverdues
SMS messages sent: $smsMessagesSent
Email messages sent: $emailMessagesSent
Print messages sent: $printMessagesSent
Patrons debarred: $patronsDebarred
EOM
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment