Bug 22600: Add 'cron' to interface types and set appropriately
[koha.git] / misc / cronjobs / thirdparty / TalkingTech_itiva_outbound.pl
1 #!/usr/bin/perl
2 #
3 # Copyright (C) 2011 ByWater Solutions
4 #
5 # This file is part of Koha.
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 use strict;
21 use warnings;
22
23 BEGIN {
24
25     # find Koha's Perl modules
26     # test carefully before changing this
27     use FindBin;
28     eval { require "$FindBin::Bin/../kohalib.pl" };
29 }
30
31 use Getopt::Long;
32 use Pod::Usage;
33
34 use Koha::Cron;
35 use C4::Context;
36 use C4::Items;
37 use C4::Letters;
38 use C4::Overdues;
39 use Koha::Calendar;
40 use Koha::DateUtils;
41 use Koha::Patrons;
42
43 sub usage {
44     pod2usage( -verbose => 2 );
45     exit;
46 }
47
48 die "TalkingTechItivaPhoneNotification system preference not activated... dying\n"
49   unless ( C4::Context->preference("TalkingTechItivaPhoneNotification") );
50
51 # Database handle
52 my $dbh = C4::Context->dbh;
53
54 # Options
55 my $verbose;
56 my $language = "EN";
57 my @types;
58 my @holds_waiting_days_to_call;
59 my $library_code;
60 my $help;
61 my $outfile;
62 my $skip_patrons_with_email;
63
64 # maps to convert I-tiva terms to Koha terms
65 my $type_module_map = {
66     'PREOVERDUE' => 'circulation',
67     'OVERDUE'    => 'circulation',
68     'RESERVE'    => 'reserves',
69 };
70
71 my $type_notice_map = {
72     'PREOVERDUE' => 'PREDUE',
73     'OVERDUE'    => 'OVERDUE',
74     'RESERVE'    => 'HOLD',
75 };
76
77 GetOptions(
78     'o|output:s'            => \$outfile,
79     'v'                     => \$verbose,
80     'lang:s'                => \$language,
81     'type:s'                => \@types,
82     'w|waiting-hold-day:s'  => \@holds_waiting_days_to_call,
83     'c|code|library-code:s' => \$library_code,
84     's|skip-patrons-with-email' => \$skip_patrons_with_email,
85     'help|h'                => \$help,
86 );
87
88 $language = uc($language);
89 $library_code ||= '';
90
91 pod2usage( -verbose => 1 ) if $help;
92
93 # output log or STDOUT
94 my $OUT;
95 if ( defined $outfile ) {
96     open( $OUT, '>', "$outfile" ) || die("Cannot open output file");
97 } else {
98     print "No output file defined; printing to STDOUT\n"
99       if ( defined $verbose );
100     $OUT = *STDOUT || die "Couldn't duplicate STDOUT: $!";
101 }
102
103 my $format = 'V';    # format for phone notifications
104
105 foreach my $type (@types) {
106     $type = uc($type);    #just in case lower or mixed-case was supplied
107     my $module = $type_module_map->{$type};    #since the module is required to get the letter
108     my $code   = $type_notice_map->{$type};    #to get the Koha name of the notice
109
110     my @loop;
111     if ( $type eq 'OVERDUE' ) {
112         @loop = GetOverdueIssues();
113     } elsif ( $type eq 'PREOVERDUE' ) {
114         @loop = GetPredueIssues();
115     } elsif ( $type eq 'RESERVE' ) {
116         @loop = GetWaitingHolds();
117     } else {
118         print "Unknown or unsupported message type $type; skipping...\n"
119           if ( defined $verbose );
120         next;
121     }
122
123     my $patrons;
124     foreach my $issues (@loop) {
125         $patrons->{$issues->{borrowernumber}} ||= Koha::Patrons->find( $issues->{borrowernumber} ) if $skip_patrons_with_email;
126         next if $skip_patrons_with_email && $patrons->{$issues->{borrowernumber}}->notice_email_address;
127
128         my $date_dt = dt_from_string ( $issues->{'date_due'} );
129         my $due_date = output_pref( { dt => $date_dt, dateonly => 1, dateformat =>'metric' } );
130
131         my $letter = C4::Letters::GetPreparedLetter(
132             module      => $module,
133             letter_code => $code,
134             lang        => 'default', # It does not sound useful to send a lang here
135             tables      => {
136                 borrowers   => $issues->{'borrowernumber'},
137                 biblio      => $issues->{'biblionumber'},
138                 biblioitems => $issues->{'biblionumber'},
139             },
140             message_transport_type => 'phone',
141         );
142
143         die "No letter found for type $type!... dying\n" unless $letter;
144
145         my $message_id = 0;
146         if ($outfile) {
147             $message_id = C4::Letters::EnqueueLetter(
148                 {   letter                 => $letter,
149                     borrowernumber         => $issues->{'borrowernumber'},
150                     message_transport_type => 'phone',
151                 }
152             );
153         }
154
155         print $OUT "\"$format\",\"$language\",\"$type\",\"$issues->{level}\",\"$issues->{cardnumber}\",\"$issues->{patron_title}\",\"$issues->{firstname}\",";
156         print $OUT "\"$issues->{surname}\",\"$issues->{phone}\",\"$issues->{email}\",\"$library_code\",";
157         print $OUT "\"$issues->{site}\",\"$issues->{site_name}\",\"$issues->{barcode}\",\"$due_date\",\"$issues->{title}\",\"$message_id\"\n";
158     }
159 }
160
161 =head1 NAME
162
163 TalkingTech_itiva_outbound.pl
164
165 =head1 SYNOPSIS
166
167   TalkingTech_itiva_outbound.pl
168   TalkingTech_itiva_outbound.pl --type=OVERDUE -w 0 -w 2 -w 6 --output=/tmp/talkingtech/outbound.csv
169   TalkingTech_itiva_outbound.pl --type=RESERVE --type=PREOVERDUE --lang=FR
170
171
172 Script to generate Spec C outbound notifications file for Talking Tech i-tiva
173 phone notification system.
174
175 =over
176
177 =item B<--help> B<-h>
178
179 Prints this help
180
181 =item B<-v>
182
183 Provide verbose log information.
184
185 =item B<--output> B<-o>
186
187 Destination for outbound notifications file (CSV format).  If no value is specified,
188 output is dumped to screen.
189
190 =item B<--lang>
191
192 Sets the language for all outbound messages.  Currently supported values are EN, FR and ES.
193 If no value is specified, EN will be used by default.
194
195 =item B<--type>
196
197 REQUIRED. Sets which messaging types are to be used.  Can be given multiple times, to
198 specify multiple types in a single output file.  Currently supported values are RESERVE, PREOVERDUE
199 and OVERDUE.  If no value is given, this script will not produce any outbound notifications.
200
201 =item B<--waiting-hold-day> B<-w>
202
203 OPTIONAL for --type=RESERVE. Sets the days after a hold has been set to waiting on which to call. Use
204 switch as many times as desired. For example, passing "-w 0 -w 2 -w 6" will cause calls to be placed
205 on the day the hold was set to waiting, 2 days after the waiting date, and 6 days after. See example above.
206 If this switch is not used with --type=RESERVE, calls will be placed every day until the waiting reserve
207 is picked up or canceled.
208
209 =item B<--library-code> B<--code> B<-c>
210
211 OPTIONAL
212 The code of the source library of the message.
213 The library code is used to group notices together for
214 consortium purposes and apply library specific settings, such as
215 prompts, to those notices.
216 This field can be blank if all messages are from a single library.
217
218 =back
219
220 =cut
221
222 sub GetOverdueIssues {
223     my $query = "SELECT borrowers.borrowernumber, borrowers.cardnumber, borrowers.title as patron_title, borrowers.firstname, borrowers.surname,
224                 borrowers.phone, borrowers.email, borrowers.branchcode, biblio.biblionumber, biblio.title, items.barcode, issues.date_due,
225                 max(overduerules.branchcode) as rulebranch, TO_DAYS(NOW())-TO_DAYS(date_due) as daysoverdue, delay1, delay2, delay3,
226                 issues.branchcode as site, branches.branchname as site_name
227                 FROM borrowers JOIN issues USING (borrowernumber)
228                 JOIN items USING (itemnumber)
229                 JOIN biblio USING (biblionumber)
230                 JOIN branches ON (issues.branchcode = branches.branchcode)
231                 JOIN overduerules USING (categorycode)
232                 JOIN overduerules_transport_types USING ( overduerules_id )
233                 WHERE ( overduerules.branchcode = borrowers.branchcode or overduerules.branchcode = '')
234                 AND overduerules_transport_types.message_transport_type = 'phone'
235                 AND ( (TO_DAYS(NOW())-TO_DAYS(date_due) ) = delay1
236                   OR  (TO_DAYS(NOW())-TO_DAYS(date_due) ) = delay2
237                   OR  (TO_DAYS(NOW())-TO_DAYS(date_due) ) = delay3 )
238                 GROUP BY items.itemnumber
239                 ";
240     my $sth = $dbh->prepare($query);
241     $sth->execute();
242     my @results;
243     while ( my $issue = $sth->fetchrow_hashref() ) {
244         if ( $issue->{'daysoverdue'} == $issue->{'delay1'} ) {
245             $issue->{'level'} = 1;
246         } elsif ( $issue->{'daysoverdue'} == $issue->{'delay2'} ) {
247             $issue->{'level'} = 2;
248         } elsif ( $issue->{'daysoverdue'} == $issue->{'delay3'} ) {
249             $issue->{'level'} = 3;
250         } else {
251
252             # this shouldn't ever happen, based our SQL criteria
253         }
254         push @results, $issue;
255     }
256     return @results;
257 }
258
259 sub GetPredueIssues {
260     my $query = "SELECT borrowers.borrowernumber, borrowers.cardnumber, borrowers.title as patron_title, borrowers.firstname, borrowers.surname,
261                 borrowers.phone, borrowers.email, borrowers.branchcode, biblio.biblionumber, biblio.title, items.barcode, issues.date_due,
262                 issues.branchcode as site, branches.branchname as site_name
263                 FROM borrowers JOIN issues USING (borrowernumber)
264                 JOIN items USING (itemnumber)
265                 JOIN biblio USING (biblionumber)
266                 JOIN branches ON (issues.branchcode = branches.branchcode)
267                 JOIN borrower_message_preferences USING (borrowernumber)
268                 JOIN borrower_message_transport_preferences USING (borrower_message_preference_id)
269                 JOIN message_attributes USING (message_attribute_id)
270                 WHERE ( TO_DAYS( date_due ) - TO_DAYS( NOW() ) ) = days_in_advance
271                 AND message_transport_type = 'phone'
272                 AND message_name = 'Advance_Notice'
273                 ";
274     my $sth = $dbh->prepare($query);
275     $sth->execute();
276     my @results;
277     while ( my $issue = $sth->fetchrow_hashref() ) {
278         $issue->{'level'} = 1;    # only one level for Predue notifications
279         push @results, $issue;
280     }
281     return @results;
282 }
283
284 sub GetWaitingHolds {
285     my $query = "SELECT borrowers.borrowernumber, borrowers.cardnumber, borrowers.title as patron_title, borrowers.firstname, borrowers.surname,
286                 borrowers.phone, borrowers.email, borrowers.branchcode, biblio.biblionumber, biblio.title, items.barcode, reserves.waitingdate,
287                 reserves.branchcode AS site, branches.branchname AS site_name,
288                 TO_DAYS(NOW())-TO_DAYS(reserves.waitingdate) AS days_since_waiting
289                 FROM borrowers JOIN reserves USING (borrowernumber)
290                 JOIN items USING (itemnumber)
291                 JOIN biblio ON (biblio.biblionumber = items.biblionumber)
292                 JOIN branches ON (reserves.branchcode = branches.branchcode)
293                 JOIN borrower_message_preferences USING (borrowernumber)
294                 JOIN borrower_message_transport_preferences USING (borrower_message_preference_id)
295                 JOIN message_attributes USING (message_attribute_id)
296                 WHERE ( reserves.found = 'W' )
297                 AND message_transport_type = 'phone'
298                 AND message_name = 'Hold_Filled'
299                 ";
300     my $pickupdelay = C4::Context->preference("ReservesMaxPickUpDelay");
301     my $sth         = $dbh->prepare($query);
302     $sth->execute();
303     my @results;
304     while ( my $issue = $sth->fetchrow_hashref() ) {
305         my $calendar = Koha::Calendar->new( branchcode => $issue->{'site'} );
306
307         my $waiting_date = dt_from_string( $issue->{waitingdate}, 'sql' );
308         my $pickup_date = $waiting_date->clone->add( days => $pickupdelay );
309         if ( $calendar->is_holiday($pickup_date) ) {
310             $pickup_date = $calendar->next_open_day( $pickup_date );
311         }
312
313         $issue->{'date_due'} = output_pref({dt => $pickup_date, dateformat => 'iso' });
314         $issue->{'level'} = 1;    # only one level for Hold Waiting notifications
315
316         my $days_to_subtract = 0;
317         if ( $calendar->is_holiday($waiting_date) ) {
318             my $next_open_day = $calendar->next_open_day( $waiting_date );
319             $days_to_subtract = $calendar->days_between($waiting_date, $next_open_day)->days;
320         }
321
322         $issue->{'days_since_waiting'} = $issue->{'days_since_waiting'} - $days_to_subtract;
323
324         if ( ( grep $_ eq $issue->{'days_since_waiting'}, @holds_waiting_days_to_call )
325             || !scalar(@holds_waiting_days_to_call) ) {
326             push @results, $issue;
327         }
328     }
329     return @results;
330
331 }