package BookPub::Import::Importer::Christianaudio::Version9;

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

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

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

sub _fieldMap {
    {
        # Header
        rProductType => 'C',

        # Data
        dateBegin  => 'B',
        isbn       => 'E',
        rTitle     => 'C',
        rUnits     => 'F',
        rListPrice => 'G',
        rDiscount  => 'J',
        rRevenue   => 'K',
    };
}

sub _unverifiedFieldMap {
    { subscriptionPrice => 'H', };
}

sub _dateFormat { [ 'excel', 'iso' ] }

sub dateBegin {
    my $self      = shift;
    my $dateBegin = $self->_getByFieldName('dateBegin');

    # date is in Excel format and is using 1904 as the epoch.  The third flag in the
    # ExcelFmt call below is setting the "is_1904_epoch" flag to true
    if ( $dateBegin =~ /(\d{5})\.(\d{11})/ || $dateBegin =~ /(\d{5})/ ) {
        $dateBegin = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $dateBegin, 1 );
    }

    return $dateBegin;
}

sub _useIsSaleRec { 1 }

sub _isSaleRec {
    my $self = shift;

    return $self->dateBegin;
}

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

    if ( defined($cashPrice) && $cashPrice ne '' && $cashPrice ne '-' && $cashPrice ne '0' ) {
        return $cashPrice;
    } elsif ( defined($subscriptionPrice) && $subscriptionPrice ne '' && $subscriptionPrice ne '-' && $subscriptionPrice ne '0' ) {
        return $subscriptionPrice;

        # Just in case neither list nor subscription price exists
    } else {
        $self->fail("Unable to determine list price");
    }
}

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

    return 1 - $discount;
}

###
1;    # Play nicely.
###
