package BookPub::Import::Importer::Follett_Higher_Education::Version20;

use strict;

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

sub _headerIdentifier { ( rTitle => 'Title' ) }

sub _fieldMap {
    {
        dateBegin            => 'V',
        rAuthor              => 'E',
        rTitle               => 'F',
        rIsbn10              => 'L',
        rIsbn13              => 'K',
        rInstitution         => 'Z',
        rState               => 'AB',
        rPostalCode          => 'AC',
        serviceProductID     => 'I',
        rReturns             => 'T',
        rSales               => 'T',
        rListPrice           => 'R',
        rListPriceCurrency   => 'AG',
        rRevenue             => 'S',
        currencyCode         => 'AG',
        isFree               => 'U',
        rDeliveryMethod      => 'Q',
        rChannel             => 'W',
        rTransactionCategory => 'U',
        rComments            => 'P'
    }
}

sub _unverifiedFieldMap {
    {
        transactionType => 'T',
    }
}

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub _dateFormat                { [ 'us', 'iso', 'numerical' ] }
sub rServiceName               { 'Follett-Higher-Education' }

sub purchaseType           { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType            { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rListPriceCurrency     { shift->_getByFieldName("rListPriceCurrency") }
sub currencyCode           { shift->_getByFieldName("currencyCode") }

sub rUnits {
    my $self    = shift;
    my $revenue = $self->_getByFieldName('rRevenue');
    my $return  = $self->rReturns();

    if ( $revenue < 0 ) {
        return -1;
    }
    if ( $revenue == 0 && $return == 1 ) {
        return -1;
    }

    return 1;
}

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

    if ( $type eq 'Refund' || $type eq 'Refund Correction' ) {
        return 1;
    }
}

sub rSales {
    my $self = shift;

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

    if ( $type eq 'Sale' || $type eq 'Sale Correction' || $type eq 'Refund Reversal' ) {
        return 1;
    }
}

sub isFree {
    my $self = shift;

    my $category = $self->_getByFieldName('rTransactionCategory');
    my $revenue  = $self->_getByFieldName('rRevenue');

    if ( $category eq 'includED instructor' && $revenue == 0 ) {
        return 'Y';
    }

    return 'N';
}

1;
