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