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

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

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

struct 'BookPub::POS::Import::SaleRec' => {
    serviceID        => '$',
    importStatus     => '$',
    productType      => '$',
    formatType       => '$',
    purchaseType     => '$',
    serviceProductID => '$',
    priceTypeID      => '$',
    isAgency         => '$',
    isFree           => '$',
    isPreOrder       => '$',
    units            => '$',
    listPrice        => '$',
    discount         => '$',
    unitPrice        => '$',
    revenue          => '$',
    conversionRate   => '$',
    currencyCode     => '$',
    countryCode      => '$',
    lineNum          => '$',
    dateBegin        => '$',
    dateEnd          => '$',

    # the following are "r_" fields
    rFormat            => '$',
    rPriceType         => '$',
    rPurchaseType      => '$',
    rServiceName       => '$',
    rDistributor       => '$',
    rProductType       => '$',
    rClientProductID   => '$',
    rIsbn10            => '$',
    rIsbn13            => '$',
    rTitle             => '$',
    rSubtitle          => '$',
    rAuthor            => '$',
    rChapterTitle      => '$',
    rChapterOrdinal    => '$',
    rImprint           => '$',
    rNarrator          => '$',
    rComments          => '$',
    rChannel           => '$',
    rUnits             => '$',
    rSales             => '$',
    rReturns           => '$',
    rAdjustments       => '$',
    rFee               => '$',
    rListPrice         => '$',
    rListPriceCurrency => '$',
    rDiscount          => '$',
    rUnitPrice         => '$',
    rRevenue           => '$',
    rPromoCode         => '$',
};

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 ( $inArg =~ /\d+\.\d+e-\d+/ ) {

            # sprintf is pure magic.
            #
            $inArg = sprintf( "%.8f", $inArg );
        }
        $self->{$field} = $inArg;
    }

    return $self->{$field};
}

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

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

1;
