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

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

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::RSDB;

use Data::Dumper;
use DB_File;

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

use Common::Log;

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

use constant kLabelRoyaltyStatementID => 0;
use constant kAmount                  => 1;
use constant kMemo                    => 2;
use constant kCheckNumber             => 3;
use constant kPendingTransactionID    => 4;
use constant kTransactionDate         => 5;
use constant kTypeCode                => 6;

use constant kLabelRef => 7;

my @gFields =
  ( 'label_royalty_statement_id', 'amount', 'memo', 'check_number', 'pending_transaction_id', 'transaction_date', 'type_code', );

sub Fields { return \@gFields; }

sub FileName { 'label_royalty_transaction_item' }

# Overridden to return 0.
# We just let the database choose ids when we write to the table.
#
sub GetCurrentMaxID {
    my ($class) = @_;

    return 0;
}

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

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

    # So, really, we don't 'accumulate' these.
    # Each line of data maps to a line in the output file.

    # Meaning, we'll do essentially everything here in the constructor.
    # process will _always_ return undef.
    #
    $item->[kLabelRoyaltyStatementID] = $label->[RPS::LabelRoyalty::Fast::Final::DB::LabelRoyaltyLabelItem::kLabelRoyaltyStatementID];
    $item->[kAmount]                  = $data->[RPS::LabelRoyalty::Fast::Mapped::Data::kPendingTransactionAmount];
    $item->[kMemo]                    = $data->[RPS::LabelRoyalty::Fast::Mapped::Data::kPendingTransactionMemo];
    $item->[kCheckNumber]             = $data->[RPS::LabelRoyalty::Fast::Mapped::Data::kPendingTransactionCheckNumber];
    $item->[kPendingTransactionID]    = $data->[RPS::LabelRoyalty::Fast::Mapped::Data::kPendingTransactionID];
    $item->[kTransactionDate]         = $data->[RPS::LabelRoyalty::Fast::Mapped::Data::kPendingTransactionDate] || 'NULL';
    $item->[kTypeCode]                = $data->[RPS::LabelRoyalty::Fast::Mapped::Data::kPendingTransactionTypeCode];

    # We push a reference to the Label object into the end of our array state.
    # Before we write to our output file we will pop that off.
    #
    push @$item, $label;

    return bless( $item, $class );
}

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

    return undef;
}

# Override _output to send our totals up to the Album
#
sub _output {
    my ( $self, $ITEMS ) = @_;
    my $label = pop @$self;

    # We need to update the transaction subtotal that is managed by the Statement object.
    # So we'll fetch a reference to that from the Label object reference we are storing.
    #
    my $statement = $label->statement();
    $statement->[RPS::LabelRoyalty::Fast::Final::DB::LabelRoyaltyStatement::kTransactionSubtotal] += $self->[kAmount];

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

1;

