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