#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2010 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------

package RPS::Import::Universal::v35;

use strict;

use lib '/app/tools/common/lib';
use Common::Assert;

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

sub _fieldMap {
    my $self = shift;
    my $version = $self->version;
    my %field_map = (
        35 => {
            dateBegin        => 'A',
            artistName       => 'C',
            albumName        => 'D',
            wholesalePrice   => 'H',
            clientProductID  => 'B',
        },
        43 => {
            dateBegin        => 'A',
            artistName       => 'C',
            albumName        => 'D',
            wholesalePrice   => 'I',
            clientProductID  => 'B',
        },
    );
    return $field_map{$version};
}

sub _unverifiedFieldMap {
    my $self = shift;
    my $version = $self->version;
    my %field_map = (
        35 => {
            priceCode   => 'G',
            config      => 'Y',
            netUnits    => 'K',
            netRevenue  => 'X',
        },
        43 => {
            priceCode   => 'H',
            config      => 'G',
            netUnits    => 'L',
            netRevenue  => 'Y',
        },
    );
    return $field_map{$version};
}

sub _headerIdentifier { ( dateBegin => qr/Sale(s)? Month/ ) }

sub currencyCode { 'AUD' }
sub countryCode  { 'AU' }
sub channel      { RPS::File::Sale::CHANNEL_RETAIL }
sub physical     { 1 }

sub productType  {
    my $self = shift;
    my $cfg = $self->_getByFieldName('config') || return;

    if( $cfg =~ /^cd album$/i ||
        $cfg =~ /^CDSP\/Full Case$/i ||
        $cfg =~ /^cd mini album$/i ||
        $cfg =~ /^cd \+ 12inch vinyl$/i )
    {
        return RPS::File::Sale::TYPE_CD;
    }
    elsif( $cfg =~ /^cd double/i ||  # starts with 'cd double'
           $cfg =~ /^cd 3 to 4 disc set$/i )
    {
        return RPS::File::Sale::TYPE_DBL_CD;
    }
    elsif( $cfg =~ /^cd\/dvd double$/i ||
           $cfg =~ /^cd\/dvd 3 disc plus set$/i )
    {
        return RPS::File::Sale::TYPE_DVD_CD_SET;
    }
    elsif( $cfg =~ /^vinyl 12" double album$/i ||
           $cfg =~ /^vinyl album$/i ||
           $cfg =~ /^vinyl multi box set$/i )
    {
        return RPS::File::Sale::TYPE_LP;
    }
    elsif( $cfg =~ /^vinyl 7 inch single$/i )
    {
        return RPS::File::Sale::TYPE_LP5;
    }
    $self->fail("Unknown config '$cfg'");
}

sub priceLevel {
    my $self = shift;
    my $priceCode = $self->_getByFieldName('priceCode') || return;
    if( $priceCode =~ /^(CC0|CC1|CC2|CC3|CC4|CC5|CC6|CC7|EE8|EE9)/i )
    {
        return RPS::File::Sale::PLEVEL_BUDGET;
    }
    elsif( $priceCode =~ /^(NN8|GG0|NN4|NN6|EE5|EE6|EE7)$/i )
    {
        return RPS::File::Sale::PLEVEL_MID;
    }
    elsif( $priceCode =~ /^(SS4|GG1|GG2|GG3|GG4|GG5|GG6|G71|GG7|GG8)$/i ||
           $priceCode =~ /^(GG9|G06|EE0|EE1|EE2|EE3|EE4|HH9|HH2|HH5)$/i ||
           $priceCode =~ /^(HH6|G72|G06|G07|G45|G45|AD9|G25|GG8|GG8|G50|G12)$/i ||
           $priceCode =~ /^(Y18|180|Y14|Y87|V100)$/i )
    {
        return RPS::File::Sale::PLEVEL_FULL;
    }
    elsif( $priceCode =~ /^(M35|V11|V20|V23|V33|V80|VB5|VB7|S03|S05|070|D03|D04|D30|D58|NN2)$/i ||
           $priceCode =~ /^(V06|V30|V51|V58|V47|V27)$/i ||
           $priceCode =~ /^(G22|V22|A12|Y66)$/i )
    {
        return RPS::File::Sale::PLEVEL_UNKNOWN;
    }
    $self->fail("Unknown price code '$priceCode'");
}

sub sales {
    my $self = shift;
    my $units = $self->_getByFieldName('netUnits');
    my $revenue = $self->_getByFieldName('netRevenue');
    return ($revenue > 0) ? abs($units) : 0;
}

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

sub returns {
    my $self = shift;
    my $units = $self->_getByFieldName('netUnits');
    my $revenue = $self->_getByFieldName('netRevenue');
    return ($revenue < 0) ? abs($units) : 0;
}

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

sub totalRevenue {
    my $self = shift;
    return $self->salesRevenue - $self->returnsRevenue;
}

sub saleDates {
    my $self = shift;

    my $date = $self->_getByFieldName('dateBegin') || return;  
    my $dateBegin;
    my $dateEnd;

    if( $date =~ /^(\d{4})(\d{2})$/ ) # YYYYMM
    {
        my $dt = sprintf("%04d-%02d-01", $1, $2);
        ( $dateBegin, $dateEnd ) = $self->_getDates( date_begin => $dt );
    }

    return ($dateBegin, $dateEnd);
}

sub _isValidSaleRecord {
    my $self = shift;
    my $albumName  = $self->_getByFieldName('albumName');

    return ( $albumName && $self->totalRevenue && $self->sales ne 'Gross Sales Qty' && $self->dateBegin );
}

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