use Getopt::Long;
use Pod::Usage;
+use Koha::Script -cron;
use C4::Context;
use C4::Items;
use C4::Letters;
use Koha::Calendar;
use Koha::DateUtils;
use Koha::Patrons;
+use Koha::Libraries;
sub usage {
pod2usage( -verbose => 2 );
my $help;
my $outfile;
my $skip_patrons_with_email;
+my $patron_branchcode;
# maps to convert I-tiva terms to Koha terms
my $type_module_map = {
};
GetOptions(
- 'o|output:s' => \$outfile,
- 'v' => \$verbose,
- 'lang:s' => \$language,
- 'type:s' => \@types,
- 'w|waiting-hold-day:s' => \@holds_waiting_days_to_call,
- 'c|code|library-code:s' => \$library_code,
+ 'o|output:s' => \$outfile,
+ 'v' => \$verbose,
+ 'lang:s' => \$language,
+ 'type:s' => \@types,
+ 'w|waiting-hold-day:s' => \@holds_waiting_days_to_call,
+ 'c|code|library-code:s' => \$library_code,
's|skip-patrons-with-email' => \$skip_patrons_with_email,
- 'help|h' => \$help,
+ 'pb|patron-branchcode:s' => \$patron_branchcode,
+ 'h|help' => \$help,
);
$language = uc($language);
pod2usage( -verbose => 1 ) if $help;
+if ($patron_branchcode) {
+ die("Invalid branchcode '$patron_branchcode' passed in -pb --patron-branchcode parameter")
+ unless Koha::Libraries->search( { branchcode => $patron_branchcode } )->count;
+}
+
# output log or STDOUT
my $OUT;
if ( defined $outfile ) {
} else {
print "No output file defined; printing to STDOUT\n"
if ( defined $verbose );
- open( $OUT, '>', "&STDOUT" ) || die("Couldn't duplicate STDOUT: $!");
+ $OUT = *STDOUT || die "Couldn't duplicate STDOUT: $!";
}
my $format = 'V'; # format for phone notifications
my @loop;
if ( $type eq 'OVERDUE' ) {
- @loop = GetOverdueIssues();
+ @loop = GetOverdueIssues( $patron_branchcode );
} elsif ( $type eq 'PREOVERDUE' ) {
- @loop = GetPredueIssues();
+ @loop = GetPredueIssues( $patron_branchcode );
} elsif ( $type eq 'RESERVE' ) {
- @loop = GetWaitingHolds();
+ @loop = GetWaitingHolds( $patron_branchcode );
} else {
print "Unknown or unsupported message type $type; skipping...\n"
if ( defined $verbose );
prompts, to those notices.
This field can be blank if all messages are from a single library.
+=item B<--patron-branchcode> B<--pb>
+
+OPTIONAL
+
+Limits the the patrons to generate notices for based on the patron's home library.
+Items and holds from other libraries will still be included for the given patron.
+
=back
=cut
sub GetOverdueIssues {
+ my ( $patron_branchcode ) = @_;
+
+ my $patron_branchcode_filter = $patron_branchcode ? "AND borrowers.branchcode = '$patron_branchcode'" : q{};
+
my $query = "SELECT borrowers.borrowernumber, borrowers.cardnumber, borrowers.title as patron_title, borrowers.firstname, borrowers.surname,
borrowers.phone, borrowers.email, borrowers.branchcode, biblio.biblionumber, biblio.title, items.barcode, issues.date_due,
max(overduerules.branchcode) as rulebranch, TO_DAYS(NOW())-TO_DAYS(date_due) as daysoverdue, delay1, delay2, delay3,
AND ( (TO_DAYS(NOW())-TO_DAYS(date_due) ) = delay1
OR (TO_DAYS(NOW())-TO_DAYS(date_due) ) = delay2
OR (TO_DAYS(NOW())-TO_DAYS(date_due) ) = delay3 )
+ $patron_branchcode_filter
GROUP BY items.itemnumber
";
my $sth = $dbh->prepare($query);
}
sub GetPredueIssues {
+ my ( $patron_branchcode ) = @_;
+
+ my $patron_branchcode_filter = $patron_branchcode ? "AND borrowers.branchcode = '$patron_branchcode'" : q{};
+
my $query = "SELECT borrowers.borrowernumber, borrowers.cardnumber, borrowers.title as patron_title, borrowers.firstname, borrowers.surname,
borrowers.phone, borrowers.email, borrowers.branchcode, biblio.biblionumber, biblio.title, items.barcode, issues.date_due,
issues.branchcode as site, branches.branchname as site_name
WHERE ( TO_DAYS( date_due ) - TO_DAYS( NOW() ) ) = days_in_advance
AND message_transport_type = 'phone'
AND message_name = 'Advance_Notice'
+ $patron_branchcode_filter
";
my $sth = $dbh->prepare($query);
$sth->execute();
}
sub GetWaitingHolds {
- my $query = "SELECT borrowers.borrowernumber, borrowers.cardnumber, borrowers.title as patron_title, borrowers.firstname, borrowers.surname,
+ my ( $patron_branchcode ) = @_;
+
+ my $patron_branchcode_filter = $patron_branchcode ? "AND borrowers.branchcode = '$patron_branchcode'" : q{};
+
+ my $query = "SELECT borrowers.borrowernumber, borrowers.cardnumber, borrowers.title as patron_title, borrowers.firstname, borrowers.surname, borrowers.categorycode,
borrowers.phone, borrowers.email, borrowers.branchcode, biblio.biblionumber, biblio.title, items.barcode, reserves.waitingdate,
reserves.branchcode AS site, branches.branchname AS site_name,
TO_DAYS(NOW())-TO_DAYS(reserves.waitingdate) AS days_since_waiting
WHERE ( reserves.found = 'W' )
AND message_transport_type = 'phone'
AND message_name = 'Hold_Filled'
+ $patron_branchcode_filter
";
my $pickupdelay = C4::Context->preference("ReservesMaxPickUpDelay");
my $sth = $dbh->prepare($query);
$sth->execute();
my @results;
while ( my $issue = $sth->fetchrow_hashref() ) {
- my $calendar = Koha::Calendar->new( branchcode => $issue->{'site'} );
+ my $item = Koha::Items->find({ barcode => $issue->{barcode} });
+ my $useDaysMode_value = Koha::CirculationRules->get_useDaysMode_effective_value(
+ {
+ categorycode => $issue->{categorycode},
+ itemtype => $item->effective_itemtype,
+ branchcode => $issue->{site},
+ }
+ );
+
+ my $calendar = Koha::Calendar->new( branchcode => $issue->{'site'}, days_mode => $useDaysMode_value );
my $waiting_date = dt_from_string( $issue->{waitingdate}, 'sql' );
my $pickup_date = $waiting_date->clone->add( days => $pickupdelay );
if ( $calendar->is_holiday($pickup_date) ) {
- $pickup_date = $calendar->next_open_day( $pickup_date );
+ $pickup_date = $calendar->next_open_days( $pickup_date, 1 );
}
$issue->{'date_due'} = output_pref({dt => $pickup_date, dateformat => 'iso' });
my $days_to_subtract = 0;
if ( $calendar->is_holiday($waiting_date) ) {
- my $next_open_day = $calendar->next_open_day( $waiting_date );
+ my $next_open_day = $calendar->next_open_days( $waiting_date, 1 );
$days_to_subtract = $calendar->days_between($waiting_date, $next_open_day)->days;
}