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

use strict;

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


sub _fieldMap {
    my $self = shift;
    my %field_map = (
        8 => {
            dateBegin        => 'A2',
            clientProductID  => 'A',
            upc              => 'D',
            totalRevenue     => 'I',
        },
        9 => {
            labelName        => 'A1',
            dateBegin        => 'A3',
            clientProductID  => 'A',
            upc              => 'D',
            totalRevenue     => 'I',
        }
    );
    return $field_map{$self->_version};
}


sub _unverifiedFieldMap {
    my $self = shift;
    my %field_map = (
        8 => {
            description      => 'B',
            netUnits         => 'G',
        },
        9 => {
            description      => 'B',
            netUnits         => 'G',
        },
    );
    return $field_map{$self->_version};
}


sub physical { 1; }


sub _headerIdentifier { (netUnits => 'Net Qty') }


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


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


sub currencyCode { 'GBP'; }


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


sub artistName { shift->{_artistName}; }


sub albumName { shift->{_albumName}; }


sub saleDates {
    my $self = shift;
    return ($self->{_startDate}, $self->{_endDate});
}


sub countryCode { shift->{_countryCode}; }


sub sales {
    my $self = shift;
    my $netUnits = $self->_getByFieldName('netUnits');
    $netUnits =~ s/\.0//;
    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');
    $netUnits =~ s/\.0//;
    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 $description = $self->_getByFieldName('description');

    if( '' ne $description && $description !~ /\./ )
    {
        $self->fail("Unable to find album or artist");
    }

    my ($artistName, $albumName) = split( /\./, $description);
    $artistName =~ s/\s+$//;
    $albumName =~ s/^\s+//;
    $self->{_artistName} = $artistName;
    $self->{_albumName} = $albumName;
    return $self->SUPER::_processLine(@_);
}


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

    # Grab data from area above the header
    #
    foreach my $line (@$sheet)
    {
        $self->{line} = $line;
        if( !$self->{_startDate} && !$self->{_endDate} )
        {
            my $hdr = $self->_getByFieldName('dateBegin');
            if ($hdr =~ /(\w+ \d{4}) - (\w+)/)
            {
                my $dt = $1;
                my $cc = $2;
                ($self->{_startDate}, $self->{_endDate}) = $self->_getDates(date_begin => $dt);

                if ($cc =~ /^us(a)?$/i)
                {
                    $self->{_countryCode} = 'US';
                }
                elsif ($cc =~ /^ca(?:nadian)?$/i )
                {
                    $self->{_countryCode} = 'CA';
                }
                last;
            }
        }
    }

    # Now process the rest of the sheet
    #
    $self->SUPER::_processSheet($sheet, $sheetnum);

} # _processSheet


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.
###
