#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package Raptor::SaleReport::Command::Main;

use lib '/app/tools/common/lib';
use RSApache::Response::XSLT;
use Common::Assert;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::Payor;
use RPS::Payor::Payor;

use lib '/app/tools/raptor/lib';
use Raptor::SaleReport::MainReport;

use Raptor::Command;
use base 'Raptor::Command';

sub cmd  { return "m"; }
sub area { return "reports"; }

use constant kTemplate => '/app/tools/raptor/templates/reports.xsl';

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

    # Give any inherited methods the first crack at this.
    # (This checks for login and basic access permissions).
    #
    my $response = $self->SUPER::execute();
    return $response if $response;

    my $periodType  = lc( $self->getParam("period_type") || 'q' );
    my $periodParam = $self->getParam("period");
    my $labelID     = $self->getParam("label") || $self->_getUser()->LabelID() || undef;
    my $countryCode = $self->getParam("country");

    $self->{xml}{Report} = Raptor::SaleReport::MainReport->new(
        periodType  => $periodType,
        periodParam => $periodParam,
        labelID     => $labelID,
        countryCode => $countryCode,
        commandObj  => $self,
    );

    my $payorDB = RPS::DB::Item::Payor->GetDefault();
    if ($payorDB) {
        my $payor = RPS::Payor::Payor->new( _dbItem => $payorDB );
        if ( $payor->LogoSourceWeb ) {
            $self->{xml}{Form}{LogoSourceWeb} = $payor->LogoSourceWeb;
        }
    }

    $response = RSApache::Response::XSLT->new( $self->{xml}, kTemplate );
    return $response;
}

1;
