#!/usr/bin/perl -w

use strict;
use DBI;
use Data::Dump qw/dump/;
use Algorithm::CheckDigits;

# sudo apt-get install libdbd-sqlite3-perl libdbd-csv-perl

$|++;

my $import = {
	borrowers => $ENV{FULL} || 0,
	issues    => $ENV{FULL} || 1,
	reserves  => $ENV{FULL} || 0,
	barcode   => $ENV{FULL} || 0,
};

my $f = DBI->connect("dbi:SQLite:dbname=knjiznica.sqlite","","", { RaiseError => 1, AutoCommit => 0 }) || die $DBI::errstr;
$f->{sqlite_unicode} = 1;
my $k = DBI->connect("dbi:mysql:database=koha_fer","","", { RaiseError => 1, AutoCommit => 0, mysql_enable_utf8 => 1 }) || die $DBI::errstr;
my $u = DBI->connect("dbi:CSV:","","", { RaiseError => 1, f_ext => '.csv', csv_sep_char => '|' }) || die $DBI::errstr;

our $lookup;
sub lookup {
	my ( $dbh, $t, $k, $v, $s, $default ) = @_;
	my $hash;
	my $key = "$t $k $v";
	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;
		while (my $row = $sth->fetchrow_hashref() ) {
			$hash->{ $row->{$k} } = $row->{$v};
		}
		$lookup->{$key} = $hash;
		warn "# lookup $key = ",dump($hash);
	}
	return unless length $s > 1;
	if ( ! exists $lookup->{$key}->{$s} ) {
		warn "ERROR: no $s in $key\n";
		return $default;
	} else {
		$lookup->{$key}->{$s};
	}
}

our $insert;
sub insert {
	my ( $table, $row ) = @_;
	$insert->{$table} ||= $k->prepare(
			"insert into $table ("
			. join(',', keys %$row)
			. ") values ("
			. join(',', map { '?' } keys %$row )
			. ")"
	);

	foreach my $c ( grep { /(date|timestamp)/ } keys %$row ) {
		$row->{$c} =~ s/^(\d\d)\.(\d\d)\.(\d\d\d\d)/$3-$2-$1/;
		$row->{$c} = undef if $row->{$c} eq ''; # XXX NULL dates istead of 0000-00-00
	}

	$insert->{$table}->execute( values %$row );
	warn "# inserted $table ", dump $row;

}

sub borrowers {
	my $sql = shift;
	my $sth = $f->prepare($sql);
	$sth->execute;

	$insert = undef;

	while (my $row = $sth->fetchrow_hashref ) {

#		warn dump $row;

		# poor man's (sqlite) outer join
		$row->{'country'} = lookup($f, 'drzava', 'ozn_drzava', 'naz_drzava', $row->{'country'}, $row->{contry} );
		$row->{'city'}    = lookup($f, 'mjesto', 'post_ozn', 'naz_mjesto', $row->{'city'}, $row->{city} );

		$row->{'B_country'} = lookup($f, 'drzava', 'ozn_drzava', 'naz_drzava', $row->{'B_country'}, $row->{B_country} );
		$row->{'B_city'}    = lookup($f, 'mjesto', 'post_ozn', 'naz_mjesto', $row->{'B_city'}, $row->{B_city} );

		$row->{'B_email'}  = lookup($f, 'clanost', 'ozn_vrclan||ozn_clan', 'email', $row->{'cardnumber'}, undef);
		$row->{'emailpro'} = lookup($f, 'clanost', 'ozn_vrclan||ozn_clan', 'email_k', $row->{'cardnumber'}, undef);

		$row->{'email'}    = lookup($u, 'users', 'jmbag', 'email', substr($row->{cardnumber},1), undef); # $row->{B_email} || $row->{emailpro} )

		$row->{'userid'}    = lookup($u, 'users', 'jmbag', 'nick', substr($row->{cardnumber},1), $row->{cardnumber});

		$row->{sex} =~ s/\x{17D}/F/;

		$row->{city} ||= '?'; # not null

		$row->{borrowernotes} = lookup($f, 'clanorg', 'ozn_vrclan||ozn_clan', 'napom_clan', $row->{'cardnumber'}, undef);

		$row->{title} = lookup($f, 'titula', 'ozn_titula', 'opis_titula', $row->{title}, $row->{title});

		$row->{dateexpiry} ||= '01.01.2020'; # next revision

		my $jmbg = delete $row->{jmbg};

		insert 'borrowers' => $row;

		insert 'borrower_attributes' => {
			borrowernumber => $k->{mysql_insertid},
			code => 'JMBG',
			attribute => $jmbg,
		} if $jmbg;

	}
}

sub sql {
	my ($db,$sql) = @_;
	foreach (split(/;/,$sql)) {
		next if m/^\s*$/;
		warn "# sql $_";
		$db->do($_);
	}
}

if ( $import->{borrowers} ) {

sql $k => qq{
delete from borrowers;
alter table borrowers auto_increment = 1;
delete from borrower_attributes;
alter table borrower_attributes auto_increment = 1;
};


borrowers qq{
select
  'S' as categorycode,
  'SRE' as branchcode,
  'S' || mbr_stud as cardnumber,
  prez_stud as surname,
  ime_stud as firstname,
  djevprezime as othernames,
  spol as sex,
  dat_rodj as dateofbirth,
--  ime_otac as ??,
--  ime_majka as ??,
  ozn_drzava_preb as country,
  post_ozn_preb as zipcode,
  post_ozn_preb as city,
  adr_preb as address,
  ozn_drzava_stan as B_country,
  post_ozn_stan as B_zipcode,
  post_ozn_stan as B_city,
  adr_stan as B_address,
  tel_stud as phone,
  dat_prava_do as dateexpiry,
--  aktivan as ??,
  jmbg_stud as jmbg
from studk
};

borrowers qq{
select
  'D' as categorycode,
  'SRE' as branchcode,
  'D' || sif_djel as cardnumber,
  ime_djel as firstname,
  prez_djel as surname,
  ozn_titula as title,
--  sif_orgjed (REF orgjed) as B_address,
--  sif_strsp_djel as ??,
  tel_djel as phonepro,
  dat_prekid_ro as dateexpiry,
--  tel_poduz as ??,
  fax_poduz as fax,
  ozn_drzava as country,
  post_ozn as zipcode,
  post_ozn as city,
  adr_stan as address,
  tel_stan as phone,
  jmbg_djel as jmbg
from djelat
};

borrowers qq{
select
  'O' as categorycode,
  'SRE' as branchcode,
  'O' || mbr_clan as cardnumber,
  ime_clan as firstname,
  prez_clan as surname,
  ozn_titula as title,
  zvanje_clan as ethnicity, -- XXX we are not allowerd by low to collect ethnicity
--  krat_poduz (REF poduz) as B_address,
  tel_poduz as B_phone,
--  fax_poduz as ??,
  ozn_drzava as country,
  post_ozn as zipcode,
  post_ozn as city,
  adr_stan as address,
  tel_stan as phone,
--  aktivan as ??,
  jmbg_clan as jmbg
from clost
};

my $sth_syspref = $k->prepare(qq{ update systempreferences set value=? where variable=? });
my $BorrowersTitles = join('|', @{ $k->selectcol_arrayref(qq{ select distinct title from borrowers where title != '' }) });
$sth_syspref->execute( $BorrowersTitles => 'BorrowersTitles' );
warn "# BorrowersTitles [$BorrowersTitles]";

} # import->{borrowers}


if ( $import->{issues} ) {

$k->do(qq{ update items set barcode=substr(barcode,1,12)*1 where length(barcode) = 13 }); # convert barcodes back for lookups to work

sub issues {
	my ($table,$sql) = @_;
	my $sth = $f->prepare($sql);
	$sth->execute;

	$insert = undef;

	while (my $row = $sth->fetchrow_hashref ) {
		$row->{borrowernumber} = lookup($k, 'borrowers', 'cardnumber' => 'borrowernumber', $row->{borrowernumber});
		$row->{itemnumber}     = lookup($k, 'items', 'barcode' => 'itemnumber', $row->{itemnumber});

		my $stat = {
			datetime => $row->{returndate} || $row->{issuedate},
			branch   => $row->{branchcode},
			value    => 0,
			type     => $row->{returndate} ? 'return' : 'issue',
		};
		$stat->{$_} = $row->{$_} foreach (qw( itemnumber borrowernumber ));

		$row->{date_due} ||= '31.12.2011';
		insert $table => $row;

		insert statistics => $stat;
	}
}

my $posud_sql = qq{
select
	ozn_vrclan||ozn_clan as borrowernumber,
	sif_primj as itemnumber,
	datum_do as date_due,
	'SRE' as branchcode, -- FIXME
	datum_vra as returndate,
	datum_pos as issuedate,
	datum_pos||' '||vrijeme_pos as timestamp
from posud
};

sql $k => qq{
delete from old_issues;
alter table old_issues auto_increment = 1;
delete from issues;
alter table issues auto_increment = 1;
};
issues 'old_issues' => qq{$posud_sql where datum_vra != ''};
issues 'issues'     => qq{$posud_sql where datum_vra == ''};

sql $k => qq{
update items
join issues on items.itemnumber=issues.itemnumber
set onloan = date_due, datelastborrowed = issuedate
;
update items set datelastseen = datelastborrowed
;
create temporary table old_returndate as
select itemnumber,max(returndate) as returndate
from old_issues
group by itemnumber,returndate
;
update items
join old_returndate on items.itemnumber=old_returndate.itemnumber
set datelastseen = returndate
where returndate > datelastseen or datelastseen is null
;
create temporary table old_issuedate as
select itemnumber,max(issuedate) as issuedate
from old_issues
group by itemnumber,issuedate
;
update items
join old_issuedate on items.itemnumber=old_issuedate.itemnumber
set datelastborrowed = issuedate
where datelastborrowed is null
;
update old_issues set timestamp = returndate
};

}; # import->{issues}


if ( $import->{reserves} ) {

sql $k => qq{
delete from reserves;
alter table reserves auto_increment = 1;
delete from old_reserves;
alter table old_reserves auto_increment = 1;
};

sub reserves {
	my ($table, $sql) = @_;
	my $sth = $f->prepare($sql);
	$sth->execute;

	$insert = undef;

	while (my $row = $sth->fetchrow_hashref ) {

		$row->{found} =
			$row->{found}      ? 'F' : 
			$row->{itemnumber} ? 'W' : undef;

		$row->{borrowernumber} = lookup($k, 'borrowers', 'cardnumber' => 'borrowernumber', $row->{borrowernumber});
		$row->{biblionumber}   = lookup($k, 'biblioitems', 'collectionvolume' => 'biblionumber', $row->{biblionumber});
		$row->{itemnumber}     = lookup($k, 'items', 'barcode' => 'itemnumber', $row->{itemnumber});

		insert $table => $row;
	}
}

my $sql_reserves = qq{
select
	ozn_vrclan||ozn_clan as borrowernumber,
	datum_rezerv as reservedate,
	sif_naslov as biblionumber,
	'a' as constrainttype,
	'SRE' as branchcode,
	datum_obavijest as notificationdate,
	datum_ponist as cancellationdate,
	2 - prior_rezerv as priority,
	datum_posud as found, -- F|W
	datum_rezerv||' '||vrijeme_rezerv as timestamp,
	sif_primj as itemnumber,
	datum_obavijest as waitingdate,
	vrijedi_do as expirationdate,
	0 as lowestPriority
from rezerv
};

reserves 'old_reserves' => qq{$sql_reserves where datum_opoziv != '' or  datum_ponist != '' or  datum_posud != ''};
reserves 'reserves'     => qq{$sql_reserves where datum_opoziv == '' and datum_ponist == '' and datum_posud == ''};

} # $import->{reserves}


if ( $import->{barcode} ) { # XXX must be last!

my $sth        = $k->prepare(qq{ select itemnumber,barcode from items where length(barcode) < 13 });
my $sth_update = $k->prepare(qq{ update items set barcode=? where itemnumber=? });

$sth->execute;
warn "update ", $sth->rows, " barcodes";

my $ean = CheckDigits('ean');

while( my $row = $sth->fetchrow_hashref ) {
	$sth_update->execute( $ean->complete( sprintf('%012d', $row->{barcode}) ), $row->{itemnumber} );
}

} # import->{barcode}




$k->commit;
