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

package RPS::Import::MusicAsUsual::v1;

use strict;

use Date::Calc qw(Days_in_Month);

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

sub _fieldMap {
    {
        artistName      => 'C',
        clientProductID => 'A',
        albumName       => 'D',
        configuration   => 'B',
        sales           => 'J',
        returns         => 'K',
        totalRevenue    => 'L',
    };
}

sub _unverifiedFieldMap {
    { payable => 'I', };
}

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

sub currencyCode { 'EUR' }
sub countryCode  { 'ES' }

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

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

sub saleDates {
    my $self = shift;

    # Dates are from the beginning of each sales block; see _processLine()
    #
    return ( $self->{_startDate}, $self->{_endDate} ) if ( $self->{_startDate} );
}

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

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

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

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

    return $sales * $payable;
}

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

    return $returns * $payable;
}

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 '===============================' );
}

sub _processLine {
    my $self = shift;

    $self->SUPER::_processLine();

    # Set date if we see a date header on current line; this will be applied
    # to subsequent sales lines.
    #
    my $clientProductID = $self->_getByFieldName('clientProductID');
    if ( $clientProductID =~ /(\d{4})-(\d{1,2})-(\d{1,2})/ )    # YYYY-MM-DD
    {
        $self->{_startDate} = sprintf( "%04d-%02d-%02d", $1, $2, 1 );
        $self->{_endDate} = sprintf( "%04d-%02d-%02d", $1, $2, Days_in_Month( $1, $2 ) );
    }
}

sub _preProcess {
    my ( $self, %args ) = @_;

    $self->SUPER::_preProcess(%args);

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

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