#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package Common::DB::Item::OrchardReport;
use lib '/app/tools/common/lib';
use Common::DB::Item;
use Common::DB::ItemCollection;
use Common::Assert;
use base 'Common::DB::Item';

use constant kDB => Common::DB::Item::kCommonDB;

use constant kTable => 'orchard_report';

use constant kFileNotFound    => 0;
use constant kFileFound       => 1;
use constant kFileRequested   => 2;
use constant kFileNotReady    => 3;
use constant kFileDownloaded  => 4;
use constant kFileError       => 5;
use constant kFileUnpacked    => 6;
use constant kFileImportReady => 7;
use constant kFileQueued      => 8;
use constant kFileImported    => 9;

my %gDef = (
    kFileNotFound()    => "file not found or unable to generate",
    kFileFound()       => "file exists on OWS",
    kFileRequested()   => "report request submitted",
    kFileNotReady()    => "report requested but not ready",
    kFileDownloaded()  => "report downloaded",
    kFileError()       => "error",
    kFileUnpacked()    => "file unpacked",
    kFileImportReady() => "file ready to import",
    kFileQueued()      => "import job queued",
    kFileImported()    => "import job completed",
);

sub printStatus {
    my ( $class, $st ) = @_;
    if ( defined $gDef{$st} ) {
        return $gDef{$st};
    } else {
        Log->error("OrchardReport: Unknown status $st");
        return "???";
    }
}

sub GetAllByClient {
    my ( $class, $clientID ) = @_;

    my $sql = "SELECT * FROM " . kTable . " WHERE client_id = $clientID ORDER BY report_id";
    return $class->SUPER::GetAll($sql);
}

sub GetAllByClientAndPeriod {
    my ( $class, $clientID ) = @_;

    my $sql = "SELECT * FROM " . kTable . " WHERE client_id = $clientID ORDER BY report_id";
    return $class->SUPER::GetAll($sql);
}

sub DeleteByClientAndFileID {
    my ( $class, $clientID, $fileID ) = @_;

    my $dbo = Common::RSApp::GetCommonDB();

    my $sql = "DELETE FROM " . kTable . " WHERE client_id = $clientID AND file_id = $fileID";

    $dbo->DoCmd($sql);

    return 1;
}

sub Lookup {
    my $class = shift;
    my %args  = @_;

    assert( $args{client_id} );
    assert( $args{period} );
    assert( $args{account_id} );
    return $class->SUPER::Lookup(%args);
}

1;

