package BookPub::Import::Importer::WilloLabs::v5;

use strict;
use warnings;


use lib '/app/tools/common/lib';

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


sub _fieldMap {
    {
        dateBegin          => 'X',
        rPurchaseType      => 'J',
        rProductType       => 'N',
        rAuthor            => 'M',
        rTitle             => 'L',
        rIsbn13            => 'I',
        rInstitution       => 'A',
        rState             => 'AA',
        rPostalCode        => 'AA',
        countryCode        => 'Z',
        rReturns           => 'U',
        rUnits             => 'T',
        rUnitPrice         => 'Q',
        rListPrice         => 'R',
        rListPriceCurrency => 'AB',
        rDiscount          => 'K',
        rRevenue           => 'W',
        currencyCode       => 'AB',
        rFee               => 'P',
        rDuration          => 'F',
        rModel             => 'J',
    };
}

sub rState {
    my $self = shift;

    my $str = $self->_getByFieldName('rState') || '';
    my $state;
    if ( $str =~ /^\w+[, ]+(\w+).+$/i ) {
        $state = 'QC' if ($1 eq 'QB');
        if ($self->countryCode eq 'CA' && length($1) > 2){
            my $ca = Common::Postal::CA->new();
            $state = $ca->getAbbr($1);
        } else {
            $state = $1;
        }
    }

    return $state if $state;
}

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') || '';
        3 => "BookPub::Import::Importer::WilloLabs::v2",

    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;
