5 use Data::Dump qw/dump/;
6 use Algorithm::CheckDigits;
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;
23 my ( $dbh, $t, $k, $v, $s, $default ) = @_;
26 if ( exists $lookup->{$key} ) {
27 $hash = $lookup->{$key};
29 warn "# select $k,$v from $t";
30 my $sth = $dbh->prepare( "select $k,$v from $t" );
32 while (my $row = $sth->fetchrow_hashref() ) {
33 $hash->{ $row->{$k} } = $row->{$v};
35 $lookup->{$key} = $hash;
36 warn "# lookup $key = ",dump($hash);
38 return unless length $s > 1;
39 if ( ! exists $lookup->{$key}->{$s} ) {
40 warn "ERROR: no $s in $key\n";
43 $lookup->{$key}->{$s};
49 my ( $table, $row ) = @_;
50 $insert->{$table} ||= $k->prepare(
51 "insert into $table ("
52 . join(',', keys %$row)
54 . join(',', map { '?' } keys %$row )
58 foreach my $c ( grep { /(date|timestamp)/ } keys %$row ) {
59 $row->{$c} =~ s/^(\d\d)\.(\d\d)\.(\d\d\d\d)/$3-$2-$1/;
62 $insert->{$table}->execute( values %$row );
63 warn "# inserted ", dump $row;
69 my $sth = $f->prepare($sql);
74 while (my $row = $sth->fetchrow_hashref ) {
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} );
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} );
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);
88 $row->{'email'} = lookup($u, 'users', 'jmbag', 'email', substr($row->{cardnumber},1), undef); # $row->{B_email} || $row->{emailpro} )
90 $row->{sex} =~ s/\x{17D}/F/;
92 $row->{city} ||= '?'; # not null
94 $row->{borrowernotes} = lookup($f, 'clanorg', 'ozn_vrclan||ozn_clan', 'napom_clan', $row->{'cardnumber'}, undef);
96 $row->{title} = lookup($f, 'titula', 'ozn_titula', 'opis_titula', $row->{title}, $row->{title});
98 $row->{dateexpiry} ||= '01.01.2020'; # next revision
100 my $jmbg = delete $row->{jmbg};
102 insert 'borrowers' => $row;
104 insert 'borrower_attributes' => {
105 borrowernumber => $k->{mysql_insertid},
113 if ( $import->{borrowers} ) {
115 $k->do(qq{ delete from borrowers where borrowernumber > 57 });
116 $k->do(qq{ delete from borrower_attributes where borrowernumber > 57 });
122 'S' || mbr_stud as cardnumber,
123 prez_stud as surname,
124 ime_stud as firstname,
125 djevprezime as othernames,
127 dat_rodj as dateofbirth,
130 ozn_drzava_preb as country,
131 post_ozn_preb as zipcode,
132 post_ozn_preb as city,
134 ozn_drzava_stan as B_country,
135 post_ozn_stan as B_zipcode,
136 post_ozn_stan as B_city,
137 adr_stan as B_address,
139 dat_prava_do as dateexpiry,
149 'D' || sif_djel as cardnumber,
150 ime_djel as firstname,
151 prez_djel as surname,
153 -- sif_orgjed (REF orgjed) as B_address,
154 -- sif_strsp_djel as ??,
155 tel_djel as phonepro,
156 dat_prekid_ro as dateexpiry,
159 ozn_drzava as country,
172 'O' || mbr_clan as cardnumber,
173 ime_clan as firstname,
174 prez_clan as surname,
176 zvanje_clan as ethnicity, -- XXX we are not allowerd by low to collect ethnicity
177 -- krat_poduz (REF poduz) as B_address,
178 tel_poduz as B_phone,
180 ozn_drzava as country,
190 my $sth_syspref = $k->prepare(qq{ update systempreferences set value=? where variable=? });
191 my $BorrowersTitles = join('|', @{ $k->selectcol_arrayref(qq{ select distinct title from borrowers where title != '' }) });
192 $sth_syspref->execute( $BorrowersTitles => 'BorrowersTitles' );
193 warn "# BorrowersTitles [$BorrowersTitles]";
195 } # import->{borrowers}
198 my ($table,$sql) = @_;
199 my $sth = $f->prepare($sql);
204 while (my $row = $sth->fetchrow_hashref ) {
205 $row->{borrowernumber} = lookup($k, 'borrowers', 'cardnumber' => 'borrowernumber', $row->{borrowernumber});
206 $row->{itemnumber} = lookup($k, 'items', 'barcode' => 'itemnumber', $row->{itemnumber});
207 insert $table => $row;
213 ozn_vrclan||ozn_clan as borrowernumber,
214 sif_primj as itemnumber,
215 datum_do as date_due,
216 'SRE' as branchcode, -- FIXME
217 datum_vra as returndate,
218 datum_pos as issuedate,
219 datum_pos||' '||vrijeme_pos as timestamp
223 if ( $import->{issues} ) {
225 $k->do('delete from old_issues');
226 $k->do('delete from issues');
227 issues 'old_issues' => qq{$posud_sql where datum_vra != ''};
228 issues 'issues' => qq{$posud_sql where datum_vra == ''};
232 join issues on items.itemnumber=issues.itemnumber
233 set onloan = date_due, datelastborrowed = issuedate
237 }; # import->{issues}
239 if ( $import->{barcode} ) {
241 my $sth = $k->prepare(qq{ select itemnumber,barcode from items });
242 my $sth_update = $k->prepare(qq{ update items set barcode=? where itemnumber=? });
245 warn "update ", $sth->rows, " barcodes";
247 my $ean = CheckDigits('ean');
249 while( my $row = $sth->fetchrow_hashref ) {
250 $sth_update->execute( $ean->complete( sprintf('%012d', $row->{barcode}) ), $row->{itemnumber} );
253 } # import->{barcode}