package BookPub::Import::Importer::CambridgeBusinessPublishers::v1;

use strict;
use warnings;

use lib '/app/tools/common/lib';

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

sub _fieldMap {
    {
        rServiceName             => 'B1',
        dateBegin                => 'A',
        dateEnd                  => 'B',
        rPurchaseType            => 'L',
        purchaseType             => 'L',
        rProductType             => 'J',
        rFormat                  => 'K',
        priceType                => 'U',
        priceTypeQualifierID     => 'V',
        rAuthor                  => 'G',
        rTitle                   => 'E',
        rSubtitle                => 'F',
        rIsbn13                  => 'D',
        rImprint                 => 'I',
        rInstitution             => 'P',
        rState                   => 'M',
        rPostalCode              => 'N',
        countryCode              => 'O',
        rUnits                   => 'W',
        rUnitPrice               => 'AD',
        rListPrice               => 'Z',
        rListPriceCurrency       => 'AA',
        rNativeListPrice         => 'Z',
        rNativeListPriceCurrency => 'AA',
        rPurchasePrice           => 'X',
        rPurchasePriceCurrency   => 'Y',
        rDiscount                => 'AB',
        rRevenue                 => 'AG',
        currencyCode             => 'H1',
        rTaxAmount               => 'AC',
        rDuration                => 'Q',
        rDistributor             => 'C',
        rChannel                 => 'U',
        rModel                   => 'T',
        rTransactionCategory     => 'R',
    }
}

sub _unverifiedFieldMap {
    {
        headerUnits   => 'D1',
        headerRevenue => 'F1',
    }
}

sub priceTypeQualifierID {
    my $self         = shift;
    my $purchaseType = $self->_getByFieldName('priceTypeQualifierID');

    if ( $purchaseType =~ /^(?:Library Sale|06|Corporate)$/i ) {
        return BookPub::DB::Item::PriceTypeQualifier::kCorporate;
    }

    return undef;
}

sub isFree {
    my $self = shift;

    return !($self->rRevenue + 0) && ($self->rUnits + 0) ? 'Y' : 'N';
}

sub _headerIdentifier { dateBegin => 'Sale Start Date' }

sub _useIsSaleRec     { 1 }
sub _dateFormat       { [ 'numerical_year_first', 'iso' ] }
sub _validTabNames    { ['RS Digital Sales Template'] }

sub _isSaleRec {
    my $self = shift;

    my $sheet = $self->sheetname();
    return unless grep {/$sheet/} @{&_validTabNames};
    return 1 if $self->rUnits() || $self->rRevenue();
}

1;
