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

package RPS::ArtistRoyalty::Fast::Mapped::MissedSales;

use strict;
use Data::Dumper;
use File::Path;
use IO::File;

use lib '/app/tools/common/lib';
use lib '/app/tools/rps/lib';
use lib '/app/tools/raptor/lib';
use Common::RSApp;

use Data::Dumper;
use DB_File;

use Common::Log;

# Data column indexes
#
use constant kAlbumID          => 0;
use constant kTrackID          => 1;
use constant kProductTypeID    => 2;
use constant kFormatType       => 3;
use constant kArtistContractID => 4;
use constant kReasonCode       => 5;
use constant kDetails          => 6;
use constant kUnits            => 7;
use constant kTotalRevenue     => 8;
use constant kGrossRevenue     => 9;


# 'reason' codes
#
use constant kNoContracts          => 1;
use constant kNoPrice              => 2;
use constant kNoIncomeSource       => 3;
use constant kNoDefaultTerm        => 4;
use constant kNoRetailPrice        => 5;
use constant kNoWholesalePrice     => 6;
use constant kNoTermSync           => 7;
use constant kNoTrackIncluded      => 8;
use constant kAlbumInactive        => 9;
use constant kProductInactive      => 10;
use constant kNoGrossRevenue       => 11;

sub FileName { 'missed_sales' }

sub Reason {
    my ($code) = @_;

    return 'No contracts'               if kNoContracts == $code;
    return 'No product price'           if kNoPrice == $code;
    return 'No income source'           if kNoIncomeSource == $code;
    return 'No matching term'           if kNoDefaultTerm == $code;
    return 'No retail price'            if kNoRetailPrice == $code;
    return 'No wholesale price'         if kNoWholesalePrice == $code;
    return 'No matching term'           if kNoTermSync == $code;
    return 'No track included'          if kNoTrackIncluded == $code;
    return 'Album inactive'             if kAlbumInactive == $code;
    return 'Product inactive'           if kProductInactive == $code;
    return 'No matching term'           if kNoGrossRevenue == $code;

    #    return $gReasons{$code} || 'unknown';
}

sub _new {
    my ( $class, $dataPath ) = @_;

    my $self = bless {}, $class;
    return $self->_init($dataPath);
}

sub _init {
    my ( $self, $dataPath ) = @_;

    $self->{FD} = IO::File->new("< $dataPath") or die "ERROR : Unable to open $dataPath for reading: $!";

    return $self;
}

sub nextLine {
    my ($self) = @_;

    my $fd   = $self->{FD};
    my $line = <$fd>;
    unless ($line) {
        close($fd) if $fd;
        $self->{FD} = undef;
        return undef;
    }

    chomp $line;
    my @data = split( "\t", $line );
    return \@data;
}

sub GetMissedSales {
    my ( $class, $dataPath ) = @_;

    my $filename = "$dataPath/" . $class->FileName() . '.sorted';

    return $class->_new($filename);
}

sub DESTROY {
    my ($self) = @_;

    if ( my $fd = $self->{FD} ) {
        close($fd);
        $self->{FD} = undef;
    }
}

1;

