3 # Copyright Dobrica Pavlinusic 2014
5 # This file is part of Koha.
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
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.
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.
20 # perl -I/srv/koha_ffzg/ ./borrower-issi.pl 'RFID_SID=0492188A741590&JMBAG='
28 use LWP::UserAgent qw();
29 use Data::Dump qw(dump); # FIXME
34 open(my $fh, '<', '/home/dpavlin/certifikat_za_pristup_apiv2/lozinka.txt');
36 $cert_password = <$fh>;
41 print "Content-type: application/json; charset=utf-8\r\n\r\n";
43 my $dbh = C4::Context->dbh;
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 = ? )";
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{)};
70 my $sql = generate_sql @where;
73 my $sth = $dbh->prepare( $sql );
74 $sth->execute( @execute );
76 $json->{rows} = $sth->rows;
78 if ( $sth->rows < 1 ) {
79 if ( exists $json->{param}->{RFID_SID} ) {
80 my $sid = $json->{param}->{RFID_SID};
82 if ( $l == 19 or $l == 14 or $l = 8 ) {
85 my $ua = LWP::UserAgent->new (
87 SSL_cert_file => '/home/dpavlin/certifikat_za_pristup_apiv2/certifikat.pfx',
88 SSL_passwd_cb => sub { $cert_password },
91 my $response = $ua->get ("https://isspapi.issp.srce.hr/Kartice/oibjmbag/$sid", Accept => "application/json");
93 if ($response->is_success) {
94 my $json_text = $response->decoded_content;
95 my $h = decode_json $json_text;
100 foreach my $f ( keys %$h ) {
102 push @where, "( code = '$name' and attribute = ? )";
103 push @execute, $h->{$f};
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";
116 warn "ISSP $sid not found in koha $json_text";
117 $json->{error} = "borrower not found";
120 warn "ERROR ISSP ", $response->status_line;
121 $json->{error} = "borrower not found";
124 $json->{error} = "borrower not found";
127 $json->{error} = "borrower not found";
129 } elsif ( $sth->rows > 1 ) {
130 $json->{error} = "more than one borrower found";
132 $json->{borrower} = $sth->fetchrow_hashref;
135 $json = encode_json( $json );
136 if ( my $callback = $query->param('callback') ) {
137 print $callback, '(', $json, ')';