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

package RPS::Import::ProperUK::v3;

use strict;

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

sub _headerIdentifier { ( productType => 'Ft' ) }

# sadly, if we add field here, we need to pay mind to
# preProcessLine and make sure we accomodate it for
# promo sales as well...
sub _fieldMap {
    {
        # Header
        dateBegin => 'D',

        # Detail
        productType     => 'A',
        artistName      => 'D',
        clientProductID => 'B',
        upc             => 'C',
        albumName       => 'E',
        wholesalePrice  => 'P',
        sales           => 'K',
        returns         => 'L',
        units           => 'M',
        totalRevenue    => 'Q',
    };
}

sub _unverifiedFieldMap {
    { promoUnits => 'J' };
}

sub physical      { 1 }
sub countryCode   { 'GB' }
sub channel       { RPS::File::Sale::CHANNEL_RETAIL }
sub mediaType     { RPS::DB::Item::MediaType::kMediaTypeAudio }
sub priceLevel    { RPS::File::Sale::PLEVEL_FULL }
sub _getDateBegin { shift->{_dateBegin} }

sub currencyCode { shift->{_currencyCode} }

sub artistName {
    my $self = shift;
    my $name = $self->_getByFieldName('artistName');
    if ( $name =~ /(\w+),(\w+)/ ) {
        return "$2 $1";
    }
    return $name;
}

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

    if ( uc($productType) eq 'CD' ) {
        return RPS::File::Sale::TYPE_CD;
    } elsif ( uc($productType) eq 'CDEP' ) {
        return RPS::File::Sale::TYPE_CD_SIN;
    } elsif ( uc($productType) eq 'LP' ) {
        return RPS::File::Sale::TYPE_LP;
    } elsif ( uc($productType) eq 'LP2' ) {
        return RPS::File::Sale::TYPE_LP;
    } elsif ( uc($productType) eq 'CD2' ) {
        return RPS::File::Sale::TYPE_DBL_CD;
    }
    $self->fail("Unable to determine product type from '$productType'");
}

sub salesRevenue {
    my $self       = shift;
    my $netUnits   = $self->_getByFieldName('units');
    my $netRevenue = $self->_getByFieldName('totalRevenue');

    if ( ( $netUnits < 0 ) || ( !$netUnits && $netRevenue < 0 ) ) {
        return 0;
    } else {
        return $netRevenue;
    }
}

sub returnsRevenue {
    my $self       = shift;
    my $netUnits   = $self->_getByFieldName('units');
    my $netRevenue = $self->_getByFieldName('totalRevenue');

    if ( ( $netUnits > 0 ) || ( !$netUnits && $netRevenue > 0 ) ) {
        return 0;
    } else {
        return $netRevenue * -1;
    }
}

sub _preProcess {
    my $self = shift;
    my %args = @_;

    Log->info("Starting _preProcess");
    $self->SUPER::_preProcess(%args);

    my $dateBegin;
    my $currencyCode;

    foreach my $line ( @{ $args{lines} } ) {

        $self->{line} = $line;

        # Get the date from the header (actually two lines above the header)
        #
        my $dateHeader = $self->_getByFieldName('dateBegin');
        if ( !$dateBegin && $dateHeader =~ /^(\d{4})-(\d{2})-(\d{2})$/ ) {
            $dateBegin = $dateHeader;
        }

        # Get the currency code from the footer
        #
        my $productType = $self->_getByFieldName('productType');
        if ( !$currencyCode && $productType =~ /please invoice/i ) {
            my $_ccode = $self->_getByFieldName('upc');
            if ( $_ccode =~ /us dollars/i ) {
                $currencyCode = 'USD';
            } elsif ( $_ccode =~ /pounds sterling/i ) {
                $currencyCode = 'GBP';
            } else {
                $self->fail("Unable to determine currency code from '$_ccode'");
            }
        }

    }

    $self->{_dateBegin}    = $dateBegin;
    $self->{_currencyCode} = $currencyCode;

}

sub preProcessLine {
    my $self = shift;

    my $promoUnits = $self->_getByFieldName('promoUnits');

    # create a promo sales record if necessary
    if ( $promoUnits =~ /^\d+$/ && $promoUnits ) {
        my $sale = RPS::Import::SaleRec->new();

        # Set the promo-specific fields
        $sale->dateBegin( $self->dateBegin );
        $sale->dateEnd( $self->dateEnd );
        $sale->productType( $self->productType );
        $sale->artistName( $self->artistName );
        $sale->clientProductID( $self->clientProductID );
        $sale->upc( $self->upc );
        $sale->albumName( $self->albumName );
        $sale->free(1);
        $sale->priceLevel(RPS::File::Sale::PLEVEL_PROMO);
        $sale->wholesalePrice(0), $sale->sales($promoUnits);
        $sale->returns(0),        $sale->units($promoUnits);
        $sale->salesRevenue(0);
        $sale->returnsRevenue(0);
        $sale->totalRevenue(0);
        $sale->countryCode( $self->countryCode );
        $sale->channel( $self->channel );
        $sale->currencyCode( $self->currencyCode );
        $sale->mediaType( $self->mediaType );
        $sale->lineNum( $self->linenum );

        $self->SUPER::_insertSale( sale => $sale );
    }
}

sub upc {
    my $self = shift;
    my $upc = $self->_getByFieldName('upc') || return;
    $upc =~ s/'//g;
    return $upc;
}

sub clientProductID {
    my $self = shift;
    my $clientProductID = $self->_getByFieldName('clientProductID') || return;
    $clientProductID =~ s/'//g;
    return $clientProductID;
}

sub _isValidSaleRecord {
    my $self       = shift;
    my $netUnits   = $self->_getByFieldName('units');
    my $netRevenue = $self->_getByFieldName('totalRevenue');

    if ( $netUnits == 0 && $netRevenue == 0 ) {
        return;
    }

    return $self->SUPER::_isValidSaleRecord();
}

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