#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::ArtistRoyalty::Command::RoyaltyBreakdown;
use strict;
use warnings;
use Data::Dumper;

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

use lib '/app/tools/rps/lib';
use RPS::ArtistRoyalty::Command::Show;
use RPS::ArtistRoyalty::Job::CreateRoyaltyBreakdownReport;
use RPS::Statement::Artist::PDF;

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

use RSApache::Response::XSLT;

sub cmd  { return 'royalty_breakdown'; }
sub area { return 'artist_royalty'; }

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

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

    my $runID = $self->getParam('RoyaltyRunID');

    # We need to create the semaphore now so that the Royalty Earnings Breakdown link can reflect its state
    my $baseFilePath = RPS::Statement::Artist::PDF::StatementPathFromRunID($runID);
    my $semaphore = $baseFilePath . "ROYALTY_BREAKDOWN_GENERATING";

    open SEMAPHORE, "> $semaphore";
    print SEMAPHORE "done\n";
    close SEMAPHORE;

    # Add the report job to the queue
    my $jobArgs = RPS::ArtistRoyalty::Job::CreateRoyaltyBreakdownReport->new( runID => $runID );
    my $job = $jobArgs->enqueue();

    my $message = RPS::Message->new( type => "success", code => Common::FormObject::kSuccessReportQueued );
    my $command = RPS::ArtistRoyalty::Command::Show->new( RoyaltyRunID => $runID, msg => $message->toString, );
    my $redirectResponse = RSApache::Response::Redirect->new( $command->getRedirect() );
    return $redirectResponse;
}

1;
