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

package RPS::Import::Goodfellas::v1;

use strict;

use Date::Calc qw(Days_in_Month);

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

sub _fieldMap {
    {
        labelName       => 'E',
        artistName      => 'C',
        clientProductID => 'A',
        albumName       => 'D',
        configuration   => 'B',
        wholesalePrice  => 'K',
        sales           => 'H',    # can be sales or returns
        salesRevenue    => 'L',    # can be sales or returns revenue
        totalRevenue    => 'L',

    };
}

sub _headerIdentifier { ( albumName => 'Title' ) }

sub currencyCode { 'EUR' }
sub countryCode  { 'IT' }

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

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

sub saleDates {
    my $self = shift;
    return ( $self->{_startDate}, $self->{_endDate} ) if ( $self->{_startDate} );    # from header of current sheet; see _processSheet
}

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

    if ( $config =~ /^cd$/i ) {
        return RPS::File::Sale::TYPE_CD;
    } elsif ( $config =~ /^cdx2$/i ) {
        return RPS::File::Sale::TYPE_DBL_CD;
    } elsif ( $config =~ /^lp$/i || $config =~ /^lp+downl$/i ) {
        return RPS::File::Sale::TYPE_LP;
    }

    $self->fail("Unable to determine config from format($config)");
}

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

    return ( $sales > 0 ) ? $sales : 0;
}

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

    return ( $salesRevenue > 0 ) ? $salesRevenue : 0;
}

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

    # returns, if any, must be reported as a postive value
    #
    return ( $sales < 0 ) ? abs($sales) : 0;
}

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

    # returns revenue, if any, must be reported as a postive amount
    #
    return ( $salesRevenue < 0 ) ? abs($salesRevenue) : 0;
}

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 && $self->albumName ne '===============================' );
}

# _preProcess is called before we start processing the spreadsheet.
# Here, we're looking for the date information from the first row.
#
sub _preProcess {
    my ( $self, %args ) = @_;
    $self->SUPER::_preProcess(%args);

    my $fileObj = $args{file};
    $fileObj->Physical(1);

    my $lines = $args{lines};

    my $dateEnd = $lines->[0][1];    # B1

    if ( $dateEnd =~ /(\d{2})\/(\d{2})\/(\d{4})$/ )    # DD/MM/YYYY  Ex: "Dal            al 31/10/2012"
    {
        $self->{_startDate} = sprintf( "%04d-%02d-%02d", $3, $2, 1 );
        $self->{_endDate} = sprintf( "%04d-%02d-%02d", $3, $2, Days_in_Month( $3, $2 ) );
    }
}

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