added inventura barcode scanner
[share-koha-fer] / intranet / cgi-bin / fer / inventura.pl
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use C4::Context;
6 use C4::Circulation;
7 use Data::Dump qw(dump);
8 use CGI qw ( -utf8 );
9
10 binmode STDOUT, ':encoding(UTF-8)';
11
12 my $q = new CGI;
13
14 my $barcode = $q->param('barcode');
15 warn "# barcode: $barcode\n";
16 $q->delete('barcode'); # empty form field
17
18 my $row;
19
20 $ENV{REQUEST_URI} =~ s{/intranet/}{/cgi-bin/koha/}; # fix plack rewrite
21
22 print $q->header( -charset => 'utf-8' ), qq{
23 <!DOCTYPE html>
24 <html>
25   <head>
26   <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
27   <style>
28         body {
29                 font-size: 200%;
30         }
31   </style>
32   <title>Inventura</title>
33 </head>
34   <body>
35 };
36
37 # Authentication
38 my ($status, $cookie, $sessionId) = C4::Auth::check_api_auth($q, { tools => 'inventory' });
39 if ($status ne "ok") {
40         print "This requres tools - inventory permission";
41         goto end_body;
42 }
43
44
45 print
46       $q->start_form( -autocomplete => 'off' )
47     , $q->textfield( -name => 'barcode', -autofocus => 'autofocus' )
48     , $q->submit( -value => 'Search' )
49 #    , $q->checkbox( -name => 'izdatnice', -label => 'izdatnice' )
50 #    , $q->checkbox( -name => 'proizvodi', -label => 'prozivodi', -checked => 1 )
51     , $q->end_form
52     , qq|
53 <!--
54 <script type="text/javascript">
55 document.getElementsByName('barcode')[0].focus();
56 </script>
57 -->
58         |
59 ;
60
61 if ( $barcode ) {
62
63         my $dbh = C4::Context->dbh;
64
65         my $sql = qq{
66         select
67                 itemnumber,
68                 items.biblionumber as biblionumber,
69                 title,
70                 author
71         from items
72         join biblio on items.biblionumber = biblio.biblionumber
73         where barcode = ?
74         };
75
76         #warn "# sql $sql\n";
77
78         my $sth = $dbh->prepare( $sql );
79         $sth->execute( $barcode );
80         if ( $sth->rows ) {
81
82                         $row = $sth->fetchrow_hashref;
83
84                         print qq|
85 BARCODE: <tt>$barcode</tt><br>
86 TITLE: <b>$row->{title}</b><br>
87 AUTHOR: $row->{author}<br>
88                         |;
89
90                         my $sth_update = $dbh->prepare(qq{
91                         update items set datelastseen = now() where barcode = ?
92                         });
93                         $sth_update->execute( $barcode );
94
95                         my $sth_inventura = $dbh->prepare(qq{
96                         insert ignore into fer_inventura (date_scanned,barcode,source_id) values (date(now()), ?, ?)
97                         });
98                         $sth_inventura->execute( $barcode, C4::Context->userenv->{'id'} );
99
100                         my $sth_issues = $dbh->prepare(qq{
101                         select firstname,surname,userid,email from issues join borrowers on issues.borrowernumber = borrowers.borrowernumber where itemnumber = ?
102                         });
103
104                         $sth_issues->execute( $row->{'itemnumber'} );
105                         while ( my $row = $sth_issues->fetchrow_hashref ) {
106                                 warn "# issues row ",dump($row);
107                                 print "issued to ", $row->{firstname}, ' ', $row->{surname}, " returning...";
108                                 AddReturn( $barcode, C4::Context->userenv->{'branch'} );
109                         }
110         } else {
111                         print "no barcode $barcode\n";
112                         warn "ERROR: can't find $barcode\n";
113         }
114
115 }
116
117 end_body:
118
119 print qq{
120 </body>
121 </html>
122 };