package BookPub::Import::Importer::Copia::Version2;

use strict;

use lib '/app/tools/common/lib';
use Common::Util;

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

sub _fieldMap {
    {
        currencyCode       => 'G',
        countryCode        => 'I',
        rState             => 'J',
        rPostalCode        => 'N',
        dateBegin          => 'AA',
        rProductType       => 'AV',
        isbn               => 'AF',
        rTitle             => 'AI',
        rAuthor            => 'AJ',
        rImprint           => 'AN',
        rUnits             => 'AO',
        rListPrice         => 'AP',
        rListPriceCurrency => 'AT',
        rPurchasePrice     => 'AU',
        rDiscount          => 'AQ',
    };
}

sub _headerIdentifier { ( rTitle => 'Product title' ) }
sub purchaseType      { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub priceType         { 'agency' }

sub rPostalCode {
    my $self = shift;
    my $code = $self->_getByFieldName('rPostalCode');

    if ( $code =~ m/^\-(\d{5})$/ ) {
        $code = $1;
    }
    return $code;
}

sub rRevenue {
    my $self = shift;

    my $listPrice = $self->_getByFieldName('rListPrice');
    my $discount  = $self->_getByFieldName('rDiscount');

    return $listPrice - ( $listPrice * $discount );
}

sub productType {
    my $self = shift;

    my $productType = $self->_getByFieldName('rProductType');

    if ( lc($productType) eq 'ebook' ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    } else {
        $self->fail("Unknown product type: $productType");
        return undef;
    }
}

###
1;    # Play nicely.
###
