package BookPub::Import::Importer::Koorong::Version2;

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';

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

sub _fieldMap {
    {
        # Detail
        countryCode    => 'I',
        dateBegin      => 'K',
        isbn           => 'A',
        rTitle         => 'B',
        rUnits         => 'C',
        rPurchasePrice => 'D',
        rTaxUnitPrice  => 'E',
        rDiscount      => 'F',
        rRevenue       => 'H',
    };
}

sub _useIsSaleRec { 1 }

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

sub rListPrice {
    my $self  = shift;
    my $price = $self->_getByFieldName('rPurchasePrice');
    my $tax   = $self->_getByFieldName('rTaxAmount');

    return $price - $tax;
}

sub _isSaleRec {
    my $self = shift;

    return if ( $self->rIsbn13 =~ /TOTAL/i );
    return if ( $self->rIsbn13 =~ /End of Report/i );

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

sub rTitle {
    my $self = shift;

    my $title = $self->SUPER::rTitle();
    if ( $title =~ /^E (.*)$/ ) {
        $title = $1;
    }
    return $title;
}

1;
