calculate EAN-13 controll checksum
[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 };
14
15 my $f = DBI->connect("dbi:SQLite:dbname=knjiznica.sqlite","","", { RaiseError => 1, AutoCommit => 0 }) || die $DBI::errstr;
16 $f->{sqlite_unicode} = 1;
17 my $k = DBI->connect("dbi:mysql:database=koha_fer","","", { RaiseError => 1, AutoCommit => 0, mysql_enable_utf8 => 1 }) || die $DBI::errstr;
18
19 our $lookup;
20 sub lookup {
21         my ( $dbh, $t, $k, $v, $s, $default ) = @_;
22         my $hash;
23         my $key = "$t $k $v";
24         if ( exists $lookup->{$key} ) {
25                 $hash = $lookup->{$key};
26         } else {
27                 warn "# select $k,$v from $t";
28                 my $sth = $dbh->prepare( "select $k,$v from $t" );
29                 $sth->execute;
30                 while (my $row = $sth->fetchrow_hashref() ) {
31                         $hash->{ $row->{$k} } = $row->{$v};
32                 }
33                 $lookup->{$key} = $hash;
34                 warn dump($lookup);
35         }
36         return unless length $s > 1;
37         if ( ! exists $lookup->{$key}->{$s} ) {
38                 warn "ERROR: no $s in $key\n";
39                 return $default;
40         } else {
41                 $lookup->{$key}->{$s};
42         }
43 }
44
45 our $insert;
46 sub insert {
47         my ( $table, $row ) = @_;
48         $insert->{$table} ||= $k->prepare(
49                         "insert into $table ("
50                         . join(',', keys %$row)
51                         . ") values ("
52                         . join(',', map { '?' } keys %$row )
53                         . ")"
54         );
55
56         foreach my $c ( grep { /(date|timestamp)/ } keys %$row ) {
57                 $row->{$c} =~ s/^(\d\d)\.(\d\d)\.(\d\d\d\d)/$3-$2-$1/;
58         }
59
60         $insert->{$table}->execute( values %$row );
61         warn "# inserted ", dump $row;
62
63 }
64
65 sub borrowers {
66         my $sql = shift;
67         my $sth = $f->prepare($sql);
68         $sth->execute;
69
70         $insert = undef;
71
72         while (my $row = $sth->fetchrow_hashref ) {
73
74                 warn dump $row;
75
76                 # poor man's (sqlite) outer join
77                 $row->{'country'} = lookup($f, 'drzava', 'ozn_drzava', 'naz_drzava', $row->{'country'}, $row->{contry} );
78                 $row->{'city'}    = lookup($f, 'mjesto', 'post_ozn', 'naz_mjesto', $row->{'city'}, $row->{city} );
79
80                 $row->{'B_country'} = lookup($f, 'drzava', 'ozn_drzava', 'naz_drzava', $row->{'B_country'}, $row->{B_country} );
81                 $row->{'B_city'}    = lookup($f, 'mjesto', 'post_ozn', 'naz_mjesto', $row->{'B_city'}, $row->{B_city} );
82
83                 $row->{'email'}    = lookup($f, 'clanost', 'ozn_vrclan||ozn_clan', 'email', $row->{'cardnumber'}, undef);
84                 $row->{'emailpro'} = lookup($f, 'clanost', 'ozn_vrclan||ozn_clan', 'email_k', $row->{'cardnumber'}, undef);
85
86                 $row->{sex} =~ s/\x{17D}/F/;
87
88                 $row->{city} ||= '?'; # not null
89
90                 $row->{borrowernotes} = lookup($f, 'clanorg', 'ozn_vrclan||ozn_clan', 'napom_clan', $row->{'cardnumber'}, undef);
91
92                 $row->{title} = lookup($f, 'titula', 'ozn_titula', 'opis_titula', $row->{title}, $row->{title});
93
94                 $row->{dateexpiry} ||= '01.01.2020'; # next revision
95
96                 my $jmbg = delete $row->{jmbg};
97
98                 insert 'borrowers' => $row;
99
100                 insert 'borrower_attributes' => {
101                         borrowernumber => $k->{mysql_insertid},
102                         code => 'JMBG',
103                         attribute => $jmbg,
104                 } if $jmbg;
105
106         }
107 }
108
109 if ( $import->{borrowers} ) {
110
111 $k->do(qq{ delete from borrowers where borrowernumber > 57 });
112 $k->do(qq{ delete from borrower_attributes where borrowernumber > 57 });
113
114 borrowers qq{
115 select
116   'S' as categorycode,
117   'SRE' as branchcode,
118   'S' || mbr_stud as cardnumber,
119   prez_stud as surname,
120   ime_stud as firstname,
121   djevprezime as othernames,
122   spol as sex,
123   dat_rodj as dateofbirth,
124 --  ime_otac as ??,
125 --  ime_majka as ??,
126   ozn_drzava_preb as country,
127   post_ozn_preb as zipcode,
128   post_ozn_preb as city,
129   adr_preb as address,
130   ozn_drzava_stan as B_country,
131   post_ozn_stan as B_zipcode,
132   post_ozn_stan as B_city,
133   adr_stan as B_address,
134   tel_stud as phone,
135   dat_prava_do as dateexpiry,
136 --  aktivan as ??,
137   jmbg_stud as jmbg
138 from studk
139 };
140
141 borrowers qq{
142 select
143   'D' as categorycode,
144   'SRE' as branchcode,
145   'D' || sif_djel as cardnumber,
146   ime_djel as firstname,
147   prez_djel as surname,
148   ozn_titula as title,
149 --  sif_orgjed (REF orgjed) as B_address,
150 --  sif_strsp_djel as ??,
151   tel_djel as phonepro,
152   dat_prekid_ro as dateexpiry,
153 --  tel_poduz as ??,
154   fax_poduz as fax,
155   ozn_drzava as country,
156   post_ozn as zipcode,
157   post_ozn as city,
158   adr_stan as address,
159   tel_stan as phone,
160   jmbg_djel as jmbg
161 from djelat
162 };
163
164 borrowers qq{
165 select
166   'O' as categorycode,
167   'SRE' as branchcode,
168   'O' || mbr_clan as cardnumber,
169   ime_clan as firstname,
170   prez_clan as surname,
171   ozn_titula as title,
172   zvanje_clan as ethnicity, -- XXX we are not allowerd by low to collect ethnicity
173 --  krat_poduz (REF poduz) as B_address,
174   tel_poduz as B_phone,
175 --  fax_poduz as ??,
176   ozn_drzava as country,
177   post_ozn as zipcode,
178   post_ozn as city,
179   adr_stan as address,
180   tel_stan as phone,
181 --  aktivan as ??,
182   jmbg_clan as jmbg
183 from clost
184 };
185
186 my $sth_syspref = $k->prepare(qq{ update systempreferences set value=? where variable=? });
187 my $BorrowersTitles = join('|', @{ $k->selectcol_arrayref(qq{ select distinct title from borrowers where title != '' }) });
188 $sth_syspref->execute( $BorrowersTitles => 'BorrowersTitles' );
189 warn "# BorrowersTitles [$BorrowersTitles]";
190
191 } # import->{borrowers}
192
193 sub issues {
194         my ($table,$sql) = @_;
195         my $sth = $f->prepare($sql);
196         $sth->execute;
197
198         $insert = undef;
199
200         while (my $row = $sth->fetchrow_hashref ) {
201                 $row->{borrowernumber} = lookup($k, 'borrowers', 'cardnumber' => 'borrowernumber', $row->{borrowernumber});
202                 $row->{itemnumber}     = lookup($k, 'items', 'barcode' => 'itemnumber', $row->{itemnumber});
203                 insert $table => $row;
204         }
205 }
206
207 my $posud_sql = qq{
208 select
209         ozn_vrclan||ozn_clan as borrowernumber,
210         sif_primj as itemnumber,
211         datum_do as date_due,
212         'SRE' as branchcode, -- FIXME
213         datum_vra as returndate,
214         datum_pos as issuedate,
215         datum_pos||' '||vrijeme_pos as timestamp
216 from posud
217 };
218
219 if ( $import->{issues} ) {
220
221 $k->do('delete from old_issues');
222 $k->do('delete from issues');
223 issues 'old_issues' => qq{$posud_sql where datum_vra != ''};
224 issues 'issues'     => qq{$posud_sql where datum_vra == ''};
225
226 $k->do(qq{
227 update items
228 join issues on items.itemnumber=issues.itemnumber
229 set onloan = date_due, datelastborrowed = issuedate
230 });
231
232
233 }; # import->{issues}
234
235
236 my $sth        = $k->prepare(qq{ select itemnumber,barcode from items });
237 my $sth_update = $k->prepare(qq{ update items set barcode=? where itemnumber=? });
238
239 $sth->execute;
240 warn "update ", $sth->rows, " barcodes";
241
242 my $ean = CheckDigits('ean');
243
244 while( my $row = $sth->fetchrow_hashref ) {
245         $sth_update->execute( $ean->complete( sprintf('%012d', $row->{barcode}) ), $row->{itemnumber} );
246 }
247
248 $k->commit;