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

package RPS::Import::UniversalNZPhysical;

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 {
    {
        configuration    => 'C',
        artistName       => 'B',
        serviceProductID => 'A',
        albumName        => 'C',
        wholesalePrice   => 'D',
        sales            => 'F',
        salesRevenue     => 'L',
        totalRevenue     => 'L',
    }
}

sub _unverifiedFieldMap {
    {
        expenses         => 'K',
    }
}

sub _headerIdentifier { ( wholesalePrice => 'PPD' ) }
sub countryCode { 'NZ' }
sub currencyCode { 'NZD' }
sub physical { 1 }
sub channel { RPS::File::Sale::CHANNEL_RETAIL }
sub priceLevel { RPS::File::Sale::PLEVEL_FULL }

# saleDates - return the start/end date information for the file.
#
sub saleDates {
    my $self = shift;

    return ($self->{_startDate}, $self->{_endDate}) if( $self->{_startDate} );

    $self->fail( "Unknown filename" ) unless( $self->filename );

    if( $self->filename =~ /(Q\d \d{4})/ )  # Ex: Q4 2016
    {
        ($self->{_startDate}, $self->{_endDate}) = $self->_getDates(date_begin => $1, type => "quarter");
    }
    elsif( $self->filename =~ /(\d)Q(\d{2})/ )  # Ex: 4Q16
    {
        my $quarter = "q" . $1 . " 20" . $2;
        ($self->{_startDate}, $self->{_endDate}) = $self->_getDates(date_begin=>$quarter, type=>"quarter");
    }
    else
    {
        $self->fail( "Could not determine period from filename '" . $self->filename . "'" );
    }

    return ($self->{_startDate}, $self->{_endDate});
}

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

sub albumName {
    my $self  = shift;
    my $title   = $self->_getByFieldName('albumName');
    $title =~ s/ \(\w+\)$//;
    
    return $title;
}

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

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

sub _isValidSaleRecord {
    my $self = shift;

    my $expenses = $self->_getByFieldName('expenses');
    if ($expenses =~ /TOTAL TO PAY/i ||
        $expenses =~ /RESERVE/i)
    {
        # skip lines in the footer section
        return;
    }

    # The line is valid if it has sales or revenue, and has an album title
    #
    return ( ($self->sales != 0 || $self->salesRevenue != 0) && $self->albumName );
}

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