package BookPub::Import::Importer::Courseload;

use strict;

use lib '/app/tools/common/lib';
use Common::Util;
use Common::Date::Text;

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

sub _fieldMap {
    {
        dateBegin  => 'A',
        dateEnd    => 'A',
        rIsbn13    => 'E',
        rTitle     => 'F',
        rAuthor    => 'H',
        rUnits     => 'M',
        rListPrice => 'N',
        rUnitPrice => 'N',
        rRevenue   => 'O',
    };
}

sub _headerIdentifier { ( rTitle => 'Purchase Description' ) }
sub purchaseType      { BookPub::DB::Item::Sale::kPurchaseTypeLoan }
sub productType       { BookPub::DB::Item::Sale::kProductTypeEBook }
sub countryCode       { 'US' }
sub currencyCode      { 'USD' }
sub priceType         { 'rrp' }
sub rServiceName      { 'Courseload' }
sub _useIsSaleRec     { 1 }
sub _autoParseTitleAndSubtitle { 1 }

sub _isSaleRec {
    my $self = shift;
    $self->SUPER::_isSaleRec || return 0;    # Bail out early on blank rows
    $self->rTitle && $self->rRevenue || return 0;    # Required fields or bail
    $self->rTitle ne 'Total' || return 0;            # Ignore subtotal line
    return 1;
}

sub discount {
    my $self     = shift;
    my $clientID = Common::RSApp::GetClientID;
    if ( $clientID == 346 ) {                        #RLPG
        return .25;
    }

    $self->fail("Discount not set for this client");
}

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

    $listPrice = $listPrice / ( 1 - $discount );

    return $listPrice;
}

1;
