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

use strict;

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

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

# This importer has to deal with three tabs, two of which share
# the same header (the third, 'INTL SALES', is slightly different).
# To make things more interesting, there's a blank column on the
# left which causes the column labels to be off by one (hence the
# _getOffset method, below).  In the _fieldMap we're using the column
# labels from the importer template.  Again, the 'INTL SALES' tab is
# different so it won't exactly match the template columns.
# 
sub _fieldMap {
    my $self = shift;
    my $version = $self->_version;
    my $sheetname = lc $self->sheetname;

    my %field_map = (
        'aust digital'  => {
            dateBegin     => 'B', # QUARTER ENDING
            productType   => 'F', # TRACK
            formatType    => 'D', # SALES TYPE
            isrc          => 'C', # CATALOGUE NO.
            trackName     => 'E', # RECORDING
            price         => 'K', # ROYALTY EARNINGS ($)
            units         => 'G', # ROYALTY SALES
        },
        'nz sales'  => {
            dateBegin     => 'B', # QUARTER ENDING
            productType   => 'F', # TRACK
            formatType    => 'D', # SALES TYPE
            isrc          => 'C', # CATALOGUE NO.
            trackName     => 'E', # RECORDING
            price         => 'K', # ROYALTY EARNINGS ($)
            units         => 'G', # ROYALTY SALES
        },
        'intl sales'  => {
            dateBegin     => 'B', # QUARTER ENDING
            productType   => 'G', # TRACK
            formatType    => 'E', # SALES TYPE
            isrc          => 'C', # CATALOGUE NO.
            trackName     => 'F', # RECORDING
            price         => 'L', # ROYALTY EARNINGS ($)
            units         => 'H', # ROYALTY SALES
            countryCode   => 'D', # TERRITORY
        },
    );
    return $field_map{$sheetname};
}

sub _getOffset {
    my $self = shift;
    my $field = shift;

    my $offset = $self->SUPER::_getOffset( $field );

    return undef if( !defined $offset );

    return $offset - 1;
}

sub _headerIdentifier { ( dateBegin => 'ENDING' ) }

sub currencyCode { 'AUD' }

sub countryCode {
    my $self = shift;

    if( exists $self->_fieldMap->{'countryCode'} )
    {
        my $country = $self->_getByFieldName('countryCode');
        my $o = Common::Country->Get( $country );
        return $o->alpha2() if ( $o );

        $self->fail("Unknown country '$country'");
    }
    return 'AU' if( $self->{sheetname} =~ /^AUST DIGITAL$/i );
    return 'NZ' if( $self->{sheetname} =~ /^NZ SALES$/i );
}

sub mediaType { RPS::DB::Item::MediaType::kMediaTypeAudio }

sub saleDates {
    my $self = shift;

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

    my $dateBegin;
    my $dateEnd;

    if( $date =~ /\d{5}/ ) # Excel
    {
        ($dateBegin, $dateEnd) = $self->_getDates( date_begin => $date, type => "msft" );

    }
    return ($dateBegin, $dateEnd);
}

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

    if( $type =~ /^etr track$/i )
    {
        return RPS::File::Sale::TYPE_TRACK;
    } 
    else
    {
        $self->fail( "Could not parse product type from '$type'" );
    }
}

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

    if( $type =~ /^PORTABLE SUBSCRIPTION INCOME$/i ||
        $type =~ /^AD-FUNDED STREAMING BY THIRD PARTY$/i )
    {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    else
    {
        $self->fail( "Could not parse format type from '$type'" );
    }
}

sub _processSheet {
    my $self     = shift;
    my $sheet    = shift;
    my $sheetnum = shift;

    if( $self->{sheetname} =~ /^INTL SALES$/i   ||
        $self->{sheetname} =~ /^AUST DIGITAL$/i ||
        $self->{sheetname} =~ /^NZ SALES$/i )
    {
        return $self->SUPER::_processSheet( $sheet, $sheetnum );
    }
}

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