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

use strict;

use lib '/app/tools/common/lib';
use Common::Country;
use Common::Assert;
use Date::Calc qw(Days_in_Month Decode_Month);

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

sub _fieldMap {
    {
        dateBegin => 'M1',
        dateEnd   => 'O1',

        labelName       => 'F',
        countryCode     => 'G',
        clientProductID => 'D',
        upc             => 'C',
        wholesalePrice  => 'L',
        retailPrice     => 'J',
        totalRevenue    => 'N',
    };
}

sub _unverifiedFieldMap {
    {
        article  => 'F',
        netUnits => 'M',
    };
}

sub physical { 1; }

sub _headerIdentifier { ( upc => 'Code barre' ) }

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

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

sub currencyCode { 'EUR'; }

sub productType { RPS::File::Sale::TYPE_CD; }

sub artistName { shift->{_artistName}; }

sub albumName { shift->{_albumName}; }

sub saleDates {
    my $self = shift;
    if ( !$self->{_startDate} && !$self->{_endDate} ) {
        my $sdate = $self->_getByFieldName('dateBegin');
        my $edate = $self->_getByFieldName('dateEnd');
        ( $self->{_startDate}, $self->{_endDate} ) = $self->_getDates(
            date_begin => $sdate,
            date_end   => $edate,
            type       => 'eur'
        );
    }
    return ( $self->{_startDate}, $self->{_endDate} );
}

sub countryCode {
    my $self        = shift;
    my $cname       = $self->_getByFieldName('countryCode');
    my $countryObj  = Common::Country->Get($cname);
    my $countryCode = $countryObj->alpha2() if ($countryObj);
    return $countryCode;
}

sub sales {
    my $self     = shift;
    my $netUnits = $self->_getByFieldName('netUnits');
    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');
    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 $article = $self->_getByFieldName('article');
    my ( $artistName, $albumName ) = split( "/", $article );
    $artistName =~ s/\s+$//;
    $albumName =~ s/^\s+//;
    $self->{_artistName} = $artistName;
    $self->{_albumName}  = $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.
###
