FFZG: add ISSP RFID sid API
[koha_ffzg] / ffzg / rfid / borrower.pl
1 #!/usr/bin/perl
2
3 # Copyright Dobrica Pavlinusic 2014
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 # perl -I/srv/koha_ffzg/ ./borrower-issi.pl 'RFID_SID=0492188A741590&JMBAG='
21
22 use strict;
23 use warnings;
24
25 use CGI;
26 use C4::Context;
27 use JSON;
28 use LWP::UserAgent qw();
29 use Data::Dump qw(dump); # FIXME
30
31
32 my $cert_password;
33 {
34         open(my $fh, '<', '/home/dpavlin/certifikat_za_pristup_apiv2/lozinka.txt');
35         local $/ = undef;
36         $cert_password = <$fh>;
37         close($fh);
38 }
39
40 my $query = new CGI;
41 print "Content-type: application/json; charset=utf-8\r\n\r\n";
42
43 my $dbh = C4::Context->dbh;
44 my $json;
45
46 my @where;
47 my @execute;
48
49 foreach my $name (qw( OIB JMBAG RFID_SID )) {
50         if ( my $val = $query->param($name) ) {
51                 $json->{param}->{$name} = $val;
52                 push @where, "( code = '$name' and attribute = ? )";
53                 push @execute, $val;
54         }
55 }
56
57 sub generate_sql {
58         my @where = @_;
59 my $sql = qq{
60 select
61         distinct
62         b.borrowernumber, firstname, surname, email, userid, cardnumber
63 from borrower_attributes ba
64 join borrowers b on b.borrowernumber = ba.borrowernumber
65 where (} . join(') or (', @where) . qq{)};
66
67         return $sql;
68 }
69
70 my $sql = generate_sql @where;
71 warn "# sql $sql\n";
72
73 my $sth = $dbh->prepare( $sql );
74 $sth->execute( @execute );
75
76 $json->{rows} = $sth->rows;
77
78 if ( $sth->rows < 1 ) {
79         if ( exists $json->{param}->{RFID_SID} ) {
80                 my $sid = $json->{param}->{RFID_SID};
81                 my $l = length($sid);
82                 if ( $l == 19 or $l == 14 or $l = 8 ) {
83                         warn "ISSP $sid\n";
84
85                         my $ua = LWP::UserAgent->new (
86                         ssl_opts => {
87                           SSL_cert_file => '/home/dpavlin/certifikat_za_pristup_apiv2/certifikat.pfx',
88                           SSL_passwd_cb => sub { $cert_password },
89                         }
90                         );
91                         my $response = $ua->get ("https://isspapi.issp.srce.hr/Kartice/oibjmbag/$sid", Accept => "application/json");
92
93                         if ($response->is_success) {
94                                 my $json_text = $response->decoded_content;
95                                 my $h = decode_json $json_text;
96
97                                 @where = ();
98                                 @execute = ();
99
100                                 foreach my $f ( keys %$h ) {
101                                         my $name = uc($f);
102                                         push @where, "( code = '$name' and attribute = ? )";
103                                         push @execute, $h->{$f};
104                                 }
105                                 my $sql = generate_sql @where;
106                                 warn "ISSP sql: $sql ",dump(@execute);
107                                 my $sth = $dbh->prepare( $sql );
108                                 $sth->execute( @execute );
109                                 if ( $sth->rows == 1 ) {
110                                         $json->{borrower} = $sth->fetchrow_hashref;
111                                         warn "borrower = ",dump( $json->{borrower} );
112                                         my $sth2 = $dbh->prepare(qq{ insert into borrower_attributes (borrowernumber, code, attribute) values (?,?,?) });
113                                         $sth2->execute( $json->{borrower}->{borrowernumber}, 'RFID_SID', $sid);
114                                         warn "ISSP updated $json->{borrower}->{borrowernumber} $sid";
115                                 } else {
116                                         warn "ISSP $sid not found in koha $json_text";
117                                         $json->{error} = "borrower not found";
118                                 }
119                         } else {
120                                 warn "ERROR ISSP ", $response->status_line;
121                                 $json->{error} = "borrower not found";
122                         }
123                 } else {
124                         $json->{error} = "borrower not found";
125                 }
126         } else {
127                 $json->{error} = "borrower not found";
128         }
129 } elsif ( $sth->rows > 1 ) {
130         $json->{error} = "more than one borrower found";
131 } else {
132         $json->{borrower} = $sth->fetchrow_hashref;
133 }
134
135 $json = encode_json( $json );
136 if ( my $callback = $query->param('callback') ) {
137         print $callback, '(', $json, ')';
138 } else {
139         print $json;
140 }