userid, returndate
[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 => 1,
12         issues => 1,
13         barcode => 1,
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         }
61
62         $insert->{$table}->execute( values %$row );
63         warn "# inserted ", 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 sub issues {
200         my ($table,$sql) = @_;
201         my $sth = $f->prepare($sql);
202         $sth->execute;
203
204         $insert = undef;
205
206         while (my $row = $sth->fetchrow_hashref ) {
207                 $row->{borrowernumber} = lookup($k, 'borrowers', 'cardnumber' => 'borrowernumber', $row->{borrowernumber});
208                 $row->{itemnumber}     = lookup($k, 'items', 'barcode' => 'itemnumber', $row->{itemnumber});
209                 $row->{returndate} = '31.12.2011';
210                 insert $table => $row;
211         }
212 }
213
214 my $posud_sql = qq{
215 select
216         ozn_vrclan||ozn_clan as borrowernumber,
217         sif_primj as itemnumber,
218         datum_do as date_due,
219         'SRE' as branchcode, -- FIXME
220         datum_vra as returndate,
221         datum_pos as issuedate,
222         datum_pos||' '||vrijeme_pos as timestamp
223 from posud
224 };
225
226 if ( $import->{issues} ) {
227
228 $k->do('delete from old_issues');
229 $k->do('delete from issues');
230 issues 'old_issues' => qq{$posud_sql where datum_vra != ''};
231 issues 'issues'     => qq{$posud_sql where datum_vra == ''};
232
233 $k->do(qq{
234 update items
235 join issues on items.itemnumber=issues.itemnumber
236 set onloan = date_due, datelastborrowed = issuedate
237 });
238
239
240 }; # import->{issues}
241
242 if ( $import->{barcode} ) {
243
244 my $sth        = $k->prepare(qq{ select itemnumber,barcode from items where length(barcode) < 13 });
245 my $sth_update = $k->prepare(qq{ update items set barcode=? where itemnumber=? });
246
247 $sth->execute;
248 warn "update ", $sth->rows, " barcodes";
249
250 my $ean = CheckDigits('ean');
251
252 while( my $row = $sth->fetchrow_hashref ) {
253         $sth_update->execute( $ean->complete( sprintf('%012d', $row->{barcode}) ), $row->{itemnumber} );
254 }
255
256 } # import->{barcode}
257
258 $k->commit;