package BookPub::Import::Importer::Amigos;

use strict;
use Date::Calc qw(Days_in_Month);

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

sub _headerIdentifier { ( rIsbn13 => 'ISBN' ) }

# map version num to the field order
sub _fieldMap {
    my $self = shift;

    my %fieldMap = (
        1 => {
            dateBegin            => 'F',
            rTitle               => 'C',
            rIsbn13              => 'B',
            serviceProductID     => 'A',
            rUnits               => 'I',
            rUnitPrice           => 'H',
            rListPrice           => 'G',
            rRevenue             => 'J',
            rModel               => 'D',
            rTransactionCategory => 'D',
        },
    );

    return $fieldMap{ $self->_version() };
}

sub _useIsSaleRec              { 1 }
sub _autoParseTitleAndSubtitle { 1 }
sub priceTypeQualifierID       { BookPub::DB::Item::PriceTypeQualifier::kCorporate }      # Library
sub priceType                  { 'rrp' }
sub purchaseType               { BookPub::DB::Item::Sale::kPurchaseTypeSubscriptionDL }
sub productType                { BookPub::DB::Item::Sale::kProductTypeEBook }
sub countryCode                { 'US' }
sub currencyCode               { 'USD' }
sub rListPriceCurrency         { 'USD' }
sub rDiscount                  { '0.3003' }

sub _processLine {
    my $self  = shift;
    my $units = $self->_getByFieldName('rUnits');

    if ( $units && $units =~ /Total/i ) {
        $self->{_endOfData} = 1;
    }

    return $self->SUPER::_processLine(@_);
}

sub _isSaleRec {
    my $self = shift;

    # If we've set the end of data flag in processLine, we are done.
    return if ( $self->{_endOfData} );

    return $self->SUPER::_isSaleRec();
}

1;
