package BookPub::Import::Importer::Audible::Version9;

use strict;

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

use lib '/app/tools/sale_import/lib/';
use Import::ValidationError;

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

sub _headerIdentifier { ( rTitle => 'Title' ) }
sub _dateFormat { [ 'quarter', 'iso' ] }
sub _getDateBegin { shift->{_dateBegin} }

sub _processLine {
    my $self = shift;

    if ( $self->_isSaleRec() ) {
        $self->_saveLine();

        $self->{_linesImported}++;
    } else {
        Common::Log::Print( " -- Ignore line sheet: " . $self->sheetname . " line: " . $self->linenum );
        $self->{_linesIgnored}++;
    }
}

sub _isSaleRec {
    my $self     = shift;
    my $id       = lc( $self->_getByFieldName('serviceProductID') );
    my $title    = $self->_getByFieldName('rTitle');
    my $code     = $self->_getByFieldName('countryCode');
    my $isbn     = $self->_getByFieldName('isbn');
    my $units    = $self->_getByFieldName('rUnits');
    my $discount = $self->_getByFieldName('rDiscount');
    my $revenue  = $self->_getByFieldName('rRevenue');

    # skipping some sub-total lines
    if ( $id eq 'total by marketplace' || $id eq 'grand total' ) {
        return 0;
    }

    # skipping another variet of subtotal line
    if ( !$id && !$title && !$code && !$isbn && !$discount ) {
        return 0;
    }

    # skipping the malformed lines that mess things up.
    if (   defined $title
        && $title ne ''
        && $id eq ''
        && $code eq ''
        && $isbn eq ''
        && $units eq ''
        && $discount eq ''
        && $revenue eq '' ) {
        return 0;
    }

    return $self->BookPub::Import::Importer::_isSaleRec();
}

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