package BookPub::Import::Importer::WilloLabs::v2;

use strict;
use warnings;


use lib '/app/tools/common/lib';
use Date::Calc qw(Days_in_Month);

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


sub _fieldMap {
    {
        dateBegin          => 'V',
        rPurchaseType      => 'I',
        rProductType       => 'M',
        rAuthor            => 'L',
        rTitle             => 'K',
        rIsbn13            => 'H',
        rInstitution       => 'A',
        rState             => 'Y',
        rPostalCode        => 'Y',
        countryCode        => 'X',
        rReturns           => 'T',
        rUnits             => 'S',
        rUnitPrice         => 'P',
        rListPrice         => 'Q',
        rListPriceCurrency => 'Z',
        rDiscount          => 'J',
        rRevenue           => 'U',
        currencyCode       => 'Z',
        rFee               => 'O',
        rModel             => 'I',
    };
}

sub rState {
    my $self = shift;

    my $str = $self->_getByFieldName('rState') || '';
    if ( $str =~ /,?\s?([a-z ]{2})(\s\d+)?$/i ) {
        return $1;
    }

    return;
}

sub rPostalCode {
    my $self = shift;

    my $str = $self->_getByFieldName('rPostalCode') || '';
    if ( $str =~ /(\d+|\w{3} \w{3})$/i ) {
        return $1;
    }

    return;
}

sub rListPriceCurrency {
    my $self = shift;

    my $currency = $self->_getByFieldName('rListPriceCurrency') || '';

    return $currency;
}

sub isFree {
    my $self = shift;

    return !$self->rRevenue && $self->rUnits ? 'Y' : 'N';
}

sub _isSaleRec {
    my $self = shift;
    return 1 if $self->rRevenue || $self->rUnits;

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

return 1;
