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

use strict;

use lib '/app/tools/rps/lib';

use base 'RPS::Import::Importer';

sub _fieldMap {
    my $self = shift;
    my $version = $self->{version};
    my %field_map = (
        5 => {
            # Header
            dateBegin       => 'B3',

            # Detail
            labelName       => 'A',
            countryCode     => 'I',
            productType     => 'B',
            artistName      => 'E',
            clientProductID => 'C',
            upc             => 'D',
            albumName       => 'F',
            currencyCode    => 'U',
            wholesalePrice  => 'G',
            sales           => 'J',
            salesRevenue    => 'L',
            returns         => 'M',
            returnsRevenue  => 'O',
            totalRevenue    => 'T',
        },
        6 => {
            # Header
            dateBegin       => 'B3',

            # Detail
            labelName       => 'A',
            countryCode     => 'I',
            productType     => 'B',
            artistName      => 'E',
            serviceProductID => 'C',
            upc             => 'D',
            albumName       => 'F',
            currencyCode    => 'U',
            wholesalePrice  => 'G',
            sales           => 'J',
            salesRevenue    => 'L',
            returns         => 'M',
            returnsRevenue  => 'O',
            totalRevenue    => 'T',
        },
   );
   return $field_map{$version};
}

sub _headerIdentifier { 'upc' => 'Barcode' }

sub physical      { 1 }
sub channel       { RPS::File::Sale::CHANNEL_RETAIL }
sub priceLevel    { RPS::File::Sale::PLEVEL_UNKNOWN }

sub upc {
    my $self = shift;
    my $upc = $self->_getByFieldName('upc');
    return Common::Util::normalize_upc($upc);
}

sub artistName {
    my $self = shift;
    my $name = $self->_getByFieldName('artistName');
    if( $name =~ /(\w+),(\w+)/ )
    {
        return "$2 $1";
    }
    return $name;
}

sub serviceProductID {
    my $self = shift;
    my $id = $self->_getByFieldName('serviceProductID');
    $id =~ s/^'//;  # remove leading quote if present
    return $id;
}

sub countryCode {
    my $self = shift;
    my $country = $self->_getByFieldName('countryCode');
    my %countryMap = (
        'UNITED KINGDOM' => 'GB',
    );
    $country = $countryMap{ uc $country } if( exists $countryMap{ uc $country } );
    my $obj = Common::Country->Get( $country );
    return $obj ? $obj->alpha2 : undef;
}

sub currencyCode {
    my $self = shift;
    my $ccode = $self->_getByFieldName('currencyCode');
    return 'USD' if( $ccode =~ /^us dollars$/i );
    return 'GBP' if( $ccode =~ /^pounds sterling$/i );
    $self->fail("Unrecognized currency '$ccode'");
}

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

    if( $productType =~ /^cd$/i )
    {
        return RPS::File::Sale::TYPE_CD;
    }
    elsif( $productType =~ /^(cdep|cdsi)$/i )
    {
        return RPS::File::Sale::TYPE_CD_SIN;
    }
    elsif( $productType =~ /^(lp2|lp|12x2|12)$/i )
    {
        return RPS::File::Sale::TYPE_LP;
    }
    elsif( $productType =~ /^12ep$/i )
    {
        return RPS::File::Sale::TYPE_LP5;
    }
    elsif( $productType =~ /^(cd2|cd3)$/i )
    {
        return RPS::File::Sale::TYPE_DBL_CD;
    }
    $self->fail( "Unable to determine product type from '$productType'" );
}

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

    return $self->SUPER::_processSheet( $sheet, $sheetnum )
        if( $self->{sheetname} =~ /^reported sales$/i );
}

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

    return( defined ( $sales || $returns ) && ($self->albumName || $self->upc ) );
}

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