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

use strict;

use Date::Calc qw(Days_in_Month Decode_Month);

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

sub _fieldMap {
    {
        dateBegin     => 'E',
        countryCode   => 'F',
        productType   => 'C', # Product Type Code
        upc           => 'A',
        albumName     => 'B',
    }
}

sub _unverifiedFieldMap {
    {
        salesChannel  => 'H',
        netUnits      => 'I',
        netRevenue    => 'S',
    }
}

sub _headerIdentifier { ( dateBegin => 'Sales Period' ) }

sub physical { 1 }

sub currencyCode { 'EUR' }

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

sub countryCode {
    my $self = shift;

    my $ccode;
    my $cname = $self->_getByFieldName('countryCode');

    $cname = "Czech Republic" if( $cname =~ /^CZECH$/i );

    if( $cname =~ /^(\w{2,3})$/ ) # 2 or 3 character 
    {
        $ccode = $cname;
    }
    else
    {
        my $o = Common::Country->Get($cname);
        if( !$o )
        {
            $self->fail("Unknown country: $cname");
        }
        $ccode = $o->alpha2();
    }
    return $ccode;
}

sub saleDates {
    my $self = shift;
    my $dt = $self->_getByFieldName('dateBegin');
    my $startDate;
    my $endDate;
    if( $dt =~ /^(\d{4})(\d{2})-(\d{4})(\d{2})$/ )  # YYYYMM-YYYYMM
    {
        my($sYear, $sMonth, $eYear, $eMonth) = ($1, $2, $3, $4);
        $startDate = sprintf("%04d-%02d-01", $sYear, $sMonth);
        $endDate = sprintf("%04d-%02d-%02d", $eYear, $eMonth, Days_in_Month($eYear, $eMonth));
    }
    return( $startDate, $endDate );
}

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

    if( $code =~ /(D1|D5|D8)$/i )
    {
        return RPS::File::Sale::TYPE_CD;
    } 
    elsif( $code =~ /^(L5|L6|S5|S6)$/i )
    {
        return RPS::File::Sale::TYPE_LP;
    } 
    elsif( $code =~ /^(K3|P1|R5|Q3|P7)$/i )
    {
        return RPS::File::Sale::TYPE_DVD;
    } 
    elsif( $code =~ /^(N8|P8)$/i )
    {
        return RPS::File::Sale::TYPE_DVD_CD_SET;
    } 
    elsif( $code =~ /^(M1|M2)$/i )
    {
        return RPS::File::Sale::TYPE_CASS;
    } 
    elsif( $code =~ /^D4$/i )
    {
        return RPS::File::Sale::TYPE_CD_SIN;
    } 
    elsif( $code =~ /^V1$/i )
    {
        return RPS::File::Sale::TYPE_VHS;
    } 
    else
    {
        $self->fail("Unknown product type from $code");
        return undef;
    }
}


sub channel {
    my $self = shift;
    my $ch = $self->_getByFieldName('salesChannel') || return;

    if( $ch =~ /^NORMAL RETAIL$/i ||
        $ch =~ /^TV ADVERTISED RETAIL$/i ||
        $ch =~ /^SURPLUS$/i ||
        $ch =~ /^THIRD PARTY$/i )
    {
        return RPS::File::Sale::CHANNEL_RETAIL;
    }
    elsif( $ch =~ /^CLUB$/i )
    {
        return RPS::File::Sale::CHANNEL_CLUB;
    }
    elsif( $ch =~ /^MAIL$/i )
    {
        return RPS::File::Sale::CHANNEL_MAILORDER;
    }
}

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 _isValidSaleRecord {
    my $self = shift;
    my $albumName  = $self->_getByFieldName('albumName');
    my $revenue = $self->_getByFieldName('netRevenue');

    return ( $albumName && $revenue );
}

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