package BookPub::Import::Importer::RedShelf::Version18;

use strict;

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

sub _fieldMap {
    {
        dateBegin            => 'B',
        rPurchaseType        => 'V',
        purchaseType         => 'V',
        rProductType         => 'U',
        rAuthor              => 'H',
        rTitle               => 'G',
        rIsbn13              => 'I',
        rImprint             => 'F',
        rInstitution         => 'E',
        rState               => 'AB',
        rPostalCode          => 'AC',
        countryCode          => 'AD',
        rClientProductID     => 'K',
        serviceProductID     => 'J',
        rListPrice           => 'L',
        rPurchasePrice       => 'M',
        rDiscount            => 'N',
        rRevenue             => 'P',
        rTaxAmount           => 'R',
        rFee                 => 'O',
        isFree               => 'AG',
        rDuration            => 'V',
        rDeliveryMethod      => 'Y',
        rPromoCode           => 'AJ',
        rChannel             => 'AL',
        rTransactionCategory => 'T',
    }
}

sub _unverifiedFieldMap {
    {
        subtotal           => 'A',
        unusedIsbn         => 'K',
        status             => 'T',
        publisherNetCredit => 'P', # PublisherCredit
        _user              => 'C',
        _name              => 'D',
    }
}

sub rServiceName { 'RedShelf' }

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


    if ( $type =~ /^(?:accesscode|digital|ebook|courseware)$/i ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    } elsif ( lc($type) eq 'print' ) {
        return BookPub::DB::Item::Sale::kProductTypePhysicalBook;
    } else {
        return $self->SUPER::productType();
    }
}

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

    if ( $type =~ /^(?:accesscode|digital|ebook|courseware)$/i ) { return 'eBook' }
    elsif ( lc($type) eq 'print' ) { return 'POD' }
}

sub rChannel {
    my $self    = shift;

    my $channel = $self->_getByFieldName('rChannel') || "";
    my $country = $self->countryCode() || "";

    $self->fail("Channel not found") unless $channel;

    if ($country !~ /^US/i && $self->filename =~ /^MacMillan Trade/i) {
        if ($channel =~ /^equitable access$/i) {
            $self->fail("Found non-US 'Equitable Access' sale. Country: '$country'");
        } elsif ($channel =~ /^highereducation$/i) {
            $self->fail("Found non-US 'HIGHEREDUCATION' sale. Country: '$country'");
        } elsif ($channel =~ /^inclusive$/i) {
            $self->fail("Found non-US 'INCLUSIVE' sale. Country: '$country'");
        }
    }

    return $channel;
}

sub rDiscount {
    my $self = shift;

    my $discount = $self->_getByFieldName('rDiscount') || "";;

    return $discount;
}

sub rUnits {
    my $self = shift;

    return $self->rRevenue < 0 ? -1 : 1;
}

sub isFree {
    my $self = shift;

    my $rListPrice         = $self->rListPrice();
    my $isFree             = $self->_getByFieldName('isFree') || '';
    my $publisherNetCredit = $self->publisherNetCredit();

    return 'Y' if !$self->rRevenue && !$self->rPurchasePrice;

    if ( $isFree =~ /^(PROF|TA)$/i && !$rListPrice && !$publisherNetCredit ) {
        return  'Y';
    }

    return 'N';
}

sub _isSaleRec {
    my $self = shift;

    my $rListPrice         = $self->rListPrice();
    my $isFree             = $self->_getByFieldName('isFree')             || '';
    my $publisherNetCredit = $self->publisherNetCredit();
    my $user               = $self->_getByFieldName("_user") || "";
    my $name               = $self->_getByFieldName("_name") || "";

    $self->fail("Contains consumer privacy data") if ($user || $name);

    return 0 if $isFree =~ /^STUDENT$/i && !$rListPrice && !$publisherNetCredit;
    return 0 unless $self->dateBegin && defined $self->rRevenue;

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



1;
