package BookPub::Import::Importer::Gardners::Version9;

use strict;

use lib '/app/tools/bookpub/lib';
use base 'BookPub::Import::Importer::Gardners::Version6';

sub _fieldMap {
    {
        # Detail
        dateBegin          => 'O',
        rPurchaseType      => 'J',
        rTitle             => 'B',
        rIsbn13            => 'A',
        rInstitution       => 'N',
        countryCode        => 'M',
        rUnits             => 'C',
        rListPrice         => 'E',
        rListPriceCurrency => 'D',
        rDiscount          => 'I',
        rRevenue           => 'H',
        currencyCode       => 'D',
        rTaxUnitPrice      => 'F',
        rModel             => 'J',
    };
}

sub _unverifiedFieldMap {
    {
        # Header
        defaultDateBegin => 'A1',    # Begin date for all items in the file if not explicitly specified in the record
                                     # Data
        priceLessTax     => 'G',
    };
}

sub _dateFormat { [ 'us', 'numerical_year_first' ] }

sub dateBegin {
    my $self = shift;
    my $date = $self->_getByFieldName('dateBegin');

    return $date;
}

sub priceTypeQualifierID { BookPub::DB::Item::PriceTypeQualifier::kCorporate }    # Library

sub rTaxUnitPrice {
    my $self         = shift;
    my $totalPrice   = $self->_getByFieldName('rTaxUnitPrice');
    my $priceLessTax = $self->_getByFieldName('priceLessTax');

    if ( $totalPrice && $priceLessTax ) {
        return $totalPrice - $priceLessTax;
    } else {
        $self->fail("Missing either rTaxUnitPrice, $totalPrice, or priceLessTax $priceLessTax");
    }
}

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

    if ( !$code || $code eq '' ) {
        return 'GB';
    } else {
        return $code;
    }
}

sub purchaseType {
    my $self = shift;
    my $type = $self->_getByFieldName('rPurchaseType');

    if (   $type eq 'RENTAL'
        || $type eq 'LIMITED CONCURRENT USER'
        || $type eq 'ANNUAL CONCURRENT ACCESS' ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan;
    } else {
        $self->fail("Unexpected purchase type: $type");
    }
}

1;
