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