5 use Data::Dump qw/dump/;
14 my $f = DBI->connect("dbi:SQLite:dbname=knjiznica.sqlite","","", { RaiseError => 1, AutoCommit => 0 }) || die $DBI::errstr;
15 $f->{sqlite_unicode} = 1;
16 my $k = DBI->connect("dbi:mysql:database=koha_fer","","", { RaiseError => 1, AutoCommit => 0, mysql_enable_utf8 => 1 }) || die $DBI::errstr;
20 my ( $dbh, $t, $k, $v, $s, $default ) = @_;
23 if ( exists $lookup->{$key} ) {
24 $hash = $lookup->{$key};
26 warn "# select $k,$v from $t";
27 my $sth = $dbh->prepare( "select $k,$v from $t" );
29 while (my $row = $sth->fetchrow_hashref() ) {
30 $hash->{ $row->{$k} } = $row->{$v};
32 $lookup->{$key} = $hash;
35 return unless length $s > 1;
36 if ( ! exists $lookup->{$key}->{$s} ) {
37 warn "ERROR: no $s in $key\n";
40 $lookup->{$key}->{$s};
46 my ( $table, $row ) = @_;
47 $insert->{$table} ||= $k->prepare(
48 "insert into $table ("
49 . join(',', keys %$row)
51 . join(',', map { '?' } keys %$row )
55 foreach my $c ( grep { /(date|timestamp)/ } keys %$row ) {
56 $row->{$c} =~ s/^(\d\d)\.(\d\d)\.(\d\d\d\d)/$3-$2-$1/;
59 $insert->{$table}->execute( values %$row );
60 warn "# inserted ", dump $row;
66 my $sth = $f->prepare($sql);
71 while (my $row = $sth->fetchrow_hashref ) {
75 # poor man's (sqlite) outer join
76 $row->{'country'} = lookup($f, 'drzava', 'ozn_drzava', 'naz_drzava', $row->{'country'}, $row->{contry} );
77 $row->{'city'} = lookup($f, 'mjesto', 'post_ozn', 'naz_mjesto', $row->{'city'}, $row->{city} );
79 $row->{'B_country'} = lookup($f, 'drzava', 'ozn_drzava', 'naz_drzava', $row->{'B_country'}, $row->{B_country} );
80 $row->{'B_city'} = lookup($f, 'mjesto', 'post_ozn', 'naz_mjesto', $row->{'B_city'}, $row->{B_city} );
82 $row->{'email'} = lookup($f, 'clanost', 'ozn_vrclan||ozn_clan', 'email', $row->{'cardnumber'}, undef);
83 $row->{'emailpro'} = lookup($f, 'clanost', 'ozn_vrclan||ozn_clan', 'email_k', $row->{'cardnumber'}, undef);
85 $row->{sex} =~ s/\x{17D}/F/;
87 $row->{city} ||= '?'; # not null
89 $row->{borrowernotes} = lookup($f, 'clanorg', 'ozn_vrclan||ozn_clan', 'napom_clan', $row->{'cardnumber'}, undef);
91 $row->{title} = lookup($f, 'titula', 'ozn_titula', 'opis_titula', $row->{title}, $row->{title});
93 my $jmbg = delete $row->{jmbg};
95 insert 'borrowers' => $row;
97 insert 'borrower_attributes' => {
98 borrowernumber => $k->{mysql_insertid},
106 if ( $import->{borrowers} ) {
108 $k->do(qq{ delete from borrowers where borrowernumber > 57 });
109 $k->do(qq{ delete from borrower_attributes where borrowernumber > 57 });
115 'S' || mbr_stud as cardnumber,
116 prez_stud as surname,
117 ime_stud as firstname,
118 djevprezime as othernames,
120 dat_rodj as dateofbirth,
123 ozn_drzava_preb as country,
124 post_ozn_preb as zipcode,
125 post_ozn_preb as city,
127 ozn_drzava_stan as B_country,
128 post_ozn_stan as B_zipcode,
129 post_ozn_stan as B_city,
130 adr_stan as B_address,
132 dat_prava_do as dateexpiry,
142 'D' || sif_djel as cardnumber,
143 ime_djel as firstname,
144 prez_djel as surname,
146 -- sif_orgjed (REF orgjed) as B_address,
147 -- sif_strsp_djel as ??,
148 tel_djel as phonepro,
149 dat_prekid_ro as dateexpiry,
152 ozn_drzava as country,
165 'O' || mbr_clan as cardnumber,
166 ime_clan as firstname,
167 prez_clan as surname,
169 zvanje_clan as ethnicity, -- XXX we are not allowerd by low to collect ethnicity
170 -- krat_poduz (REF poduz) as B_address,
171 tel_poduz as B_phone,
173 ozn_drzava as country,
183 my $sth_syspref = $k->prepare(qq{ update systempreferences set value=? where variable=? });
184 my $BorrowersTitles = join('|', @{ $k->selectcol_arrayref(qq{ select distinct title from borrowers where title != '' }) });
185 $sth_syspref->execute( $BorrowersTitles => 'BorrowersTitles' );
186 warn "# BorrowersTitles [$BorrowersTitles]";
188 } # import->{borrowers}
191 my ($table,$sql) = @_;
192 my $sth = $f->prepare($sql);
197 while (my $row = $sth->fetchrow_hashref ) {
198 $row->{borrowernumber} = lookup($k, 'borrowers', 'cardnumber' => 'borrowernumber', $row->{borrowernumber});
199 $row->{itemnumber} = lookup($k, 'items', 'barcode' => 'itemnumber', $row->{itemnumber});
200 insert $table => $row;
206 ozn_vrclan||ozn_clan as borrowernumber,
207 sif_primj as itemnumber,
208 datum_do as date_due,
209 'SRE' as branchcode, -- FIXME
210 datum_vra as returndate,
211 datum_pos as issuedate,
212 datum_pos||' '||vrijeme_pos as timestamp
216 if ( $import->{issuses} ) {
218 $k->do('delete from old_issues');
219 $k->do('delete from issues');
220 issues 'old_issues' => qq{$posud_sql where datum_vra != ''};
221 issues 'issues' => qq{$posud_sql where datum_vra == ''};
223 }; # import->{issues}