package WebPAC::Input::Excel;

use warnings;
use strict;

use WebPAC::Input;
use Spreadsheet::ParseExcel;
use Spreadsheet::ParseExcel::Utility qw/int2col/;

=head1 NAME

WebPAC::Input::Excel - support for Microsoft Excell and compatibile files

=head1 VERSION

Version 0.02

=cut

our $VERSION = '0.02';


=head1 SYNOPSIS

Open Microsoft Excell, or compatibile format (for e.g. from OpenOffice.org
or Gnuemeric) in C<.xls> format.

 my $isis = new WebPAC::Input::Excel();
 $isis->open( path => '/path/to/workbook.xls' );

=head1 FUNCTIONS

=head2 open_db

Returns handle to database and size

  my ($db,$size) = $open_db(
  	path => '/path/to/workbook.xls'
	worksheet => 'name of sheet',
	from => 42,
	to => 9999,
  }

C<worksheet> is case and white-space insensitive name of worksheet in Excel
file to use. If not specified, it will use first worksheet in file.

C<from> and C<to> specify row numbers to start and finish import.

=cut

my $sheet;
my ($from,$to);

sub open_db {
	my $self = shift;

	my $arg = {@_};

	my $log = $self->_get_logger();

	$log->logdie("can't open excel file $arg->{path}: $!") unless (-r $arg->{path} && -f $arg->{path});

	my $workbook = Spreadsheet::ParseExcel::Workbook->Parse($arg->{path});

	my ($size, $wanted_worksheet);

	if ($wanted_worksheet = $arg->{worksheet}) {
		my $name;
		do {
			$sheet = shift @{ $workbook->{Worksheet} };
			$log->logdie("can't find sheet '$wanted_worksheet' in $arg->{path}\n") unless (defined($sheet));
			$name = $sheet->{Name};
			$name =~ s/\s\s+/ /g;
		} until ($name =~ m/^\s*\Q$wanted_worksheet\E\s*$/i);

	} else {

		$sheet = shift @{ $workbook->{Worksheet} };
	
	}

	$from = $arg->{from} || $sheet->{MinRow};
	$to = $arg->{to} || $sheet->{MaxRow};

	$size = $to - $from;

	$log->warn("opening Excel file '$arg->{path}', using ",
		$wanted_worksheet ? '' : 'first ',
		"worksheet: $sheet->{Name} [$size rows]"
	);

	return (42, $size);
}

=head2 fetch_rec

Return record with ID C<$mfn> from database

  my $rec = $self->fetch_rec( $db, $mfn );

}

=cut

sub fetch_rec {
	my $self = shift;

	my (undef, $mfn) = @_;

	my $log = $self->_get_logger();

	$log->logdie("can't find sheet hash") unless (defined($sheet));
	$log->logdie("sheet hash isn't Spreadsheet::ParseExcel::Worksheet") unless ($sheet->isa('Spreadsheet::ParseExcel::Worksheet'));

	my $rec;

	my $row = $from + $mfn - 1;

	$log->debug("fetch_rec( $mfn ) row: $row cols: ",$sheet->{MinCol}," - ",$sheet->{MaxCol});

	foreach my $col ( $sheet->{MinCol} ... $sheet->{MaxCol} ) {
		if (my $v = $sheet->{Cells}->[$row]->[$col]->{Val}) {
			$rec->{ int2col($col) } = $v;
		}
	}

	# add mfn only to records with data
	$rec->{'000'} = [ $mfn ] if ($rec);
	
	return $rec;
}

=head1 AUTHOR

Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>

=head1 COPYRIGHT & LICENSE

Copyright 2005-2006 Dobrica Pavlinusic, All Rights Reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

1; # End of WebPAC::Input::Excel
