fill statistics
[ferlib2koha.git] / ferlib2koha.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use DBI;
5 use Data::Dump qw/dump/;
6 use Algorithm::CheckDigits;
7
8 $|++;
9
10 my $import = {
11         borrowers => $ENV{FULL},
12         issues    => $ENV{FULL},
13         barcode   => $ENV{FULL},
14 };
15
16 my $f = DBI->connect("dbi:SQLite:dbname=knjiznica.sqlite","","", { RaiseError => 1, AutoCommit => 0 }) || die $DBI::errstr;
17 $f->{sqlite_unicode} = 1;
18 my $k = DBI->connect("dbi:mysql:database=koha_fer","","", { RaiseError => 1, AutoCommit => 0, mysql_enable_utf8 => 1 }) || die $DBI::errstr;
19 my $u = DBI->connect("dbi:CSV:","","", { RaiseError => 1, f_ext => '.csv', csv_sep_char => '|' }) || die $DBI::errstr;
20
21 our $lookup;
22 sub lookup {
23         my ( $dbh, $t, $k, $v, $s, $default ) = @_;
24         my $hash;
25         my $key = "$t $k $v";
26         if ( exists $lookup->{$key} ) {
27                 $hash = $lookup->{$key};
28         } else {
29                 warn "# select $k,$v from $t";
30                 my $sth = $dbh->prepare( "select $k,$v from $t" );
31                 $sth->execute;
32                 while (my $row = $sth->fetchrow_hashref() ) {
33                         $hash->{ $row->{$k} } = $row->{$v};
34                 }
35                 $lookup->{$key} = $hash;
36                 warn "# lookup $key = ",dump($hash);
37         }
38         return unless length $s > 1;
39         if ( ! exists $lookup->{$key}->{$s} ) {
40                 warn "ERROR: no $s in $key\n";
41                 return $default;
42         } else {
43                 $lookup->{$key}->{$s};
44         }
45 }
46
47 our $insert;
48 sub insert {
49         my ( $table, $row ) = @_;
50         $insert->{$table} ||= $k->prepare(
51                         "insert into $table ("
52                         . join(',', keys %$row)
53                         . ") values ("
54                         . join(',', map { '?' } keys %$row )
55                         . ")"
56         );
57
58         foreach my $c ( grep { /(date|timestamp)/ } keys %$row ) {
59                 $row->{$c} =~ s/^(\d\d)\.(\d\d)\.(\d\d\d\d)/$3-$2-$1/;
60                 $row->{$c} = undef if $row->{$c} eq ''; # XXX NULL dates istead of 0000-00-00
61         }
62
63         $insert->{$table}->execute( values %$row );
64         warn "# inserted $table ", dump $row;
65
66 }
67
68 sub borrowers {
69         my $sql = shift;
70         my $sth = $f->prepare($sql);
71         $sth->execute;
72
73         $insert = undef;
74
75         while (my $row = $sth->fetchrow_hashref ) {
76
77 #               warn dump $row;
78
79                 # poor man's (sqlite) outer join
80                 $row->{'country'} = lookup($f, 'drzava', 'ozn_drzava', 'naz_drzava', $row->{'country'}, $row->{contry} );
81                 $row->{'city'}    = lookup($f, 'mjesto', 'post_ozn', 'naz_mjesto', $row->{'city'}, $row->{city} );
82
83                 $row->{'B_country'} = lookup($f, 'drzava', 'ozn_drzava', 'naz_drzava', $row->{'B_country'}, $row->{B_country} );
84                 $row->{'B_city'}    = lookup($f, 'mjesto', 'post_ozn', 'naz_mjesto', $row->{'B_city'}, $row->{B_city} );
85
86                 $row->{'B_email'}  = lookup($f, 'clanost', 'ozn_vrclan||ozn_clan', 'email', $row->{'cardnumber'}, undef);
87                 $row->{'emailpro'} = lookup($f, 'clanost', 'ozn_vrclan||ozn_clan', 'email_k', $row->{'cardnumber'}, undef);
88
89                 $row->{'email'}    = lookup($u, 'users', 'jmbag', 'email', substr($row->{cardnumber},1), undef); # $row->{B_email} || $row->{emailpro} )
90
91                 $row->{'userid'}    = lookup($u, 'users', 'jmbag', 'nick', substr($row->{cardnumber},1), $row->{cardnumber});
92
93                 $row->{sex} =~ s/\x{17D}/F/;
94
95                 $row->{city} ||= '?'; # not null
96
97                 $row->{borrowernotes} = lookup($f, 'clanorg', 'ozn_vrclan||ozn_clan', 'napom_clan', $row->{'cardnumber'}, undef);
98
99                 $row->{title} = lookup($f, 'titula', 'ozn_titula', 'opis_titula', $row->{title}, $row->{title});
100
101                 $row->{dateexpiry} ||= '01.01.2020'; # next revision
102
103                 my $jmbg = delete $row->{jmbg};
104
105                 insert 'borrowers' => $row;
106
107                 insert 'borrower_attributes' => {
108                         borrowernumber => $k->{mysql_insertid},
109                         code => 'JMBG',
110                         attribute => $jmbg,
111                 } if $jmbg;
112
113         }
114 }
115
116 if ( $import->{borrowers} ) {
117
118 $k->do(qq{ delete from borrowers where borrowernumber > 57 });
119 $k->do(qq{ delete from borrower_attributes where borrowernumber > 57 });
120
121 borrowers qq{
122 select
123   'S' as categorycode,
124   'SRE' as branchcode,
125   'S' || mbr_stud as cardnumber,
126   prez_stud as surname,
127   ime_stud as firstname,
128   djevprezime as othernames,
129   spol as sex,
130   dat_rodj as dateofbirth,
131 --  ime_otac as ??,
132 --  ime_majka as ??,
133   ozn_drzava_preb as country,
134   post_ozn_preb as zipcode,
135   post_ozn_preb as city,
136   adr_preb as address,
137   ozn_drzava_stan as B_country,
138   post_ozn_stan as B_zipcode,
139   post_ozn_stan as B_city,
140   adr_stan as B_address,
141   tel_stud as phone,
142   dat_prava_do as dateexpiry,
143 --  aktivan as ??,
144   jmbg_stud as jmbg
145 from studk
146 };
147
148 borrowers qq{
149 select
150   'D' as categorycode,
151   'SRE' as branchcode,
152   'D' || sif_djel as cardnumber,
153   ime_djel as firstname,
154   prez_djel as surname,
155   ozn_titula as title,
156 --  sif_orgjed (REF orgjed) as B_address,
157 --  sif_strsp_djel as ??,
158   tel_djel as phonepro,
159   dat_prekid_ro as dateexpiry,
160 --  tel_poduz as ??,
161   fax_poduz as fax,
162   ozn_drzava as country,
163   post_ozn as zipcode,
164   post_ozn as city,
165   adr_stan as address,
166   tel_stan as phone,
167   jmbg_djel as jmbg
168 from djelat
169 };
170
171 borrowers qq{
172 select
173   'O' as categorycode,
174   'SRE' as branchcode,
175   'O' || mbr_clan as cardnumber,
176   ime_clan as firstname,
177   prez_clan as surname,
178   ozn_titula as title,
179   zvanje_clan as ethnicity, -- XXX we are not allowerd by low to collect ethnicity
180 --  krat_poduz (REF poduz) as B_address,
181   tel_poduz as B_phone,
182 --  fax_poduz as ??,
183   ozn_drzava as country,
184   post_ozn as zipcode,
185   post_ozn as city,
186   adr_stan as address,
187   tel_stan as phone,
188 --  aktivan as ??,
189   jmbg_clan as jmbg
190 from clost
191 };
192
193 my $sth_syspref = $k->prepare(qq{ update systempreferences set value=? where variable=? });
194 my $BorrowersTitles = join('|', @{ $k->selectcol_arrayref(qq{ select distinct title from borrowers where title != '' }) });
195 $sth_syspref->execute( $BorrowersTitles => 'BorrowersTitles' );
196 warn "# BorrowersTitles [$BorrowersTitles]";
197
198 } # import->{borrowers}
199
200 $k->do(qq{ update items set barcode=substr(barcode,1,12)*1 where length(barcode) = 13 }); # convert barcodes back for lookups to work
201
202 sub issues {
203         my ($table,$sql) = @_;
204         my $sth = $f->prepare($sql);
205         $sth->execute;
206
207         $insert = undef;
208
209         while (my $row = $sth->fetchrow_hashref ) {
210                 $row->{borrowernumber} = lookup($k, 'borrowers', 'cardnumber' => 'borrowernumber', $row->{borrowernumber});
211                 $row->{itemnumber}     = lookup($k, 'items', 'barcode' => 'itemnumber', $row->{itemnumber});
212                 $row->{returndate} = '31.12.2011';
213                 insert $table => $row;
214
215                 my $stat = {
216                         datetime => $row->{timestamp},
217                         branch   => $row->{branchcode},
218                         value    => 0,
219                         type     => $table =~ m/old/ ? 'return' : 'issue',
220                 };
221                 $stat->{$_} = $row->{$_} foreach (qw( itemnumber borrowernumber ));
222                 insert statistics => $stat;
223         }
224 }
225
226 my $posud_sql = qq{
227 select
228         ozn_vrclan||ozn_clan as borrowernumber,
229         sif_primj as itemnumber,
230         datum_do as date_due,
231         'SRE' as branchcode, -- FIXME
232         datum_vra as returndate,
233         datum_pos as issuedate,
234         datum_pos||' '||vrijeme_pos as timestamp
235 from posud
236 };
237
238 if ( $import->{issues} ) {
239
240 $k->do('delete from old_issues');
241 $k->do('delete from issues');
242 issues 'old_issues' => qq{$posud_sql where datum_vra != ''};
243 issues 'issues'     => qq{$posud_sql where datum_vra == ''};
244
245 $k->do(qq{
246 update items
247 join issues on items.itemnumber=issues.itemnumber
248 set onloan = date_due, datelastborrowed = issuedate
249 });
250
251
252 }; # import->{issues}
253
254
255
256 $k->do(qq{ delete from reserves where borrowernumber > 100 });
257 $k->do(qq{ delete from old_reserves where borrowernumber > 100 });
258
259 sub reserves {
260         my ($table, $sql) = @_;
261         my $sth = $f->prepare($sql);
262         $sth->execute;
263
264         $insert = undef;
265
266         while (my $row = $sth->fetchrow_hashref ) {
267
268                 $row->{found} =
269                         $row->{found}      ? 'F' : 
270                         $row->{itemnumber} ? 'W' : undef;
271
272                 $row->{borrowernumber} = lookup($k, 'borrowers', 'cardnumber' => 'borrowernumber', $row->{borrowernumber});
273                 $row->{biblionumber}   = lookup($k, 'biblioitems', 'collectionvolume' => 'biblionumber', $row->{biblionumber});
274                 $row->{itemnumber}     = lookup($k, 'items', 'barcode' => 'itemnumber', $row->{itemnumber});
275
276                 insert $table => $row;
277         }
278 }
279
280 my $sql_reserves = qq{
281 select
282         ozn_vrclan||ozn_clan as borrowernumber,
283         datum_rezerv as reservedate,
284         sif_naslov as biblionumber,
285         'a' as constrainttype,
286         'SRE' as branchcode,
287         datum_obavijest as notificationdate,
288         datum_ponist as cancellationdate,
289         prior_rezerv as priority,
290         datum_posud as found, -- F|W
291         datum_rezerv||' '||vrijeme_rezerv as timestamp,
292         sif_primj as itemnumber,
293         datum_obavijest as waitingdate,
294         vrijedi_do as expirationdate,
295         0 as lowestPriority
296 from rezerv
297 };
298
299 reserves 'old_reserves' => qq{$sql_reserves where datum_opoziv != '' or  datum_ponist != '' or  datum_posud != ''};
300 reserves 'reserves'     => qq{$sql_reserves where datum_opoziv == '' and datum_ponist == '' and datum_posud == ''};
301
302
303
304 if ( $import->{barcode} ) { # XXX must be last!
305
306 my $sth        = $k->prepare(qq{ select itemnumber,barcode from items where length(barcode) < 13 });
307 my $sth_update = $k->prepare(qq{ update items set barcode=? where itemnumber=? });
308
309 $sth->execute;
310 warn "update ", $sth->rows, " barcodes";
311
312 my $ean = CheckDigits('ean');
313
314 while( my $row = $sth->fetchrow_hashref ) {
315         $sth_update->execute( $ean->complete( sprintf('%012d', $row->{barcode}) ), $row->{itemnumber} );
316 }
317
318 } # import->{barcode}
319
320
321
322
323 $k->commit;