package BookPub::Import::Importer::Amazon::Version74;

use strict;
use warnings;

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

sub _fieldMap {
    {
        dateBegin              => 'D',
        rAuthor                => 'I',
        rTitle                 => 'H',
        rIsbn13                => 'F',
        rState                 => 'AD',
        rPostalCode            => 'X',
        countryCode            => 'Y',
        serviceProductID       => 'G',
        rUnits                 => 'K',
        rListPrice             => 'M',
        rListPriceCurrency     => 'N',
        rPurchasePrice         => 'O',
        rPurchasePriceCurrency => 'P',
        rRevenue               => 'BK',
        currencyCode           => 'BL',
        rTaxAmount             => 'BM',
        rFee                   => 'BU',
        rIncentiveAmount       => 'BQ',
        rModel                 => 'BO',
        rTransactionCategory   => 'L',
    };
}

sub _unverifiedFieldMap {
    {
        transactionStatus            => 'L',
        transactionType              => 'R',
        incentiveCurrency            => 'BR',
        billToState                  => 'W',
        pub_rewards_credits_currency => 'BV',
        net_cogs                     => 'BW'
    };
}

sub _preProcess {
    my $self = shift;

    $self->SUPER::_preProcess(@_);

    unless ( Common::RSApp::GetClientID() =~ /^318$/ ) { # MMUS
        $self->fail("Incentive rules not configured for this client");
    }

    return 1;
}

sub rFee {
    my $self = shift;

    my $currencyCode      = $self->currencyCode;
    my $feeCurrency       = $self->_getByFieldName('pub_rewards_credits_currency') || '';
    my $rFee              = $self->_getByFieldName('rFee') || 0;
    my $netCogs           = $self->_getByFieldName('net_cogs') || 0;
    my $revenue           = $self->SUPER::revenue();

    # to be sure they are all digits
    $rFee    += 0;
    $netCogs += 0;

    # don't check if no amount
    return $rFee unless $rFee;

    # currencies must match
    if ($currencyCode ne $feeCurrency) {
        $self->fail("currencyCode '$currencyCode' doesn't match feeCurrency '$feeCurrency'");
    }

    # make sure columns reconcile
    my $adjRev = $revenue - $rFee;
    if (Common::RSMath::round($adjRev, 2) != Common::RSMath::round($netCogs, 2)) {
        $self->fail("Revenue '$revenue' minus fee '$rFee' does not equal netCogs '$netCogs'");
    }

    return $rFee;
}

sub _isSaleRec {
    my $self = shift;

    my $transactionStatus = uc( $self->_getByFieldName('transactionStatus') || '' );
    my $revenue           = $self->SUPER::revenue();

    # We're going to skip some of the lines if they have a certain status AND no revenue.
    if ( ( $transactionStatus eq "REFUNDED_NOT_RECEIVED" || $transactionStatus eq "FULFILLED_NOT_PAID" ) && $revenue == 0 ) {
        return 0;
    } else {
        return $self->BookPub::Import::Importer::_isSaleRec();
    }
}

sub isFree {
    my $self = shift;

    return !$self->rRevenue && $self->rUnits ? 'Y' : 'N';
}


1;
