package BookPub::Import::Importer::OLF::Version7;

use strict;

use lib '/app/tools/bookpub/lib';
use base 'BookPub::Import::Importer::OLF::Base';

sub _fieldMap {
    {
        # Header
        dateEnd => 'M',

        # Data
        countryCode => 'A',
        rIsbn13     => 'H',
        rTitle      => 'I',
        rAuthor     => 'K',
        rUnits      => 'O',
        rListPrice  => 'R',
        rDiscount   => 'V',
        rRevenue    => 'X',
    };
}

sub currencyCode  { 'GBP' }
sub _dateFormat   { [ 'eur', 'iso' ] }
sub _useIsSaleRec { 1 }

# The sales file is a little odd.  The data comes in country blocks.  The first line of the
# data has the country and also the line after the last line of the block.  So when we find
# a country, we stuff it into $self->{_country} and use that value for all the lines with
# a blank country.  When we get a new country, we use that one.

sub countryCode {
    my $self    = shift;
    my $country = $self->_getByFieldName('countryCode');

    if ( length($country) == 0 ) {
        $country = $self->{_country};
    }

    if ( length($country) > 0 ) {
        $self->{_country} = $country;
    }

    my $countryObj = Common::Country->Get($country) || $self->fail("Unknown country '$country'");
    return $countryObj->alpha2();
}

sub _isSaleRec {
    my $self = shift;

    my $code = $self->_getByFieldName('countryCode');

    if ( !$code || $code eq '' || $code =~ m/please kindly issue/i ) {
        return 0;
    }

    return $self->SUPER::_isSaleRec();
}

1;
