package BookPub::Import::Importer::Follett_Higher_Education::Version5;

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

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

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

sub _fieldMap {
    {
        rState      => 'T',
        rPostalCode => 'U',
        dateBegin   => 'A',
        rIsbn13     => 'I',
        rTitle      => 'O',
        rAuthor     => 'N',
        rSales      => 'G',
        rReturns    => 'G',
        rListPrice  => 'P',
        rRevenue    => 'Q',
    };
}

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

sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub priceType    { 'RRP' }
sub currencyCode { 'USD' }
sub countryCode  { 'US' }
sub productType  { BookPub::DB::Item::Sale::kProductTypeEBook }

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

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

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

    ( $type eq 'Sale' || $type eq 'Sale Correction' ) ? return 1 : return;

}

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

    ( $type eq 'Refund' ) ? return 1 : return;

}

sub rUnits {
    my $self = shift;
    my $type = $self->_getByFieldName('rSales');

    if ( $type eq 'Sale' || $type eq 'Sale Correction' ) {
        return 1;
    } elsif ( $type eq 'Refund' ) {
        return -1;
    } else {
        $self->fail("Bad transaction type found: $type");
    }
}

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