#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2013 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package RPS::Import::PIAS::v10;

use strict;

use lib '/app/tools/rps/lib';
use base 'RPS::Import::Importer';

sub _fieldMap {
    {
        clientProductID => 'B',
        productType     => 'D',
        totalRevenue    => 'N',
    };
}

sub _unverifiedFieldMap {
    {
        description => 'C',
        netUnits    => 'O',
    };
}

sub physical { 1; }

sub _headerIdentifier { ( netUnits => 'Nett Sales Qty' ) }

sub channel { RPS::File::Sale::CHANNEL_RETAIL }

sub priceLevel { RPS::File::Sale::PLEVEL_UNKNOWN; }

sub currencyCode { 'GBP'; }

sub countryCode { 'GB'; }

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

    if ( $type =~ /^cd$/i ) {
        return RPS::File::Sale::TYPE_CD;
    } elsif ( $type =~ /^cdx2$/i ) {
        return RPS::File::Sale::TYPE_DBL_CD;
    } elsif ( $type =~ /^lp(x2)?$/i ) {
        return RPS::File::Sale::TYPE_LP;
    }
    $self->fail("Unknown configuration '$type'");
}

sub artistName { shift->{_artistName}; }

sub albumName { shift->{_albumName}; }

sub saleDates {
    my $self = shift;
    if ( !defined $self->{_startDate} ) {
        if ( $self->filename =~ /(\w+\s+\d{2})\.xls(x)?/i ) {
            ( $self->{_startDate}, $self->{_endDate} ) = $self->_getDates( date_begin => $1 );
        }
    }
    return ( $self->{_startDate}, $self->{_endDate} );
}

sub sales {
    my $self     = shift;
    my $netUnits = $self->_getByFieldName('netUnits');
    $netUnits =~ s/\.0//;
    return ( $netUnits > 0 ) ? $netUnits : 0;
}

sub salesRevenue {
    my $self    = shift;
    my $revenue = $self->_getByFieldName('totalRevenue');
    return ( $revenue > 0 ) ? $revenue : 0;
}

sub returns {
    my $self     = shift;
    my $netUnits = $self->_getByFieldName('netUnits');
    $netUnits =~ s/\.0//;
    return ( $netUnits < 0 ) ? abs($netUnits) : 0;
}

sub returnsRevenue {
    my $self    = shift;
    my $revenue = $self->_getByFieldName('totalRevenue');
    return ( $revenue < 0 ) ? abs($revenue) : 0;
}

sub _processLine {
    my $self        = shift;
    my $description = $self->_getByFieldName('description');
    if ( '' ne $description && $description !~ /\-/ ) {
        $self->fail("Unable to find artist or album");
    }

    my ( $albumName, $artistName ) = split( /\-/, $description );

    $self->{_artistName} = Common::Util::trimspaces($artistName);
    $self->{_albumName}  = Common::Util::trimspaces($albumName);
    return $self->SUPER::_processLine(@_);
}

sub _isValidSaleRecord {
    my $self = shift;

    # The line is valid if it has sales or returns, and has an album title
    #
    return ( ( $self->sales != 0 || $self->returns != 0 ) && $self->albumName );
}

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