package BookPub::Import::Importer::Booksfree::Version1;

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

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

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

sub _fieldMap {
    {
        dateBegin  => 'B',
        rIsbn13    => 'G',
        rTitle     => 'H',
        rAuthor    => 'I',
        rListPrice => 'D',
        rDiscount  => 'E',
        rRevenue   => 'F',
    };
}

sub _headerIdentifier { ( rTitle => 'Title' ) }

sub productType  { BookPub::DB::Item::Sale::kProductTypeAudioBook }
sub purchaseType { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub currencyCode { 'USD' }
sub countryCode  { 'US' }
sub _dateFormat  { [ 'excel', 'iso' ] }
sub units        { ( shift->rRevenue() < 0 ) ? -1 : 1 }
sub dateEnd      { shift->dateBegin }

sub dateBegin {
    my $self = shift;
    my $dateBegin = $self->SUPER::dateBegin() || return;

    if ( $dateBegin =~ m/(\d{4})-(\d{2})-(\d{2})/ ) {

        # The dates are importing as two days ahead of what is displayed in the sales file.
        my ( $y, $m, $d ) = Add_Delta_Days( $1, $2, $3, -2 );
        $dateBegin = $y . "-" . $m . "-" . $d;
        $dateBegin = Common::Util::normalize_date($dateBegin);
    }
    return $dateBegin;
}

1;
