fix detection of selected check in/out tab
[koha.git] / 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 use strict;
21 use warnings;
22
23 use CGI;
24 use C4::Context;
25 use JSON;
26
27 my $query = new CGI;
28 print "Content-type: application/json; charset=utf-8\r\n\r\n";
29
30 my $dbh = C4::Context->dbh;
31 my $json;
32
33 my @where;
34 my @execute;
35
36 foreach my $name (qw( OIB JMBAG RFID_SID )) {
37         if ( my $val = $query->param($name) ) {
38                 $json->{param}->{$name} = $val;
39                 push @where, "( code = '$name' and attribute = ? )";
40                 push @execute, $val;
41         }
42 }
43
44 my $sql = qq{
45 select
46         distinct
47         b.borrowernumber, firstname, surname, email, userid, cardnumber
48 from borrower_attributes ba
49 join borrowers b on b.borrowernumber = ba.borrowernumber
50 where (} . join(') or (', @where) . qq{)};
51
52 warn "# sql $sql\n";
53
54 my $sth = $dbh->prepare( $sql );
55 $sth->execute( @execute );
56
57 $json->{rows} = $sth->rows;
58
59 if ( $sth->rows < 1 ) {
60         $json->{error} = "borrower not found";
61 } elsif ( $sth->rows > 1 ) {
62         $json->{error} = "more than one borrower found";
63 } else {
64         $json->{borrower} = $sth->fetchrow_hashref;
65 }
66
67 $json = encode_json( $json );
68 if ( my $callback = $query->param('callback') ) {
69         print $callback, '(', $json, ')';
70 } else {
71         print $json;
72 }