package BookPub::Import::Importer::Pansing::v1;

use strict;
use warnings;

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

sub _fieldMap {
    {
        dateBegin  => 'B2',
        dateEnd    => 'B2',
        rTitle     => 'H',
        rIsbn13    => 'B',
        rUnits     => 'V',
        rListPrice => 'Z',
        rDiscount  => 'AC',
        rRevenue   => 'AG',
     }
}

sub _headerIdentifier { rTitle => 'Title' }

sub _useIsSaleRec      { 1 }
sub rServiceName       { 'Pansing' }
sub priceType          { 'rrp' }
sub countryCode        { 'SG' }
sub currencyCode       { 'GBP' }
sub rListPriceCurrency { 'GBP' }
sub purchaseType       { BookPub::DB::Item::Sale::kPurchaseTypeDownload }
sub productType        { BookPub::DB::Item::Sale::kProductTypeEBook }
sub rProductType       { BookPub::DB::Item::Sale::kProductTypeEBook }


sub dateBegin {
    my $self = shift;

    if ( Common::RSApp::GetClientID() !~ /^(?:314)$/ ) { # BBUSA only
        $self->fail("Sales date must be confirmed");
    }

    my $date = $self->_getByFieldName( 'dateBegin' ) || '';
    if ( my ($d, $m, $y) = $date =~ /(\d{2})\/(\d{2})\/(\d{4})/ ) {
        return sprintf "%04d-%02d-%02d", $y, $m, $d;
    }

    $self->fail("Unbale to determine date begin from: '$date'");
}

sub dateEnd { shift->dateBegin }

sub isFree {
    my $self = shift;

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

sub rUnits {
    my $self = shift;

    my $rUnits = $self->_getByFieldName( 'rUnits' ) || 0;

    return $rUnits;
}

sub _isSaleRec {
    my $self = shift;

    my $rRevenue = $self->_getByFieldName( 'rRevenue' ) || 0;
    my $rUnits   = $self->_getByFieldName( 'rUnits' )   || 0;
    return if (!$rUnits && !$rRevenue);

    return if !$rUnits && !$self->rTitle;

    return 1;
}


1;
