ffzg/recall_notices.pl: added --interval and --dedup
[koha.git] / ffzg / recall_notices.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright (C) 2018 Dobrica Pavlinusic <dpavlin@rot13.org>
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 =head1 NAME
21
22 recall_notices.pl - cron script to generate and send recall notices
23
24 =head1 SYNOPSIS
25
26 ./recall_notices.pl [--send-notices]
27
28 or, in crontab:
29 0 3 * * * recall_notices.pl
30
31 =head1 DESCRIPTION
32
33 This script searches for reserves issued in last day.
34
35 It is B<NOT> related to Koha's recall feature which is under development
36 L<https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=19532>
37
38 =head1 OPTIONS
39
40 =over
41
42 =item B<--send-notices>
43
44 Send FFZG_RECALL notices to patrons.
45
46 Note that this option does not support digest yet.
47
48 =back
49
50 =cut
51
52 use Modern::Perl;
53 use Pod::Usage;
54 use Getopt::Long;
55
56 use lib '/srv/koha_ffzg';
57
58 use C4::Circulation;
59 use C4::Context;
60 use C4::Log;
61 use C4::Letters;
62 use Koha::Checkouts;
63 use Koha::Libraries;
64 use Koha::Patrons;
65
66 use Data::Dump qw(dump);
67
68 my ( $help, $send_notices );
69 my $interval = 'now() - interval 1 day';
70 my $dedup = 1;
71 GetOptions(
72     'h|help' => \$help,
73     'send-notices' => \$send_notices,
74     'interval=s' => \$interval,
75     'dedup!' => \$dedup,
76 ) || pod2usage(1);
77
78 pod2usage(0) if $help;
79
80 cronlogaction();
81
82
83 # XXX quote iso date correctly for insertion into SQL
84 $interval = qq{'$interval'} if $interval =~ m/^\d\d\d\d-\d+-\d+$/;
85
86 my $sql = <<"END_SQL";
87 select
88         issues.borrowernumber,
89         reserves.biblionumber,
90         issues.itemnumber,
91         reservedate,
92         issuedate
93 from reserves
94 join items on reserves.biblionumber=items.biblionumber
95 join issues on issues.itemnumber=items.itemnumber
96 join borrowers on borrowers.borrowernumber=issues.borrowernumber
97 where (
98                 (
99                         (
100                                         categorycode = 'N1' or
101                                         categorycode = 'POC' or
102                                         categorycode = 'KNJIZ' or
103                                         categorycode = 'Z1' or
104                                         categorycode = 'V1'
105                         ) and
106                                 datediff(reservedate,issuedate) > 30
107                 ) or (
108                         categorycode like 'S%' and datediff(reservedate,issuedate) > 14
109                 )
110 ) and reservedate > $interval
111
112 END_SQL
113
114 $sql .= qq{
115
116 and borrowers.borrowernumber not in (
117         select message_queue.borrowernumber
118         from message_queue
119         where letter_code = 'FFZG_RECALL' and time_queued >= $interval
120 )
121
122 } if $dedup;
123
124 $sql .= qq{
125 order by reservedate;
126 };
127
128 warn "## << SQL\n$sql\n## >> SQL\n";
129
130 my $dbh = C4::Context->dbh();
131 my $sth = $dbh->prepare( $sql );
132 $sth->execute();
133 print "$0 dedup:$dedup interval [$interval] got ",$sth->rows,$/;
134
135 while ( my $row = $sth->fetchrow_hashref ) {
136         print "# recall_notice = ",dump($row),$/;
137
138    if ( $send_notices ) {
139         my $patron = Koha::Patrons->find($row->{borrowernumber});
140
141         my $letter = C4::Letters::GetPreparedLetter(
142             module      => 'circulation',
143             letter_code => 'FFZG_RECALL',
144             tables      => {
145                 borrowers => $row->{borrowernumber},
146                 issues    => $row->{itemnumber},
147                 items     => $row->{itemnumber},
148                 biblio    => $row->{biblionumber},
149             },
150         );
151
152         my $library = Koha::Libraries->find( $patron->branchcode );
153         my $admin_email_address = $library->branchemail || C4::Context->preference('KohaAdminEmailAddress');
154
155         C4::Letters::EnqueueLetter(
156             {   letter                 => $letter,
157                 borrowernumber         => $row->{borrowernumber},
158                 message_transport_type => 'email',
159                 from_address           => $admin_email_address,
160             }
161         );
162     }
163 }