c9708fc3a510a41428130fe40e2c3279118c31ca
[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 GetOptions(
70     'h|help' => \$help,
71     'send-notices' => \$send_notices,
72 ) || pod2usage(1);
73
74 pod2usage(0) if $help;
75
76 cronlogaction();
77
78 my $dbh = C4::Context->dbh();
79 my $sth = $dbh->prepare( <<"END_SQL" );
80 select
81         issues.borrowernumber,
82         reserves.biblionumber,
83         issues.itemnumber
84 from reserves
85 join items on reserves.biblionumber=items.biblionumber
86 join issues on issues.itemnumber=items.itemnumber
87 join borrowers on borrowers.borrowernumber=issues.borrowernumber
88 where (
89                 (
90                         (
91                                         categorycode = 'N1' or
92                                         categorycode = 'POC' or
93                                         categorycode = 'KNJIZ' or
94                                         categorycode = 'Z1' or
95                                         categorycode = 'V1'
96                         ) and
97                                 datediff(reservedate,issuedate) > 30
98                 ) or (
99                         categorycode like 'S%' and datediff(reservedate,issuedate) > 14
100                 )
101 ) and reservedate > now() - interval 1 day
102 order by reserves.biblionumber ;
103 END_SQL
104
105 $sth->execute();
106
107 while ( my $row = $sth->fetchrow_hashref ) {
108         warn "# row = ",dump($row),$/;
109
110         if ( $send_notices ) {
111         my $patron = Koha::Patrons->find($row->{borrowernumber});
112
113         my $letter = C4::Letters::GetPreparedLetter(
114             module      => 'circulation',
115             letter_code => 'FFZG_RECALL',
116             tables      => {
117                 borrowers => $row->{borrowernumber},
118                 issues    => $row->{itemnumber},
119                 items     => $row->{itemnumber},
120                 biblio    => $row->{biblionumber},
121             },
122         );
123
124         my $library = Koha::Libraries->find( $patron->branchcode );
125         my $admin_email_address = $library->branchemail || C4::Context->preference('KohaAdminEmailAddress');
126
127         C4::Letters::EnqueueLetter(
128             {   letter                 => $letter,
129                 borrowernumber         => $row->{borrowernumber},
130                 message_transport_type => 'email',
131                 from_address           => $admin_email_address,
132             }
133         );
134     }
135 }