package BookPub::Import::Importer::Amazon::Version53;

use strict;

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

sub _fieldMap {
    {
        serviceProductID       => 'B',
        currencyCode           => 'W',
        countryCode            => 'X',
        dateBegin              => 'A',
        rFormat                => 'I',
        isbn                   => 'E',
        rTitle                 => 'F',
        rAuthor                => 'G',
        rImprint               => 'H',
        rUnits                 => 'L',
        rSales                 => 'J',
        rReturns               => 'K',
        rAdjustments           => 'N',
        rListPrice             => 'S',
        rListPriceCurrency     => 'T',
        rPurchasePrice         => 'Q',
        rPurchasePriceCurrency => 'R',
        rTaxAmount             => 'O',    # not really tax, listPrice without tax, derived below
        rDiscount              => 'U',
        rRevenue               => 'V',
    };
}

sub _unverifiedFieldMap {
    { taxCurrency => 'P', };
}

# and we've decided to fully utilize sales, returns and adjustments
# sub _advancedUnitProcessing { 1 }
# I'm opposed to leaving "commented-out code" in place, the line above
# should be deleted.  But I'm leaving this here to note that we're
# no longer using aUP for versions 20, 52 and 53.
# Upstream changes to adjustment processing in version 5 introduced
# some discrepencies.

sub priceType { 'agencyplustax' }

sub rListPriceCurrency {
    my $self              = shift;
    my $listPriceCurrency = $self->_getByFieldName('rListPriceCurrency');

    return $listPriceCurrency;
}

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

    return abs($listPrice);
}

sub rTaxAmount {
    my $self = shift;
    my $tax;

    my $adjustments = $self->_getByFieldName('rAdjustments');
    if ( $adjustments > 0 ) {
        return;
    }

    my $priceWithTaxCurrency = $self->rListPriceCurrency;
    my $priceCurrency        = $self->_getByFieldName('taxCurrency');

    # We can only calculate this if the two prices are in the same currency.
    if ( $priceWithTaxCurrency eq $priceCurrency ) {
        my $priceWithTax = $self->rListPrice;
        my $price        = $self->_getByFieldName('rTaxAmount');    # really listPrice without tax

        $tax = $priceWithTax - $price;

        # Tax Amount is meant to represent the tax total for the sale, not just for one unit.
        # So, multiply this by units.

        $tax *= $self->units();
    }

    return $tax;
}

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