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

package RPS::ArtistRoyalty::Fast::Mapped::Data;

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;

use constant kTypePayee         => 0;
use constant kTypeAccount       => 0;
use constant kTypeSale          => 1;
use constant kTypeReserve       => 1;
use constant kTypeLicenseIncome => 3;
use constant kTypeTransaction   => 5;
use constant kTypeExpense       => 6;


# Common columns
#
use constant kPayeeID             => 0;
use constant kAlbumID             => 1;
use constant kArtistContractID    => 2;
use constant kType                => 3;
use constant kTrackID             => 4;
use constant kTermID              => 5;
use constant kCrossed             => 6;


# Income item columns (also shared by reserve data)
#
use constant kRegionID            => 7;
use constant kIncomeSourceID      => 8;
use constant kChannelID           => 9;
use constant kPriceLevelID        => 10;
use constant kRate                => 11;
use constant kPrice               => 12;
use constant kConversionRate      => 13;
use constant kSaleID              => 14;
use constant kProductID           => 15;
use constant kSales               => 16;
use constant kReturns             => 17;
use constant kRevenue             => 18;
use constant kUnitsLiquidated     => 19;
use constant kRevenueLiquidated   => 20;
use constant kIsDigital           => 21;
use constant kDistributionFee     => 22;
use constant kProrateTrackCount   => 23;

# This column should only affect reserve data.
#
use constant kReservePeriodsRemaining => 24;


# Expense item columns
#
use constant kExpenseID             => 7;
use constant kExpenseName           => 8;
use constant kExpenseMemo           => 9;
use constant kExpenseCost           => 10;
use constant kExpenseRate           => 11;
use constant kExpenseTotal          => 12;
use constant kExpenseDefaultTermRate => 13;

# License income columns
#
use constant kLicenseIncomeID       => 7;
use constant kLicenseIncomeTypeID   => 8;
use constant kLicenseIncomeRate     => 9;
use constant kLicenseIncomeUnits    => 10;
use constant kLicenseIncomeRevenue  => 11;
use constant kLicenseIncomeNetRevenue  => 12;
use constant kLicenseIncomeMemo     => 13;


# Pending transaction columns
#
use constant kPendingTransactionID          => 7;
use constant kPendingTransactionAmount      => 8;
use constant kPendingTransactionMemo        => 9;
use constant kPendingTransactionCheckNumber => 10;
use constant kPendingTransactionTypeCode    => 11;
use constant kPendingTransactionDate        => 12;


# One scenario I did not consider was payees that have a previous balance, but no other activity.
# Some of these will need to be paid, because for whatever reason they neglected to pay last time.
# I will therefore need a way to inject the previous balance all by itself into the data stream.

sub FileName { 'mapped_items' }



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>;
    return undef unless $line;

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



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

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

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




1;

