#------------------------------------------------------------
# Copyright (C) 2013 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::Statement::Command::DownloadArtistExcel;
use strict;
use warnings;

use IO::File;

use lib '/app/tools/rps/lib';
use RPS::Statement::Artist::Excel;

use lib '/app/tools/common/lib';
use Common::WriteXML;
use RSApache::Response::Download;
use Common::Timer;

use RPS::Command;
use RPS::CommandLog;
use base 'RPS::Command';

sub cmd  { return 'excel_artist'; }
sub area { return 'statement'; }


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

    my $timer = Common::Timer->new();

    my $response = $self->SUPER::execute();
    return $response if $response;

    my $id = $self->getParam("ArtistStatementID");
    my $statementData = RPS::DB::Item::ArtistRoyaltyStatement->Lookup(artist_royalty_statement_id => $id);
    my $runID = $statementData->artist_royalty_run_id;


    my $prettyFileName = RPS::Statement::Artist::Excel::PrettyStatementFileNameFromDBItem($statementData);
    my $fileName = RPS::Statement::Artist::Excel::StatementFileNameFromDBItem($statementData);
    my $fullPath = RPS::Statement::Artist::Excel::StatementPathFromRunID($runID) . $fileName;

    my $fh = new IO::File;
    open($fh, "$fullPath") or die "ERROR: Unable to open $fullPath for reading: $!";

    my $contentType = 'application/octet-stream';

    my @stat = $fh->stat;
    my $fileSize = $stat[7];
    $response = RSApache::Response::Download->new($fh, $contentType, $prettyFileName, $fileSize);
    return $response;
}

1;
