package BookPub::Import::Importer::Dragonmount::Version3;

use strict;
use Date::Calc qw(Add_Delta_Days);

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

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

sub _fieldMap {
    {
        dateBegin      => 'B',
        rAuthor        => 'F',
        rTitle         => 'D',
        rIsbn13        => 'C',
        rState         => 'I',
        rPostalCode    => 'G',
        countryCode    => 'I',
        rListPrice     => 'K',
        rPurchasePrice => 'J',
        rRevenue       => 'L',
        rTaxAmount     => 'M',
        rTaxUnitPrice  => 'N',
    };
}

sub rUnits                 { shift->units }
sub rProductType           { shift->productType }
sub rListPriceCurrency     { shift->currencyCode }
sub rPurchasePriceCurrency { shift->currencyCode }

sub rServiceName {
    return BookPub::Tracker::Service::GetDefaultServiceName( service_id => shift->serviceID );
}

sub _isSaleRec {
    my $self = shift;
    return $self->rRevenue && $self->rTitle;
}

sub dateBegin {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin') || '';
    $date =~ s/^(\d{4}-\d{1,2}-\d{1,2}).*$/$1/;
    return $date;
}

sub rState {
    my $self = shift;

    my $rState = $self->_getByFieldName('rState') || '';
    if ( my $oPostal = Common::Postal->new( $self->countryCode() ) ) {
        $rState = $rState =~ /^[a-z]{2}$/i ? $oPostal->getName($rState) : $rState;
    }

    return $rState;
}


1;
