package BookPub::Import::Importer::BakerAndTaylor::Version7;

use strict;
use Date::Calc qw(Add_Delta_Days);

use lib '/app/tools/common/lib';
use Common::Util;

use lib '/app/tools/bookpub/lib';
use base 'BookPub::Import::Importer::BakerAndTaylor::Version5';

sub _fieldMap {
    {
        # Header
        currencyCode => 'H',

        # Detail
        rPriceType           => 'AA',
        countryCode          => 'Y',
        dateBegin            => 'E',
        rProductType         => 'G',
        rFormat              => 'G',
        isbn                 => 'K',
        rTitle               => 'N',
        rAuthor              => 'O',
        rUnits               => 'U',
        rSales               => 'S',
        rReturns             => 'T',
        rListPrice           => 'AR',
        rListPriceCurrency   => 'AB',
        rPurchasePrice       => 'Z',
        rDiscount            => 'AC',
        rRevenue             => 'AP',
        rComments            => 'G',
        rInstitution         => 'D',
        priceTypeQualifierID => 'G',
    };
}

sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }

# overloading this method from BISG::Version1.pm
sub rReturns { shift->_getByFieldName('rReturns') }

sub rPriceType {
    my $self = shift;

    my $type = $self->_getByFieldName('rPriceType');

    if ( lc($type) eq "01" ) {
        return 'rrp';
    } elsif ( lc($type) eq "41" ) {
        return 'agency';
    }
    return $self->fail( "Invalid priceType: " . $type );
}

sub _processHeader {
    my $self         = shift;
    my $currencyCode = $self->_getByFieldName('currencyCode');

    if ( $self->{_firstHeaderFound} ) {
        $self->{_currencyCode}     = $self->_getByFieldName('currencyCode');
        $self->{_firstHeaderFound} = undef;

    } elsif ( $currencyCode && $currencyCode eq 'Reporting currency' ) {
        $self->{_firstHeaderFound} = 1;
    }

    # Check to ensure we found the first header
    if ( $self->_isHeader() ) {
        $self->fail("No currency code found in the header") unless ( $self->{_currencyCode} );
    }
}

###
1;    # Play nicely.
###
