package BookPub::Import::Importer::ReadCloud::Version4;

use strict;

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

sub _headerIdentifier { ( rIsbn13 => 'ISBN' ) }

sub rDiscount {
    my $self                = shift;
    my $undiscountedRevenue = $self->_getByFieldName('undiscountedRevenue') || return;
    my $partnerShare        = $self->_getByFieldName('partnerShare') || return;

    my $discount = $partnerShare / $undiscountedRevenue;

    return $discount;
}

sub rTaxUnitPrice {
    my $self          = shift;
    my $purchasePrice = $self->_getByFieldName('rPurchasePrice') || return;
    my $listPrice     = $self->_getByFieldName('rListPrice') || return;

    my $tax = $purchasePrice - $listPrice;

    return $tax;
}

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

    return $units;
}

sub isFree {
    my $self = shift;

    return !$self->rRevenue && $self->rUnits ? 'Y' : 'N';
}

sub rUnitPrice {
    my $self = shift;

    my $revenue = $self->rRevenue() || 0;
    my $units = $self->units() || 0;

    return ($revenue / $units) if ($revenue && $units);
}

sub _isSaleRec {
    my $self = shift;

    # We're going to check for the subtotal line, which now contains
    # totals for columns G, H, M, N, and O
    if (   $self->rRevenue
        && !$self->rIsbn13
        && !$self->rTitle
        && !$self->currencyCode
        && !$self->countryCode
        && !$self->dateBegin ) {
        return 0;
    }

    return $self->SUPER::_isSaleRec();
}

1;
