package BookPub::Import::Importer::Kobo::Version42;

use strict;
use warnings;

use lib '/app/tools/bookpub/lib';

use base 'BookPub::Import::Importer';

sub _fieldMap {
    {
        dateBegin              => 'A',
        rProductType           => 'V',
        rAuthor                => 'D',
        rTitle                 => 'E',
        rIsbn13                => 'C',
        rImprint               => 'B',
        countryCode            => 'I',
        rUnits                 => 'K',
        rListPrice             => 'G',
        rListPriceCurrency     => 'H',
        rPurchasePrice         => 'F',
        rPurchasePriceCurrency => 'H',
        rCOGS                  => 'S',
        rRevenue               => 'T',
        currencyCode           => 'O',
        rTaxAmount             => 'U',
    };
}

sub _headerIdentifier { rIsbn13 => 'eISBN' }

sub _useIsSaleRec { 1 }
sub purchaseType  { BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL }
sub rPurchaseType { BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL }
sub productType   { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rProductType  { BookPub::DB::Item::Sale::kProductTypeEBook }
sub priceType     { 'rrp' }

sub rServiceName {
    return BookPub::Tracker::Service::GetDefaultServiceName( service_id => shift->serviceID );
}

sub rUnitPrice {
    my $self = shift;

    return $self->rRevenue / $self->rUnits;
}

sub rTaxAmount {
    my $self = shift;

    my $taxAmount = $self->_getByFieldName('rTaxAmount') || 0;
    my $rUnits    = $self->_getByFieldName('rUnits')     || 0;
    my $rRevenue  = $self->_getByFieldName('rRevenue')   || 0;

    $taxAmount = abs($taxAmount)      if $rUnits > 0 && $rRevenue > 0;
    $taxAmount = abs($taxAmount) * -1 if $rUnits < 0 && $rRevenue < 0;

    return $taxAmount;
}

sub isFree {
    my $self = shift;

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

    return !$revenue && $units ? 'Y' : 'N';
}


1;
