package BookPub::Import::Importer::RedShelf::Version11;

use strict;

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

sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        11 => {
            dateBegin            => 'B',
            rPurchaseType        => 'U',
            purchaseType         => 'U',
            rProductType         => 'T',
            rAuthor              => 'H',
            rTitle               => 'G',
            rIsbn13              => 'I',
            rImprint             => 'F',
            rInstitution         => 'E',
            rState               => 'AA',
            rPostalCode          => 'AB',
            countryCode          => 'AC',
            rListPrice           => 'K',
            rPurchasePrice       => 'L',
            rDiscount            => 'M',
            rRevenue             => 'O',
            rTaxAmount           => 'Q',
            rFee                 => 'N',
            isFree               => 'AF',
            rDuration            => 'U',
            rDeliveryMethod      => 'X',
            rPromoCode           => 'AI',
            rChannel             => 'AJ',
            rTransactionCategory => 'S',
        },
        12 => {
            dateBegin            => 'B',
            rPurchaseType        => 'U',
            purchaseType         => 'U',
            rProductType         => 'T',
            rAuthor              => 'H',
            rTitle               => 'G',
            rIsbn13              => 'I',
            rImprint             => 'F',
            rInstitution         => 'E',
            rState               => 'AA',
            rPostalCode          => 'AB',
            countryCode          => 'AC',
            serviceProductID     => 'J',
            rListPrice           => 'K',
            rPurchasePrice       => 'L',
            rDiscount            => 'M',
            rRevenue             => 'O',
            rTaxAmount           => 'Q',
            rFee                 => 'N',
            isFree               => 'AF',
            rDuration            => 'U',
            rDeliveryMethod      => 'X',
            rPromoCode           => 'AI',
            rChannel             => 'AK',
            rTransactionCategory => 'S',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub _unverifiedFieldMap {
    my $self = shift;

    # subtotal and unusedIsbn are used in isSaleRec
    my %fieldMap = (
        11 => {
            subtotal           => 'A',
            unusedIsbn         => 'J',
            status             => 'S',
            publisherNetCredit => 'R',
        },
        12 => {
            subtotal           => 'A',
            unusedIsbn         => 'J',
            status             => 'S',
            publisherNetCredit => 'R',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub rServiceName { 'RedShelf' }

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

    if ( $type =~ /^(?:accesscode|digital)$/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)$/i ) { return 'eBook' }
    elsif ( lc($type) eq 'print' ) { return 'POD' }
}

sub rChannel {
    my $self    = shift;
    my $channel = $self->_getByFieldName('rChannel');

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

    return $channel;
}

sub rState {
    my $self = shift;

    my $state = $self->_getByFieldName('rState');
    $state = 'Alabama' if $state =~ /^Alaba$/i;

    return $state;
}

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 rListPrice {
    my $self = shift;

    my $rListPrice = $self->_getByFieldName('rListPrice') || 0;
    $rListPrice = $self->_correctFormat($rListPrice);

    return $rListPrice;
}

sub rRevenue {
    my $self = shift;

    my $rRevenue = $self->_getByFieldName('rRevenue') || 0;
    $rRevenue = $self->_correctFormat($rRevenue);

    return $rRevenue;
}

sub rPurchasePrice {
    my $self = shift;

    my $rPurchasePrice = $self->_getByFieldName('rPurchasePrice') || 0;
    $rPurchasePrice = $self->_correctFormat($rPurchasePrice);

    return $rPurchasePrice;
}

sub publisherNetCredit {
    my $self = shift;

    my $publisherNetCredit = $self->_getByFieldName('publisherNetCredit') || 0;
    $publisherNetCredit = $self->_correctFormat($publisherNetCredit);

    return $publisherNetCredit;
}

sub rTaxAmount {
    my $self = shift;

    my $rTaxAmount = $self->_getByFieldName('rTaxAmount') || 0;
    $rTaxAmount = $self->_correctFormat($rTaxAmount);

    return $rTaxAmount;
}

sub _isSaleRec {
    my $self = shift;

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

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

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



1;
