Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it |
6 |
# under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 3 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
# GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use Getopt::Long qw( GetOptions ); |
21 |
use Pod::Usage qw( pod2usage ); |
22 |
use DateTime; |
23 |
use DateTime::Duration; |
24 |
|
25 |
use C4::Context; |
26 |
use C4::Letters; |
27 |
use C4::Log qw( cronlogaction ); |
28 |
use Koha::DateUtils qw( dt_from_string ); |
29 |
use Koha::Notice::Templates; |
30 |
use Koha::Patrons; |
31 |
use Koha::Bookings; |
32 |
use Koha::Script -cron; |
33 |
|
34 |
=head1 NAME |
35 |
|
36 |
bookings_overdated.pl - prepare messages to be sent to patrons with bookings ended a day before and not been converted into a loan. |
37 |
|
38 |
=head1 SYNOPSIS |
39 |
|
40 |
bookings_overdated.pl |
41 |
-lettercode <notice to send> |
42 |
[ -c ][ -v ] |
43 |
|
44 |
Options: |
45 |
-help brief help message |
46 |
-v verbose |
47 |
-lettercode <lettercode> predefined notice to use, default is BOOKING_OUTDATED |
48 |
|
49 |
=head1 OPTIONS |
50 |
|
51 |
=over 8 |
52 |
|
53 |
=item B<-help> |
54 |
|
55 |
Print a brief help message and exits. |
56 |
|
57 |
=item B<-v> |
58 |
|
59 |
Verbose. Without this flag set, only fatal errors are reported. |
60 |
|
61 |
=item B<-lettercode> |
62 |
|
63 |
Optional parameter, choose a notice to use. Default is 'BOOKING_OUTDATED'. |
64 |
|
65 |
=back |
66 |
|
67 |
=head1 DESCRIPTION |
68 |
|
69 |
This script is designed to alert patrons with bookings |
70 |
ended a day before and not been converted into a loan. |
71 |
|
72 |
=head2 Configuration |
73 |
|
74 |
This script sends alerts to patrons using a notice |
75 |
defined in the Tools > Notices and slips module within Koha. The lettercode |
76 |
is passed into this script and, along with other options, determine the content |
77 |
of the notices sent to patrons. |
78 |
|
79 |
|
80 |
=head1 USAGE EXAMPLES |
81 |
|
82 |
C<bookings_overdated.pl -help> Show help for this script |
83 |
C<bookings_overdated.pl> Launch without verbose |
84 |
C<bookings_overdated.pl -lettercode=FOO -v> Launch with custom notice template code and verbose |
85 |
|
86 |
=cut |
87 |
|
88 |
# These variables are set by command line options. |
89 |
# They are initially set to default values. |
90 |
my $dbh = C4::Context->dbh(); |
91 |
my $help = 0; |
92 |
my $verbose = 0; |
93 |
my $lettercode; |
94 |
|
95 |
my $command_line_options = join( " ", @ARGV ); |
96 |
|
97 |
GetOptions( |
98 |
'help|?' => \$help, |
99 |
'v' => \$verbose, |
100 |
'lettercode=s' => \$lettercode, |
101 |
); |
102 |
pod2usage(1) if $help; |
103 |
|
104 |
$lettercode ||= 'BOOKINGS_OUTDATED'; |
105 |
|
106 |
cronlogaction( { info => $command_line_options } ); |
107 |
|
108 |
my $outdated_date_start = dt_from_string()->clone->subtract( days => '1' ); |
109 |
$outdated_date_start = $outdated_date_start->set( |
110 |
hour => 00, |
111 |
minute => 00, |
112 |
second => 00 |
113 |
); |
114 |
my $outdated_date_end = dt_from_string()->clone->subtract( days => '1' ); |
115 |
$outdated_date_end = $outdated_date_end->set( |
116 |
hour => 23, |
117 |
minute => 59, |
118 |
second => 59 |
119 |
); |
120 |
|
121 |
my $template_exists = Koha::Notice::Templates->find_effective_template( |
122 |
{ |
123 |
module => 'bookings', |
124 |
code => $lettercode, |
125 |
lang => 'default', |
126 |
} |
127 |
); |
128 |
unless ($template_exists) { |
129 |
$verbose and print qq|Message '$lettercode' content not found\n|; |
130 |
next; |
131 |
} |
132 |
|
133 |
my $dtf = Koha::Database->new->schema->storage->datetime_parser; |
134 |
my $booking_outdated_start = $dtf->format_datetime($outdated_date_start); |
135 |
my $booking_outdated_end = $dtf->format_datetime($outdated_date_end); |
136 |
|
137 |
my $bookings = Koha::Bookings->search( |
138 |
{ end_date => [ -and => { '>=' => $booking_outdated_start }, { '<=' => $booking_outdated_end } ] }, |
139 |
{ prefetch => 'patron' } |
140 |
); |
141 |
|
142 |
$verbose and warn $bookings->count . " bookings outdated between $booking_outdated_start and $booking_outdated_end \n"; |
143 |
|
144 |
my %done; |
145 |
|
146 |
my %patrons; |
147 |
while ( my $booking = $bookings->next ) { |
148 |
my $booking = Koha::Bookings->find( $booking->booking_id ); |
149 |
push @{ $patrons{ $booking->patron_id }->{bookings} }, $booking; |
150 |
} |
151 |
|
152 |
foreach my $borrowernumber ( keys %patrons ) { |
153 |
|
154 |
my $patron = Koha::Patrons->find( { borrowernumber => $borrowernumber } ); |
155 |
$verbose |
156 |
and print " borrower " |
157 |
. $patron->surname . ", " |
158 |
. $patron->firstname . " has " |
159 |
. scalar @{ $patrons{$borrowernumber}->{bookings} } |
160 |
. " booking(s) outdated.\n"; |
161 |
|
162 |
my $letter = C4::Letters::GetPreparedLetter( |
163 |
module => 'bookings', |
164 |
letter_code => $lettercode, |
165 |
borrowernumber => $borrowernumber, |
166 |
tables => { |
167 |
borrowers => $borrowernumber, |
168 |
}, |
169 |
objects => { bookings => $patrons{$borrowernumber}->{bookings} }, |
170 |
); |
171 |
|
172 |
if ($letter) { |
173 |
C4::Letters::EnqueueLetter( |
174 |
{ |
175 |
letter => $letter, |
176 |
borrowernumber => $patron->borrowernumber, |
177 |
message_transport_type => 'email', |
178 |
} |
179 |
); |
180 |
} |
181 |
|
182 |
# Mark this borrower as completed |
183 |
$done{ $patron->borrowernumber } = 1; |
184 |
} |
185 |
|
186 |
cronlogaction( { action => 'End', info => "COMPLETED" } ); |