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

package RPS::Import::Sony::v15;

use strict;

use Date::Calc qw(Days_in_Month);

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

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

sub _fieldMap {
    {
        # Note: the sales file has a blank first column, thus from an
        # importer standpoint column A starts at the first non-blank column.
        # E.g., column B is really column A, column C is really column B,
        # and so on.  The values here match what's in the importer template,
        # however in order to properly match the fields to what the importer
        # 'sees' we have to decrement the internal offset so that column B
        # is seen as column A.  This is done in _getOffset( ), below.

        # header
        #
        labelName        => 'C',

        # detail
        #
        dateBegin        => 'B',
        artistName       => 'D',
        upc              => 'C',
        albumName        => 'F',
        configuration    => 'H',
    }
}

sub _unverifiedFieldMap {
    {
        netQuantity       => 'Q',
        netValue          => 'V',
        commissionPayable => 'AB',
        returnsFee        => 'Z',
        copyrightFee      => 'X',
    }
};

sub _getOffset {
    my $self = shift;
    my $field = shift;

    my $offset = $self->SUPER::_getOffset( $field );

    return undef if( !defined $offset );

    return $offset - 1;
}

 
sub _headerIdentifier {
    ( 'upc' => 'Catalog' )
}

sub countryCode  { 'AU' }
sub currencyCode { 'AUD' }

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

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

sub saleDates {
    my $self = shift;
    my $startDate = $self->_getByFieldName('dateBegin');

    my $dateBegin;
    my $dateEnd;
    if( $startDate =~ /(\d{4})(\d{2})/ )
    {
        $dateBegin = sprintf("%04d-%02d-%02d", $1, $2, 1 );
        $dateEnd   = sprintf("%04d-%02d-%02d", $1, $2, Days_in_Month($1, $2) );
    }

    return ( $dateBegin, $dateEnd );
}

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

    if( $config =~ /^cd album$/i )
    {
        return RPS::File::Sale::TYPE_CD;
    }
    elsif( $config =~ /^cd\/dvd combo$/i )
    {
        return RPS::File::Sale::TYPE_DVD_CD_SET;
    }

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

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

    if( $config =~ /^cd album$/i )
    {
        return "CD";
    }
    elsif( $config =~ /^cd\/dvd combo$/i )
    {
        return "DVDCD";
    }

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

sub totalRevenue {
    my $self = shift;
    my $netValue = $self->_getByFieldName( 'netValue' );
    my $distFee  = $self->_getByFieldName( 'commissionPayable' );
    my $returnsFee = $self->_getByFieldName( 'returnsFee' );
    my $copyrightFee = $self->_getByFieldName( 'copyrightFee' );
    return $netValue - $distFee - $returnsFee - $copyrightFee;
}

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

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

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

sub returns {
    my $self = shift;
    my $sales        = $self->_getByFieldName( 'netQuantity' );
    my $salesRevenue = $self->totalRevenue;
    return ( $sales < 0 || $salesRevenue < 0 ) ? abs($sales) : 0;
}

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

    return ( $sales < 0 || $salesRevenue < 0 ) ? abs($salesRevenue) : 0;
}

sub labelName {
    my $self = shift;
    return $self->{_labelName};
}

# _processSheet is used to process a tab from the spreadsheet.
# Here, we're extracting the sale date and label information
# from the cells called out in the template.
#
sub _processSheet {
    my $self     = shift;
    my $sheet    = shift;
    my $sheetnum = shift;

    if( $self->{sheetname} =~ /^physical sales$/i )
    {
        # Get the label information from the header
        #
        my $foundLabelHeader;
        foreach my $line(@$sheet)
        {
            $self->{line} = $line;

            my $_label = $self->_getByFieldName('labelName');

            if( !$foundLabelHeader )
            {
                if( $_label && $_label =~ /^Catalog Current BU Description$/i )
                {
                    $foundLabelHeader = 1;
                }
            }
            else
            {
                $self->{_labelName} = $_label;
                last;
            }
        }

        # Now process the detail lines
        #
        return $self->SUPER::_processSheet( $sheet, $sheetnum );
    }
}


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

sub physical { 1 }

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