package BookPub::Import::Importer::OneClickdigital::Version4;

use strict;

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

sub _fieldMap {
    {
        dateBegin            => 'E',
        dateEnd              => 'F',
        rProductType         => 'J',
        priceTypeQualifierID => 'C',
        rAuthor              => 'I',
        rTitle               => 'H',
        rIsbn13              => 'L',
        countryCode          => 'D',
        serviceProductID     => 'K',
        rUnits               => 'P',
        rAdjustments         => 'M',
        rCOGS                => 'O',
        rRevenue             => 'Q',
        currencyCode         => 'C',
    };
}

sub rListPriceCurrency     { shift->currencyCode }
sub rPurchasePriceCurrency { }

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

    if ( lc($type) eq 'ebook' ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    }

    return;
}

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

    if ( $id =~ /^Pan Macmillan$/i ) {
        return BookPub::DB::Item::PriceTypeQualifier::kCorporate;
    }

    return;
}

sub currencyCode {
    my $self     = shift;
    my $currency = $self->_getByFieldName('currencyCode');

    if ( $currency =~ /^Pan Macmillan$/i ) {
        return "GBP";
    } elsif ( $currency =~ /^Bloomsbury Publishing$/i ) {
        return "GBP";
    } elsif ( $currency =~ /^Pan Macmillan Australia$/i ) {
        return "AUD";
    }

    $self->fail("Unable to determine currency code for $currency");
}

sub rListPrice {
    my $self = shift;

    my $price;
    my $revenue  = $self->revenue;
    my $units    = $self->units;
    my $discount = ( 100 - $self->_getByFieldName('rCOGS') ) / 100;

    if ( $units != 0 && $discount != 1 ) {
        my $unitPrice = $revenue / $units;
        $price = $unitPrice / ( 1 - $discount );
    }

    return $price;
}

1;
