package BookPub::Import::Importer::EBSCO::Version5;

use strict;
use warnings;

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

sub _headerIdentifier { ( dateBegin => 'TRANS DATE' ) }

sub _fieldMap {
    return {
        dateBegin            => 'C',
        rPurchaseType        => 'K',
        rAuthor              => 'G',
        rTitle               => 'F',
        rIsbn13              => 'E',
        rImprint             => 'A',
        rInstitution         => 'H',
        countryCode          => 'I',
        rUnits               => 'N',
        rAdjustments         => 'K',
        rListPrice           => 'M',
        rListPriceCurrency   => 'H7',
        rDiscount            => 'T',
        rRevenue             => 'U',
        currencyCode         => 'H7',
        rDuration            => 'L',
        rPromoCode           => 'R',
        rModel               => 'L',
        rTransactionCategory => 'K',
    }
}

sub _unverifiedFieldMap {
    return {
        adjListPrice => 'S'
    }
}

sub rServiceName {
    return BookPub::Tracker::Service::GetDefaultServiceName( service_id => shift->serviceID );
}

sub rListPrice {
    my $self = shift;

    my $listPrice    = $self->_getByFieldName('rListPrice');      # M
    my $adjListPrice = $self->_getByFieldName('adjListPrice');    # S
    my $promoCode    = $self->_getByFieldName('rPromoCode');      # R
    my $rAdjustments = $self->_getByFieldName('rAdjustments');

    my $price = $promoCode =~ /^SUPC$/i ? $adjListPrice : $listPrice;
    if ( $rAdjustments =~ /^ADJ$/i ) {
        $price = $listPrice if $adjListPrice == 0;
    }

    return $price;
}

sub purchaseType {
    my $self = shift;

    my $duration = $self->_getByFieldName('rDuration')         || '';
    my $purchaseType = $self->_getByFieldName('rPurchaseType') || '';

    if ( $purchaseType =~ /^adj$/i ) {
        return BookPub::DB::Item::Sale::kPurchaseTypeLoan     if $duration =~ /^pdl(?:1|7|14|28)$/i;
        return BookPub::DB::Item::Sale::kPurchaseTypeDownload if $duration =~ /^1b(?:1|3|u)u$/i;
    }

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

sub rAdjustments {
    my $self = shift;

    my $adjustments = $self->_getByFieldName('rAdjustments');
    return $adjustments =~ /^ADJ$/i ? 1 : 0;
}


1;
