Merge remote-tracking branch 'origin/new/bug_7889'
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 use Date::Calc qw(Add_Delta_Days);
34
35 use C4::Context;
36 use C4::Items;
37 use C4::Letters;
38 use C4::Overdues;
39 use C4::Dates;
40 use C4::Calendar;
41
42 sub usage {
43     pod2usage( -verbose => 2 );
44     exit;
45 }
46
47 die
48   "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
63 # maps to convert I-tiva terms to Koha terms
64 my $type_module_map = {
65     'PREOVERDUE' => 'circulation',
66     'OVERDUE'    => 'circulation',
67     'RESERVE'    => 'reserves',
68 };
69
70 my $type_notice_map = {
71     'PREOVERDUE' => 'PREDUE_PHONE',
72     'OVERDUE'    => 'OVERDUE_PHONE',
73     'RESERVE'    => 'HOLD_PHONE',
74 };
75
76 GetOptions(
77     'o|output:s'            => \$outfile,
78     'v'                     => \$verbose,
79     'lang:s'                => \$language,
80     'type:s'                => \@types,
81     'w|waiting-hold-day:s'  => \@holds_waiting_days_to_call,
82     'c|code|library-code:s' => \$library_code,
83     'help|h'                => \$help,
84 );
85
86 $language = uc($language);
87 $library_code ||= '';
88
89 pod2usage( -verbose => 1 ) if $help;
90
91 # output log or STDOUT
92 my $OUT;
93 if ( defined $outfile ) {
94     open( $OUT, '>', "$outfile" ) || die("Cannot open output file");
95 }
96 else {
97     print "No output file defined; printing to STDOUT\n"
98       if ( defined $verbose );
99     open( $OUT, '>', "&STDOUT" ) || die("Couldn't duplicate STDOUT: $!");
100 }
101
102 my $format = 'V';    # format for phone notifications
103
104 foreach my $type (@types) {
105     $type = uc($type);    #just in case lower or mixed-case was supplied
106     my $module =
107       $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     }
114     elsif ( $type eq 'PREOVERDUE' ) {
115         @loop = GetPredueIssues();
116     }
117     elsif ( $type eq 'RESERVE' ) {
118         @loop = GetWaitingHolds();
119     }
120     else {
121         print "Unknown or unsupported message type $type; skipping...\n"
122           if ( defined $verbose );
123         next;
124     }
125
126     foreach my $issues (@loop) {
127         my $date = C4::Dates->new( $issues->{'date_due'}, 'iso' );
128         my $due_date = $date->output('metric');
129
130         # gets the placeholder message, and enqueues the letter
131         my $letter = getletter( $module, $code );
132         die "No letter found for type $type!... dying\n" unless $letter;
133
134         # covers basic variable parsing in letter
135         $letter =
136           C4::Letters::parseletter( $letter, 'borrowers',
137             $issues->{'borrowernumber'} );
138         $letter =
139           C4::Letters::parseletter( $letter, 'biblio',
140             $issues->{'biblionumber'} );
141         $letter =
142           C4::Letters::parseletter( $letter, 'biblioitems',
143             $issues->{'biblionumber'} );
144
145         my $message_id = 0;
146         if ($outfile) {
147             $message_id = C4::Letters::EnqueueLetter(
148                 {
149                     letter                 => $letter,
150                     borrowernumber         => $issues->{'borrowernumber'},
151                     message_transport_type => 'phone',
152                 }
153             );
154         }
155
156         print $OUT
157 "\"$format\",\"$language\",\"$type\",\"$issues->{level}\",\"$issues->{cardnumber}\",\"$issues->{patron_title}\",\"$issues->{firstname}\",";
158         print $OUT
159 "\"$issues->{surname}\",\"$issues->{phone}\",\"$issues->{email}\",\"$library_code\",";
160         print $OUT
161 "\"$issues->{site}\",\"$issues->{site_name}\",\"$issues->{barcode}\",\"$due_date\",\"$issues->{title}\",\"$message_id\"\n";
162     }
163 }
164
165 =head1 NAME
166
167 TalkingTech_itiva_outbound.pl
168
169 =head1 SYNOPSIS
170
171   TalkingTech_itiva_outbound.pl
172   TalkingTech_itiva_outbound.pl --type=OVERDUE -w 0 -w 2 -w 6 --output=/tmp/talkingtech/outbound.csv
173   TalkingTech_itiva_outbound.pl --type=RESERVE --type=PREOVERDUE --lang=FR
174
175
176 Script to generate Spec C outbound notifications file for Talking Tech i-tiva
177 phone notification system.
178
179 =item B<--help> B<-h>
180
181 Prints this help
182
183 =item B<-v>
184
185 Provide verbose log information.
186
187 =item B<--output> B<-o>
188
189 Destination for outbound notifications file (CSV format).  If no value is specified,
190 output is dumped to screen.
191
192 =item B<--lang>
193
194 Sets the language for all outbound messages.  Currently supported values are EN, FR and ES.
195 If no value is specified, EN will be used by default.
196
197 =item B<--type>
198
199 REQUIRED. Sets which messaging types are to be used.  Can be given multiple times, to
200 specify multiple types in a single output file.  Currently supported values are RESERVE, PREOVERDUE
201 and OVERDUE.  If no value is given, this script will not produce any outbound notifications.
202
203 =item B<--waiting-hold-day> B<-w>
204
205 OPTIONAL for --type=RESERVE. Sets the days after a hold has been set to waiting on which to call. Use
206 switch as many times as desired. For example, passing "-w 0 -w 2 -w 6" will cause calls to be placed
207 on the day the hold was set to waiting, 2 days after the waiting date, and 6 days after. See example above.
208 If this switch is not used with --type=RESERVE, calls will be placed every day until the waiting reserve
209 is picked up or canceled.
210
211 =item B<--library-code> B<--code> B<-c>
212
213 OPTIONAL
214 The code of the source library of the message.
215 The library code is used to group notices together for
216 consortium purposes and apply library specific settings, such as
217 prompts, to those notices.
218 This field can be blank if all messages are from a single library.
219
220 =cut
221
222 sub GetOverdueIssues {
223     my $query =
224 "SELECT borrowers.borrowernumber, borrowers.cardnumber, borrowers.title as patron_title, borrowers.firstname, borrowers.surname,
225                 borrowers.phone, borrowers.email, borrowers.branchcode, biblio.biblionumber, biblio.title, items.barcode, issues.date_due,
226                 max(overduerules.branchcode) as rulebranch, TO_DAYS(NOW())-TO_DAYS(date_due) as daysoverdue, delay1, delay2, delay3,
227                 issues.branchcode as site, branches.branchname as site_name
228                 FROM borrowers JOIN issues USING (borrowernumber)
229                 JOIN items USING (itemnumber)
230                 JOIN biblio USING (biblionumber)
231                 JOIN branches ON (issues.branchcode = branches.branchcode)
232                 JOIN overduerules USING (categorycode)
233                 WHERE ( overduerules.branchcode = borrowers.branchcode or overduerules.branchcode = '')
234                 AND ( (TO_DAYS(NOW())-TO_DAYS(date_due) ) = delay1
235                   OR  (TO_DAYS(NOW())-TO_DAYS(date_due) ) = delay2
236                   OR  (TO_DAYS(NOW())-TO_DAYS(date_due) ) = delay3 )
237                 GROUP BY items.itemnumber
238                 ";
239     my $sth = $dbh->prepare($query);
240     $sth->execute();
241     my @results;
242     while ( my $issue = $sth->fetchrow_hashref() ) {
243         if ( $issue->{'daysoverdue'} == $issue->{'delay1'} ) {
244             $issue->{'level'} = 1;
245         }
246         elsif ( $issue->{'daysoverdue'} == $issue->{'delay2'} ) {
247             $issue->{'level'} = 2;
248         }
249         elsif ( $issue->{'daysoverdue'} == $issue->{'delay3'} ) {
250             $issue->{'level'} = 3;
251         }
252         else {
253
254             # this shouldn't ever happen, based our SQL criteria
255         }
256         push @results, $issue;
257     }
258     return @results;
259 }
260
261 sub GetPredueIssues {
262     my $query =
263 "SELECT borrowers.borrowernumber, borrowers.cardnumber, borrowers.title as patron_title, borrowers.firstname, borrowers.surname,
264                 borrowers.phone, borrowers.email, borrowers.branchcode, biblio.biblionumber, biblio.title, items.barcode, issues.date_due,
265                 issues.branchcode as site, branches.branchname as site_name
266                 FROM borrowers JOIN issues USING (borrowernumber)
267                 JOIN items USING (itemnumber)
268                 JOIN biblio USING (biblionumber)
269                 JOIN branches ON (issues.branchcode = branches.branchcode)
270                 JOIN borrower_message_preferences USING (borrowernumber)
271                 JOIN borrower_message_transport_preferences USING (borrower_message_preference_id)
272                 JOIN message_attributes USING (message_attribute_id)
273                 WHERE ( TO_DAYS( date_due ) - TO_DAYS( NOW() ) ) = days_in_advance
274                 AND message_transport_type = 'phone'
275                 AND message_name = 'Advance_Notice'
276                 ";
277     my $sth = $dbh->prepare($query);
278     $sth->execute();
279     my @results;
280     while ( my $issue = $sth->fetchrow_hashref() ) {
281         $issue->{'level'} = 1;    # only one level for Predue notifications
282         push @results, $issue;
283     }
284     return @results;
285 }
286
287 sub GetWaitingHolds {
288     my $query =
289 "SELECT borrowers.borrowernumber, borrowers.cardnumber, borrowers.title as patron_title, borrowers.firstname, borrowers.surname,
290                 borrowers.phone, borrowers.email, borrowers.branchcode, biblio.biblionumber, biblio.title, items.barcode, reserves.waitingdate,
291                 reserves.branchcode AS site, branches.branchname AS site_name,
292                 TO_DAYS(NOW())-TO_DAYS(reserves.waitingdate) AS days_since_waiting
293                 FROM borrowers JOIN reserves USING (borrowernumber)
294                 JOIN items USING (itemnumber)
295                 JOIN biblio ON (biblio.biblionumber = items.biblionumber)
296                 JOIN branches ON (reserves.branchcode = branches.branchcode)
297                 JOIN borrower_message_preferences USING (borrowernumber)
298                 JOIN borrower_message_transport_preferences USING (borrower_message_preference_id)
299                 JOIN message_attributes USING (message_attribute_id)
300                 WHERE ( reserves.found = 'W' )
301                 AND message_transport_type = 'phone'
302                 AND message_name = 'Hold_Filled'
303                 ";
304     my $pickupdelay = C4::Context->preference("ReservesMaxPickUpDelay");
305     my $sth         = $dbh->prepare($query);
306     $sth->execute();
307     my @results;
308     while ( my $issue = $sth->fetchrow_hashref() ) {
309         my @waitingdate = split( /-/, $issue->{'waitingdate'} );
310         my @date_due =
311           Add_Delta_Days( $waitingdate[0], $waitingdate[1], $waitingdate[2],
312             $pickupdelay );
313         $issue->{'date_due'} =
314           sprintf( "%04d-%02d-%02d", $date_due[0], $date_due[1], $date_due[2] );
315         $issue->{'level'} = 1;   # only one level for Hold Waiting notifications
316
317         my $days_to_subtract = 0;
318         my $calendar = C4::Calendar->new( branchcode => $issue->{'site'} );
319         while (
320             $calendar->isHoliday(
321                 reverse(
322                     Add_Delta_Days(
323                         $waitingdate[0], $waitingdate[1],
324                         $waitingdate[2], $days_to_subtract
325                     )
326                 )
327             )
328           )
329         {
330             $days_to_subtract++;
331         }
332         $issue->{'days_since_waiting'} =
333           $issue->{'days_since_waiting'} - $days_to_subtract;
334
335         if (
336             (
337                 grep $_ eq $issue->{'days_since_waiting'},
338                 @holds_waiting_days_to_call
339             )
340             || !scalar(@holds_waiting_days_to_call)
341           )
342         {
343             push @results, $issue;
344         }
345     }
346     return @results;
347
348 }