package Support::SaleFileExplorer::Period;
use strict;

use lib '/app/tools/common/lib';
use lib '/app/tools/raptor/lib';
use lib '/app/tools/support/lib';
use Common::XMLObject;
use base 'Common::XMLObject';

use Raptor::DB::Item::Period;

sub _init {
    my ( $self, %args ) = @_;

    if ( $args{dbItem} ) {
        $self->_initFromDBItem( $args{dbItem} );
    } elsif ( defined $args{periodID} ) {
        $self->_initFromID( $args{periodID} );
    }

    return $self;
}

sub _initFromID {
    my ( $self, $id ) = @_;

    my $periodDBItem = Raptor::DB::Item::Period->Lookup( period_id => $id );
    return $self->_initFormDBItem($periodDBItem);
}

sub _initFromDBItem {
    my ( $self, $dbItem ) = @_;

    foreach my $fieldName ( keys %$dbItem ) {
        next if '_' eq substr( $fieldName, 0, 1 );
        $self->{$fieldName} = $dbItem->$fieldName;
    }

    if ( 0 == $self->{period_id} ) {
        $self->{name} = 'OPEN';
    }
}

1;
