package BookPub::Import::Importer::AIDC::Version1;

use strict;

use lib '/app/tools/common/lib';
use Common::Country;

use lib '/app/tools/bookpub/lib';
use BookPub::Tracker::Service;
use base 'BookPub::Import::Importer::AIDC::Base';

sub _headerIdentifier { ( dateBegin => 'ACU.07/04/98.gwt' ) }

sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub priceType    { 'rrp' }
sub rDistributor { 'Berrett-Koehler' }
sub currencyCode { 'USD' }
sub productType  { BookPub::DB::Item::Sale::kProductTypeEBook }

sub _fieldMap {
    {
        serviceID   => 'S',
        countryCode => 'AO',
        dateBegin   => 'A',
        isbn        => 'D',
        rTitle      => 'F',
        rUnits      => 'C',
        rListPrice  => 'G',
        rDiscount   => 'H',
        rRevenue    => 'J',
    };
}

# Older sales files sometimes have an isbn10 in the isbn13 field.  Accepting
# both here...
sub isbn {
    my $self = shift;
    my $isbn = $self->_getByFieldName('isbn');

    if ( length($isbn) == 10 || length($isbn) == 13 ) {
        return $isbn;
    } else {
        return undef;
    }
}

sub _dateNormalization { 'month' }

sub _getDateBegin {
    my $self = shift;
    my $date = $self->_getByFieldName('dateBegin') || return;

    # the date is being returned in the "days since 1/1/1900" format, so we need
    # to convert it.  We'll check if it's a 5 digit number and greater than the
    # number of days since 1/1/1900 on 1/1/2006, which is 38716
    if ( $date =~ /^\d{5}$/ and $date > 38716 ) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $date );
        return $date;
    }

    return $date

}

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

    if ( $units != 0 && $revenue == 0 ) {
        return 'Y';
    }

    return 'N';

}

1;
