Bug 19532: (follow-up) Fixes along recall workflow
[koha-ffzg.git] / misc / cronjobs / thirdparty / TalkingTech_itiva_inbound.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
29 sub usage {
30     pod2usage( -verbose => 2 );
31     exit;
32 }
33
34 die
35   "TalkingTechItivaPhoneNotification system preference not activated... dying\n"
36   unless ( C4::Context->preference("TalkingTechItivaPhoneNotification") );
37
38 # Database handle
39 my $dbh = C4::Context->dbh;
40
41 # Benchmarking
42 my $updated = 0;
43 my $total   = 0;
44
45 # Options
46 my $verbose;
47 my $help;
48 my $infile;
49
50 GetOptions(
51     'i|input:s' => \$infile,
52     'v'         => \$verbose,
53     'help|h'    => \$help,
54 );
55
56 die pod2usage() if $help;
57
58 # initialize the input data, either file or query
59 if ( defined $infile ) {
60     open( my $IN, '<', $infile ) || die("Cannot open input file");
61     print "Opening $infile\n" if ( defined $verbose );
62
63     while (<$IN>) {
64
65         # data should take to form "<Transaction ID>","<SUCCESS or FAIL>"
66         s/["\n]//g;    # strip quotes and newlines: they're unnecessary
67         my @data   = split(/,/);
68         my $result = update_notice(@data);
69         $updated += $result;
70         $total++;
71     }
72     close($IN);
73 }
74 else {
75     die pod2usage( -verbose => 1 );
76 }
77
78 print "$updated of $total results lines processed\n" if ( defined $verbose );
79
80 =head1 NAME
81
82 TalkingTech_itiva_inbound.pl
83
84 =head1 SYNOPSIS
85
86   TalkingTech_itiva_inbound.pl
87   TalkingTech_itiva_inbound.pl -v --input=/tmp/talkingtech/results.csv
88
89 Script to process received Results files for Talking Tech i-tiva
90 phone notification system.
91
92 =over 8
93
94 =item B<--help> B<-h>
95
96 Prints this help
97
98 =item B<-v>
99
100 Provide verbose log information.
101
102 =item B<--input> B<-i>
103
104 REQUIRED. Path to incoming results file.
105
106 =back
107
108 =cut
109
110 sub update_notice {
111     my $message_id = shift;
112     my $status     = shift;
113
114     if ( $status =~ m/SUCCESS/i ) {
115         $status = 'sent';
116     }
117     elsif ( $status =~ m/FAIL/i ) {
118         $status = 'failed';
119     }
120     else {
121         warn "unexpected status $status for message ID $message_id\n";
122         return 0;
123     }
124
125     my $query =
126 "UPDATE message_queue SET status = ? WHERE message_id = ? and status = 'pending'";
127     my $sth = $dbh->prepare($query);
128
129     my $result = $sth->execute( $status, $message_id );
130     return $result;
131 }