#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2011 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#---------------------------------------------------------------
package BookPub::Matcher::Sale::Revenue;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::Assert;
use RSApache::Message;
use base 'BookPub::Matcher::Sale';

use lib '/app/tools/bookpub/lib';
use BookPub::DB::Item::Sale;
use BookPub::Sale::Match;

#
# This class encapsulates the original 'sale' table records.
# 'Revenue' refers to the fact that these sales come from revenue files.
#

sub _propertiesFromDB {
    my ( $self, $hrProperties, $saleID ) = @_;
    assert($saleID);

    my $dbItem = BookPub::DB::Item::Sale->Lookup( sale_id => $saleID );
    $self->_propertiesFromDBItem( $hrProperties, $dbItem );
}

sub _propertiesFromDBViaFileAndLineNum {
    my ( $self, $hrProperties, $fileID, $lineNum ) = @_;
    assert( defined $fileID );
    assert( defined $lineNum );

    my $dbItem = BookPub::DB::Item::Sale->Lookup( file_id => $fileID, line_num => $lineNum );
    return unless $dbItem;
    $self->_propertiesFromDBItem( $hrProperties, $dbItem );
}

sub _propertiesFromDBItem {
    my ( $self, $hrProperties, $dbItem ) = @_;
    assert($dbItem);

    # Save a reference to the dbItem we used, so we can tell later if this object
    # exists in the database already...
    #
    $self->{_dbItem} = $dbItem;

    my $columns = BookPub::DB::Item::Sale->Columns();
    assert($columns);

    foreach my $column (@$columns) {
        $hrProperties->{$column} = $dbItem->$column;
    }
}

sub _initProperties {
    my ( $self, $props ) = @_;

    $self->SUPER::_initProperties($props);

    # Get some additional information for navigation.
    #
    my $saleID = $props->{sale_id};
    my $fileID = $props->{file_id};
    if ( $saleID && $fileID ) {
        $self->{PreviousErrorID} = BookPub::DB::Item::Sale->GetPreviousErrorID( $saleID, $fileID );
        $self->{PreviousUnfixedID} = BookPub::DB::Item::Sale->GetPreviousUnfixedID( $saleID, $fileID );
        $self->{NextErrorID} = BookPub::DB::Item::Sale->GetNextErrorID( $saleID, $fileID );
        $self->{NextUnfixedID} = BookPub::DB::Item::Sale->GetNextUnfixedID( $saleID, $fileID );
    }

    # I'd like to present the sale format type, in a human-readable format.
    my $formatTypeString;
    if ( $props->{format_type} ) {
        $formatTypeString = BookPub::DB::Item::Sale->GetFormatString( $props->{product_type}, $props->{format_type} );
    }
    $self->{FormatTypeString} = $formatTypeString;

    my $productTypeString;
    if ( $props->{product_type} ) {
        $productTypeString = BookPub::DB::Item::Sale->GetProductTypeString( $props->{product_type} );
    }
    $self->{ProductTypeString} = $productTypeString;

    return $self;
}

1;
