#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2010 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package RPS::Import::FontanaPhysical::v8;

use strict;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::Util qw(normalize_date normalize_upc);

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

sub _fieldMap {
    {
        labelName        => 'E',
        dateBegin        => 'A',
        productType      => 'P',
        artistName       => 'F',
        serviceProductID => 'I',
        countryCode      => 'L',
        upc              => 'H',
        albumName        => 'G',
        sales            => 'Q',
        salesRevenue     => 'V',
        returns          => 'R',
        returnsRevenue   => 'W',
        totalRevenue     => 'X',
    }
}

sub _unverifiedFieldMap {
    {
        salesType        => 'N',
    }
}

sub _headerIdentifier { ( labelName => 'Label' ) }

sub currencyCode { 'USD' }
sub physical{ 1 }

sub upc {
    my $self = shift;
    return normalize_upc($self->_getByFieldName('upc'));
}

sub channel {
    my $self = shift;
    my $salesType = $self->_getByFieldName('salesType');
    my $channel;
    if( $salesType =~ /^(11|29|41|53)$/ )
    {
        return RPS::File::Sale::CHANNEL_RETAIL;
    }
    elsif( $salesType eq '21' )
    {
        return RPS::File::Sale::CHANNEL_MILITARY;
    }
    print STDERR "Unable to determine channel from '$salesType'\n";
    $self->fail("Unable to determine channel from '$salesType'");
}

sub priceLevel {
    my $self = shift;
    my $salesType = $self->_getByFieldName('salesType');
    my $plevel = RPS::File::Sale::PLEVEL_UNKNOWN;
    if( $salesType =~ /^(11|21|29|41)$/ )
    {
        return RPS::File::Sale::PLEVEL_UNKNOWN;
    }
    elsif( $salesType eq '53' )
    {
        return RPS::File::Sale::PLEVEL_PROMO;
    }
    print STDERR "Unable to determine price tier from '$salesType'\n";
    $self->fail("Unable to determine price tier from '$salesType'");
}

sub productType {
    my $self = shift;
    my $config = $self->_getByFieldName('productType');
    my $prodtype;
    if( $config =~ /^(CD|CF|DX)$/i || $config eq 'COMPACT DISC' || $config =~ /^UNKNOWN-CF$/i )
    {
        $prodtype = RPS::File::Sale::TYPE_CD;
    }
    elsif( $config =~ /^DV$/i || $config =~ /DVD VIDEO/i )
    {
        $prodtype = RPS::File::Sale::TYPE_DVD;
    }
    elsif( $config =~ /CD\/TEMP DVD/i || $config =~ /MIXED CD\/DVD/i )
    {
        $prodtype = RPS::File::Sale::TYPE_DVD_CD_SET;
    }
    elsif( $config =~ /SINGLE 12 INCH/i || $config =~ /^(ALBUM|LP)$/i)
    {
        $prodtype = RPS::File::Sale::TYPE_LP;
    }
    $self->fail("Unable to determine product type from '$config'") if( !$prodtype );
    return $prodtype;
}

sub sales {
    my $self = shift;
    my $sales = $self->_getByFieldName('sales');
    if( $sales < 0 )
    {
        $sales = 0;
    }
    return $sales;
}

sub salesRevenue {
    my $self = shift;
    my $salesRevenue = $self->_getByFieldName('salesRevenue');
    if( $salesRevenue < 0 )
    {
        $salesRevenue = 0;
    }
    return $salesRevenue;
}

sub returns {
    my $self = shift;
    my $sales = $self->_getByFieldName('sales');
    my $returns = $self->_getByFieldName('returns');
    if( $sales < 0 )
    {
        $returns += $sales;
    }  
    if( $returns < 0 )
    {
        $returns *= -1;
    }
    return $returns;
}

sub returnsRevenue {
    my $self = shift;
    my $salesRevenue = $self->_getByFieldName('salesRevenue');
    my $returnsRevenue = $self->_getByFieldName('returnsRevenue');
    if( $salesRevenue < 0 )
    {
        $returnsRevenue += $salesRevenue;
    }
    if( $returnsRevenue < 0 )
    {
        $returnsRevenue *= -1;
    }
    return $returnsRevenue;
}

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

    my $dateBegin;
    my $dateEnd;
    if( $sDate =~ /P\d{2} \d{2}\((\w+ \d{2})\)/i )
    {
        ($dateBegin, $dateEnd) = $self->_getDates( date_begin => $1 );
    }
    elsif( $sDate =~ /^(\d{4})-(\d{1,2})$/ )
    {
        my $year = $1;
        my $month = $2;
        $dateBegin = sprintf("%04d-%02d-%02d", $year, $month, 1);
        $dateEnd   = sprintf("%04d-%02d-%02d", $year, $month, Days_in_Month($year,  $month ));
    }
    return( $dateBegin, $dateEnd );
}

sub _isValidSaleRecord {
    my $self = shift;

    my $album = $self->_getByFieldName('albumName');
    return undef if( !$album || '' eq $album );

    # Generate an error if line has revenue without units
    #
    if( ($self->salesRevenue || $self->returnsRevenue) &&
        !$self->sales && !$self->returns )
    {
        $self->fail("Line ". $self->linenum . ": revenue with no units");
    }

    # We have some lines with no units or revenue of any kind.
    # We need to ignore these.
    return $self->sales || $self->returns ? 1 : undef;
}

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