package BookPub::Import::Importer::DeMarque::v2;

use strict;

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

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

sub _fieldMap {
    {
        # Header
        currencyCode => 'H1',

        # Body
        rPurchaseType => 'M',
        priceType     => 'S',
        rDistributor  => 'C',
        rServiceName  => 'D',

        conversionRate => 'AB',

        countryCode => 'P',
        rState      => 'N',
        rPostalCode => 'O',

        dateBegin => 'A',
        dateEnd   => 'B',

        rProductType => 'K',
        rFormat      => 'L',
        isbn         => 'E',
        rTitle       => 'F',
        rSubtitle    => 'G',
        rAuthor      => 'H',
        rImprint     => 'J',

        rUnits                   => 'T',
        rListPrice               => 'W',
        rListPriceCurrency       => 'X',
        rPurchasePrice           => 'U',
        rPurchasePriceCurrency   => 'V',
        rTaxAmount               => 'Z',
        rDiscount                => 'Y',
        rUnitPrice               => 'AC',
        rRevenue                 => 'AD',
        rDuration                => 'R',
        rInstitution             => 'Q',
        rNativeListPrice         => 'W',
        rNativeListPriceCurrency => 'X',
    }
}

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

sub rListPriceCurrency {
    my $self              = shift;
    my $listPriceCurrency = $self->_getByFieldName('rListPriceCurrency');
    my $revenueCurrency   = $self->_getByFieldName('currencyCode');

    if ( $self->listPriceRequired && $listPriceCurrency && $listPriceCurrency ne $revenueCurrency ) {
        $listPriceCurrency = $revenueCurrency;
    }

    return $listPriceCurrency;
}

sub rListPrice {
    my $self              = shift;
    my $listPrice         = $self->_getByFieldName('rListPrice');
    my $listPriceCurrency = $self->_getByFieldName('rListPriceCurrency');
    my $revenueCurrency   = $self->_getByFieldName('currencyCode');
    my $conversionRate    = $self->_getByFieldName('conversionRate');


    if ( $self->listPriceRequired && $listPriceCurrency && $listPriceCurrency ne $revenueCurrency && $listPrice != 0 ) {
        $listPrice *= $conversionRate;
    }

    return abs($listPrice);
}

1;
