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