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