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