Bug 28617: Remove kohalib.pl and rely on PERL5LIB
[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 use Getopt::Long qw( GetOptions );
24 use Pod::Usage qw( pod2usage );
25
26 use Koha::Script -cron;
27 use C4::Context;
28 use C4::Letters;
29 use C4::Overdues;
30 use Koha::Calendar;
31 use Koha::DateUtils qw( dt_from_string output_pref );
32 use Koha::Patrons;
33 use Koha::Libraries;
34
35 sub usage {
36     pod2usage( -verbose => 2 );
37     exit;
38 }
39
40 die "TalkingTechItivaPhoneNotification system preference not activated... dying\n"
41   unless ( C4::Context->preference("TalkingTechItivaPhoneNotification") );
42
43 # Database handle
44 my $dbh = C4::Context->dbh;
45
46 # Options
47 my $verbose;
48 my $language = "EN";
49 my @types;
50 my @holds_waiting_days_to_call;
51 my $library_code;
52 my $help;
53 my $outfile;
54 my $skip_patrons_with_email;
55 my $patron_branchcode;
56
57 # maps to convert I-tiva terms to Koha terms
58 my $type_module_map = {
59     'PREOVERDUE' => 'circulation',
60     'OVERDUE'    => 'circulation',
61     'RESERVE'    => 'reserves',
62 };
63
64 my $type_notice_map = {
65     'PREOVERDUE' => 'PREDUE',
66     'OVERDUE'    => 'OVERDUE',
67     'RESERVE'    => 'HOLD',
68 };
69
70 GetOptions(
71     'o|output:s'             => \$outfile,
72     'v'                      => \$verbose,
73     'lang:s'                 => \$language,
74     'type:s'                 => \@types,
75     'w|waiting-hold-day:s'   => \@holds_waiting_days_to_call,
76     'c|code|library-code:s'  => \$library_code,
77     's|skip-patrons-with-email' => \$skip_patrons_with_email,
78     'pb|patron-branchcode:s' => \$patron_branchcode,
79     'h|help'                 => \$help,
80 );
81
82 $language = uc($language);
83 $library_code ||= '';
84
85 pod2usage( -verbose => 1 ) if $help;
86
87 if ($patron_branchcode) {
88     die("Invalid branchcode '$patron_branchcode' passed in -pb --patron-branchcode parameter")
89       unless Koha::Libraries->search( { branchcode => $patron_branchcode } )->count;
90 }
91
92 # output log or STDOUT
93 my $OUT;
94 if ( defined $outfile ) {
95     open( $OUT, '>', "$outfile" ) || die("Cannot open output file");
96 } else {
97     print "No output file defined; printing to STDOUT\n"
98       if ( defined $verbose );
99     $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 = $type_module_map->{$type};    #since the module is required to get the letter
107     my $code   = $type_notice_map->{$type};    #to get the Koha name of the notice
108
109     my @loop;
110     if ( $type eq 'OVERDUE' ) {
111         @loop = GetOverdueIssues( $patron_branchcode );
112     } elsif ( $type eq 'PREOVERDUE' ) {
113         @loop = GetPredueIssues( $patron_branchcode );
114     } elsif ( $type eq 'RESERVE' ) {
115         @loop = GetWaitingHolds( $patron_branchcode );
116     } else {
117         print "Unknown or unsupported message type $type; skipping...\n"
118           if ( defined $verbose );
119         next;
120     }
121
122     my $patrons;
123     foreach my $issues (@loop) {
124         $patrons->{$issues->{borrowernumber}} ||= Koha::Patrons->find( $issues->{borrowernumber} ) if $skip_patrons_with_email;
125         next if $skip_patrons_with_email && $patrons->{$issues->{borrowernumber}}->notice_email_address;
126
127         my $date_dt = dt_from_string ( $issues->{'date_due'} );
128         my $due_date = output_pref( { dt => $date_dt, dateonly => 1, dateformat =>'metric' } );
129
130         my $letter = C4::Letters::GetPreparedLetter(
131             module      => $module,
132             letter_code => $code,
133             lang        => 'default', # It does not sound useful to send a lang here
134             tables      => {
135                 borrowers   => $issues->{'borrowernumber'},
136                 biblio      => $issues->{'biblionumber'},
137                 biblioitems => $issues->{'biblionumber'},
138             },
139             message_transport_type => 'itiva',
140         );
141
142         die "No letter found for type $type!... dying\n" unless $letter;
143
144         my $message_id = 0;
145         if ($outfile) {
146             $message_id = C4::Letters::EnqueueLetter(
147                 {   letter                 => $letter,
148                     borrowernumber         => $issues->{'borrowernumber'},
149                     message_transport_type => 'itiva',
150                 }
151             );
152         }
153
154         $issues->{title} =~ s/'//g;
155         $issues->{title} =~ s/"//g;
156
157         print $OUT "\"$format\",\"$language\",\"$type\",\"$issues->{level}\",\"$issues->{cardnumber}\",\"$issues->{patron_title}\",\"$issues->{firstname}\",";
158         print $OUT "\"$issues->{surname}\",\"$issues->{phone}\",\"$issues->{email}\",\"$library_code\",";
159         print $OUT "\"$issues->{site}\",\"$issues->{site_name}\",\"$issues->{barcode}\",\"$due_date\",\"$issues->{title}\",\"$message_id\"\n";
160     }
161 }
162
163 =head1 NAME
164
165 TalkingTech_itiva_outbound.pl
166
167 =head1 SYNOPSIS
168
169   TalkingTech_itiva_outbound.pl
170   TalkingTech_itiva_outbound.pl --type=OVERDUE -w 0 -w 2 -w 6 --output=/tmp/talkingtech/outbound.csv
171   TalkingTech_itiva_outbound.pl --type=RESERVE --type=PREOVERDUE --lang=FR
172
173
174 Script to generate Spec C outbound notifications file for Talking Tech i-tiva
175 phone notification system.
176
177 =over
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 =item B<--patron-branchcode> B<--pb>
221
222 OPTIONAL
223
224 Limits the the patrons to generate notices for based on the patron's home library.
225 Items and holds from other libraries will still be included for the given patron.
226
227 =back
228
229 =cut
230
231 sub GetOverdueIssues {
232     my ( $patron_branchcode ) = @_;
233
234     my $patron_branchcode_filter = $patron_branchcode ? "AND borrowers.branchcode = '$patron_branchcode'" : q{};
235
236     my $query = "SELECT borrowers.borrowernumber, borrowers.cardnumber, borrowers.title as patron_title, borrowers.firstname, borrowers.surname,
237                 borrowers.phone, borrowers.email, borrowers.branchcode, biblio.biblionumber, biblio.title, items.barcode, issues.date_due,
238                 max(overduerules.branchcode) as rulebranch, TO_DAYS(NOW())-TO_DAYS(date_due) as daysoverdue, delay1, delay2, delay3,
239                 issues.branchcode as site, branches.branchname as site_name
240                 FROM borrowers JOIN issues USING (borrowernumber)
241                 JOIN items USING (itemnumber)
242                 JOIN biblio USING (biblionumber)
243                 JOIN branches ON (issues.branchcode = branches.branchcode)
244                 JOIN overduerules USING (categorycode)
245                 JOIN overduerules_transport_types USING ( overduerules_id )
246                 WHERE ( overduerules.branchcode = borrowers.branchcode or overduerules.branchcode = '')
247                 AND overduerules_transport_types.message_transport_type = 'itiva'
248                 AND ( (TO_DAYS(NOW())-TO_DAYS(date_due) ) = delay1
249                   OR  (TO_DAYS(NOW())-TO_DAYS(date_due) ) = delay2
250                   OR  (TO_DAYS(NOW())-TO_DAYS(date_due) ) = delay3 )
251                 $patron_branchcode_filter
252                 GROUP BY items.itemnumber
253                 ";
254     my $sth = $dbh->prepare($query);
255     $sth->execute();
256     my @results;
257     while ( my $issue = $sth->fetchrow_hashref() ) {
258         if ( $issue->{'daysoverdue'} == $issue->{'delay1'} ) {
259             $issue->{'level'} = 1;
260         } elsif ( $issue->{'daysoverdue'} == $issue->{'delay2'} ) {
261             $issue->{'level'} = 2;
262         } elsif ( $issue->{'daysoverdue'} == $issue->{'delay3'} ) {
263             $issue->{'level'} = 3;
264         } else {
265
266             # this shouldn't ever happen, based our SQL criteria
267         }
268         push @results, $issue;
269     }
270     return @results;
271 }
272
273 sub GetPredueIssues {
274     my ( $patron_branchcode ) = @_;
275
276     my $patron_branchcode_filter = $patron_branchcode ? "AND borrowers.branchcode = '$patron_branchcode'" : q{};
277
278     my $query = "SELECT borrowers.borrowernumber, borrowers.cardnumber, borrowers.title as patron_title, borrowers.firstname, borrowers.surname,
279                 borrowers.phone, borrowers.email, borrowers.branchcode, biblio.biblionumber, biblio.title, items.barcode, issues.date_due,
280                 issues.branchcode as site, branches.branchname as site_name
281                 FROM borrowers JOIN issues USING (borrowernumber)
282                 JOIN items USING (itemnumber)
283                 JOIN biblio USING (biblionumber)
284                 JOIN branches ON (issues.branchcode = branches.branchcode)
285                 JOIN borrower_message_preferences USING (borrowernumber)
286                 JOIN borrower_message_transport_preferences USING (borrower_message_preference_id)
287                 JOIN message_attributes USING (message_attribute_id)
288                 WHERE ( TO_DAYS( date_due ) - TO_DAYS( NOW() ) ) = days_in_advance
289                 AND message_transport_type = 'itiva'
290                 AND message_name = 'Advance_Notice'
291                 $patron_branchcode_filter
292                 ";
293     my $sth = $dbh->prepare($query);
294     $sth->execute();
295     my @results;
296     while ( my $issue = $sth->fetchrow_hashref() ) {
297         $issue->{'level'} = 1;    # only one level for Predue notifications
298         push @results, $issue;
299     }
300     return @results;
301 }
302
303 sub GetWaitingHolds {
304     my ( $patron_branchcode ) = @_;
305
306     my $patron_branchcode_filter = $patron_branchcode ? "AND borrowers.branchcode = '$patron_branchcode'" : q{};
307
308     my $query = "SELECT borrowers.borrowernumber, borrowers.cardnumber, borrowers.title as patron_title, borrowers.firstname, borrowers.surname, borrowers.categorycode,
309                 borrowers.phone, borrowers.email, borrowers.branchcode, biblio.biblionumber, biblio.title, items.barcode, reserves.waitingdate,
310                 reserves.branchcode AS site, branches.branchname AS site_name,
311                 TO_DAYS(NOW())-TO_DAYS(reserves.waitingdate) AS days_since_waiting
312                 FROM borrowers JOIN reserves USING (borrowernumber)
313                 JOIN items USING (itemnumber)
314                 JOIN biblio ON (biblio.biblionumber = items.biblionumber)
315                 JOIN branches ON (reserves.branchcode = branches.branchcode)
316                 JOIN borrower_message_preferences USING (borrowernumber)
317                 JOIN borrower_message_transport_preferences USING (borrower_message_preference_id)
318                 JOIN message_attributes USING (message_attribute_id)
319                 WHERE ( reserves.found = 'W' )
320                 AND message_transport_type = 'itiva'
321                 AND message_name = 'Hold_Filled'
322                 $patron_branchcode_filter
323                 ";
324     my $pickupdelay = C4::Context->preference("ReservesMaxPickUpDelay");
325     my $sth         = $dbh->prepare($query);
326     $sth->execute();
327     my @results;
328     while ( my $issue = $sth->fetchrow_hashref() ) {
329         my $item = Koha::Items->find({ barcode => $issue->{barcode} });
330         my $daysmode = Koha::CirculationRules->get_effective_daysmode(
331             {
332                 categorycode => $issue->{categorycode},
333                 itemtype     => $item->effective_itemtype,
334                 branchcode   => $issue->{site},
335             }
336         );
337
338         my $calendar = Koha::Calendar->new( branchcode => $issue->{'site'}, days_mode => $daysmode );
339
340         my $waiting_date = dt_from_string( $issue->{waitingdate}, 'sql' );
341         my $pickup_date = $waiting_date->clone->add( days => $pickupdelay );
342         if ( $calendar->is_holiday($pickup_date) ) {
343             $pickup_date = $calendar->next_open_days( $pickup_date, 1 );
344         }
345
346         $issue->{'date_due'} = output_pref({dt => $pickup_date, dateformat => 'iso' });
347         $issue->{'level'} = 1;    # only one level for Hold Waiting notifications
348
349         my $days_to_subtract = 0;
350         if ( $calendar->is_holiday($waiting_date) ) {
351             my $next_open_day = $calendar->next_open_days( $waiting_date, 1 );
352             $days_to_subtract = $calendar->days_between($waiting_date, $next_open_day)->days;
353         }
354
355         $issue->{'days_since_waiting'} = $issue->{'days_since_waiting'} - $days_to_subtract;
356
357         if ( ( grep $_ eq $issue->{'days_since_waiting'}, @holds_waiting_days_to_call )
358             || !scalar(@holds_waiting_days_to_call) ) {
359             push @results, $issue;
360         }
361     }
362     return @results;
363
364 }