package BookPub::Import::Importer::Follett_Higher_Education::Version15;

use strict;

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

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

# map version num to the field order
sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        15 => {
            dateBegin            => 'D',
            dateEnd              => 'D',
            rAuthor              => 'J',
            rTitle               => 'K',
            rIsbn10              => 'G',
            rIsbn13              => 'F',
            rInstitution         => 'S',
            rState               => 'U',
            rUnits               => 'O',
            rUnitPrice           => 'P',
            rListPrice           => 'Q',
            rRevenue             => 'R',
            rChannel             => 'B',
            rTransactionCategory => 'C',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }

sub _getDateBegin {
    my $self = shift;
    my $date = $self->_getByFieldName('dateBegin');

    if ( !defined $date || $date eq '' ) {
        if ( $self->filename =~ /[ _](\d{4})[ _](\d{2})\./ ) {
            return "$1-$2-1";
        } else {
            $self->fail( "Date not found in filename: " . $self->filename );
        }
    }
    return $date;
}

sub units {
    my $self    = shift;
    my $units   = $self->_getByFieldName('rUnits');
    my $revenue = $self->_getByFieldName('rRevenue');

    if ( $revenue < 0 ) {
        return -1 * abs($units);
    }

    return $units;
}

sub rListPrice {
    my $self      = shift;
    my $listPrice = $self->_getByFieldName('rListPrice');

    if ( undef $listPrice || $listPrice eq '' ) {
        return $self->unitPrice();
    }

    return $listPrice;
}

sub unitPrice {
    my $self      = shift;
    my $unitPrice = $self->_getByFieldName('rUnitPrice');

    return abs($unitPrice);
}

1;
