package BookPub::Import::Importer::BISG::Version5;

use strict;

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

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

# This version has no header or footer.  All of the data is repeated for each sale row.
#

sub _fieldMap {
    {
        # Detail
        rPriceType         => 'BR',
        currencyCode       => 'AO',
        countryCode        => 'AL',
        dateBegin          => 'S',
        rProductType       => 'AD',
        rFormat            => 'AD',
        rIsbn13            => 'U',
        rTitle             => 'X',
        rAuthor            => 'Y',
        rImprint           => 'AC',
        rUnits             => 'AH',
        rSales             => 'AF',
        rReturns           => 'AG',
        rListPrice         => 'BQ',
        rListPriceCurrency => 'AO',
        rDiscount          => 'AP',
        rRevenue           => 'BM',
    };
}

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub _headerIdentifier          { ( rTitle => 'Product Title' ) }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeDownload }

sub priceTypeID {
    my $self = shift;
    my $type = $self->_getByFieldName('rPriceType');

    return $type;
}

sub productType {
    my $self = shift;
    my $type = lc( $self->rProductType ) || return;

    if ( $type eq 'other' || $type eq 'e101' || $type eq 'e107' ) {
        $type = BookPub::DB::Item::Sale::kProductTypeEBook;
    } elsif ( $type eq 'a103' || $type eq 'a106' ) {
        $type = BookPub::DB::Item::Sale::kProductTypeAudioBook;
    }

    return $type;
}

sub rReturns {
    my $self = shift;
    my $returns = $self->SUPER::rReturns() || return;

    return $returns < 0 ? abs($returns) : 0;
}

sub rSales {
    my $self = shift;
    my $sales = $self->SUPER::rSales() || return;

    return $sales >= 0 ? $sales : 0;
}

sub _getDateBegin {
    my $date = shift->SUPER::_getDateBegin() || return;
    my $d = substr( $date, 0, 10 );

    return $d;
}

1;
