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

use strict;

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

sub _fieldMap {
    {
        labelName        => 'O',
        dateBegin        => 'AA',
        productType      => 'X',
        formatType       => 'W',
        artistName       => 'P',
        serviceProductID => 'B',
        upc              => 'A',
        isrc             => 'C',
        trackName        => 'R',
        albumName        => 'Q',
        price            => 'M',
        units            => 'D',
        currencyCode     => 'N',
        countryCode      => 'U',
    }
}

sub _headerIdentifier { ( dateBegin => 'Transaction Date' ) }

sub saleDates {
    my $self = shift;
    my $startDate = $self->_getDateBegin();
    return $self->_getDates( date_begin => $startDate );
}

sub units {
    my $self = shift;
    my $units = $self->_getByFieldName('units');
    my $price = $self->_getByFieldName('price');
    return ($price < 0) ? (abs($units)*-1) : $units;
}

sub unitPrice {
    my $self = shift;
    my $units = $self->_getByFieldName('units') || return;
    my $price = $self->_getByFieldName('price');
    return abs($price/$units);
}

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

    if( $type =~ /^album$/i )
    {
        return RPS::File::Sale::TYPE_ALBUM;
    } 
    elsif( $type =~ /^tracks$/i )
    {
        return RPS::File::Sale::TYPE_TRACK;
    } 
    else
    {
        $self->fail("Could not parse product type from '$type'");
    }
}

sub formatType  {
    my $self = shift;
    my $type = $self->_getByFieldName('formatType');
    if( $type == 0 )
    {
        return RPS::File::Sale::FORMAT_DOWNLOAD;
    }
    elsif($type == 1)
    {
        return RPS::File::Sale::FORMAT_STREAM;
    }
    $self->fail("Could not parse format type from '$type'");
}

sub mediaType {
    return RPS::DB::Item::MediaType::kMediaTypeAudio;
}

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