bccbaa863edb9dbf7687eab7b05347d86368b999
[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 autodie;
11
12 my $q = new CGI;
13 my $dbh = C4::Context->dbh;
14
15 print
16     $q->header( -charset => 'utf-8' ),
17     , $q->start_html( -title => 'DLA RFID tag import from share' )
18         ;
19
20 while(<DATA>) {
21         if ( m/^select/i ) {
22                 print qq{<table>\n};
23                 my $sth = $dbh->prepare($_);
24                 $sth->execute;
25                 while ( my $row = $sth->fetchrow_arrayref ) {
26                         print qq{<tr><td>}, join(qq{</td><td>}, @$row), qq{</td></tr>\n};
27                 }
28                 print qq{</table>\n};
29         } else {
30                 print "$_<br>\n";
31         }
32 }
33
34 if ( $q->param('scan') ) {
35
36                 print qq{<pre>};
37
38                 my $sth = $dbh->prepare(qq{
39 select date_scanned,source_id,count(*) from ffzg_inventura group by date_scanned,source_id order by date_scanned desc;
40                 });
41                 $sth->execute;
42
43                 my $sth_insert = $dbh->prepare(qq{
44 insert ignore into ffzg_inventura (date_scanned,source_id,barcode) values (?,?,?)
45                 });
46
47                 my @database;
48                 my $date_scanned_source_id;
49                 while ( my $row = $sth->fetchrow_arrayref ) {
50                         push @database, $row;
51                         $date_scanned_source_id->{ $row->[0] }->{ $row->[1] } = $row->[2];
52                 }
53
54
55                 open(my $find, '-|', qq{find /mnt/share/DLA/ . -name '*.pdX'});
56                 while(<$find>) {
57                         chomp;
58                         if ( m{/(\w+)/(\w+)/(20\d\d-\d\d-\d\d)/upload/inv/.*pdX$} ) {
59                                 my ($path, $dir , $source_id, $date_scanned ) = ( $_, $1, $2, $3 );
60
61                                 if ( my $count = $date_scanned_source_id->{ $date_scanned }->{ $source_id } ) {
62                                         print "$date_scanned $source_id [$count] $path\n";
63                                         next;
64                                 }
65
66                                 my $cache_path = "/tmp/dla.$date_scanned.$source_id";
67                                 if ( -e $cache_path ) {
68                                         print "$date_scanned $source_id NOT-IN-DATABASE $path $cache_path size ",-s $cache_path, "\n";
69                                         next;
70                                 }
71
72                                 print "CONVERT $path -> $source_id $date_scanned $cache_path\n";
73                                 open(my $cache, '>', $cache_path);
74
75                                 open(my $string, '-|', qq{strings $path | sort -u});
76                                 while(<$string>) {
77                                         while ( s/^.*?(130\d{7})//g ) {
78                                                 my $barcode = $1;
79                                                 warn "## barcode: $barcode";
80                                                 $sth_insert->execute( $date_scanned, $source_id, $barcode );
81                                                 print "++ $date_scanned $source_id ++ $barcode ++\n";
82                                                 print $cache "$barcode\n";
83                                         }
84                                 }
85
86                                 close($cache);
87                                 print "# created $cache_path ", -s $cache_path, " bytes\n";
88
89                                 last; # FIXME import just one on one page load
90                         }
91                 }
92                 print qq{</pre>};
93 }
94
95
96 __DATA__
97
98 -- total scanned by date ranges and source_id
99 select min(date_scanned),max(date_scanned),source_id,count(*) from ffzg_inventura group by source_id;
100
101 -- date scanned, source_id, tags seen