package BookPub::Import::Importer::OReilly::Version2;

use strict;

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

sub _fieldMap {
    {
        dateBegin  => 'C',
        isbn       => 'H',
        rTitle     => 'F',
        rUnits     => 'P',
        rSales     => 'N',
        rReturns   => 'O',
        rListPrice => 'M',
    };
}

sub _isValidSaleRecord {
    my $self = shift;

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

sub revenue {
    my $self     = shift;
    my $price    = $self->_getByFieldName('rListPrice');
    my $discount = $self->SUPER::rDiscount();

    return $price * ( 1 - $discount );
}

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

    if ($units) {
        $listPrice = abs( $listPrice / $units );
    }

    return $listPrice;
}

1;
