package BookPub::Import::Importer::Christianbook::Version3;

use strict;

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

sub _fieldMap {
    {
        rPriceType         => 'BR',
        currencyCode       => 'I',
        conversionRate     => 'BP',
        countryCode        => 'AL',
        dateBegin          => 'O',
        rProductType       => 'AD',
        rIsbn13            => 'U',
        rTitle             => 'X',
        rAuthor            => 'Y',
        rImprint           => 'AC',
        rSales             => 'AF',
        rReturns           => 'AG',
        rListPrice         => 'AM',
        rListPriceCurrency => 'AO',
        rPurchasePrice     => 'BQ',
        rDiscount          => 'AP',
        rUnitPrice         => 'AM',
        rRevenue           => 'BC',
    };
}

sub _headerIdentifier { ( rTitle => 'Product_Title' ) }

sub _dateFormat { ['numerical_year_first'] }

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

    if ( $product eq 'E101' || length($product) == 0 || undef $product ) {
        return BookPub::DB::Item::Sale::kProductTypeEBook;
    } else {
        $self->fail("Unknown product type '$product'");
    }
}

sub rPurchasePrice {
    my $self  = shift;
    my $price = $self->_getByFieldName('rPurchasePrice');
    $price =~ s/,|\$//g;

    return $price;
}

# sometimes they're passing in a whole date-time string, stripping the time off
sub _getDateBegin {
    my $self      = shift;
    my $dateBegin = $self->_getByFieldName('dateBegin');

    if ( $dateBegin =~ m/(\d{8})(\d{6})/ ) {
        return $1;
    }
    return $dateBegin;
}

sub _useIsSaleRec { 1 }

sub _isSaleRec {
    my $self = shift;

    return $self->rTitle && ( $self->rIsbn10 || $self->rIsbn13 ) && $self->dateBegin;
}

sub rRevenue {
    my $self = shift;

    my $revenue = $self->SUPER::rRevenue();
    $revenue =~ s/,|\$//g;

    return $revenue;
}

sub rListPrice {
    my $self = shift;

    my $price = $self->SUPER::rListPrice();
    $price =~ s/\$//g;

    return $price;
}

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