#
# By putting SaleRec into its own package, we can override accessor methods to do fancy stuff.
#
package RPS::Import::SaleRec;
use strict;

use Class::Struct;
use Data::Dumper;

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

# JPK - Getting an annoying warning about "function 'x' already defined" in the Importer subclasses.
#       I'd like that to go away... Without having to move SaleRec into its own file.
#
struct 'RPS::Import::SaleRec' =>
{
    albumName           => '$',
    artistName          => '$',
    averagePrice        => '$',
    channel             => '$',
    clientProductID     => '$',
    configuration       => '$',
    conversionRate      => '$',
    countryCode         => '$',
    currencyCode        => '$',
    dateBegin           => '$',
    dateEnd             => '$',
    formatType          => '$',
    free                => '$',
    importStatus        => '$',
    isrc                => '$',
    labelName           => '$',
    lineNum             => '$',
    mediaType           => '$',
    outlet              => '$',
    payoutType          => '$',
    price               => '$',
    wholesalePrice      => '$',
    retailPrice         => '$',
    priceLevel          => '$',
    priceType           => '$',
    productType         => '$',
    retail              => '$',
    returns             => '$',
    returnsRevenue      => '$',
    sales               => '$',
    salesRevenue        => '$',
    serviceID           => '$',
    serviceProductID    => '$',
    totalRevenue        => '$',
    trackName           => '$',
    trackNum            => '$',
    units               => '$',
    upc                 => '$',
    wholesaleRate       => '$',
    mcps_adjustment     => '$',
    comments            => '$',
    mechanical_royalty_status => '$',
    uk_mechanical_royalty_status => '$',
    ca_mechanical_royalty_status => '$',
    artist_royalty_status => '$',
    label_royalty_status => '$',
};


sub _floatAccessor
{
    my ($self, $field, $inArg) = @_;

    if (defined $inArg)
    {
        # Convert negative scientific notation back to an expanded string notation.
        # If the value seems like gibberish, go ahead and store it...
        # We'll validate later.
        #
        if (scalar($inArg =~ /^\s*\d(\.\d+)?e-\d+\s*$/))
        {
            # sprintf is pure magic.
            #
            $inArg = sprintf("%.8f", $inArg);
        }
        $self->{$field} = $inArg;
    }

    return $self->{$field};
}


sub averagePrice
{
    my ($self, $inArg) = @_;

    return $self->_floatAccessor('averagePrice', $inArg);
}

sub wholesalePrice
{
    my ($self, $inArg) = @_;

    return $self->_floatAccessor('wholesalePrice', $inArg);
}

sub retailPrice
{
    my ($self, $inArg) = @_;

    return $self->_floatAccessor('retailPrice', $inArg);
}

sub returnsRevenue
{
    my ($self, $inArg) = @_;

    return $self->_floatAccessor('returnsRevenue', $inArg);
}

sub salesRevenue
{
    my ($self, $inArg) = @_;

    return $self->_floatAccessor('salesRevenue', $inArg);
}

sub totalRevenue
{
    my ($self, $inArg) = @_;

    return $self->_floatAccessor('totalRevenue', $inArg);
}

sub price
{
    my ($self, $inArg) = @_;

    return $self->_floatAccessor('price', $inArg);
}

1;
