convert barcodes back for lookups to work
[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 => 0,
12         issues => 0,
13         barcode => 0,
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/ or $row->{$c} = undef; # XXX NULL dates istead of 0000-00-00
60         }
61
62         $insert->{$table}->execute( values %$row );
63         warn "# inserted $table ", dump $row;
64
65 }
66
67 sub borrowers {
68         my $sql = shift;
69         my $sth = $f->prepare($sql);
70         $sth->execute;
71
72         $insert = undef;
73
74         while (my $row = $sth->fetchrow_hashref ) {
75
76                 warn dump $row;
77
78                 # poor man's (sqlite) outer join
79                 $row->{'country'} = lookup($f, 'drzava', 'ozn_drzava', 'naz_drzava', $row->{'country'}, $row->{contry} );
80                 $row->{'city'}    = lookup($f, 'mjesto', 'post_ozn', 'naz_mjesto', $row->{'city'}, $row->{city} );
81
82                 $row->{'B_country'} = lookup($f, 'drzava', 'ozn_drzava', 'naz_drzava', $row->{'B_country'}, $row->{B_country} );
83                 $row->{'B_city'}    = lookup($f, 'mjesto', 'post_ozn', 'naz_mjesto', $row->{'B_city'}, $row->{B_city} );
84
85                 $row->{'B_email'}  = lookup($f, 'clanost', 'ozn_vrclan||ozn_clan', 'email', $row->{'cardnumber'}, undef);
86                 $row->{'emailpro'} = lookup($f, 'clanost', 'ozn_vrclan||ozn_clan', 'email_k', $row->{'cardnumber'}, undef);
87
88                 $row->{'email'}    = lookup($u, 'users', 'jmbag', 'email', substr($row->{cardnumber},1), undef); # $row->{B_email} || $row->{emailpro} )
89
90                 $row->{'userid'}    = lookup($u, 'users', 'jmbag', 'nick', substr($row->{cardnumber},1), $row->{cardnumber});
91
92                 $row->{sex} =~ s/\x{17D}/F/;
93
94                 $row->{city} ||= '?'; # not null
95
96                 $row->{borrowernotes} = lookup($f, 'clanorg', 'ozn_vrclan||ozn_clan', 'napom_clan', $row->{'cardnumber'}, undef);
97
98                 $row->{title} = lookup($f, 'titula', 'ozn_titula', 'opis_titula', $row->{title}, $row->{title});
99
100                 $row->{dateexpiry} ||= '01.01.2020'; # next revision
101
102                 my $jmbg = delete $row->{jmbg};
103
104                 insert 'borrowers' => $row;
105
106                 insert 'borrower_attributes' => {
107                         borrowernumber => $k->{mysql_insertid},
108                         code => 'JMBG',
109                         attribute => $jmbg,
110                 } if $jmbg;
111
112         }
113 }
114
115 if ( $import->{borrowers} ) {
116
117 $k->do(qq{ delete from borrowers where borrowernumber > 57 });
118 $k->do(qq{ delete from borrower_attributes where borrowernumber > 57 });
119
120 borrowers qq{
121 select
122   'S' as categorycode,
123   'SRE' as branchcode,
124   'S' || mbr_stud as cardnumber,
125   prez_stud as surname,
126   ime_stud as firstname,
127   djevprezime as othernames,
128   spol as sex,
129   dat_rodj as dateofbirth,
130 --  ime_otac as ??,
131 --  ime_majka as ??,
132   ozn_drzava_preb as country,
133   post_ozn_preb as zipcode,
134   post_ozn_preb as city,
135   adr_preb as address,
136   ozn_drzava_stan as B_country,
137   post_ozn_stan as B_zipcode,
138   post_ozn_stan as B_city,
139   adr_stan as B_address,
140   tel_stud as phone,
141   dat_prava_do as dateexpiry,
142 --  aktivan as ??,
143   jmbg_stud as jmbg
144 from studk
145 };
146
147 borrowers qq{
148 select
149   'D' as categorycode,
150   'SRE' as branchcode,
151   'D' || sif_djel as cardnumber,
152   ime_djel as firstname,
153   prez_djel as surname,
154   ozn_titula as title,
155 --  sif_orgjed (REF orgjed) as B_address,
156 --  sif_strsp_djel as ??,
157   tel_djel as phonepro,
158   dat_prekid_ro as dateexpiry,
159 --  tel_poduz as ??,
160   fax_poduz as fax,
161   ozn_drzava as country,
162   post_ozn as zipcode,
163   post_ozn as city,
164   adr_stan as address,
165   tel_stan as phone,
166   jmbg_djel as jmbg
167 from djelat
168 };
169
170 borrowers qq{
171 select
172   'O' as categorycode,
173   'SRE' as branchcode,
174   'O' || mbr_clan as cardnumber,
175   ime_clan as firstname,
176   prez_clan as surname,
177   ozn_titula as title,
178   zvanje_clan as ethnicity, -- XXX we are not allowerd by low to collect ethnicity
179 --  krat_poduz (REF poduz) as B_address,
180   tel_poduz as B_phone,
181 --  fax_poduz as ??,
182   ozn_drzava as country,
183   post_ozn as zipcode,
184   post_ozn as city,
185   adr_stan as address,
186   tel_stan as phone,
187 --  aktivan as ??,
188   jmbg_clan as jmbg
189 from clost
190 };
191
192 my $sth_syspref = $k->prepare(qq{ update systempreferences set value=? where variable=? });
193 my $BorrowersTitles = join('|', @{ $k->selectcol_arrayref(qq{ select distinct title from borrowers where title != '' }) });
194 $sth_syspref->execute( $BorrowersTitles => 'BorrowersTitles' );
195 warn "# BorrowersTitles [$BorrowersTitles]";
196
197 } # import->{borrowers}
198
199 $k->do(qq{ update items set barcode=substr(barcode,1,12)*1 where length(barcode) = 13 }); # convert barcodes back for lookups to work
200
201 sub issues {
202         my ($table,$sql) = @_;
203         my $sth = $f->prepare($sql);
204         $sth->execute;
205
206         $insert = undef;
207
208         while (my $row = $sth->fetchrow_hashref ) {
209                 $row->{borrowernumber} = lookup($k, 'borrowers', 'cardnumber' => 'borrowernumber', $row->{borrowernumber});
210                 $row->{itemnumber}     = lookup($k, 'items', 'barcode' => 'itemnumber', $row->{itemnumber});
211                 $row->{returndate} = '31.12.2011';
212                 insert $table => $row;
213         }
214 }
215
216 my $posud_sql = qq{
217 select
218         ozn_vrclan||ozn_clan as borrowernumber,
219         sif_primj as itemnumber,
220         datum_do as date_due,
221         'SRE' as branchcode, -- FIXME
222         datum_vra as returndate,
223         datum_pos as issuedate,
224         datum_pos||' '||vrijeme_pos as timestamp
225 from posud
226 };
227
228 if ( $import->{issues} ) {
229
230 $k->do('delete from old_issues');
231 $k->do('delete from issues');
232 issues 'old_issues' => qq{$posud_sql where datum_vra != ''};
233 issues 'issues'     => qq{$posud_sql where datum_vra == ''};
234
235 $k->do(qq{
236 update items
237 join issues on items.itemnumber=issues.itemnumber
238 set onloan = date_due, datelastborrowed = issuedate
239 });
240
241
242 }; # import->{issues}
243
244
245
246 $k->do(qq{ delete from reserves where borrowernumber > 100 });
247 $k->do(qq{ delete from old_reserves where borrowernumber > 100 });
248
249 sub reserves {
250         my ($table, $sql) = @_;
251         my $sth = $f->prepare($sql);
252         $sth->execute;
253
254         $insert = undef;
255
256         while (my $row = $sth->fetchrow_hashref ) {
257
258                 $row->{found} =
259                         $row->{found}      ? 'F' : 
260                         $row->{itemnumber} ? 'W' : undef;
261
262                 $row->{borrowernumber} = lookup($k, 'borrowers', 'cardnumber' => 'borrowernumber', $row->{borrowernumber});
263                 $row->{biblionumber}   = lookup($k, 'biblioitems', 'collectionvolume' => 'biblionumber', $row->{biblionumber});
264                 $row->{itemnumber}     = lookup($k, 'items', 'barcode' => 'itemnumber', $row->{itemnumber});
265
266                 insert $table => $row;
267         }
268 }
269
270 my $sql_reserves = qq{
271 select
272         ozn_vrclan||ozn_clan as borrowernumber,
273         datum_rezerv as reservedate,
274         sif_naslov as biblionumber,
275         'a' as constrainttype,
276         'SRE' as branchcode,
277         datum_obavijest as notificationdate,
278         datum_ponist as cancellationdate,
279         prior_rezerv as priority,
280         datum_posud as found, -- F|W
281         datum_rezerv||' '||vrijeme_rezerv as timestamp,
282         sif_primj as itemnumber,
283         datum_obavijest as waitingdate,
284         vrijedi_do as expirationdate,
285         0 as lowestPriority
286 from rezerv
287 };
288
289 reserves 'old_reserves' => qq{$sql_reserves where datum_opoziv != '' or  datum_ponist != '' or  datum_posud != ''};
290 reserves 'reserves'     => qq{$sql_reserves where datum_opoziv == '' and datum_ponist == '' and datum_posud == ''};
291
292
293
294 if ( $import->{barcode} ) { # XXX must be last!
295
296 my $sth        = $k->prepare(qq{ select itemnumber,barcode from items where length(barcode) < 13 });
297 my $sth_update = $k->prepare(qq{ update items set barcode=? where itemnumber=? });
298
299 $sth->execute;
300 warn "update ", $sth->rows, " barcodes";
301
302 my $ean = CheckDigits('ean');
303
304 while( my $row = $sth->fetchrow_hashref ) {
305         $sth_update->execute( $ean->complete( sprintf('%012d', $row->{barcode}) ), $row->{itemnumber} );
306 }
307
308 } # import->{barcode}
309
310
311
312
313 $k->commit;