package BookPub::Import::Importer::CourseSmart::Version4;

use strict;

use Date::Calc qw(Days_in_Month);
use Data::Dumper;

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

use lib '/app/tools/data_classes/lib';

use lib '/app/tools/bookpub/lib';
use BookPub::DB::Item::Sale;
use BookPub::Import::SaleRec;
use base 'BookPub::Import::Importer';

sub _fieldMap {
    {
        rState     => 'S',
        dateBegin  => 'E',
        isbn       => 'N',
        rTitle     => 'R',
        rAuthor    => 'Q',
        rUnits     => 'U',
        rListPrice => 'V',
        rRevenue   => 'V',
    };
}

sub _headerIdentifier { ( dateBegin => 'Transaction Date' ) }

sub purchaseType  { BookPub::DB::Item::Sale::kPurchaseTypeLoan }
sub priceType     { 'rrp' }
sub currencyCode  { 'USD' }
sub productType   { BookPub::DB::Item::Sale::kProductTypeEBook }
sub _dateFormat   { [ 'numerical_year_first', 'iso' ] }
sub _useIsSaleRec { 1 }

sub countryCode {
    my $self = shift;

    # Throwing an error if there is not a US state or CA province code in this field.

    my %provinces = (
        'alberta'                   => 1,
        'british columbia'          => 1,
        'manitoba'                  => 1,
        'new brunswick'             => 1,
        'newfoundland and labrador' => 1,
        'northwest territories'     => 1,
        'nova scotia'               => 1,
        'nunavit'                   => 1,
        'ontario'                   => 1,
        'prince edward island'      => 1,
        'quebec'                    => 1,
        'saskatchewan'              => 1,
        'yukon'                     => 1,
    );

    my $state = $self->SUPER::rState();

    my $postalObject = Common::Postal::US->new();
    my $stateAbbr    = $postalObject->getAbbr($state);
    if ( $stateAbbr || $state eq '' ) {
        return 'US';
    } elsif ( $provinces{ lc($state) } ) {
        return 'CA';
    } else {
        $self->fail("Invalid state: $state");
    }
}

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

    return $revenue * 0.70;    #applying 30% fee
}

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