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