package BookPub::Import::Importer::EBook::Version14;

use strict;

use Data::Dumper;

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

sub _headerIdentifier { ( rTitle => 'Title' ) }

sub purchaseType   { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType    { BookPub::DB::Item::Sale::kProductTypeEBook }

sub _requiredColumnMap {{
    priceType          => 'CoreCurrency',
	serviceProductID   => 'ReferenceNumber',
	currencyCode       => 'CoreCurrency',
	conversionRate     => 'ConversionRate',
	countryCode        => 'IsoCountryCode',
	rState             => 'StateAbbreviation',
	rPostalCode        => 'ZipCode',
	dateBegin          => 'DatePurchased',
    rFormat            => 'Format',
    rIsbn13            => 'FormatIsbn13',
    rTitle             => 'Title',
    rAuthor            => 'Authors',
    rImprint           => 'Imprint',
	rComments          => 'Refunded',
	rReturns           => 'Refunded',
	rListPrice         => 'CorePrice',
	rListPriceCurrency => 'CoreCurrency',
    rPurchasePrice     => 'SalePrice',
    rPurchasePriceCurrency => 'SaleCurrency',
    rDiscount          => 'Discount',
}}


sub _unverifiedFieldMap {{
    eIsbn13            => 'G',
    countryTax         => 'AA',
    stateTax           => 'AB',
    countyTax          => 'AC',
    cityTax            => 'AD',
    districtTax        => 'AE',
}}


sub rIsbn13 {
	my $self = shift;
	my $isbn = $self->_getByFieldName( 'rIsbn13' );

	my $eisbn = $self->_getByFieldName( 'eIsbn13' );

    if ( defined($isbn) && $isbn ne '' ) {
        return $isbn;
    } else {
        return $eisbn;
    }
}

sub priceType { 
    my $self = shift;

    my $priceType = $self->_getByFieldName( 'currencyCode' );
    return 'agency' if( $priceType =~ /(agency model)/i );
    return 'rrp' if( $priceType =~ /(retailer model)/i );
    return 'rrp';
}

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

    return $price eq 0 ? 'Y' : 'N';
}

sub units {
	my $revenue = shift->rRevenue || return 1;
	return $revenue < 0 ? -1 : 1
}

sub currencyCode {
	my $self = shift || return;
	my $currencyCode = $self->SUPER::currencyCode() || return;

	$currencyCode =~ /^(\w{3})/;
	return $1;
}

sub rListPriceCurrency {
	my $self = shift || return;
	my $currencyCode = $self->SUPER::rListPriceCurrency() || return;

	$currencyCode =~ /^(\w{3})/;
	return $1;
}

sub rPurchasePriceCurrency {
	my $self = shift || return;
	my $currencyCode = $self->SUPER::rPurchasePriceCurrency() || return;

	$currencyCode =~ /^(\w{3})/;
	return $1;
}

sub rTaxAmount {
    my $self = shift;

    return $self->_getByFieldName('countryTax') +
           $self->_getByFieldName('stateTax')   +
           $self->_getByFieldName('countyTax')  +
           $self->_getByFieldName('cityTax')    +
           $self->_getByFieldName('districtTax');
}

sub units {
	my $revenue = shift->rRevenue || return 1;
	return $revenue < 0 ? -1 : 1
}

sub rRevenue {
    my $self = shift;
	my $listPrice      = $self->_getByFieldName('rListPrice');
	my $discount       = $self->_getByFieldName('rDiscount');
	
	my $revenue        = $listPrice * ((100 - $discount)/100);
	return $revenue;
}


1;
