package BookPub::Import::Importer::Anobii::Version2;

use strict;

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

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

sub _fieldMap {
    {
        currencyCode           => 'S',
        countryCode            => 'G',
        dateBegin              => 'A',
        isbn                   => 'D',
        rTitle                 => 'B',
        rAuthor                => 'E',
        rImprint               => 'F',
        rSales                 => 'H',
        rReturns               => 'I',
        rListPrice             => 'M',
        rListPriceCurrency     => 'S',
        rPurchasePrice         => 'J',
        rPurchasePriceCurrency => 'S',
        rTaxAmount             => 'K',
        rDiscount              => 'O',
        rRevenue               => 'R',

    };
}

sub _headerIdentifier { ( rTitle => 'TITLE & SUB-TITLE' ) }

sub priceType    { 'rrp' }
sub productType  { BookPub::DB::Item::Sale::kProductTypeEBook }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub _dateFormat  { [ 'eur', 'iso' ] }

sub _useIsSaleRec { 1 }

sub _isSaleRec {
    my $self = shift;

    if (   $self->_getByFieldName('dateBegin') eq 'TOTAL SALES'
        || $self->_getByFieldName('dateBegin') eq 'TOTAL REFUNDS'
        || $self->_getByFieldName('dateBegin') eq 'NET TOTAL' ) {
        return 0;
    }

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

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

    my $units = $sales - $returns;

    return $units;
}

sub _getDateBegin {
    my $date = shift->SUPER::_getDateBegin() || return;
    my $d = substr( $date, 0, 10 );

    return $d;
}

1;
