#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package Support::Reports::Command::OrchardSales;
use strict;
use warnings;

use lib '/app/tools/support/lib';
use Support::Reports::OrchardSales;

use base 'Support::Command::Submit';

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

sub cmd  { return 'orchard_sales'; }
sub area { return 'reports'; }

use constant kTemplate => "/app/tools/support/templates/reports/orchard_sales.xsl";

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

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

    my $client     = $self->getParam('client');
    my $clientType = $self->getParam('clientType');
    my $status     = $self->getParam('status');
    my $action     = $self->getParam('a') || '';

    my $report = new Support::Reports::OrchardSales();

    if ( $action eq 'create' ) {
        my $st = Support::Reports::OrchardSales->generate();
        if ( $st != 1 ) {
            $self->{xml}{Error} = "Report generation returned error $st";
        } else {
            my $url = '/' . $self->app() . '/' . $self->area() . '?c=' . $self->cmd();
            $response = RSApache::Response::Redirect->new($url);
        }

    } elsif( $action eq 'get' ) {
        my $file = $self->getParam('file');
        $response = $self->download($file);

    } elsif( $action eq 'del' ) {

        my $file = $self->getParam('file');
        my $st   = $self->deleteReport( $file );
    }

    $self->{xml}{Collection} = $report->getFiles(); # Get the set of available reports

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

sub getFullReportPath {
    my ( $self, $filename ) = @_; 
    my $fullpath = Support::Reports::OrchardSales::kReportDir . '/' . $filename;
    return $fullpath;
}

sub isValidReportFile {
    my ( $self, $filename ) = @_; 
    my $st = 0; # default is not valid

    my $fullpath = $self->getFullReportPath($filename);

    # Make sure that the path is for the report dir ONLY
    if ( $filename =~ /^(\/|\.)/ ) {  # file cannot contain any path references
        $self->{xml}{Error} = "Directory path not allowed";
    } elsif( -d $fullpath ) { # prevent directory deletion
        $self->{xml}{Error} = "Directory not allowed";
    } elsif( ! -e $fullpath ) { # make sure file exists
        $self->{xml}{Error} = "File not found";
    } elsif ( -e "$fullpath" ) { 
        $st = 1;
    }
    return $st;
}

sub download {
    my ( $self, $filename ) = @_; 

    if ( $self->isValidReportFile( $filename ) ) {
        my $fullpath       = $self->getFullReportPath($filename);
        my $prettyFilename = $filename; # download the file using the original name
        my $contentType    = 'application/octet-stream';

        my $fh       = new IO::File;
        open( $fh, $fullpath ) or die "ERROR: Unable to open $fullpath for reading: $!";
        my @stat     = $fh->stat;
        my $fileSize = $stat[7];

        my $response    = RSApache::Response::Download->new( $fh, $contentType, $prettyFilename, $fileSize );
        return $response;

    } else {
        $self->{xml}{Error} = "File download error";
    }   
    return undef;
}

sub deleteReport {
    my ( $self, $filename ) = @_; 

    if ( $self->isValidReportFile( $filename ) ) {
        my $fullpath = $self->getFullReportPath($filename);
        unlink($fullpath);
        $self->{xml}{Params}{msg} = "delete_success";
    }

    return undef;
}

1;
