package BookPub::Import::Importer::RedShelf::Version13;

use strict;
use warnings;

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

sub _fieldMap {
    {
        dateBegin            => 'B',
        dateEnd              => 'B',
        rPurchaseType        => 'U',
        purchaseType         => 'U',
        rProductType         => 'T',
        rAuthor              => 'H',
        rTitle               => 'G',
        rIsbn13              => 'I',
        rImprint             => 'F',
        rInstitution         => 'E',
        rState               => 'AA',
        rPostalCode          => 'AB',
        countryCode          => 'AC',
        rUnits               => 'O',
        rListPrice           => 'K',
        rPurchasePrice       => 'L',
        rDiscount             => 'M',
        rRevenue             => 'O',
        rTaxAmount           => 'Q',
        rFee                 => 'N',
        isFree               => 'AF',
        rDuration            => 'U',
        rDeliveryMethod      => 'X',
        rPromoCode           => 'AI',
        rChannel             => 'AK',
        rTransactionCategory => 'S',
    }
}

sub _unverifiedFieldMap {
    {
        subtotal           => 'A',
        unusedIsbn         => 'J',
        status             => 'S',
        publisherNetCredit => 'R',
    }
}

sub dateBegin {
    my $self = shift;

    my $dateBegin =  $self->_getByFieldName('dateBegin');
    $dateBegin =~ s/(\d{4}[.\/-]\d{2}[.\/-]\d{2}).*/$1/;

    return $dateBegin;
}

sub dateEnd {
    my $self = shift;

    my $dateEnd =  $self->_getByFieldName('dateEnd');
    $dateEnd =~ s/(\d{4}[.\/-]\d{2}[.\/-]\d{2}).*/$1/;

    return $dateEnd;
}

sub rUnits {
    my $self = shift;

    my $revenue = $self->_getByFieldName('rRevenue');
    return $revenue < 0 ? return -1 : return 1;
}

sub isFree {
    my $self = shift;

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

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

    return 'N';
}

sub _isSaleRec {
    my $self = shift;

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

    return 0 if $isFree =~ /^STUDENT$/i && !$rListPrice && !$publisherNetCredit;

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


1;