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

package RPS::Import::WMG::WarnerNZPhysical2;

use strict;

use Spreadsheet::ParseExcel;

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

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

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

sub _fieldMap {
    {
        # The date appears once per block of sales lines that the date applies to
        #
        dateBegin => 'A',

        artistName      => 'B',
        albumName       => 'C',
        clientProductID => 'D',
        totalRevenue    => 'O',
    };
}

sub _unverifiedFieldMap {
    {
        netQuantity => 'G',
        netSales    => 'O',
    };
}

sub _headerIdentifier { ( netSales => 'Through' ) }

sub countryCode { 'NZ' }

sub currencyCode { 'NZD' }

sub labelName { 'Future Classic' }

sub physical { 1 }

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

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

sub productType {
    my $self = shift;
    my $cfg  = $self->_getByFieldName('clientProductID');
    if ( $cfg =~ /\w+\d+(\w+)?$/ ) {
        if ( '' eq $1 || $1 =~ /cd/i ) {
            RPS::File::Sale::TYPE_CD;
        } elsif ( $1 =~ /lp/i ) {
            RPS::File::Sale::TYPE_LP;
        } else {
            $self->fail("Unable to get configuration from '$cfg'");
        }
    }
}

sub saleDates {
    my $self = shift;
    return ( $self->{_dateBegin}, $self->{_dateEnd} );    # See _processLine
}

sub _processLine {
    my $self = shift;
    my $date = $self->_getByFieldName('dateBegin');
    if ($date) {
        $date = Spreadsheet::ParseExcel::Utility::ExcelFmt( "yyyy-mm-dd", $date ) if ( $date =~ /^\d{5}$/ );    # Excel
        ( $self->{_dateBegin}, $self->{_dateEnd} ) = $self->_getDates( date_begin => $date );

    }

    return $self->SUPER::_processLine(@_);
}

sub sales {
    my $self    = shift;
    my $sales   = $self->_getByFieldName('netQuantity');
    my $revenue = $self->_getByFieldName('netSales');
    return ( $sales >= 0 && $revenue > 0 ) ? $sales : 0;
}

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

sub returns {
    my $self    = shift;
    my $sales   = $self->_getByFieldName('netQuantity');
    my $revenue = $self->_getByFieldName('netSales');
    return ( $revenue < 0 ) ? abs($sales) : 0;    # Follow the sign of the revenue
}

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

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

    return $self->SUPER::_processSheet( $sheet, $sheetnum ) if ( $self->{sheetname} =~ /[\w- ]+(\d\d)/i );    # MMM-YY
}

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