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

package RPS::Report::Dynamic::Contract::Label;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::Client;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::DynamicReport::Contract::Label;

use base 'RPS::Report::Dynamic::Contract::Base';

sub Type    { 'Label' }
sub FilenameType { 'Label Contracts' }

# defined at the end of this file
#
sub _ParamClass { 'RPS::Report::Dynamic::Contract::Label::Parameters'; }
sub ClientTypeMask { return Common::Client::kLabel; }

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

    my $cols = [];

    push( @$cols, $self->_getNameColumnHash('contract-title', 'title' ) );
    push( @$cols, $self->_getNameColumnHash('contract-id', 'client_contract_id' ) );
    push( @$cols, $self->_getNameColumnHash('payor', 'payor' ) );
    push( @$cols, $self->_getNameColumnHash('payee', 'payee' ) );
    push( @$cols, $self->_getNameColumnHash('rs-payee-id', 'payee_id' ) );
    push( @$cols, $self->_getNameColumnHash('client-account-no', 'client_account_no' ) );
    push @$cols, {
        header => {
            name => 'issue-date',
        },
        column => {
            field => 'issue_date',
            excel => 'date:Y-M-D',
            method => '_formatDate',
        },
    };

    push( @$cols, $self->_getNameColumnHash('label', 'label_name' ) );
    push( @$cols, $self->_getNameColumnHash('service', 'service_name' ) );
    push @$cols, {
        header => {
            name => 'country',
        },
        column => {
            field => 'country_code',
            method => '_countryCodeToCountryName',
        },
    };       
    push( @$cols, $self->_getNameColumnHash('dist-fee', 'distribution_fee' ) );
    push @$cols, {
        header => {
            name => 'contract-date-created',
        },
        column => {
            field => 'date_created',
            excel => 'date:Y-M-D',
            method => '_formatDate',
        },
    };
    push @$cols, {
        header => {
            name => 'contract-created-by',
        },
        column => {
            field => 'created_by',
            method => '_userIDToUserName',
        },
    };
    push @$cols, {
        header => {
            name => 'contract-date-modified',
        },
        column => {
            field => 'date_modified',
            excel => 'date:Y-M-D',
            method => '_formatDate',
        },
    };
    push @$cols, {
        header => {
            name => 'contract-modified-by',
        },
        column => {
            field => 'modified_by',
            method => '_userIDToUserName',
        },
    };
    push( @$cols, $self->_getNameColumnHash('rs-contract-id', 'rs_contract_id' ) );

    return $cols;
}

sub _getNameColumnHash {
    my $self = shift;
    my ($name, $col) = @_;

    return {
            header => {
                name => $name,
            },
            column => {
                field => $col,
            }
    };
}


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

    # Dereference the parameters, see if we're constraining
    # the query by date somehow.
    #
    # !!! Don't want to just pass the CGI parameter straight through - That could
    # !!! cause problems...
    #
    my $dateType;
    my $dateTypeParam = $self->parameters()->{Criteria}->DateType();
    if ($dateTypeParam)
    {
        if ('Date Created' eq $dateTypeParam)
        {
            $dateType = 'date_created';
        }
        elsif ('Date Modified' eq $dateTypeParam)
        {
            $dateType = 'date_modified';
        }
    }

    return RPS::DB::Item::DynamicReport::Contract::Label->ReportQuery
    (
        dateType => $dateType,
        startDate => $self->parameters()->{Criteria}->StartDate(),
        endDate => $self->parameters()->{Criteria}->EndDate(),
    );
}


1;

package RPS::Report::Dynamic::Contract::Label::Parameters;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use lib '/app/tools/report/lib';
use lib '/app/tools/rps/lib';
use base 'RPS::Report::Dynamic::Contract::Parameters';


sub _OptionalData
{
}


1;
