insert email into borrower_message_preferences
[ferlib2koha.git] / ferlib2koha.pl
old mode 100755 (executable)
new mode 100644 (file)
index fdec3c7..34b6502
@@ -4,14 +4,15 @@ use strict;
 use DBI;
 use Data::Dump qw/dump/;
 use Algorithm::CheckDigits;
+use Text::Unaccent;
 
-# sudo apt-get install libdbd-sqlite3-perl libdbd-csv-perl
+# sudo apt-get install libdbd-sqlite3-perl libdbd-csv-perl libtext-unaccent-perl
 
 $|++;
 
 my $import = {
        borrowers => $ENV{FULL} || 0,
-       issues    => $ENV{FULL} || 1,
+       issues    => $ENV{FULL} || 0,
        reserves  => $ENV{FULL} || 0,
        barcode   => $ENV{FULL} || 0,
 };
@@ -26,15 +27,25 @@ sub lookup {
        my ( $dbh, $t, $k, $v, $s, $default ) = @_;
        my $hash;
        my $key = "$t $k $v";
+       my $max = $v =~ s/max\((\w+)\)/$1/;
        if ( exists $lookup->{$key} ) {
                $hash = $lookup->{$key};
        } else {
                warn "# select $k,$v from $t";
                my $sth = $dbh->prepare( "select $k,$v from $t" );
                $sth->execute;
+               my @non_unique;
                while (my $row = $sth->fetchrow_hashref() ) {
+                       if ( ! $max && exists $hash->{ $row->{$k} } ) {
+                               push @non_unique, $row->{$k};
+                               next;
+                       }
                        $hash->{ $row->{$k} } = $row->{$v};
                }
+               if ( @non_unique ) {
+                       warn "# remove non-unique ", dump(@non_unique);
+                       delete $hash->{$_} foreach @non_unique;
+               }
                $lookup->{$key} = $hash;
                warn "# lookup $key = ",dump($hash);
        }
@@ -341,7 +352,7 @@ sub reserves {
                        $row->{itemnumber} ? 'W' : undef;
 
                $row->{borrowernumber} = lookup($k, 'borrowers', 'cardnumber' => 'borrowernumber', $row->{borrowernumber});
-               $row->{biblionumber}   = lookup($k, 'biblioitems', 'collectionvolume' => 'biblionumber', $row->{biblionumber});
+               $row->{biblionumber}   = lookup($k, 'biblioitems', 'collectionvolume' => 'max(biblionumber)', $row->{biblionumber});
                $row->{itemnumber}     = lookup($k, 'items', 'barcode' => 'itemnumber', $row->{itemnumber});
 
                insert $table => $row;
@@ -390,6 +401,56 @@ while( my $row = $sth->fetchrow_hashref ) {
 } # import->{barcode}
 
 
+sub aqbooksellers {
+       my ($sql) = @_;
+       my $sth = $f->prepare($sql);
+       $sth->execute;
+
+       $insert = undef;
+
+       while (my $row = $sth->fetchrow_hashref ) {
+               insert 'aqbooksellers' => $row;
+       }
+}
 
+sql $k => qq{
+delete from aqbooksellers;
+alter table aqbooksellers auto_increment = 1;
+};
+aqbooksellers qq{
+select
+       ozn_nacdob as notes,
+       opis_nacdob as name,
+       'HRK' as currency
+from nacdob
+};
+
+my $sth = $k->prepare(qq{
+select
+       borrowernumber,
+       firstname,
+       surname,
+       cardnumber
+from borrowers
+where categorycode = 'D' and email is null
+group by firstname,surname
+having count(*) = 1 -- update only unique users
+});
+my $update_borrower = $k->prepare(qq{
+update borrowers
+set email = ?, userid = ?, cardnumber = ?
+where borrowernumber = ?
+});
+$sth->execute;
+while (my $row = $sth->fetchrow_hashref) {
+       my $email = lc unac_string( 'utf-8', $row->{firstname} . '.' . $row->{surname} . '@fer.hr' );
+       if ( my $nick = lookup($u, 'users', 'email', 'nick', $email, undef) ) {
+               my $cardnumber = lookup($u, 'users', 'email', 'jmbag', $email, undef) || next;
+               warn "# FIXED nick $row->{borrowernumber} $nick $email $cardnumber\n";
+eval {
+               $update_borrower->execute( $email, $nick, $cardnumber, $row->{borrowernumber} );
+};
+       }
+}
 
 $k->commit;