dla: fix plack PATH_INFO
[koha.git] / ffzg / barcode / dla.pl
1 #!/usr/bin/perl
2
3 # KOHA_CONF=/etc/koha/sites/ffzg/koha-conf.xml perl -I /srv/koha_ffzg dlna.pl scan=1
4
5 use Modern::Perl;
6
7 use C4::Context;
8 use Data::Dump qw(dump);
9 use CGI qw ( -utf8 );
10 use C4::Auth;
11 use autodie;
12
13 my $q = new CGI;
14 my $dbh = C4::Context->dbh;
15
16 $ENV{REQUEST_URI} =~ s{/intranet/}{/cgi-bin/koha/}; # fix plack rewrite
17
18 print
19     $q->header( -charset => 'utf-8' ),
20     , $q->start_html( -title => 'DLA RFID tag import from share' )
21         ;
22
23 # Authentication
24 my ($status, $cookie, $sessionId) = C4::Auth::check_api_auth($q, { tools => 'inventory' });
25 if ($status ne "ok") {
26         print "This requres tools - inventory permission";
27         exit;
28 }
29
30 if ( $q->param('scan') ) {
31
32                 print qq{<pre>};
33
34                 my $sth = $dbh->prepare(qq{
35 select date_scanned,source_id,count(*) from ffzg_inventura group by date_scanned,source_id order by date_scanned desc;
36                 });
37                 $sth->execute;
38
39                 my $sth_insert = $dbh->prepare(qq{
40 insert ignore into ffzg_inventura (date_scanned,source_id,barcode) values (?,?,?)
41                 });
42
43                 my @database;
44                 my $date_scanned_source_id;
45                 while ( my $row = $sth->fetchrow_arrayref ) {
46                         push @database, $row;
47                         $date_scanned_source_id->{ $row->[0] }->{ $row->[1] } = $row->[2];
48                 }
49
50
51                 open(my $find, '-|', qq{find /mnt/share/DLA/ . -name '*.pdX'});
52                 while(<$find>) {
53                         chomp;
54                         if ( m{/(\w+)/(\w+)/(20\d\d-\d\d-\d\d)/upload/inv/.*pdX$} ) {
55                                 my ($path, $dir , $source_id, $date_scanned ) = ( $_, $1, $2, $3 );
56
57                                 if ( my $count = $date_scanned_source_id->{ $date_scanned }->{ $source_id } ) {
58                                         print "$date_scanned $source_id [$count] $path\n";
59                                         next;
60                                 }
61
62                                 my $cache_path = "/tmp/dla.$date_scanned.$source_id";
63                                 if ( -e $cache_path ) {
64                                         print "$date_scanned $source_id NOT-IN-DATABASE $path $cache_path size ",-s $cache_path, "\n";
65                                         next;
66                                 }
67
68                                 print "CONVERT $path -> $source_id $date_scanned $cache_path\n";
69                                 open(my $cache, '>', $cache_path);
70
71                                 open(my $string, '-|', qq{strings $path | sort -u});
72                                 while(<$string>) {
73                                         while ( s/^.*?(130\d{7})//g ) {
74                                                 my $barcode = $1;
75                                                 warn "## barcode: $barcode";
76                                                 $sth_insert->execute( $date_scanned, $source_id, $barcode );
77                                                 print "++ $date_scanned $source_id ++ $barcode ++\n";
78                                                 print $cache "$barcode\n";
79                                         }
80                                 }
81
82                                 close($cache);
83                                 print "# created $cache_path ", -s $cache_path, " bytes\n";
84
85                                 my $sth_update = $dbh->prepare(qq{
86 update items as a
87 inner join (
88         select ffzg_inventura.barcode,items.datelastseen,max(ffzg_inventura.date_scanned) as date_scanned
89         from ffzg_inventura
90         join items on items.barcode = ffzg_inventura.barcode
91         where date_scanned = ? and source_id = ?
92         group by ffzg_inventura.barcode
93         having datelastseen < max(date_scanned)
94 ) as b
95 on a.barcode = b.barcode
96 set a.datelastseen = b.date_scanned, a.itemlost = 0 ;
97                                 });
98
99                                 $sth_update->execute( $date_scanned, $source_id );
100
101                                 last; # FIXME import just one on one page load
102                         }
103                 }
104                 print qq{</pre>};
105 } else {
106
107                 while(<DATA>) {
108                         if ( m/^select/i ) {
109                                 print qq{<table>\n};
110                                 my $sth = $dbh->prepare($_);
111                                 $sth->execute;
112                                 while ( my $row = $sth->fetchrow_arrayref ) {
113                                         print qq{<tr><td>}, join(qq{</td><td>}, @$row), qq{</td></tr>\n};
114                                 }
115                                 print qq{</table>\n};
116                         } else {
117                                 print "$_<br>\n";
118                         }
119                 }
120
121 }
122
123 print $q->start_form, $q->submit(-name => 'scan'), $q->end_form;
124
125 print $q->end_html;
126
127 __DATA__
128
129 -- total scanned by date ranges and source_id
130 select min(date_scanned),max(date_scanned),source_id,count(*) from ffzg_inventura group by source_id;
131