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

package RPS::LabelRoyalty::Fast::Final::DB::LabelRoyaltyLabelItem;

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

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

use Data::Dumper;
use DB_File;

use RPS::LabelRoyalty::Fast::Mapped::Data;
use RPS::LabelRoyalty::Fast::Final::DB::LabelRoyaltyStatement;

use Common::Log;

use base 'RPS::LabelRoyalty::Fast::Final::DB';

use constant kLabelRoyaltyLabelItemID => 0;
use constant kLabelRoyaltyStatementID => 1;
use constant kLabelID                 => 2;
use constant kLabelContractID         => 3;
use constant kGrossSales              => 4;
use constant kDistributionFee         => 5;
use constant kFee                     => 6;
use constant kNetSales                => 7;
use constant kExceptionID             => 8;
use constant kExceptionService        => 9;
use constant kExceptionCountry        => 10;

# Private
use constant kStatementRef => 11;

my @gFields = (
    'label_royalty_label_item_id', 'label_royalty_statement_id', 'label_id', 'label_contract_id', 'gross_sales', 'distribution_fee', 'fee',
    'net_sales', 'label_contract_group_term_exception_id',
    'service_id', 'country_code'
);

sub Fields { return \@gFields; }

sub FileName { 'label_royalty_label_item' }

sub new {
    my ( $class, $data, $statement ) = @_;

    my $item = $class->BlankItem();
    $class->_IncrementID();

    $item->[kLabelRoyaltyLabelItemID] = $class->_CurrentID();
    $item->[kLabelRoyaltyStatementID] = $statement->id();

    $item->[kLabelContractID] = $data->[RPS::LabelRoyalty::Fast::Mapped::Data::kLabelContractID];
    $item->[kLabelID]         = $data->[RPS::LabelRoyalty::Fast::Mapped::Data::kLabelID];
    $item->[kDistributionFee] = $data->[RPS::LabelRoyalty::Fast::Mapped::Data::kDistributionFee];

    $item->[kExceptionID]      = $data->[RPS::LabelRoyalty::Fast::Mapped::Data::kExceptionID];
    $item->[kExceptionService] = $data->[RPS::LabelRoyalty::Fast::Mapped::Data::kExceptionService];
    $item->[kExceptionCountry] = $data->[RPS::LabelRoyalty::Fast::Mapped::Data::kExceptionCountry];

    push @$item, $statement;

    bless $item, $class;

    return $item;
}

sub statement {
    my $self = shift;
    return $self->[kStatementRef];
}

sub process {
    my ( $self, $data ) = @_;

    if (   $self->[kLabelID] != $data->[RPS::LabelRoyalty::Fast::Mapped::Data::kLabelID]
        || $self->[kLabelContractID] != $data->[RPS::LabelRoyalty::Fast::Mapped::Data::kLabelContractID]
        || $self->[kExceptionID] != $data->[RPS::LabelRoyalty::Fast::Mapped::Data::kExceptionID] ) {
        Log->info("RPS::LabelRoyalty::Fast::Final::DB::LabelRoyaltyLabelItem::process - returning UNDEF");
        return undef;
    }

    Log->info("RPS::LabelRoyalty::Fast::Final::DB::LabelRoyaltyLabelItem::process - returning 1");
    return 1;
}

sub _output {
    my ( $self, $fd ) = @_;

    # We may end up with 'null' Labels.  For example, pending transactions don't have a label id.
    # In these cases, we'll output nothing.
    #
    return unless $self->[kLabelID] != 0;

    my $statement = pop @$self;

    $statement->[RPS::LabelRoyalty::Fast::Final::DB::LabelRoyaltyStatement::kTotal] += $self->[kNetSales];

    $self->SUPER::_output($fd);
}

1;

